|
a |
|
b/evaluation/Evaluation_Primary.ipynb |
|
|
1 |
{ |
|
|
2 |
"cells": [ |
|
|
3 |
{ |
|
|
4 |
"cell_type": "code", |
|
|
5 |
"execution_count": null, |
|
|
6 |
"metadata": {}, |
|
|
7 |
"outputs": [], |
|
|
8 |
"source": [ |
|
|
9 |
"import pickle\n", |
|
|
10 |
"import pandas as pd\n", |
|
|
11 |
"import numpy as np\n", |
|
|
12 |
"import scipy\n", |
|
|
13 |
"import joblib" |
|
|
14 |
] |
|
|
15 |
}, |
|
|
16 |
{ |
|
|
17 |
"cell_type": "code", |
|
|
18 |
"execution_count": null, |
|
|
19 |
"metadata": {}, |
|
|
20 |
"outputs": [], |
|
|
21 |
"source": [ |
|
|
22 |
"# Load ensemble of models\n", |
|
|
23 |
"\n", |
|
|
24 |
"models_dict = joblib.load('models_dict.joblib')\n", |
|
|
25 |
"models_dict.keys()" |
|
|
26 |
] |
|
|
27 |
}, |
|
|
28 |
{ |
|
|
29 |
"cell_type": "code", |
|
|
30 |
"execution_count": null, |
|
|
31 |
"metadata": {}, |
|
|
32 |
"outputs": [], |
|
|
33 |
"source": [ |
|
|
34 |
"# Load data: Data should be formatted as sample cohort. See README for example\n", |
|
|
35 |
"\n", |
|
|
36 |
"df_cohort = pd.read_csv('sample_cohort.csv')\n", |
|
|
37 |
"test_hosp, test_window, test_y = df_cohort['hosp_id'], df_cohort['window_id'], df_cohort['y']\n", |
|
|
38 |
"cohort_IDs = df_cohort.set_index('ID')[[]]" |
|
|
39 |
] |
|
|
40 |
}, |
|
|
41 |
{ |
|
|
42 |
"cell_type": "code", |
|
|
43 |
"execution_count": null, |
|
|
44 |
"metadata": {}, |
|
|
45 |
"outputs": [], |
|
|
46 |
"source": [ |
|
|
47 |
"df_cohort" |
|
|
48 |
] |
|
|
49 |
}, |
|
|
50 |
{ |
|
|
51 |
"cell_type": "code", |
|
|
52 |
"execution_count": null, |
|
|
53 |
"metadata": {}, |
|
|
54 |
"outputs": [], |
|
|
55 |
"source": [ |
|
|
56 |
"# Functions to assist with evaluation \n", |
|
|
57 |
"\n", |
|
|
58 |
"from sklearn import metrics, utils\n", |
|
|
59 |
"from joblib import Parallel, delayed\n", |
|
|
60 |
"\n", |
|
|
61 |
"\n", |
|
|
62 |
"def bootstrap_func(i, y_true, y_score):\n", |
|
|
63 |
" # Bootstrap resample to calculate AUROC\n", |
|
|
64 |
" yte_true_b, yte_pred_b = utils.resample(y_true, y_score, replace=True, random_state=i)\n", |
|
|
65 |
" return metrics.roc_curve(yte_true_b, yte_pred_b), metrics.roc_auc_score(yte_true_b, yte_pred_b)\n", |
|
|
66 |
"\n", |
|
|
67 |
"def get_roc_CI(y_true, y_score):\n", |
|
|
68 |
" # Bootstrap confidence intervals \n", |
|
|
69 |
" roc_curves, auc_scores = zip(*Parallel(n_jobs=4)(delayed(bootstrap_func)(i, y_true, y_score) for i in range(1000)))\n", |
|
|
70 |
" print('Test AUC: ({:.3f}, {:.3f}) percentile 95% CI'.format(np.percentile(auc_scores, 2.5), np.percentile(auc_scores, 97.5)))\n", |
|
|
71 |
"\n", |
|
|
72 |
" tprs = []\n", |
|
|
73 |
" aucs = []\n", |
|
|
74 |
" mean_fpr = np.linspace(0, 1, 100)\n", |
|
|
75 |
" for fpr, tpr, _ in roc_curves:\n", |
|
|
76 |
" tprs.append(np.interp(mean_fpr, fpr, tpr))\n", |
|
|
77 |
" tprs[-1][0] = 0.0\n", |
|
|
78 |
" aucs.append(metrics.auc(fpr, tpr))\n", |
|
|
79 |
"\n", |
|
|
80 |
" mean_tpr = np.mean(tprs, axis=0)\n", |
|
|
81 |
" std_tpr = np.std(tprs, axis=0)\n", |
|
|
82 |
" tprs_upper = np.minimum(mean_tpr + 1.96 * std_tpr, 1)\n", |
|
|
83 |
" tprs_lower = np.maximum(mean_tpr - 1.96 * std_tpr, 0)\n", |
|
|
84 |
" return roc_curves, auc_scores, mean_fpr, tprs_lower, tprs_upper\n", |
|
|
85 |
"\n", |
|
|
86 |
"def eval3():\n", |
|
|
87 |
" # Calculate hospital admission level AUROC for every complete window\n", |
|
|
88 |
" df_Yte = df_Yte_all.copy()\n", |
|
|
89 |
" df_Yte = df_Yte[df_Yte['window_id'] >= 1]\n", |
|
|
90 |
" df_Yte_agg = df_Yte.groupby(['hosp_id']).max()\n", |
|
|
91 |
" return df_Yte_agg" |
|
|
92 |
] |
|
|
93 |
}, |
|
|
94 |
{ |
|
|
95 |
"cell_type": "markdown", |
|
|
96 |
"metadata": {}, |
|
|
97 |
"source": [ |
|
|
98 |
"## M-CURES model performance and scores" |
|
|
99 |
] |
|
|
100 |
}, |
|
|
101 |
{ |
|
|
102 |
"cell_type": "code", |
|
|
103 |
"execution_count": null, |
|
|
104 |
"metadata": {}, |
|
|
105 |
"outputs": [], |
|
|
106 |
"source": [ |
|
|
107 |
"# Load exact model and features of patients. Features should be generated from preprocessing script. \n", |
|
|
108 |
"\n", |
|
|
109 |
"mcures_clfs = models_dict['M-CURES']\n", |
|
|
110 |
"df_mcures = pd.read_csv('../preprocessing/sample_output/mcures.csv').set_index('ID')" |
|
|
111 |
] |
|
|
112 |
}, |
|
|
113 |
{ |
|
|
114 |
"cell_type": "code", |
|
|
115 |
"execution_count": null, |
|
|
116 |
"metadata": {}, |
|
|
117 |
"outputs": [], |
|
|
118 |
"source": [ |
|
|
119 |
"df_mcures" |
|
|
120 |
] |
|
|
121 |
}, |
|
|
122 |
{ |
|
|
123 |
"cell_type": "code", |
|
|
124 |
"execution_count": null, |
|
|
125 |
"metadata": {}, |
|
|
126 |
"outputs": [], |
|
|
127 |
"source": [ |
|
|
128 |
"### AUROC on given dataset\n", |
|
|
129 |
"\n", |
|
|
130 |
"# Calculate model outputs for all patients, average over all models\n", |
|
|
131 |
"eval_matrix = scipy.sparse.csr_matrix(cohort_IDs.join(df_mcures).values.astype(float))\n", |
|
|
132 |
"all_y = np.array([clf.predict_proba(eval_matrix)[:,1] for clf in mcures_clfs])\n", |
|
|
133 |
"y_scores = all_y.mean(0)\n", |
|
|
134 |
"\n", |
|
|
135 |
"# To evaluate models, take maximum over all windows\n", |
|
|
136 |
"df_Yte_all = pd.DataFrame({'hosp_id': test_hosp, 'window_id': test_window, 'y': test_y, 'y_score': y_scores})\n", |
|
|
137 |
"df_Yte_agg = eval3()\n", |
|
|
138 |
"y_score = df_Yte_agg['y_score']\n", |
|
|
139 |
"y_true = df_Yte_agg['y']\n", |
|
|
140 |
"fpr, tpr, thresholds = metrics.roc_curve(y_true, y_score)\n", |
|
|
141 |
"print('Test AUC: {:.3f}'.format(metrics.roc_auc_score(y_true, y_score)))\n", |
|
|
142 |
"\n", |
|
|
143 |
"# # Optionally: Generate 95% CI\n", |
|
|
144 |
"# try:\n", |
|
|
145 |
"# roc_curves, auc_scores, mean_fpr, tprs_lower, tprs_upper = get_roc_CI(y_true, y_score)\n", |
|
|
146 |
"# except:\n", |
|
|
147 |
"# pass" |
|
|
148 |
] |
|
|
149 |
}, |
|
|
150 |
{ |
|
|
151 |
"cell_type": "code", |
|
|
152 |
"execution_count": null, |
|
|
153 |
"metadata": {}, |
|
|
154 |
"outputs": [], |
|
|
155 |
"source": [ |
|
|
156 |
"# Generate list of scores for each example \n", |
|
|
157 |
"\n", |
|
|
158 |
"y_score_lst = df_Yte_all.groupby(['hosp_id'])['y_score'].apply(list)\n", |
|
|
159 |
"df1 = pd.DataFrame({'y_scores_mcures_lst': y_score_lst})\n", |
|
|
160 |
"df2 = pd.DataFrame({'id': df_Yte_agg.index, 'y_scores_mcures': y_score})\n", |
|
|
161 |
"\n", |
|
|
162 |
"outcome_outputs = df1.merge(df2, left_index=True, right_index=True)" |
|
|
163 |
] |
|
|
164 |
}, |
|
|
165 |
{ |
|
|
166 |
"cell_type": "code", |
|
|
167 |
"execution_count": null, |
|
|
168 |
"metadata": {}, |
|
|
169 |
"outputs": [], |
|
|
170 |
"source": [ |
|
|
171 |
"outcome_outputs " |
|
|
172 |
] |
|
|
173 |
}, |
|
|
174 |
{ |
|
|
175 |
"cell_type": "code", |
|
|
176 |
"execution_count": null, |
|
|
177 |
"metadata": {}, |
|
|
178 |
"outputs": [], |
|
|
179 |
"source": [] |
|
|
180 |
} |
|
|
181 |
], |
|
|
182 |
"metadata": { |
|
|
183 |
"kernelspec": { |
|
|
184 |
"display_name": "Python 3 (ipykernel)", |
|
|
185 |
"language": "python", |
|
|
186 |
"name": "python3" |
|
|
187 |
}, |
|
|
188 |
"language_info": { |
|
|
189 |
"codemirror_mode": { |
|
|
190 |
"name": "ipython", |
|
|
191 |
"version": 3 |
|
|
192 |
}, |
|
|
193 |
"file_extension": ".py", |
|
|
194 |
"mimetype": "text/x-python", |
|
|
195 |
"name": "python", |
|
|
196 |
"nbconvert_exporter": "python", |
|
|
197 |
"pygments_lexer": "ipython3", |
|
|
198 |
"version": "3.9.7" |
|
|
199 |
} |
|
|
200 |
}, |
|
|
201 |
"nbformat": 4, |
|
|
202 |
"nbformat_minor": 4 |
|
|
203 |
} |