-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patherror_generator.py
368 lines (335 loc) · 15.7 KB
/
error_generator.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import numpy as np
import io
import os
import time
from collections import defaultdict, deque
import datetime
import torch
import torch.distributed as dist
import math
import warnings
import yaml
import cv2
import random
import pandas as pd
from tqdm import tqdm
from dataset.utils import pre_caption
import json
class ErrorGenerator(object):
def __init__(self, entire_corpus, entire_labels, entire_pair=None, probability=0.25, random_state=1234):
'probability: individual probability'
self.probability = probability
self.entire_corpus = entire_corpus
self.entire_label = entire_labels
random.seed(random_state)
np.random.seed(random_state)
if entire_pair:
self.entire_pair = entire_pair
else:
self.entire_pair = {}
# for study_id in tqdm(list(entire_labels.study_id)):
# labels = entire_labels.loc[entire_labels.study_id == study_id]
# study_id_ = 's' + str(study_id) + '.txt'
# impression = entire_corpus.loc[entire_corpus.filename == study_id_].impression.values[0]
# try:
# impression = pre_caption(impression)
# except:
# impression = impression
#
# all_labels = []
#
# if not np.isnan(labels['Atelectasis'].values)[0]:
# if labels['Atelectasis'].values[0] == 1:
# all_labels.append('positive_Atelectasis')
# elif labels['Atelectasis'].values[0] == -1:
# all_labels.append('probable_Atelectasis')
# elif labels['Atelectasis'].values[0] == -0:
# all_labels.append('negative_Atelectasis')
#
# if not np.isnan(labels['Cardiomegaly'].values)[0]:
# if labels['Cardiomegaly'].values[0] == 1:
# all_labels.append('positive_Cardiomegaly')
# elif labels['Cardiomegaly'].values[0] == -1:
# all_labels.append('probable_Cardiomegaly')
# elif labels['Cardiomegaly'].values[0] == -0:
# all_labels.append('negative_Cardiomegaly')
#
# if not np.isnan(labels['Consolidation'].values)[0]:
# if labels['Consolidation'].values[0] == 1:
# all_labels.append('positive_Consolidation')
# elif labels['Consolidation'].values[0] == -1:
# all_labels.append('probable_Consolidation')
# elif labels['Consolidation'].values[0] == -0:
# all_labels.append('negative_Consolidation')
#
# if not np.isnan(labels['Edema'].values)[0]:
# if labels['Edema'].values[0] == 1:
# all_labels.append('positive_Edema')
# elif labels['Edema'].values[0] == -1:
# all_labels.append('probable_Edema')
# elif labels['Edema'].values[0] == -0:
# all_labels.append('negative_Edema')
#
# if not np.isnan(labels['Enlarged Cardiomediastinum'].values)[0]:
# if labels['Enlarged Cardiomediastinum'].values[0] == 1:
# all_labels.append('positive_Enlarged Cardiomediastinum')
# elif labels['Enlarged Cardiomediastinum'].values[0] == -1:
# all_labels.append('probable_Enlarged Cardiomediastinum')
# elif labels['Enlarged Cardiomediastinum'].values[0] == -0:
# all_labels.append('negative_Enlarged Cardiomediastinum')
#
# if not np.isnan(labels['Fracture'].values)[0]:
# if labels['Fracture'].values[0] == 1:
# all_labels.append('positive_Fracture')
# elif labels['Fracture'].values[0] == -1:
# all_labels.append('probable_Fracture')
# elif labels['Fracture'].values[0] == -0:
# all_labels.append('negative_Fracture')
#
# if not np.isnan(labels['Lung Lesion'].values)[0]:
# if labels['Lung Lesion'].values[0] == 1:
# all_labels.append('positive_Lung Lesion')
# elif labels['Lung Lesion'].values[0] == -1:
# all_labels.append('probable_Lung Lesion')
# elif labels['Lung Lesion'].values[0] == -0:
# all_labels.append('negative_Lung Lesion')
#
# if not np.isnan(labels['Lung Opacity'].values)[0]:
# if labels['Lung Opacity'].values[0] == 1:
# all_labels.append('positive_Lung Opacity')
# elif labels['Lung Opacity'].values[0] == -1:
# all_labels.append('probable_Lung Opacity')
# elif labels['Lung Opacity'].values[0] == -0:
# all_labels.append('negative_Lung Opacity')
#
# if not np.isnan(labels['No Finding'].values)[0]:
# if labels['No Finding'].values[0] == 1:
# all_labels.append('positive_No Finding')
# elif labels['No Finding'].values[0] == -1:
# all_labels.append('probable_No Finding')
# elif labels['No Finding'].values[0] == -0:
# all_labels.append('negative_No Finding')
#
# if not np.isnan(labels['Pleural Effusion'].values)[0]:
# if labels['Pleural Effusion'].values[0] == 1:
# all_labels.append('positive_Pleural Effusion')
# elif labels['Pleural Effusion'].values[0] == -1:
# all_labels.append('probable_Pleural Effusion')
# elif labels['Pleural Effusion'].values[0] == -0:
# all_labels.append('negative_Pleural Effusion')
#
# if not np.isnan(labels['Pleural Other'].values)[0]:
# if labels['Pleural Other'].values[0] == 1:
# all_labels.append('positive_Pleural Other')
# elif labels['Pleural Other'].values[0] == -1:
# all_labels.append('probable_Pleural Other')
# elif labels['Pleural Other'].values[0] == -0:
# all_labels.append('negative_Pleural Other')
#
# if not np.isnan(labels['Pneumonia'].values)[0]:
# if labels['Pneumonia'].values[0] == 1:
# all_labels.append('positive_Pneumonia')
# elif labels['Pneumonia'].values[0] == -1:
# all_labels.append('probable_Pneumonia')
# elif labels['Pneumonia'].values[0] == -0:
# all_labels.append('negative_Pneumonia')
#
# if not np.isnan(labels['Pneumothorax'].values)[0]:
# if labels['Pneumothorax'].values[0] == 1:
# all_labels.append('positive_Pneumothorax')
# elif labels['Pneumothorax'].values[0] == -1:
# all_labels.append('probable_Pneumothorax')
# elif labels['Pneumothorax'].values[0] == -0:
# all_labels.append('negative_Pneumothorax')
#
# if not np.isnan(labels['Support Devices'].values)[0]:
# if labels['Support Devices'].values[0] == 1:
# all_labels.append('positive_Support Devices')
# elif labels['Support Devices'].values[0] == -1:
# all_labels.append('probable_Support Devices')
# elif labels['Support Devices'].values[0] == -0:
# all_labels.append('negative_Support Devices')
#
# self.entire_pair[impression] = all_labels
#
# with open('./entire_pair_final.json', 'w') as f:
# json.dump(self.entire_pair, f)
def location(self, report):
keywords = ['left', 'right', 'left-sided', 'right-sided', 'upper', 'lower', 'apical', 'basal', 'central', 'peripheral']
counter_keywords = {'left': 'right', 'right': 'left', 'left-sided': 'right-sided', 'right-sided': 'left-sided', 'upper': 'lower', 'lower': 'upper', 'apical': 'basal',
'basal': 'apical', 'central': 'peripheral', 'peripheral': 'central'}
text_split = report.split()
error_report = []
for word in text_split:
if word in keywords:
counter_word = counter_keywords[word]
error_report.append(counter_word)
else:
error_report.append(word)
error_report = " ".join(error_report)
return error_report
def extent(self, report):
keywords = ['mild', 'slight', 'severe', 'minimal', 'extensive', 'small', 'large', 'mildly', 'severely', 'slightly']
counter_keywords = {'mild': 'severe', 'slight': 'severe', 'severe': 'mild', 'minimal': 'extensive', 'extensive': 'minimal',
'small': 'large', 'large': 'small', 'severely': 'mildly', 'mildly': 'severely', 'slightly': 'severely'}
text_split = report.split()
error_report = []
for word in text_split:
if word in keywords:
counter_word = counter_keywords[word]
error_report.append(counter_word)
else:
error_report.append(word)
error_report = " ".join(error_report)
return error_report
def mismatch(self, impression):
'Entirely change report with not overlapping report'
original_class = self.entire_pair[impression]
while True:
selected_idx = np.random.randint(0, len(list(self.entire_corpus.keys()))-1)
selected_impression = self.entire_corpus.iloc[selected_idx].impression
selected_impression = pre_caption(selected_impression)
selected_class = self.entire_pair[selected_impression]
intersection = list(set(original_class).intersection(selected_class))
if len(intersection) == 0:
break
return selected_impression
def omission(self, impression):
'Set: F_T -> F_t, one sentence ommission'
orig_class = self.entire_pair[impression]
if 'positive_No Finding' in orig_class:
return impression
else:
sentences_ = impression.split('.')
sentences = []
for s in sentences_:
if len(s) < 3:
continue
if s[0] == ' ':
s = s[1:]
sentences.append(s)
if len(sentences) == 1:
return impression
else:
del_idx = random.randint(0, len(sentences) - 1)
sentences.pop(del_idx)
impression = '. '.join(sentences) + '.'
return impression
def confusion(self, impression):
'Set: F1 -> F2 (random), one sentence change'
orig_class = self.entire_pair[impression]
if 'positive_No Finding' in orig_class:
return impression
else:
while True:
selected_impression = random.choice(list(self.entire_pair.keys()))
selected_class = self.entire_pair[selected_impression]
overlap_class = list(set(orig_class).intersection(selected_class))
if len(overlap_class) == 0:
break
# replace with one sentence from candidate
selected_sentences_ = selected_impression.split('.')
selected_sentences = []
for s in selected_sentences_:
if len(s) < 3:
continue
if s[0] == ' ':
s = s[1:]
selected_sentences.append(s)
sel_idx = random.randint(0, len(selected_sentences) - 1)
selected_sentence = selected_sentences[sel_idx]
sentences_ = impression.split('.')
sentences = []
for s_ in sentences_:
if len(s_) < 3:
continue
if s_[0] == ' ':
s_ = s_[1:]
sentences.append(s_)
idx = random.randint(0, len(sentences) - 1)
sentences[idx] = selected_sentence
impression = '. '.join(sentences) + '.'
return impression
def positive(self, impression):
'Set: N -> F (random), false positive'
original_class = self.entire_pair[impression]
if 'positive_No Finding' in original_class:
while True:
selected_impression = random.choice(list(self.entire_pair.keys()))
selected_class = self.entire_pair[selected_impression]
if not 'positive_No Finding' in selected_class:
break
return selected_impression
else:
return impression
def negative(self, impression):
'Set: F -> N (random), false positive'
original_class = self.entire_pair[impression]
if not 'positive_No Finding' in original_class:
while True:
selected_impression = random.choice(list(self.entire_pair.keys()))
selected_class = self.entire_pair[selected_impression]
if 'positive_No Finding' in selected_class:
break
return selected_impression
else:
return impression
def __call__(self, impression, FP=False, FN=False, L=False, E=False, M=False, train=True):
"""
Realistic error generation
input: report (text)
output: report with error (text)
"""
if train:
if random.random() <= self.probability:
selected = random.randint(0, 4)
# if O:
# if random.random() <= self.probability:
# impression = self.omission(impression)
# if C:
# if random.random() <= self.probability:
# impression = self.confusion(impression)
if selected == 0:
impression = self.positive(impression)
if selected == 1:
impression = self.negative(impression)
if selected == 2:
impression = self.location(impression)
if selected == 3:
impression = self.extent(impression)
if selected == 4:
impression = self.mismatch(impression)
else:
if random.random() <= self.probability:
if FP:
impression = self.positive(impression)
elif FN:
impression = self.negative(impression)
elif L:
impression = self.location(impression)
elif E:
impression = self.extent(impression)
elif M:
impression = self.mismatch(impression)
else:
pass
return impression
if __name__ == '__main__':
corpus_dir = '/COVID_8TB/sangjoon/mimic_CXR/mimic_impressions_final.csv'
label_dir = '/COVID_8TB/sangjoon/mimic-cxr/mimic-cxr-2.0.0-chexpert.csv'
pair_dir = './entire_pair_final.json'
with open(pair_dir, 'r') as f:
entire_pair = json.load(f, encoding='cp949')
entire_texts = pd.read_csv(corpus_dir)
entire_labels = pd.read_csv(label_dir)
error_gen = ErrorGenerator(entire_texts, entire_labels, entire_pair)
# error_gen = ErrorGenerator(entire_pair, probability=0.5)
for sentence in list(entire_texts.impression):
sentence = pre_caption(sentence)
print('original sentence: {}'.format(sentence))
try:
error_report = error_gen(sentence, M=True)
except:
error_report = sentence
print('error sentence: {}'.format(error_report))
print('-----------------------------------------------------')