forked from AnthonyMRios/multi-label-zero-shot
-
Notifications
You must be signed in to change notification settings - Fork 2
/
load_data.py
353 lines (319 loc) · 11.9 KB
/
load_data.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
import re
import csv
import sys
import random
import json
import numpy as np
import gensim
from gensim.models.keyedvectors import KeyedVectors
import nltk
nltk.download('punkt')
def short_to_decimal1(code):
"""
Convert an ICD9 code from short format to decimal format.
"""
if len(code) > 2:
return code[:2] + '.' + code[2:]
else:
return code
def _zero_pad(x, n=2):
if len(x) < n:
x = (n - len(x)) * "0" + x
return x
def decimal_to_short(code):
"""
Convert an ICD9 code from decimal format to short format.
"""
parts = code.split(".")
parts[0] = _zero_pad(parts[0])
return "".join(parts)
def short_to_decimal(code):
"""
Convert an ICD9 code from short format to decimal format.
"""
if 'V' not in code and 'E' not in code:
if len(code) > 3:
return code[:3] + '.' + code[3:]
else:
return code
elif 'V' in code:
if len(code) > 3:
return code[:3] + '.' + code[3:]
else:
return code
else:
if len(code) > 4:
return code[:4] + '.' + code[4:]
else:
return code
def load_descriptions_mim2():
lookup = {}
tmp_desc = {}
with open('/home/amri228/final_paper/data/mimic2/ICD9_descriptions', 'r') as in_file:
for row in in_file:
data = row.strip().split('\t')
tmp_desc[data[0]] = data[1]
with open('/home/amri228/final_paper/data/mimic2/lookup_ids_labels.txt', 'r') as in_file:
for row in in_file:
data = row.strip().split('|')
lookup[data[0]] = data[1]
descs = []
with open('/home/amri228/final_paper/data/mimic2/all_labels_final.txt', 'r') as in_file:
for row in in_file:
data = row.strip()
#if lookup[data] in tmp_desc:
descs.append(tmp_desc[lookup[data]])
#else:
# descs.append('unknownabc')
return descs
def load_descriptions():
lookup = {}
tmp_desc = {}
missing = 0
with open('/home/amri228/final_paper/data/mimic2/ICD9_descriptions', 'r') as in_file:
for row in in_file:
data = row.strip().split('\t')
tmp_desc[data[0]] = data[1]
if data[0] == '53.83':
print decimal_to_short(data[0])
#lookup[decimal_to_short(data[0])] = data[0]
lookup[data[0]] = data[0]
with open('/home/amri228/final_paper/data/icd9.txt', 'r') as in_file:
for row in in_file:
data = row.strip().split('\t')
if data[0] in lookup:
continue
tmp_desc[short_to_decimal(data[0])] = data[1]
lookup[short_to_decimal(data[0])] = short_to_decimal(data[0])
with open('/home/amri228/final_paper/data/mimic3/all_data/D_ICD_PROCEDURES.csv', 'r') as in_file:
iCSV = csv.reader(in_file, delimiter=',')
for row in iCSV:
data = []
data.append(short_to_decimal1(row[1]))
data.append(row[3])
if data[0] in lookup:
continue
if row[1] == '400':
print data
tmp_desc[short_to_decimal1(data[0])] = data[1]
lookup[short_to_decimal1(data[0])] = short_to_decimal1(data[0])
with open('/home/amri228/final_paper/data/mimic3/all_data/D_ICD_DIAGNOSES.csv', 'r') as in_file:
iCSV = csv.reader(in_file, delimiter=',')
for row in iCSV:
data = []
data.append(short_to_decimal(row[1]))
data.append(row[3])
if data[0] in lookup:
continue
tmp_desc[short_to_decimal(data[0])] = data[1]
lookup[short_to_decimal(data[0])] = short_to_decimal(data[0])
with open('/home/amri228/final_paper/data/concept_embeddings/eval/cui_icd9.txt', 'r') as in_file:
for row in in_file:
data = row.strip().split('|')
if data[0] in lookup:
continue
missing += 1
tmp_desc[data[10]] = data[14]
#lookup[decimal_to_short(data[10])] = data[10]
lookup[data[10]] = data[10]
descs = []
#with open('/home/amri228/final_paper/data/mimic2/all_labels_final.txt', 'r') as in_file:
missing = []
with open('/home/amri228/final_paper/data/mimic3/fixed2_all_labels_final.txt', 'r') as in_file:
for row in in_file:
data = row.strip()
if data in lookup:
#descs.append(tmp_desc[lookup[data]])
descs.append(tmp_desc[data])
else:
missing.append(data)
descs.append('UNK')
print 'total missing', len(missing), missing
sys.stdout.flush()
return descs
def load_data_file(txt_filename):
txt = open(txt_filename, 'r')
X_txt = []
Y = []
for row in txt:
data = json.loads(row.strip())
#X_txt.append(' '.join(nltk.word_tokenize(data['text'])))
if 'txt' in data:
X_txt.append(data['txt'])
else:
X_txt.append(data['text'])
Y.append([x for x in data['labels'] if x != ''])
txt.close()
return X_txt, Y
class ProcessData(object):
def __init__(self, pretrain_wv=None, lower=True, min_df=5, nltk=False):
self.pattern = re.compile(r'(?u)\b\w\w+\b')
#self.pattern = re.compile('[A-Z][a-z]+')
self.nltk = nltk
self.min_df = min_df
self.lower = lower
if pretrain_wv is not None:
#self.wv = gensim.models.Word2Vec.load(pretrain_wv)
self.wv = KeyedVectors.load_word2vec_format('/home/amri228/chemprot/data2/glove/glove_300d_w2v_format.txt', binary=False)
else:
self.wv = None
self.embs = [np.zeros((300,)),
np.random.uniform(-1.,1., (300,))*0.01]
self.word_index = {None:0, 'UNK':1}
def _tokenize(self, string):
if self.lower:
example = string.strip().lower()
else:
example = string.strip().lower()
if self.nltk and False:
return nltk.word_tokenize(example)
else:
return re.findall(self.pattern, example)
def fit(self, data):
token_cnts = {}
excnt = 1
for ex in data:
#print excnt
excnt += 1
example_tokens = self._tokenize(ex)
for token in example_tokens:
if token not in token_cnts:
token_cnts[token] = 1
else:
token_cnts[token] += 1
index = 2
for value, key in enumerate(token_cnts):
if value < self.min_df:
continue
self.word_index[key] = index
if self.wv is not None:
if key in self.wv:
self.embs.append(self.wv[key])
else:
self.embs.append(np.random.uniform(-1.,1., (300,))*0.01)
else:
#self.embs.append(np.random.random((300,))*0.01)
self.embs.append(np.random.uniform(-1.,1., (300,))*0.01)
index += 1
self.embs = np.array(self.embs)
del self.wv
return
def fit_transform(self, data):
self.fit(data)
return self.transform(data)
def transform(self, data):
return_dataset = []
for ex in data:
example = self._tokenize(ex)
index_example = []
for token in example:
if token in self.word_index:
index_example.append(self.word_index[token])
else:
index_example.append(self.word_index['UNK'])
return_dataset.append(index_example)
return return_dataset
def pad_data(self, data, to_shuffle=False):
max_len = np.max([len(x) for x in data]) + 5
padded_dataset = []
for ex in data:
if to_shuffle:
#example = random.sample(ex, len(ex))
example = ex
else:
example = ex
zeros = [0]*(max_len-len(example))
padded_dataset.append(example+zeros)
return np.array(padded_dataset)
def pad_data_hier(self, data):
max_sents = np.max([len(x) for x in data])
max_len = np.max([len(x) for y in data for x in y])
padded_dataset = []
for par in data:
pad_sents = []
for example in par:
zeros = [0]*(max_len-len(example))
pad_sents.append(example+zeros)
for x in range(max_sents-len(par)):
zeros = [0]*max_len
pad_sents.append(zeros)
padded_dataset.append(pad_sents)
return np.array(padded_dataset)
class ProcessHierData(object):
def __init__(self, pretrain_wv=None, lower=True, min_df=5):
self.pattern = re.compile(r'(?u)\b\w\w+\b')
self.min_df = min_df
self.lower = lower
if pretrain_wv is not None:
self.wv = gensim.models.Word2Vec.load(pretrain_wv)
else:
self.wv = None
self.embs = [np.zeros((300,)),
np.random.random((300,))*0.01]
self.word_index = {None:0, 'UNK':1}
def _tokenize(self, string):
if self.lower:
example = string.strip().lower()
else:
example = string.strip()
return re.findall(self.pattern, example)
def fit(self, data):
token_cnts = {}
for par in data:
sent_text = nltk.sent_tokenize(par)
for ex in sent_text:
example_tokens = self._tokenize(ex)
for token in example_tokens:
if token not in token_cnts:
token_cnts[token] = 1
else:
token_cnts[token] += 1
index = 2
for value, key in enumerate(token_cnts):
if value < self.min_df:
continue
self.word_index[key] = index
if self.wv is not None:
if key in self.wv:
self.embs.append(self.wv[key])
else:
self.embs.append(np.random.random((300,))*0.01)
else:
self.embs.append(np.random.random((300,))*0.01)
index += 1
self.embs = np.array(self.embs)
del self.wv
return
def fit_transform(self, data):
self.fit(data)
return self.transform(data)
def transform(self, data):
return_dataset = []
for par in data:
sent_text = nltk.sent_tokenize(par)
index_sents = []
for ex in sent_text:
example = self._tokenize(ex)
index_example = []
for token in example:
if token in self.word_index:
index_example.append(self.word_index[token])
else:
index_example.append(self.word_index['UNK'])
index_sents.append(index_example)
return_dataset.append(index_sents)
return return_dataset
def pad_data(self, data):
max_sents = np.max([len(x) for x in data])
max_len = np.max([len(x) for y in data for x in y])
padded_dataset = []
for par in data:
pad_sents = []
for example in par:
zeros = [0]*(max_len-len(example))
pad_sents.append(example+zeros)
for x in range(max_sents-len(par)):
zeros = [0]*max_len
pad_sents.append(zeros)
padded_dataset.append(pad_sents)
return np.array(padded_dataset)