| import json |
| import os |
| import shutil |
| from typing import Any |
| from concurrent.futures import ThreadPoolExecutor, as_completed |
| from tqdm import tqdm |
|
|
|
|
| def read_json(path: str) -> Any: |
| with open(path, "r", encoding="utf-8") as f: |
| return json.load(f) |
|
|
|
|
| def write_json(path: str, data: Any): |
| os.makedirs(os.path.dirname(path), exist_ok=True) |
| with open(path, "w", encoding="utf-8") as f: |
| json.dump(data, f, ensure_ascii=False, indent=4) |
|
|
|
|
| def copy_one(src: str, dst: str): |
| if not os.path.exists(src): |
| raise FileNotFoundError(src) |
|
|
| os.makedirs(os.path.dirname(dst), exist_ok=True) |
|
|
| |
| if os.path.exists(dst): |
| return |
|
|
| shutil.copy2(src, dst) |
|
|
|
|
| def parallel_copy(copy_tasks, desc: str, colour: str = "green", max_workers: int = 32): |
| with ThreadPoolExecutor(max_workers=max_workers) as executor: |
| futures = [ |
| executor.submit(copy_one, src, dst) |
| for src, dst in copy_tasks |
| ] |
|
|
| for future in tqdm(as_completed(futures), total=len(futures), desc=desc, colour=colour): |
| future.result() |
|
|
|
|
| EditScore_source_dir = "/inspire/qb-ilm/project/deepgen/wangdianyi-240107110022/BXH/RL_Data/EditScore-Reward-Data" |
| EditScore_dst_dir = "/inspire/qb-ilm/project/deepgen/wangdianyi-240107110022/BXH/RL_Data/ESData/EditScore-Reward-Data" |
|
|
| EditReward_source_dir = "/inspire/qb-ilm/project/deepgen/wangdianyi-240107110022/BXH/RL_Data/EditReward-Data/Images" |
| EditReward_dst_dir = "/inspire/qb-ilm/project/deepgen/wangdianyi-240107110022/BXH/RL_Data/ESData/EditReward-Data" |
|
|
| editscore_data = read_json( |
| "/inspire/qb-ilm/project/deepgen/wangdianyi-240107110022/BXH/RL_Data/EditScore-Reward-Data/filter_metadata.json" |
| ) |
|
|
| editreward_data = read_json( |
| "/inspire/qb-ilm/project/deepgen/wangdianyi-240107110022/BXH/RL_Data/EditReward-Data/unique_matadata_type.json" |
| ) |
|
|
|
|
| |
| editscore_tasks = [] |
| for item in editscore_data: |
| for img in item["images"]: |
| src = os.path.join(EditScore_source_dir, img) |
| dst = os.path.join(EditScore_dst_dir, img) |
| editscore_tasks.append((src, dst)) |
|
|
| parallel_copy(editscore_tasks, desc="Copy EditScore", colour="green", max_workers=32) |
|
|
| write_json( |
| "/inspire/qb-ilm/project/deepgen/wangdianyi-240107110022/BXH/RL_Data/ESData/EditScore-Reward-Data/data.json", |
| editscore_data, |
| ) |
|
|
|
|
| |
| filtered_editreward_data = [] |
| editreward_tasks = [] |
|
|
| for item in editreward_data: |
| item = dict(item) |
|
|
| item.pop("status", None) |
|
|
| if item.get("confidence", 0) < 0.9: |
| continue |
|
|
| item.pop("confidence", None) |
|
|
| for img in item["images"]: |
| src = os.path.join(EditReward_source_dir, img) |
| dst = os.path.join(EditReward_dst_dir, img) |
| editreward_tasks.append((src, dst)) |
|
|
| filtered_editreward_data.append(item) |
|
|
| print(len(filtered_editreward_data)) |
| parallel_copy(editreward_tasks, desc="Copy EditReward", colour="red", max_workers=48) |
|
|
| write_json( |
| "/inspire/qb-ilm/project/deepgen/wangdianyi-240107110022/BXH/RL_Data/ESData/EditReward-Data/data.json", |
| filtered_editreward_data, |
| ) |