-
Notifications
You must be signed in to change notification settings - Fork 9
/
umls.py
executable file
·61 lines (47 loc) · 1.7 KB
/
umls.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
import json
from collections import Counter
class UMLS_KB(object):
def __init__(self, umls_version):
self.umls_data = None
self.umls_version = umls_version
self.load(umls_version)
def load(self, umls_version):
json_path = 'data/UMLS/%s.json' % umls_version
with open(json_path, 'r') as json_f:
self.umls_data = json.load(json_f)
def get_sts(self, cui):
return self.umls_data[cui]['STY']
def get_aliases(self, cui, include_name=True):
aliases = self.umls_data[cui]['STR']
if include_name:
aliases.append(self.umls_data[cui]['Name'])
return aliases
def get_all_cuis(self):
#
return list(self.umls_data.keys())
def get_all_stys(self):
#
all_stys = set()
for cui in self.get_all_cuis():
for sty in self.get_sts(cui):
all_stys.add(sty)
return list(all_stys)
def get_sty_sizes(self):
#
sty_sizes = Counter()
for cui in self.get_all_cuis():
for sty in self.get_sts(cui):
sty_sizes[sty] += 1
return list(sty_sizes.most_common())
def pprint(self, cui):
cui_info = self.umls_data[cui]
s = ''
s += 'CUI: %s Name: %s\n' % (cui, cui_info['Name'])
# s += 'Definition: %s\n' % '; '.join(cui_info['DEF'])
s += 'Aliases (%d): %s\n' % (len(cui_info['STR']), '; '.join(cui_info['STR'][:5]))
s += 'Types: %s\n' % '; '.join(cui_info['STY'])
print(s)
umls_kb_st21pv = UMLS_KB('umls.2017AA.active.st21pv')
umls_kb_full = UMLS_KB('umls.2017AA.active.full')
if __name__ == '__main__':
umls_kb_st21pv.pprint('C0001097')