-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsynthetic_compute_metrics_full.py
153 lines (133 loc) · 6.97 KB
/
synthetic_compute_metrics_full.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
import os
import torch
import pickle
import numpy as np
from models import *
from data_object import *
from loss_metrics import *
import torch.nn.functional as F
from scipy.stats import bootstrap
from torch.utils.data import DataLoader
import ranking_metrics as ranking_metrics
from torch.distributions import Categorical
from torch.utils.data.sampler import SubsetRandomSampler
device = 'cuda' if torch.cuda.is_available() else 'cpu'
print('Using {} device'.format(device))
def compute_test_metrics(net, K, M):
rel_dif=[]
precision=[]
recall = []
model_rep=[]
base_rep = []
model_entr = []
base_entr = []
NDCG = []
for batch in test_dataloader:
trial = batch['trial'].to(device)
label = batch['label'].to(device).squeeze()
eth_label = batch['eth_label'].to(device)
inv_static = batch['inv_static'].to(device)
inv_dx = F.one_hot(batch['inv_dx'].type(torch.int64), num_classes=260).to(device)
inv_dx_len = batch['inv_dx_len'].cpu().type(torch.int64)
inv_rx = F.one_hot(batch['inv_rx'].type(torch.int64), num_classes=100).to(device)
inv_rx_len = batch['inv_rx_len'].cpu().type(torch.int64)
inv_enroll = batch['inv_enroll'].to(device)
inv_enroll_len = batch['inv_enroll_len'].cpu().type(torch.int64)
inv_mask = batch['inv_mask'].to(device)
if torch.sum(label) == 0:
continue
with torch.no_grad():
if net is None:
score = torch.rand(label.size(0),M).to(device)
elif isinstance(net, MCAT):
score = net(trial, inv_static, inv_dx, inv_dx_len, inv_rx, inv_rx_len, inv_enroll, inv_enroll_len, inv_mask, M).squeeze(-1)
elif isinstance(net, MCATFullData):
score = net(trial, inv_static, inv_dx, inv_dx_len, inv_rx, inv_rx_len, inv_enroll, inv_enroll_len, M).squeeze(-1)
rel_dif.extend(ranking_metrics.rel_err(score, label, K))
precision.extend(ranking_metrics.precision_K(score, label, K))
recall.extend(ranking_metrics.recall_K(score, label, K))
NDCG.extend(ranking_metrics.NDCG_K(score, label, K))
[avg_repn, base_repn] = ranking_metrics.compute_repn(score, label, K, eth_label)
model_entr.extend(Categorical(probs = avg_repn).entropy().tolist())
base_entr.extend(Categorical(probs = base_repn).entropy().tolist())
model_rep.extend(avg_repn)
base_rep.extend(base_repn)
model_rep = torch.mean(torch.stack(model_rep), dim=0).tolist()
base_rep = torch.mean(torch.stack(base_rep), dim=0).tolist()
rel_dif = bootstrap(np.expand_dims(np.array(rel_dif), axis=0), np.nanmean, vectorized=False).confidence_interval
precision = bootstrap(np.expand_dims(np.array(precision), axis=0), np.nanmean, vectorized=False).confidence_interval
recall = bootstrap(np.expand_dims(np.array(recall), axis=0), np.nanmean, vectorized=False).confidence_interval
model_entr = bootstrap(np.expand_dims(np.array(model_entr), axis=0), np.nanmean, vectorized=False).confidence_interval
base_entr = bootstrap(np.expand_dims(np.array(base_entr), axis=0), np.nanmean, vectorized=False).confidence_interval
NDCG = bootstrap(np.expand_dims(np.array(NDCG), axis=0), np.nanmean, vectorized=False).confidence_interval
return np.mean(rel_dif), np.mean(precision), np.mean(recall), np.mean(model_entr), np.mean(base_entr), np.mean(NDCG), model_rep, base_rep, np.mean(rel_dif) - rel_dif[0], np.mean(precision) - precision[0], np.mean(recall) - recall[0], np.mean(model_entr) - model_entr[0], np.mean(base_entr) - base_entr[0], np.mean(NDCG) - NDCG[0]
combos = {
20: {10: [0, 0.5, 1, 2, 4, 8]}
}
key_list = ['MCAT',
'MCAT_Full',
'FRAMM_FC_Full',
'Random']
if os.path.exists('synthetic_save/metrics_full.pkl'):
metrics = pickle.load(open('synthetic_save/metrics_full.pkl', 'rb'))
else:
metrics = {}
for M in combos:
trials_file = f'synthetic_data/trial_features_M{M}_test.pt'
labels_file = f'synthetic_data/labels_M{M}_test.pt'
inv_feat_static_file = f'synthetic_data/inv_features_static_M{M}_test.pt'
inv_feat_diagnosis_file = f'synthetic_data/inv_features_diagnosis_M{M}_test.pt'
inv_feat_diagnosis_lens_file = f'synthetic_data/inv_features_diagnosis_lens_M{M}_test.pt'
inv_feat_prescription_file = f'synthetic_data/inv_features_prescription_M{M}_test.pt'
inv_feat_prescription_lens_file = f'synthetic_data/inv_features_prescription_lens_M{M}_test.pt'
inv_feat_enrollment_file = f'synthetic_data/inv_features_history_M{M}_test.pt'
inv_feat_enrollment_lens_file = f'synthetic_data/inv_features_history_lens_M{M}_test.pt'
inv_masks_file = f'synthetic_data/inv_masks_M{M}_test_full.pt'
eth_labels_file = f'synthetic_data/eth_labels_M{M}_test.pt'
td = TrialDataset(trials_file, inv_feat_static_file, inv_feat_diagnosis_file, inv_feat_diagnosis_lens_file, inv_feat_prescription_file, inv_feat_prescription_lens_file, inv_feat_enrollment_file, inv_feat_enrollment_lens_file, inv_masks_file, labels_file, eth_labels_file)
test_sampler = SubsetRandomSampler(list(range(len(td))))
batch_size = 32
test_dataloader = DataLoader(td, batch_size, sampler=test_sampler)
trial_dim = len(td[0]['trial'])
dx_dim = 260
rx_dim = 100
lstm_dim = 128
embedding_dim = 128
num_layers = 1
hidden_dim = 64
static_dim = len(td[0]['inv_static'][0])
n_heads = 4
final_layer = 'Linear'
for K in combos[M]:
if M > K:
for lam in combos[M][K]:
for key in key_list:
print('\n\n\n')
print(key, 'M =', M, 'K = ', K, 'lam: ', lam)
if (key, M, K, lam) not in metrics:
if key == 'MCAT':
net = MCAT(trial_dim, static_dim, dx_dim, rx_dim, lstm_dim, embedding_dim, num_layers, hidden_dim, n_heads).to(device, non_blocking=True)
elif key == 'MCAT_Full':
net = MCAT(trial_dim, static_dim, dx_dim, rx_dim, lstm_dim, embedding_dim, num_layers, hidden_dim, n_heads).to(device, non_blocking=True)
elif key == 'FRAMM_FC_Full':
net = MCATFullData(trial_dim, static_dim, dx_dim, rx_dim, lstm_dim, embedding_dim, num_layers, hidden_dim, n_heads).to(device, non_blocking=True)
elif key == 'Random':
net = None
if key != 'Random':
model_path = f'synthetic_save/{key}_M_{M}_K_{K}_lam_{lam}.pt'
mdl = torch.load(model_path)
net.load_state_dict(mdl['model_state_dict'])
results_keys = ['rel_err', 'precision', 'recall', 'model_entr', 'base_entr', 'NDCG', 'model_rep', 'base_rep', 'rel_err_ci', 'precision_ci', 'recall_ci', 'model_entr_ci', 'base_entr_ci', 'NDCG_ci']
perf = compute_test_metrics(net, K, M)
results = {}
results.update(zip(results_keys, perf))
metrics[(key, M, K, lam)] = results
print('Mean relative error: ', metrics[(key, M, K, lam)]['rel_err'])
print('Mean precision: ', metrics[(key, M, K, lam)]['precision'])
print('Mean recall: ', metrics[(key, M, K, lam)]['recall'])
print('Mean model entropy: ', metrics[(key, M, K, lam)]['model_entr'])
print('Mean base entropy: ', metrics[(key, M, K, lam)]['base_entr'])
print('Mean NDCG: ', np.mean((metrics[(key, M, K, lam)]['NDCG'])))
print('Mean model repn: ', metrics[(key, M, K, lam)]['model_rep'])
print('Mean base repn: ', metrics[(key, M, K, lam)]['base_rep'])
pickle.dump(metrics, open('synthetic_save/metrics_full.pkl', 'wb'))