forked from gsn245/Selective_SSM_for_EHR_Classification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mortality_classification.py
321 lines (281 loc) · 10.4 KB
/
mortality_classification.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
import logging
import torch
import numpy as np
import matplotlib.pyplot as plt
import tqdm
from torch.utils.data import DataLoader
from torch import nn
from sklearn import metrics
import json
import pandas as pd
from mortality_part_preprocessing import PairedDataset, MortalityDataset
from models.regular_transformer import EncoderClassifierRegular
from models.early_stopper import EarlyStopping
from models.deep_set_attention import DeepSetAttentionModel
from models.grud import GRUDModel
from models.ip_nets import InterpolationPredictionModel
def train_test(
train_pair,
val_data,
test_data,
output_path,
model_type,
model_args,
batch_size=64,
epochs=300,
patience=5,
lr=0.0001,
early_stop_criteria="auroc"
):
train_batch_size = batch_size // 2 # we concatenate 2 batches together
train_collate_fn = PairedDataset.paired_collate_fn_truncate
val_test_collate_fn = MortalityDataset.non_pair_collate_fn_truncate
train_dataloader = DataLoader(train_pair, train_batch_size, shuffle=True, num_workers=16, collate_fn=train_collate_fn, pin_memory=True)
test_dataloader = DataLoader(test_data, batch_size, shuffle=True, num_workers=16, collate_fn=val_test_collate_fn, pin_memory=True)
val_dataloader = DataLoader(val_data, batch_size, shuffle=False, num_workers=16, collate_fn=val_test_collate_fn, pin_memory=True)
# assign GPU
if torch.cuda.is_available():
dev = "cuda"
else:
dev = "cpu"
device = torch.device(dev)
val_loss, model = train(
train_dataloader=train_dataloader,
val_dataloader=val_dataloader,
output_path=output_path,
epochs=epochs,
patience=patience,
device=device,
model_type=model_type,
batch_size=batch_size,
lr=lr,
early_stop_criteria=early_stop_criteria,
model_args=model_args
)
loss, accuracy_score, auprc_score, auc_score = test(
test_dataloader=test_dataloader,
output_path=output_path,
device=device,
model_type=model_type,
model=model,
model_args=model_args,
)
return loss, accuracy_score, auprc_score, auc_score
def train(
train_dataloader,
val_dataloader,
output_path,
epochs,
patience,
device,
model_type,
lr,
early_stop_criteria,
model_args,
**kwargs,
):
"""
training
"""
iterable_inner_dataloader = iter(train_dataloader)
test_batch = next(iterable_inner_dataloader)
max_seq_length = test_batch[0].shape[2]
sensor_count = test_batch[0].shape[1]
static_size = test_batch[2].shape[1]
# make a new model and train
if model_type == "grud":
model = GRUDModel(
input_dim=sensor_count,
static_dim=static_size,
output_dims=2,
device=device,
**model_args
)
elif model_type == "ipnets":
model = InterpolationPredictionModel(
output_dims=2,
sensor_count=sensor_count,
**model_args
)
elif model_type == "seft":
model = DeepSetAttentionModel(
output_activation=None,
n_modalities=sensor_count,
output_dims=2,
**model_args
)
elif model_type == "transformer":
model = EncoderClassifierRegular(
num_classes=2,
device=device,
max_timepoint_count=max_seq_length,
sensors_count=sensor_count,
static_count=static_size,
return_intermediates=False,
**model_args
)
model_parameters = filter(lambda p: p.requires_grad, model.parameters())
params = sum([np.prod(p.size()) for p in model_parameters])
print(f"# of trainable parameters: {params}")
criterion = nn.CrossEntropyLoss() # loss
optimizer = torch.optim.Adam(
model.parameters(), lr=lr
)
early_stopping = EarlyStopping(
patience=patience, verbose=True, path=f"{output_path}/checkpoint.pt"
) # set up early stopping
# initialize results file
with open(f"{output_path}/training_log.csv", "w") as train_log:
train_log.write(
",".join(["epoch", "train_loss", "val_loss", "val_roc_auc_score"]) + "\n"
)
for epoch in range(epochs):
# training step
model.train().to(device) # sets training mode
loss_list = []
for batch in tqdm.tqdm(train_dataloader, total=len(train_dataloader)):
data, times, static, labels, mask, delta = batch
if model_type != "grud":
data = data.to(device)
static = static.to(device)
times = times.to(device)
mask = mask.to(device)
delta = delta.to(device)
optimizer.zero_grad()
predictions = model(
x=data, static=static, time=times, sensor_mask=mask, delta=delta
)
if type(predictions) == tuple:
predictions, recon_loss = predictions
else:
recon_loss = 0
predictions = predictions.squeeze(-1)
loss = criterion(predictions.cpu(), labels) + recon_loss
loss_list.append(loss.item())
loss.backward()
optimizer.step()
accum_loss = np.mean(loss_list)
# validation step
model.eval().to(device)
labels_list = torch.LongTensor([])
predictions_list = torch.FloatTensor([])
with torch.no_grad():
for batch in val_dataloader:
data, times, static, labels, mask, delta = batch
labels_list = torch.cat((labels_list, labels), dim=0)
if model_type != "grud":
data = data.to(device)
static = static.to(device)
times = times.to(device)
mask = mask.to(device)
delta = delta.to(device)
predictions = model(
x=data, static=static, time=times, sensor_mask=mask, delta=delta
)
if type(predictions) == tuple:
predictions, _ = predictions
predictions = predictions.squeeze(-1)
predictions_list = torch.cat(
(predictions_list, predictions.cpu()), dim=0
)
probs = torch.nn.functional.softmax(predictions_list, dim=1)
auc_score = metrics.roc_auc_score(labels_list, probs[:, 1])
aupr_score = metrics.average_precision_score(labels_list, probs[:, 1])
val_loss = criterion(predictions_list.cpu(), labels_list)
with open(f"{output_path}/training_log.csv", "a") as train_log:
train_log.write(
",".join(map(str, [epoch + 1, accum_loss, val_loss.item(), auc_score]))
+ "\n"
)
print(f"Epoch: {epoch+1}, Train Loss: {accum_loss}, Val Loss: {val_loss}")
# set early stopping
if early_stop_criteria == "auroc":
early_stopping(1 - auc_score, model)
elif early_stop_criteria == "auprc":
early_stopping(1 - aupr_score, model)
elif early_stop_criteria == "auprc+auroc":
early_stopping(1 - (aupr_score + auc_score), model)
elif early_stop_criteria == "loss":
early_stopping(val_loss, model)
if early_stopping.early_stop:
print("Early stopping")
break
# save training curves
training_log = pd.read_csv(f"{output_path}/training_log.csv")
fig = plt.figure()
fig.suptitle("training curves")
ax0 = fig.add_subplot(121, title="loss")
ax0.plot(training_log["train_loss"], label="Training")
ax0.plot(training_log["val_loss"], label="Validation")
ax0.legend()
ax1 = fig.add_subplot(122, title="auroc")
ax1.plot(training_log["val_roc_auc_score"], label="Training")
ax1.legend()
fig.savefig(f"{output_path}/train_curves.jpg")
return val_loss, model
def test(
test_dataloader,
output_path,
device,
model_type,
model,
**kwargs,
):
iterable_dataloader = iter(test_dataloader)
test_batch = next(iterable_dataloader)
max_seq_length = test_batch[0].shape[2]
sensor_count = test_batch[0].shape[1]
static_size = test_batch[2].shape[1]
criterion = nn.CrossEntropyLoss()
model.load_state_dict(
torch.load(f"{output_path}/checkpoint.pt")
) # NEW: reload best model
model.eval().to(device)
labels_list = torch.LongTensor([])
predictions_list = torch.FloatTensor([])
with torch.no_grad():
for batch in test_dataloader:
data, times, static, labels, mask, delta = batch
labels_list = torch.cat((labels_list, labels), dim=0)
if model_type != "grud":
data = data.to(device)
static = static.to(device)
times = times.to(device)
mask = mask.to(device)
delta = delta.to(device)
predictions = model(
x=data, static=static, time=times, sensor_mask=mask, delta=delta
)
if type(predictions) == tuple:
predictions, _ = predictions
predictions = predictions.squeeze(-1)
predictions_list = torch.cat((predictions_list, predictions.cpu()), dim=0)
loss = criterion(predictions_list.cpu(), labels_list)
print(f"Test Loss: {loss}")
probs = torch.nn.functional.softmax(predictions_list, dim=1)
results = metrics.classification_report(
labels_list, torch.argmax(probs, dim=1), output_dict=True # predictions_list
)
cm = metrics.confusion_matrix(
labels_list, torch.argmax(probs, dim=1)
)
auc_score = metrics.roc_auc_score(labels_list, probs[:, 1])
auprc_score = metrics.average_precision_score(labels_list, probs[:, 1])
accuracy_score = metrics.accuracy_score(labels_list, np.argmax(probs, axis=1))
print(results)
print(cm)
print(f"Accuracy = {accuracy_score}")
print(f"AUPRC = {auprc_score}")
print(f"AUROC = {auc_score}")
# save test metrics
test_metrics = {
"test_loss": loss.item(),
"accuracy": accuracy_score,
"AUPRC": auprc_score,
"AUROC": auc_score,
}
test_metrics.update(results)
# test_metrics.update(cm) # TO DO: add later
with open(f"{output_path}/test_results.json", "w") as fp:
json.dump(test_metrics, fp)
return loss, accuracy_score, auprc_score, auc_score