|
a |
|
b/table_2.ipynb |
|
|
1 |
{ |
|
|
2 |
"cells": [ |
|
|
3 |
{ |
|
|
4 |
"cell_type": "markdown", |
|
|
5 |
"metadata": {}, |
|
|
6 |
"source": [ |
|
|
7 |
"#### Importing python libraries" |
|
|
8 |
] |
|
|
9 |
}, |
|
|
10 |
{ |
|
|
11 |
"cell_type": "code", |
|
|
12 |
"execution_count": 1, |
|
|
13 |
"metadata": {}, |
|
|
14 |
"outputs": [ |
|
|
15 |
{ |
|
|
16 |
"name": "stderr", |
|
|
17 |
"output_type": "stream", |
|
|
18 |
"text": [ |
|
|
19 |
"Using TensorFlow backend.\n" |
|
|
20 |
] |
|
|
21 |
}, |
|
|
22 |
{ |
|
|
23 |
"name": "stdout", |
|
|
24 |
"output_type": "stream", |
|
|
25 |
"text": [ |
|
|
26 |
"Python version:\n", |
|
|
27 |
"3.7.3 (default, Mar 27 2019, 22:11:17) \n", |
|
|
28 |
"[GCC 7.3.0]\n", |
|
|
29 |
"\n", |
|
|
30 |
"matplotlib version: 3.1.3\n", |
|
|
31 |
"pandas version: 0.24.2\n", |
|
|
32 |
"numpy version: 1.18.3\n", |
|
|
33 |
"sklearn version: 0.22.1\n", |
|
|
34 |
"keras version: 2.2.4\n" |
|
|
35 |
] |
|
|
36 |
} |
|
|
37 |
], |
|
|
38 |
"source": [ |
|
|
39 |
"import warnings\n", |
|
|
40 |
"warnings.filterwarnings('ignore',category=FutureWarning)\n", |
|
|
41 |
"import warnings\n", |
|
|
42 |
"warnings.filterwarnings(\"ignore\")\n", |
|
|
43 |
"\n", |
|
|
44 |
"import sys\n", |
|
|
45 |
"#import os\n", |
|
|
46 |
"import matplotlib\n", |
|
|
47 |
"import matplotlib.pyplot as plt\n", |
|
|
48 |
"import pandas as pd\n", |
|
|
49 |
"import numpy as np\n", |
|
|
50 |
"import h5py as h5\n", |
|
|
51 |
"import sklearn\n", |
|
|
52 |
"from sklearn.multioutput import MultiOutputClassifier\n", |
|
|
53 |
"from sklearn import metrics\n", |
|
|
54 |
"from sklearn import preprocessing\n", |
|
|
55 |
"from sklearn.ensemble import RandomForestClassifier\n", |
|
|
56 |
"from sklearn.metrics import confusion_matrix, classification_report, average_precision_score, precision_recall_curve, accuracy_score, confusion_matrix \n", |
|
|
57 |
"from sklearn.metrics import average_precision_score\n", |
|
|
58 |
"import pickle\n", |
|
|
59 |
"import keras\n", |
|
|
60 |
"from keras.models import load_model\n", |
|
|
61 |
"\n", |
|
|
62 |
"import util\n", |
|
|
63 |
"\n", |
|
|
64 |
"print(\"Python version:\\n{}\\n\".format(sys.version))\n", |
|
|
65 |
"print(\"matplotlib version: {}\".format(matplotlib.__version__))\n", |
|
|
66 |
"print(\"pandas version: {}\".format(pd.__version__))\n", |
|
|
67 |
"print(\"numpy version: {}\".format(np.__version__))\n", |
|
|
68 |
"print(\"sklearn version: {}\".format(sklearn.__version__))\n", |
|
|
69 |
"print(\"keras version: {}\".format(keras.__version__))\n" |
|
|
70 |
] |
|
|
71 |
}, |
|
|
72 |
{ |
|
|
73 |
"cell_type": "markdown", |
|
|
74 |
"metadata": {}, |
|
|
75 |
"source": [ |
|
|
76 |
"## Loading data: Held out test set" |
|
|
77 |
] |
|
|
78 |
}, |
|
|
79 |
{ |
|
|
80 |
"cell_type": "code", |
|
|
81 |
"execution_count": 2, |
|
|
82 |
"metadata": {}, |
|
|
83 |
"outputs": [ |
|
|
84 |
{ |
|
|
85 |
"name": "stdout", |
|
|
86 |
"output_type": "stream", |
|
|
87 |
"text": [ |
|
|
88 |
"(17617, 800, 1)\n", |
|
|
89 |
"(17617, 3)\n", |
|
|
90 |
"(17617, 2)\n", |
|
|
91 |
"(17617, 3)\n" |
|
|
92 |
] |
|
|
93 |
} |
|
|
94 |
], |
|
|
95 |
"source": [ |
|
|
96 |
"path = '/path/to/data/'\n", |
|
|
97 |
"\n", |
|
|
98 |
"data_test = np.load(path + 'test.npz', allow_pickle=True)\n", |
|
|
99 |
"test_x = data_test['signal']\n", |
|
|
100 |
"test_qa = data_test['qa_label']\n", |
|
|
101 |
"test_r = data_test['rhythm']\n", |
|
|
102 |
"test_p = pd.DataFrame(data_test['parameters'])\n", |
|
|
103 |
"print(test_x.shape)\n", |
|
|
104 |
"print(test_qa.shape)\n", |
|
|
105 |
"print(test_r.shape)\n", |
|
|
106 |
"print(test_p.shape)\n", |
|
|
107 |
"test_p.rename(index=str, columns={0:'timestamp', \n", |
|
|
108 |
" 1:'stream', \n", |
|
|
109 |
" 2:'ID'}, inplace=True)" |
|
|
110 |
] |
|
|
111 |
}, |
|
|
112 |
{ |
|
|
113 |
"cell_type": "markdown", |
|
|
114 |
"metadata": {}, |
|
|
115 |
"source": [ |
|
|
116 |
"## VGG model : single-task\n" |
|
|
117 |
] |
|
|
118 |
}, |
|
|
119 |
{ |
|
|
120 |
"cell_type": "code", |
|
|
121 |
"execution_count": 3, |
|
|
122 |
"metadata": {}, |
|
|
123 |
"outputs": [ |
|
|
124 |
{ |
|
|
125 |
"name": "stdout", |
|
|
126 |
"output_type": "stream", |
|
|
127 |
"text": [ |
|
|
128 |
"WARNING:tensorflow:From /home/users/jntorres/jessica/anaconda3/envs/tf_gpu/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.\n", |
|
|
129 |
"Instructions for updating:\n", |
|
|
130 |
"Colocations handled automatically by placer.\n", |
|
|
131 |
"WARNING:tensorflow:From /home/users/jntorres/jessica/anaconda3/envs/tf_gpu/lib/python3.7/site-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.\n", |
|
|
132 |
"Instructions for updating:\n", |
|
|
133 |
"Use tf.cast instead.\n" |
|
|
134 |
] |
|
|
135 |
} |
|
|
136 |
], |
|
|
137 |
"source": [ |
|
|
138 |
"path = '/path/to/model/'\n", |
|
|
139 |
"vgg = load_model(path + 'VGG_singletask.h5')\n" |
|
|
140 |
] |
|
|
141 |
}, |
|
|
142 |
{ |
|
|
143 |
"cell_type": "markdown", |
|
|
144 |
"metadata": {}, |
|
|
145 |
"source": [ |
|
|
146 |
"#### VGG Single-task \n" |
|
|
147 |
] |
|
|
148 |
}, |
|
|
149 |
{ |
|
|
150 |
"cell_type": "code", |
|
|
151 |
"execution_count": 4, |
|
|
152 |
"metadata": {}, |
|
|
153 |
"outputs": [ |
|
|
154 |
{ |
|
|
155 |
"name": "stdout", |
|
|
156 |
"output_type": "stream", |
|
|
157 |
"text": [ |
|
|
158 |
"TPR: 0.92\n", |
|
|
159 |
"TNR: 0.71\n", |
|
|
160 |
"FPR: 0.29\n", |
|
|
161 |
"FNR: 0.08\n", |
|
|
162 |
"PPV: 0.50\n", |
|
|
163 |
"NPV: 0.96\n", |
|
|
164 |
"F1: 0.64\n" |
|
|
165 |
] |
|
|
166 |
} |
|
|
167 |
], |
|
|
168 |
"source": [ |
|
|
169 |
"# weighted macro-average across all indivduals\n", |
|
|
170 |
"\n", |
|
|
171 |
"test_metrics = util.collecting_individual_metrics_singletask(vgg, test_x, test_p, test_r, out_message=False)\n", |
|
|
172 |
"test_metrics = pd.DataFrame.from_dict(test_metrics).T.rename(columns={0:'TPR', 1:'TNR', 2:'FPR', 3:'FNR', 4:\"total_samples\"})\n", |
|
|
173 |
"\n", |
|
|
174 |
"for m in ['TPR', 'TNR', 'FPR', 'FNR']:\n", |
|
|
175 |
" metric_wmu = np.average(test_metrics[m][~test_metrics[m].isna()], weights=test_metrics['total_samples'][~test_metrics[m].isna()])\n", |
|
|
176 |
" print('%s: %0.2f' % (m, metric_wmu))\n", |
|
|
177 |
" \n", |
|
|
178 |
"# PPV, NPV and F1\n", |
|
|
179 |
"\n", |
|
|
180 |
"episode_m = util.episode_metrics_singletask(vgg, test_x, test_p, test_r, out_message=False)\n", |
|
|
181 |
"episode_metrics = pd.DataFrame(episode_m).T\n", |
|
|
182 |
"episode_metrics.rename(columns={0:'TPR', 1:'TNR', 2:'PPV', 3:'NPV', 4:\"FPR\", 5:'FNR', 6:'F1', 7:'total_samples'}, inplace=True)\n", |
|
|
183 |
"for m in ['PPV', 'NPV', 'F1']:\n", |
|
|
184 |
" print('%s: %0.2f' % (m, episode_metrics[m]))\n" |
|
|
185 |
] |
|
|
186 |
}, |
|
|
187 |
{ |
|
|
188 |
"cell_type": "markdown", |
|
|
189 |
"metadata": {}, |
|
|
190 |
"source": [ |
|
|
191 |
"## DeepBeat Single task (no pretrain CDAE + AF task)" |
|
|
192 |
] |
|
|
193 |
}, |
|
|
194 |
{ |
|
|
195 |
"cell_type": "markdown", |
|
|
196 |
"metadata": {}, |
|
|
197 |
"source": [ |
|
|
198 |
"#### Loading non pretrained DeepBeat single task model" |
|
|
199 |
] |
|
|
200 |
}, |
|
|
201 |
{ |
|
|
202 |
"cell_type": "code", |
|
|
203 |
"execution_count": 5, |
|
|
204 |
"metadata": {}, |
|
|
205 |
"outputs": [ |
|
|
206 |
{ |
|
|
207 |
"name": "stdout", |
|
|
208 |
"output_type": "stream", |
|
|
209 |
"text": [ |
|
|
210 |
"WARNING:tensorflow:From /home/users/jntorres/.local/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:3445: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.\n", |
|
|
211 |
"Instructions for updating:\n", |
|
|
212 |
"Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.\n" |
|
|
213 |
] |
|
|
214 |
} |
|
|
215 |
], |
|
|
216 |
"source": [ |
|
|
217 |
"path = '/path/to/model/'\n", |
|
|
218 |
"model_reint = load_model(path + 'deepbeat_singletask_nopretrain.h5')\n" |
|
|
219 |
] |
|
|
220 |
}, |
|
|
221 |
{ |
|
|
222 |
"cell_type": "markdown", |
|
|
223 |
"metadata": {}, |
|
|
224 |
"source": [ |
|
|
225 |
"#### DeepBeat Single task" |
|
|
226 |
] |
|
|
227 |
}, |
|
|
228 |
{ |
|
|
229 |
"cell_type": "code", |
|
|
230 |
"execution_count": 6, |
|
|
231 |
"metadata": {}, |
|
|
232 |
"outputs": [ |
|
|
233 |
{ |
|
|
234 |
"name": "stdout", |
|
|
235 |
"output_type": "stream", |
|
|
236 |
"text": [ |
|
|
237 |
"TPR: 0.49\n", |
|
|
238 |
"TNR: 0.90\n", |
|
|
239 |
"FPR: 0.10\n", |
|
|
240 |
"FNR: 0.51\n", |
|
|
241 |
"PPV: 0.60\n", |
|
|
242 |
"NPV: 0.85\n", |
|
|
243 |
"F1: 0.54\n" |
|
|
244 |
] |
|
|
245 |
} |
|
|
246 |
], |
|
|
247 |
"source": [ |
|
|
248 |
"# weighted macro-average across all indivduals\n", |
|
|
249 |
"\n", |
|
|
250 |
"test_metrics = util.collecting_individual_metrics_singletask(model_reint, test_x, test_p, test_r, out_message=False)\n", |
|
|
251 |
"test_metrics = pd.DataFrame.from_dict(test_metrics).T.rename(columns={0:'TPR', 1:'TNR', 2:'FPR', 3:'FNR', 4:\"total_samples\"})\n", |
|
|
252 |
"\n", |
|
|
253 |
"\n", |
|
|
254 |
"for m in ['TPR', 'TNR', 'FPR', 'FNR']:\n", |
|
|
255 |
" metric_wmu = np.average(test_metrics[m][~test_metrics[m].isna()], weights=test_metrics['total_samples'][~test_metrics[m].isna()])\n", |
|
|
256 |
" print('%s: %0.2f' % (m, metric_wmu))\n", |
|
|
257 |
" \n", |
|
|
258 |
"# PPV, NPV and F1\n", |
|
|
259 |
"\n", |
|
|
260 |
"episode_m = util.episode_metrics_singletask(model_reint, test_x, test_p, test_r, out_message=False)\n", |
|
|
261 |
"episode_metrics = pd.DataFrame(episode_m).T\n", |
|
|
262 |
"episode_metrics.rename(columns={0:'TPR', 1:'TNR', 2:'PPV', 3:'NPV', 4:\"FPR\", 5:'FNR', 6:'F1', 7:'total_samples'}, inplace=True)\n", |
|
|
263 |
"for m in ['PPV', 'NPV', 'F1']:\n", |
|
|
264 |
" print('%s: %0.2f' % (m, episode_metrics[m]))\n" |
|
|
265 |
] |
|
|
266 |
}, |
|
|
267 |
{ |
|
|
268 |
"cell_type": "markdown", |
|
|
269 |
"metadata": {}, |
|
|
270 |
"source": [ |
|
|
271 |
"## DeepBeat Single task (pretrain with CDAE + AF)" |
|
|
272 |
] |
|
|
273 |
}, |
|
|
274 |
{ |
|
|
275 |
"cell_type": "markdown", |
|
|
276 |
"metadata": {}, |
|
|
277 |
"source": [ |
|
|
278 |
"#### Loading pretrained DeepBeat single task model" |
|
|
279 |
] |
|
|
280 |
}, |
|
|
281 |
{ |
|
|
282 |
"cell_type": "code", |
|
|
283 |
"execution_count": 7, |
|
|
284 |
"metadata": {}, |
|
|
285 |
"outputs": [], |
|
|
286 |
"source": [ |
|
|
287 |
"path_to_model ='/path/to/model/'\n", |
|
|
288 |
"model_name = 'deepbeat_singletask_pretrained.h5'\n", |
|
|
289 |
"deepbeat_st = load_model(path_to_model + model_name) " |
|
|
290 |
] |
|
|
291 |
}, |
|
|
292 |
{ |
|
|
293 |
"cell_type": "markdown", |
|
|
294 |
"metadata": {}, |
|
|
295 |
"source": [ |
|
|
296 |
"#### DeepBeat Single task" |
|
|
297 |
] |
|
|
298 |
}, |
|
|
299 |
{ |
|
|
300 |
"cell_type": "code", |
|
|
301 |
"execution_count": 8, |
|
|
302 |
"metadata": {}, |
|
|
303 |
"outputs": [ |
|
|
304 |
{ |
|
|
305 |
"name": "stdout", |
|
|
306 |
"output_type": "stream", |
|
|
307 |
"text": [ |
|
|
308 |
"TPR: 0.52\n", |
|
|
309 |
"TNR: 0.88\n", |
|
|
310 |
"FPR: 0.12\n", |
|
|
311 |
"FNR: 0.48\n", |
|
|
312 |
"PPV: 0.59\n", |
|
|
313 |
"NPV: 0.85\n", |
|
|
314 |
"F1: 0.56\n" |
|
|
315 |
] |
|
|
316 |
} |
|
|
317 |
], |
|
|
318 |
"source": [ |
|
|
319 |
"# weighted macro-average across all indivduals\n", |
|
|
320 |
"\n", |
|
|
321 |
"test_metrics = util.collecting_individual_metrics_singletask(deepbeat_st, test_x, test_p, test_r, out_message=False)\n", |
|
|
322 |
"test_metrics = pd.DataFrame.from_dict(test_metrics).T.rename(columns={0:'TPR', 1:'TNR', 2:'FPR', 3:'FNR', 4:\"total_samples\"})\n", |
|
|
323 |
"\n", |
|
|
324 |
"\n", |
|
|
325 |
"for m in ['TPR', 'TNR', 'FPR', 'FNR']:\n", |
|
|
326 |
" metric_wmu = np.average(test_metrics[m][~test_metrics[m].isna()], weights=test_metrics['total_samples'][~test_metrics[m].isna()])\n", |
|
|
327 |
" print('%s: %0.2f' % (m, metric_wmu))\n", |
|
|
328 |
" \n", |
|
|
329 |
"# PPV, NPV and F1\n", |
|
|
330 |
"\n", |
|
|
331 |
"episode_m = util.episode_metrics_singletask(deepbeat_st, test_x, test_p, test_r, out_message=False)\n", |
|
|
332 |
"episode_metrics = pd.DataFrame(episode_m).T\n", |
|
|
333 |
"episode_metrics.rename(columns={0:'TPR', 1:'TNR', 2:'PPV', 3:'NPV', 4:\"FPR\", 5:'FNR', 6:'F1', 7:'total_samples'}, inplace=True)\n", |
|
|
334 |
"for m in ['PPV', 'NPV', 'F1']:\n", |
|
|
335 |
" print('%s: %0.2f' % (m, episode_metrics[m]))" |
|
|
336 |
] |
|
|
337 |
}, |
|
|
338 |
{ |
|
|
339 |
"cell_type": "markdown", |
|
|
340 |
"metadata": {}, |
|
|
341 |
"source": [ |
|
|
342 |
"## DeepBeat Multi-task (no pretrain CDAE + AF + Excellent QA)" |
|
|
343 |
] |
|
|
344 |
}, |
|
|
345 |
{ |
|
|
346 |
"cell_type": "markdown", |
|
|
347 |
"metadata": {}, |
|
|
348 |
"source": [ |
|
|
349 |
"#### Loading non pretrained DeepBeat model" |
|
|
350 |
] |
|
|
351 |
}, |
|
|
352 |
{ |
|
|
353 |
"cell_type": "code", |
|
|
354 |
"execution_count": 9, |
|
|
355 |
"metadata": {}, |
|
|
356 |
"outputs": [], |
|
|
357 |
"source": [ |
|
|
358 |
"path_to_model ='/path/to/model/'\n", |
|
|
359 |
"model_reint = load_model(path_to_model + 'deepbeat_no_pretraining.h5')" |
|
|
360 |
] |
|
|
361 |
}, |
|
|
362 |
{ |
|
|
363 |
"cell_type": "markdown", |
|
|
364 |
"metadata": {}, |
|
|
365 |
"source": [ |
|
|
366 |
"#### DeepBeat Multi-task (no pretrain CDAE + AF + Excellent QA) " |
|
|
367 |
] |
|
|
368 |
}, |
|
|
369 |
{ |
|
|
370 |
"cell_type": "code", |
|
|
371 |
"execution_count": 10, |
|
|
372 |
"metadata": {}, |
|
|
373 |
"outputs": [ |
|
|
374 |
{ |
|
|
375 |
"name": "stdout", |
|
|
376 |
"output_type": "stream", |
|
|
377 |
"text": [ |
|
|
378 |
"TPR: 0.97\n", |
|
|
379 |
"TNR: 0.89\n", |
|
|
380 |
"FPR: 0.11\n", |
|
|
381 |
"FNR: 0.03\n", |
|
|
382 |
"PPV: 0.55\n", |
|
|
383 |
"NPV: 1.00\n", |
|
|
384 |
"F1: 0.71\n" |
|
|
385 |
] |
|
|
386 |
} |
|
|
387 |
], |
|
|
388 |
"source": [ |
|
|
389 |
"## QA results\n", |
|
|
390 |
"\n", |
|
|
391 |
"predictions_qa, predictions_r = model_reint.predict(test_x)\n", |
|
|
392 |
"predictions_QA = np.argmax(predictions_qa, axis=1)\n", |
|
|
393 |
"\n", |
|
|
394 |
"#print(classification_report(np.argmax(test_qa, axis=1), predictions_QA))\n", |
|
|
395 |
"\n", |
|
|
396 |
"\n", |
|
|
397 |
"excellent_qa_indx = np.where(predictions_QA==2)[0]\n", |
|
|
398 |
"x_test_excellent = test_x[excellent_qa_indx,:]\n", |
|
|
399 |
"p_test_excellent = test_p.iloc[excellent_qa_indx,:]\n", |
|
|
400 |
"rhythm_test_excellent = test_r[excellent_qa_indx,:]\n", |
|
|
401 |
"quality_assessment_test_excellent = test_qa[excellent_qa_indx,:]\n", |
|
|
402 |
"\n", |
|
|
403 |
"\n", |
|
|
404 |
"# weighted macro-average across all indivduals\n", |
|
|
405 |
"\n", |
|
|
406 |
"test_metrics = util.collecting_individual_metrics(model_reint, x_test_excellent, p_test_excellent, rhythm_test_excellent, out_message=False)\n", |
|
|
407 |
"test_metrics = pd.DataFrame.from_dict(test_metrics).T.rename(columns={0:'TPR', 1:'TNR', 2:'FPR', 3:'FNR', 4:\"total_samples\"})\n", |
|
|
408 |
"\n", |
|
|
409 |
"\n", |
|
|
410 |
"for m in ['TPR', 'TNR', 'FPR', 'FNR']:\n", |
|
|
411 |
" metric_wmu = np.average(test_metrics[m][~test_metrics[m].isna()], weights=test_metrics['total_samples'][~test_metrics[m].isna()])\n", |
|
|
412 |
" print('%s: %0.2f' % (m, metric_wmu))\n", |
|
|
413 |
" \n", |
|
|
414 |
"# PPV, NPV and F1\n", |
|
|
415 |
"\n", |
|
|
416 |
"episode_m = util.episode_metrics(model_reint, x_test_excellent, p_test_excellent, rhythm_test_excellent, out_message=False)\n", |
|
|
417 |
"episode_metrics = pd.DataFrame(episode_m).T\n", |
|
|
418 |
"episode_metrics.rename(columns={0:'TPR', 1:'TNR', 2:'PPV', 3:'NPV', 4:\"FPR\", 5:'FNR', 6:'F1', 7:'total_samples'}, inplace=True)\n", |
|
|
419 |
"for m in ['PPV', 'NPV', 'F1']:\n", |
|
|
420 |
" print('%s: %0.2f' % (m, episode_metrics[m]))\n" |
|
|
421 |
] |
|
|
422 |
}, |
|
|
423 |
{ |
|
|
424 |
"cell_type": "markdown", |
|
|
425 |
"metadata": {}, |
|
|
426 |
"source": [ |
|
|
427 |
"## DeepBeat Multi-task (pretrained with CDAE + AF + Excellent QA)" |
|
|
428 |
] |
|
|
429 |
}, |
|
|
430 |
{ |
|
|
431 |
"cell_type": "markdown", |
|
|
432 |
"metadata": {}, |
|
|
433 |
"source": [ |
|
|
434 |
"#### Loading pretrained DeepBeat model" |
|
|
435 |
] |
|
|
436 |
}, |
|
|
437 |
{ |
|
|
438 |
"cell_type": "code", |
|
|
439 |
"execution_count": 11, |
|
|
440 |
"metadata": {}, |
|
|
441 |
"outputs": [], |
|
|
442 |
"source": [ |
|
|
443 |
"path_to_model ='/path/to/model/'\n", |
|
|
444 |
"model_name = 'deepbeat.h5'\n", |
|
|
445 |
"deepbeat = load_model(path_to_model + model_name) " |
|
|
446 |
] |
|
|
447 |
}, |
|
|
448 |
{ |
|
|
449 |
"cell_type": "markdown", |
|
|
450 |
"metadata": {}, |
|
|
451 |
"source": [ |
|
|
452 |
"#### DeepBeat Multi-task (pretrain CDAE + AF + Excellent QA) \n" |
|
|
453 |
] |
|
|
454 |
}, |
|
|
455 |
{ |
|
|
456 |
"cell_type": "code", |
|
|
457 |
"execution_count": 12, |
|
|
458 |
"metadata": {}, |
|
|
459 |
"outputs": [ |
|
|
460 |
{ |
|
|
461 |
"name": "stdout", |
|
|
462 |
"output_type": "stream", |
|
|
463 |
"text": [ |
|
|
464 |
"TPR: 0.98\n", |
|
|
465 |
"TNR: 0.99\n", |
|
|
466 |
"FPR: 0.01\n", |
|
|
467 |
"FNR: 0.02\n", |
|
|
468 |
"PPV: 0.94\n", |
|
|
469 |
"NPV: 1.00\n", |
|
|
470 |
"F1: 0.96\n" |
|
|
471 |
] |
|
|
472 |
} |
|
|
473 |
], |
|
|
474 |
"source": [ |
|
|
475 |
"## QA results\n", |
|
|
476 |
"\n", |
|
|
477 |
"predictions_qa, predictions_r = deepbeat.predict(test_x)\n", |
|
|
478 |
"predictions_QA = np.argmax(predictions_qa, axis=1)\n", |
|
|
479 |
"\n", |
|
|
480 |
"#print(classification_report(np.argmax(test_qa, axis=1), predictions_QA))\n", |
|
|
481 |
"\n", |
|
|
482 |
"\n", |
|
|
483 |
"excellent_qa_indx = np.where(predictions_QA==2)[0]\n", |
|
|
484 |
"x_test_excellent = test_x[excellent_qa_indx,:]\n", |
|
|
485 |
"p_test_excellent = test_p.iloc[excellent_qa_indx,:]\n", |
|
|
486 |
"rhythm_test_excellent = test_r[excellent_qa_indx,:]\n", |
|
|
487 |
"quality_assessment_test_excellent = test_qa[excellent_qa_indx,:]\n", |
|
|
488 |
"\n", |
|
|
489 |
"\n", |
|
|
490 |
"# weighted macro-average across all indivduals\n", |
|
|
491 |
"\n", |
|
|
492 |
"test_metrics = util.collecting_individual_metrics(deepbeat, x_test_excellent, p_test_excellent, rhythm_test_excellent, out_message=False)\n", |
|
|
493 |
"test_metrics = pd.DataFrame.from_dict(test_metrics).T.rename(columns={0:'TPR', 1:'TNR', 2:'FPR', 3:'FNR', 4:\"total_samples\"})\n", |
|
|
494 |
"\n", |
|
|
495 |
"\n", |
|
|
496 |
"for m in ['TPR', 'TNR', 'FPR', 'FNR']:\n", |
|
|
497 |
" metric_wmu = np.average(test_metrics[m][~test_metrics[m].isna()], weights=test_metrics['total_samples'][~test_metrics[m].isna()])\n", |
|
|
498 |
" print('%s: %0.2f' % (m, metric_wmu))\n", |
|
|
499 |
" \n", |
|
|
500 |
"# PPV, NPV and F1\n", |
|
|
501 |
"\n", |
|
|
502 |
"episode_m = util.episode_metrics(deepbeat, x_test_excellent, p_test_excellent, rhythm_test_excellent, out_message=False)\n", |
|
|
503 |
"episode_metrics = pd.DataFrame(episode_m).T\n", |
|
|
504 |
"episode_metrics.rename(columns={0:'TPR', 1:'TNR', 2:'PPV', 3:'NPV', 4:\"FPR\", 5:'FNR', 6:'F1', 7:'total_samples'}, inplace=True)\n", |
|
|
505 |
"for m in ['PPV', 'NPV', 'F1']:\n", |
|
|
506 |
" print('%s: %0.2f' % (m, episode_metrics[m]))" |
|
|
507 |
] |
|
|
508 |
}, |
|
|
509 |
{ |
|
|
510 |
"cell_type": "code", |
|
|
511 |
"execution_count": null, |
|
|
512 |
"metadata": {}, |
|
|
513 |
"outputs": [], |
|
|
514 |
"source": [] |
|
|
515 |
} |
|
|
516 |
], |
|
|
517 |
"metadata": { |
|
|
518 |
"kernelspec": { |
|
|
519 |
"display_name": "Python 3", |
|
|
520 |
"language": "python", |
|
|
521 |
"name": "python3" |
|
|
522 |
}, |
|
|
523 |
"language_info": { |
|
|
524 |
"codemirror_mode": { |
|
|
525 |
"name": "ipython", |
|
|
526 |
"version": 3 |
|
|
527 |
}, |
|
|
528 |
"file_extension": ".py", |
|
|
529 |
"mimetype": "text/x-python", |
|
|
530 |
"name": "python", |
|
|
531 |
"nbconvert_exporter": "python", |
|
|
532 |
"pygments_lexer": "ipython3", |
|
|
533 |
"version": "3.7.3" |
|
|
534 |
} |
|
|
535 |
}, |
|
|
536 |
"nbformat": 4, |
|
|
537 |
"nbformat_minor": 2 |
|
|
538 |
} |