pengcc1 commited on
Commit
a7d4c7b
1 Parent(s): 0ef318d

Upload extract_questions.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. extract_questions.py +172 -0
extract_questions.py ADDED
@@ -0,0 +1,172 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import shutil
4
+
5
+ # 读取关键词文件并构建关键词映射字典
6
+ keyword_file = '/mnt/petrelfs/chenpengcheng/benchmark_preprocess/GMAI-MMbench-CoT/output_multi_column.txt'
7
+ keyword_dict = {}
8
+
9
+ with open(keyword_file, 'r', encoding='utf-8') as f:
10
+ for line in f:
11
+ line = line.strip()
12
+ if not line:
13
+ continue # 跳过空行
14
+ parts = line.split(',')
15
+ if len(parts) != 4:
16
+ print(f"格式错误,跳过此行:{line}")
17
+ continue
18
+ keyword, department, task, modality = [p.strip() for p in parts]
19
+ keyword_dict[keyword] = {
20
+ 'department': department,
21
+ 'task': task,
22
+ 'modality': modality
23
+ }
24
+
25
+ print(f"总共加载了 {len(keyword_dict)} 个关键词。")
26
+
27
+ # 定义需要处理的科室列表
28
+ departments = [
29
+ 'Cardiovascular Surgery',
30
+ 'Dermatology',
31
+ 'Endocrinology',
32
+ 'Gastroenterology and Hepatology',
33
+ 'General Surgery',
34
+ 'Hematology',
35
+ 'Infectious Diseases',
36
+ 'Laboratory Medicine and Pathology',
37
+ 'Nephrology and Hypertension',
38
+ 'Neurosurgery',
39
+ 'Obstetrics and Gynecology',
40
+ 'Oncology (Medical)',
41
+ 'Ophthalmology',
42
+ 'Orthopedic Surgery',
43
+ 'Otolaryngology (ENT)/Head and Neck Surgery',
44
+ 'Pulmonary Medicine',
45
+ 'Sports Medicine',
46
+ 'Urology'
47
+ ]
48
+
49
+ # 创建科室到目录名称的映射,处理特殊情况
50
+ def get_department_dir_name(department):
51
+ if department == 'Otolaryngology (ENT)/Head and Neck Surgery':
52
+ return 'Otolaryngology (ENT)'
53
+ else:
54
+ return department
55
+
56
+ # 将科室列表转换为集合,方便查找
57
+ departments_set = set(departments)
58
+
59
+ # 定义源目录列表
60
+ source_dirs = [
61
+ '/mnt/petrelfs/chenpengcheng/benchmark_preprocess/GMAI/cls_2d',
62
+ '/mnt/petrelfs/chenpengcheng/benchmark_preprocess/GMAI/det_2d',
63
+ '/mnt/petrelfs/chenpengcheng/benchmark_preprocess/GMAI/semantic_seg_2d',
64
+ '/mnt/petrelfs/chenpengcheng/benchmark_preprocess/GMAI/semantic_seg_3d'
65
+ ]
66
+
67
+ # 定义目标基础目录
68
+ destination_root = '/mnt/petrelfs/chenpengcheng/benchmark_preprocess/GMAI-MMbench-CoT'
69
+
70
+ # 用于统计和调试
71
+ total_files_processed = 0
72
+ files_matched = 0
73
+ images_copied = 0
74
+
75
+ # 用于统计每个科室的匹配文件数
76
+ department_file_counts = {dept: 0 for dept in departments}
77
+
78
+ # 要处理的图片键列表
79
+ image_keys = ['img_mask_path', 'img_contour_path', 'img_bbox_path', 'img_path']
80
+
81
+ # 遍历每个源目录
82
+ for source_dir in source_dirs:
83
+ print(f"正在遍历目录:{source_dir}")
84
+ for root, dirs, files in os.walk(source_dir):
85
+ for file in files:
86
+ if file.endswith('.json'):
87
+ total_files_processed += 1
88
+ source_file_path = os.path.join(root, file)
89
+ try:
90
+ with open(source_file_path, 'r', encoding='utf-8') as f:
91
+ data = json.load(f)
92
+ answer_letter = data.get('answer', '').strip()
93
+ options = data.get('options', [])
94
+ if not answer_letter or not options:
95
+ print(f"文件缺少 'answer' 或 'options' 字段,跳过:{source_file_path}")
96
+ continue
97
+ # 创建选项字典,映射字母到选项文本
98
+ option_dict = {}
99
+ for opt in options:
100
+ if len(opt) > 2 and opt[1] == '.':
101
+ opt_letter = opt[0]
102
+ opt_text = opt[3:].strip()
103
+ option_dict[opt_letter] = opt_text
104
+ else:
105
+ print(f"选项格式错误,文件:{source_file_path},选项:{opt}")
106
+ # 获取关键词
107
+ keyword = option_dict.get(answer_letter)
108
+ if not keyword:
109
+ print(f"答案字母 '{answer_letter}' 在选项中未找到,文件:{source_file_path}")
110
+ continue
111
+ print(f"处理文件:{source_file_path}")
112
+ print(f"关键词:'{keyword}'")
113
+ # 检查关键词是否在关键词字典中
114
+ if keyword in keyword_dict:
115
+ department_info = keyword_dict[keyword]
116
+ department = department_info['department']
117
+ print(f"关键词 '{keyword}' 的科室为:'{department}'")
118
+ if department in departments_set:
119
+ files_matched += 1
120
+ department_dir_name = get_department_dir_name(department)
121
+ destination_base = os.path.join(destination_root, department_dir_name)
122
+ # 构造目标文件路径
123
+ relative_path = os.path.relpath(source_file_path, '/mnt/petrelfs/chenpengcheng/benchmark_preprocess/GMAI')
124
+ destination_file_path = os.path.join(destination_base, relative_path)
125
+ # 创建目标目录(如果不存在)
126
+ destination_dir = os.path.dirname(destination_file_path)
127
+ if not os.path.exists(destination_dir):
128
+ os.makedirs(destination_dir)
129
+ print(f"创建目录:{destination_dir}")
130
+ # 复制JSON文件
131
+ shutil.copy2(source_file_path, destination_file_path)
132
+ print(f"已复制文件到:{destination_file_path}")
133
+ # 处理并复制图片
134
+ for image_key in image_keys:
135
+ if image_key in data:
136
+ image_path = data[image_key]
137
+ # 图片路径是相对于 source_dir + '/images' 的
138
+ source_image_path = os.path.join(source_dir, 'images', image_path)
139
+ if not os.path.exists(source_image_path):
140
+ print(f"源图片不存在,跳过:{source_image_path}")
141
+ continue
142
+ # 构造相对路径,从 GMAI 之后开始,包括 'images' 目录
143
+ relative_image_path = os.path.relpath(source_image_path, '/mnt/petrelfs/chenpengcheng/benchmark_preprocess/GMAI')
144
+ # 构造目标图片路径
145
+ destination_image_path = os.path.join(destination_base, relative_image_path)
146
+ destination_image_dir = os.path.dirname(destination_image_path)
147
+ if not os.path.exists(destination_image_dir):
148
+ os.makedirs(destination_image_dir)
149
+ print(f"创建图片目录:{destination_image_dir}")
150
+ # 复制图片文件
151
+ shutil.copy2(source_image_path, destination_image_path)
152
+ images_copied += 1
153
+ print(f"已复制图片到:{destination_image_path}")
154
+ # 增加对应科室的文件计数
155
+ department_file_counts[department] += 1
156
+ else:
157
+ print(f"科室 '{department}' 不在处理列表中,不复制文件。")
158
+ else:
159
+ print(f"关键词 '{keyword}' 不在关键词列表中。")
160
+ except Exception as e:
161
+ print(f"处理文件 {source_file_path} 时发生错误:{e}")
162
+
163
+ print(f"总共处理了 {total_files_processed} 个 JSON 文件。")
164
+ print(f"总共匹配并复制了 {files_matched} 个 JSON 文件。")
165
+ print(f"总共复制了 {images_copied} 张图片。")
166
+
167
+ # 打印每个科室的文件计数
168
+ print("每个科室匹配并复制的文件数量:")
169
+ for dept in departments:
170
+ count = department_file_counts[dept]
171
+ dept_dir_name = get_department_dir_name(dept)
172
+ print(f"{dept_dir_name}: {count} 个文件")