forked from ulzee/dkbehrt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
202 lines (164 loc) · 7.03 KB
/
utils.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
import torch
from torch.utils.data import Dataset
import numpy as np
from random import shuffle
import pickle as pk
import pandas as pd
from tqdm import tqdm
def gather_hidden_params(mdl):
c = 0
for ch in mdl.children():
c += gather_hidden_params(ch)
return c
def count_parameters(model):
trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
trainable_params += gather_hidden_params(model)
return trainable_params
def paramscan(mdl, d=0):
for ch in mdl.children():
print(' '*d, count_parameters(ch), str(ch)[:10])
paramscan(ch, d=d+1)
mf = lambda c: f'{c/1000/1000:.1f}M'
bf = lambda c: f'{c/1000/1000/1000:.1f}B'
class ICDDataset(Dataset):
def __init__(self, dxs, tokenizer, patient_ids, covs, separator, max_length=None, shuffle_in_visit=True):
self.dxs = dxs
self.shuffle_in_visit = shuffle_in_visit
self.max_length = max_length
self.covs = covs
self.tokenizer = tokenizer
self.separator = separator
pdict = { i: True for i in patient_ids }
self.pids = [i for i in dxs if i in pdict]
def __len__(self):
return len(self.pids)
def __getitem__(self, i):
concepts = self.dxs[self.pids[i]]['codes']['concepts']
visits = self.dxs[self.pids[i]]['codes']['visits']
vid = visits[0]
byvisit = []
cinv = []
current_v = visits[0]
for c, v in zip(concepts, visits):
if v != current_v:
byvisit += [(current_v, cinv)]
current_v = v
cinv = []
cinv += [c]
if len(cinv):
byvisit += [(current_v, cinv)]
code_series = ''
if self.covs is not None: cov_hist = []
for vi, (vid, cinv) in enumerate(byvisit):
if self.shuffle_in_visit:
shuffle(cinv)
for c in cinv:
code_series += f' {c}'
if self.covs is not None: cov_hist += [self.covs[vid]]
if vi != len(byvisit) - 1:
code_series += f' {self.separator}'
if self.covs is not None: cov_hist += [self.covs[None]]
sample = { k: v for k, v in self.tokenizer(
code_series, padding=True,
truncation=self.max_length is not None,
max_length=self.max_length).items() if k not in ['token_type_ids']}
if self.covs is not None:
blank_cov = [self.covs[None]]
if self.max_length is not None:
cov_hist = cov_hist[:self.max_length-2]
sample['position_ids'] = blank_cov + cov_hist + blank_cov # matches tokenizer pad
return sample
class EHROutcomesDataset(Dataset):
def __init__(self, task, ehr_outcomes_path, tokenizer, patient_ids, covs=None, code_resolution=5, separator='[SEP]', max_length=None, shuffle_in_visit=True, verbose=True):
with open(f'{ehr_outcomes_path}/dx.pk', 'rb') as fl:
self.dxs = pk.load(fl)
if task in ['mortality', 'los72']:
self.stays = pd.read_csv(f'{ehr_outcomes_path}/targets_by_icustay.csv')
else:
self.stays = pd.read_csv(f'{ehr_outcomes_path}/targets_diagnosis_{task}.csv')
self.shuffle_in_visit = shuffle_in_visit
self.tokenizer = tokenizer
self.separator = separator
self.code_resolution = code_resolution
self.max_length = max_length
self.covs = covs
self.task = task
pdict = { i: True for i in patient_ids }
self.pids = [i for i in self.stays['subject_id'].unique() if i in pdict]
self.stays = self.stays.set_index('subject_id').loc[self.pids].reset_index()
self.history = [
[[c[:code_resolution].lower() for c in self.dxs[h] if type(c) == str] if h in self.dxs else [] \
for h in eval(hids)] \
for hids in (tqdm if verbose else lambda x: x)(self.stays['past_visits'])]
self.labels = self.stays[task].values.tolist()
def __len__(self):
return len(self.stays)
def __getitem__(self, i):
code_series = ''
byvisit = self.history[i]
visit_ids = eval(self.stays['past_visits'][i])
if self.covs is not None: cov_hist = []
for vi, (vid, cinv) in enumerate(zip(visit_ids, byvisit)):
if self.shuffle_in_visit:
shuffle(cinv)
for c in cinv:
code_series += f' {c}'
if self.covs is not None: cov_hist += [self.covs[vid]]
if vi != len(byvisit) - 1:
code_series += f' {self.separator}'
if self.covs is not None: cov_hist += [self.covs[None]]
sample = { k: v for k, v in self.tokenizer(
code_series, padding=True,
truncation=self.max_length is not None,
max_length=self.max_length).items() if k not in ['token_type_ids']}
sample['labels'] = [[1.0, 0.0], [0.0, 1.0]][self.labels[i]]
if self.covs is not None:
blank_cov = [self.covs[None]]
if self.max_length is not None:
cov_hist = cov_hist[:self.max_length-2]
sample['position_ids'] = blank_cov + cov_hist + blank_cov # matches tokenizer pad
return sample
def topk_accuracy(output, target, topk=(1,)):
"""Computes the precision@k for the specified values of k"""
maxk = max(topk)
batch_size = target.shape[0]
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].reshape(-1).float().sum(0)
res.append(correct_k.mul_(100.0 / batch_size))
return res
class HugMetrics:
class TopN:
def __call__(self, eval_pred, mask_value=-100, topns=(1, 5, 10)):
logits, labels = eval_pred
bsize, seqlen = labels.shape
logits = torch.from_numpy(np.reshape(logits, (bsize*seqlen, -1)))
labels = torch.from_numpy(np.reshape(labels, (bsize*seqlen)))
where_prediction = labels != mask_value
topaccs = topk_accuracy(logits[where_prediction], labels[where_prediction], topk=topns)
assert { n: acc for n, acc in zip(topns, topaccs) }
def load_covariates(covfile='saved/cov.csv', covlist=['gender', 'age']):
cdf = pd.read_csv(covfile)
covs = dict()
def format_covs_as_position(covls):
flip_factor = 1
pos_value = 0.01
for cv, cname in zip(covls, covlist):
if cname == 'gender':
if cv in 'MF':
flip_factor = [-1, 1]['MF'.index(cv)]
else:
raise Exception('Not supported')
elif cname == 'age':
pos_value = cv/100/100
else:
print('WARN: unknown covariate')
return flip_factor * pos_value
for sample_covs in zip(*([cdf['hadm_id']] + [cdf[c] for c in covlist])):
sid, use_covs = sample_covs[0], sample_covs[1:]
covs[sid] = format_covs_as_position(use_covs)
covs[None] = 0
return covs