forked from danlou/MedLinker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
medlinker.py
executable file
·318 lines (243 loc) · 10.8 KB
/
medlinker.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
import numpy as np
from sklearn.externals import joblib
from pytt_hf import toks2vecs
from matcher_simstring import SimString_UMLS
from matcher_exactmatch import WhitespaceTokenizer # ???
from vectorspace import VSM
# from vectorspace import FaissVSM
from matcher_softmax import SoftMax_CLF
def norm(v):
return v / np.linalg.norm(v)
class MedLinkerDoc(object):
def __init__(self, text, tokens, spans):
#
self.text = text
self.tokens = tokens
self.spans = spans
self.vectors = []
def set_contextual_vectors(self):
#
self.vectors = toks2vecs(self.tokens, return_tokens=False)
assert len(self.vectors) == len(self.tokens)
def get_span_vector(self, span_start, span_end, normalize=False):
#
span_vecs = [self.vectors[i] for i in range(span_start, span_end)]
span_vec = np.array(span_vecs).mean(axis=0)
if normalize:
span_vec = norm(span_vec)
return span_vec
def get_spans(self, include_vectors=True, normalize=False):
#
output_spans = []
for span_start, span_end in self.spans:
if include_vectors and len(self.vectors) > 0:
span_vec = self.get_span_vector(span_start, span_end, normalize=normalize)
output_spans.append((span_start, span_end, span_vec))
else:
output_spans.append((span_start, span_end))
return output_spans
class MedLinker(object):
def __init__(self, medner, umls_kb):
#
self.medner = medner
self.umls_kb = umls_kb
self.string_matcher = None
self.exact_matcher = None
self.sty_clf = None
self.cui_clf = None
self.st_vsm = None
self.cui_vsm = None
self.cui_validator = None
self.cui_validator_thresh = None
self.st_validator = None
self.st_validator_thresh = None
def load_sty_clf(self, model_path):
#
self.sty_clf = SoftMax_CLF(threshold=0.5)
self.sty_clf.load(model_path, model_path.replace('.h5', '.map'))
def load_cui_clf(self, model_path):
#
self.cui_clf = SoftMax_CLF(threshold=0.5)
self.cui_clf.load(model_path, model_path.replace('.h5', '.map'))
def load_st_VSM(self, st_vsm_path):
#
self.st_vsm = VSM(st_vsm_path)
def load_cui_VSM(self, cui_vecs_path):
#
self.cui_vsm = VSM(cui_vecs_path)
def load_cui_validator(self, clf_path, validator_thresh=0.5):
#
self.cui_validator = joblib.load(clf_path)
self.cui_validator_thresh = validator_thresh
def load_st_validator(self, clf_path, validator_thresh=0.5):
#
self.st_validator = joblib.load(clf_path)
self.st_validator_thresh = validator_thresh
def load_string_matcher(self, ngram_db_path, ngram_map_path):
#
self.string_matcher = SimString_UMLS(self.umls_kb, ngram_db_path, ngram_map_path)
def load_exact_matcher(self, em_path):
# TO-DO
pass
def predict(self, sentence,
gold_tokens=None, gold_spans=None,
predict_cui=True, predict_sty=True,
require_cui=True, require_sty=False):
#
if (gold_tokens is not None) and (gold_spans is not None):
tokens, spans = gold_tokens, gold_spans
else:
tokens, spans = self.medner.predict(sentence)
doc = MedLinkerDoc(sentence, tokens, spans)
doc.set_contextual_vectors()
r = {'sentence': sentence, 'tokens': tokens, 'spans': []}
# for span_start, span_end, span_vec in doc.get_spans(include_vectors=True, normalize=True):
for span_start, span_end, span_vec in doc.get_spans(include_vectors=True, normalize=False):
span_str = ' '.join(doc.tokens[span_start:span_end])
span_info = {'start': span_start, 'end': span_end, 'text': span_str, 'st': None, 'cui': None}
if predict_cui:
span_cuis = self.match_cui(span_str, span_vec)
if span_cuis is not None:
span_info['cui'] = span_cuis[0]
elif require_cui:
continue
if predict_sty:
span_sts = self.match_sty(span_str, span_vec)
if span_sts is not None:
span_info['st'] = span_sts[0]
elif require_sty:
continue
r['spans'].append(span_info)
return r
def match_sty(self, span_str, span_ctx_vec):
#
matches_str = []
if self.string_matcher is not None:
matches_str = self.string_matcher.match_sts(span_str.lower())
elif self.exact_matcher is not None:
matches_str = self.exact_matcher.match_sts(span_str.lower())
matches_str = [(st, 1/(1+idx)) for idx, (st, _, _, _) in enumerate(matches_str)]
matches_ctx = []
if self.sty_clf is not None:
matches_ctx = self.sty_clf.predict(span_ctx_vec)
elif self.st_vsm is not None:
span_ctx_vec = norm(span_ctx_vec)
matches_ctx = self.st_vsm.most_similar(span_ctx_vec, threshold=0.5)
scores_str, scores_ctx = dict(matches_str), dict(matches_ctx)
matches = {sty: max(scores_str.get(sty, 0), scores_ctx.get(sty, 0))
for sty in scores_str.keys() | scores_ctx.keys()}
matches = sorted(matches.items(), key=lambda x: x[1], reverse=True)
if (self.st_validator is not None) and (len(matches) > 0):
pred_valid = self.validate_st_pred(matches_str, scores_str,
matches_ctx, scores_ctx,
matches)
if pred_valid == False:
matches = {}
if len(matches) > 0:
return matches
else:
return None
def match_cui(self, span_str, span_ctx_vec):
#
matches_str = []
if self.string_matcher is not None:
matches_str = self.string_matcher.match_cuis(span_str.lower())
elif self.exact_matcher is not None:
matches_str = self.exact_matcher.match_cuis(span_str.lower())
matches_str = [(cui, 1/(1+idx)) for idx, (cui, _, _, _) in enumerate(matches_str)]
matches_ctx = []
if self.cui_clf is not None:
matches_ctx = self.cui_clf.predict(span_ctx_vec)
elif self.cui_vsm is not None:
span_ctx_vec = norm(span_ctx_vec)
matches_ctx = self.cui_vsm.most_similar(span_ctx_vec, threshold=0.5)
scores_str, scores_ctx = dict(matches_str), dict(matches_ctx)
matches = {cui: max(scores_str.get(cui, 0), scores_ctx.get(cui, 0))
for cui in scores_str.keys() | scores_ctx.keys()}
matches = sorted(matches.items(), key=lambda x: x[1], reverse=True)
if (self.cui_validator is not None) and (len(matches) > 0):
pred_valid = self.validate_cui_pred(matches_str, scores_str,
matches_ctx, scores_ctx,
matches)
if pred_valid == False:
matches = {}
if len(matches) > 0:
return matches
else:
return None
def validate_cui_pred(self, matches_str, scores_str, matches_vsm, scores_vsm, matches_joint):
matchers_agree = False
if len(matches_str) > 0 and len(matches_vsm) > 0:
if matches_str[0][0] == matches_vsm[0][0]:
matchers_agree = True
top_match = matches_joint[0][0]
x = []
if len(matches_str) > 0:
x.append(matches_str[0][1])
else:
x.append(0)
if len(matches_vsm) > 0:
x.append(matches_vsm[0][1])
else:
x.append(0)
x.append(matches_joint[0][1])
x.append((scores_str.get(top_match, 0) + scores_vsm.get(top_match, 0))/2)
x.append(int(matchers_agree))
prob_F, prob_T = self.cui_validator.predict_proba([x])[0]
if prob_T >= self.cui_validator_thresh:
return True
else:
return False
def validate_st_pred(self, matches_str, scores_str, matches_vsm, scores_vsm, matches_joint):
matchers_agree = False
if len(matches_str) > 0 and len(matches_vsm) > 0:
if matches_str[0][0] == matches_vsm[0][0]:
matchers_agree = True
top_match = matches_joint[0][0]
x = []
if len(matches_str) > 0:
x.append(matches_str[0][1])
else:
x.append(0)
if len(matches_vsm) > 0:
x.append(matches_vsm[0][1])
else:
x.append(0)
x.append(matches_joint[0][1])
x.append((scores_str.get(top_match, 0) + scores_vsm.get(top_match, 0))/2)
x.append(int(matchers_agree))
prob_F, prob_T = self.st_validator.predict_proba([x])[0]
if prob_T >= self.st_validator_thresh:
return True
else:
return False
if __name__ == '__main__':
from medner import MedNER
from umls import umls_kb_st21pv as umls_kb
# default models, best configuration from paper
# to experiment with different configurations, just comment/uncomment components
cx_ner_path = 'models/ContextualNER/mm_st21pv_SCIBERT_uncased/'
em_ner_path = 'models/ExactMatchNER/umls.2017AA.active.st21pv.nerfed_nlp_and_matcher.max3.p'
ngram_db_path = 'models/SimString/umls.2017AA.active.st21pv.aliases.3gram.5toks.db'
ngram_map_path = 'models/SimString/umls.2017AA.active.st21pv.aliases.5toks.map'
st_vsm_path = 'models/VSMs/mm_st21pv.sts_anns.scibert_scivocab_uncased.vecs'
cui_vsm_path = 'models/VSMs/mm_st21pv.cuis.scibert_scivocab_uncased.vecs'
cui_clf_path = 'models/Classifiers/softmax.cui.h5'
sty_clf_path = 'models/Classifiers/softmax.sty.h5'
cui_val_path = 'models/Validators/mm_st21pv.lr_clf_cui.dev.joblib'
sty_val_path = 'models/Validators/mm_st21pv.lr_clf_sty.dev.joblib'
print('Loading MedNER ...')
medner = MedNER(umls_kb)
medner.load_contextual_ner(cx_ner_path)
print('Loading MedLinker ...')
medlinker = MedLinker(medner, umls_kb)
medlinker.load_string_matcher(ngram_db_path, ngram_map_path) # simstring approximate string matching
# medlinker.load_st_VSM(st_vsm_path)
medlinker.load_sty_clf(sty_clf_path)
# medlinker.load_st_validator(sty_val_path, validator_thresh=0.45)
# medlinker.load_cui_VSM(cui_vsm_path)
medlinker.load_cui_clf(cui_clf_path)
# medlinker.load_cui_validator(cui_val_path, validator_thresh=0.70)
s = 'Myeloid derived suppressor cells (MDSC) are immature myeloid cells with immunosuppressive activity.'
r = medlinker.predict(s)
print(r)