-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_openI.py
144 lines (113 loc) · 5.09 KB
/
extract_openI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import os
import os.path as osp
import argparse
import csv
import numpy
import matplotlib
from matplotlib import pyplot, image
import cv2
from tqdm import tqdm
import numpy as np
import matplotlib.pyplot as plt
import math
from shutil import copyfile
import re
import xml.dom.minidom
import pandas as pd
import time
import re
import random
import json
import re
import matplotlib.pyplot as pl
from PIL import Image
from collections import defaultdict
import pickle
import gzip
import random
import sys
from pathlib import Path
def extract_findings_and_impression_openi():
root = r"G:\Medical Reports datasets\NLMCXR_reports\ecgen-radiology"
valid_count = 0
list_writer = csv.writer(
open(r"G:\Medical Reports datasets\NLMCXR_reports\openi_report_sections_all.csv", 'a+', encoding='utf-8', newline=""))
list_writer.writerow(["ids", "finding", "impression"])
for file_name in tqdm(os.listdir(root)):
dom = xml.dom.minidom.parse(osp.join(root, file_name))
file_id = file_name.split('.')[0]
try:
cc = dom.getElementsByTagName('AbstractText')
c1 = cc[2]
finding = c1.firstChild.data
c1 = cc[3]
impression = c1.firstChild.data
list_writer.writerow([file_id, finding, impression])
valid_count += 1
except AttributeError:
continue
def gen_openi_data():
all_label_csv = open(osp.join(r"G:\Medical Reports datasets\NLMCXR_reports\openi_findings_label.csv"), "r").readlines()
all_ids = []
for line in all_label_csv[1:]:
id = line.split(',')[0]
all_ids.append(int(id))
random.shuffle(all_ids)
random.shuffle(all_ids)
# print(all_ids)
test_576_ids = random.sample(all_ids, 576)
train_ids = []
for ii in all_ids:
if ii not in test_576_ids:
train_ids.append(ii)
save_dict = {"test": test_576_ids, "train": train_ids}
print('test', len(test_576_ids), 'train', len(train_ids))
json.dump(save_dict, open(r"G:\Medical Reports datasets\NLMCXR_reports\openi_random_split.json", 'w'))
def gen_report_and_label():
save_root = r"G:\Medical Reports datasets\NLMCXR_reports\test_576"
data_csv = pd.read_csv(r"G:\Medical Reports datasets\NLMCXR_reports\openi_report_sections_all.csv")
label_csv = pd.read_csv(r"G:\Medical Reports datasets\NLMCXR_reports\openi_findings_label.csv")
fold_json = json.load(open(r"G:\Medical Reports datasets\NLMCXR_reports\openi_random_split.json", 'r'))
header_all = open(r"G:\Medical Reports datasets\NLMCXR_reports\openi_findings_label.csv", "r").readlines()[0].split(',')
header_label = [header_all[0]] + header_all[2:]
header_data = open(r"G:\Medical Reports datasets\NLMCXR_reports\openi_report_sections_all.csv", "r").readlines()[
0].split(',')
if not os.path.exists(save_root):
os.makedirs(save_root)
test_ids = fold_json["test"]
test_data_df = pd.DataFrame(data=None, columns=header_data)
test_label_df = pd.DataFrame(data=None, columns=header_label)
train_data_df = pd.DataFrame(data=None, columns=header_data)
train_label_df = pd.DataFrame(data=None, columns=header_label)
for index, row in data_csv.iterrows():
report_id = int(row["ids"])
if report_id in test_ids:
test_findings = row["finding"]
test_impression = row["impression"]
data_line_df = pd.DataFrame([[report_id, test_findings, test_impression]], columns=header_data)
test_data_df = test_data_df.append(data_line_df, ignore_index=True)
test_label_line = label_csv.iloc[index]
label = np.array(test_label_line[2:])
label = label.astype('float')
label[np.isnan(label)] = 2
label_line_df = pd.DataFrame([[report_id] + list(label)], columns=header_label)
test_label_df = test_label_df.append(label_line_df, ignore_index=True)
else:
train_findings = row["finding"]
train_impression = row["impression"]
data_line_df = pd.DataFrame([[report_id, train_findings, train_impression]], columns=header_data)
train_data_df = train_data_df.append(data_line_df, ignore_index=True)
train_label_line = label_csv.iloc[index]
label = np.array(train_label_line[2:])
label = label.astype('float')
label[np.isnan(label)] = 2
label_line_df = pd.DataFrame([[report_id] + list(label)], columns=header_label)
train_label_df = train_label_df.append(label_line_df, ignore_index=True)
test_data_df.to_csv(osp.join(save_root, "test_data.csv"), index=False)
test_label_df.to_csv(osp.join(save_root, "test_label.csv"), index=False)
train_data_df.to_csv(osp.join(save_root, "train_data.csv"), index=False)
train_label_df.to_csv(osp.join(save_root, "train_label.csv"), index=False)
if __name__ == '__main__':
# extract_findings_and_impression_openi()
# gen_openi_data()
gen_report_and_label()