Skip to content

Commit 9d0b40a

Browse files
conoratoWilliam Harveymateobelanger
authored
Web/performance (#98)
* updated 78yo anon prediction for clf, electro, physionet * added results on test set * added performance metrics on test set * added openbci dataset intro * completed electro vs clf * added further dev section * added intro alex vs sleepedf * finish * Update web/src/views/performance/index.js Co-authored-by: William Harvey <william.harvey@polymtl.ca> * oups * updated youtube link!!! * Apply suggestions from code review Co-authored-by: Mathieu Bélanger <mbelanger.poly@gmail.com> * update link to embed * Apply suggestions from code review Co-authored-by: Mathieu Bélanger <mbelanger.poly@gmail.com> Co-authored-by: William Harvey <william.harvey@polymtl.ca> Co-authored-by: Mathieu Bélanger <mbelanger.poly@gmail.com>
1 parent ba9b1a5 commit 9d0b40a

10 files changed

+779
-6268
lines changed

ai/models/RF_HMM_smaller.ipynb

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
},
3131
{
3232
"cell_type": "code",
33-
"execution_count": 18,
33+
"execution_count": 2,
3434
"metadata": {},
3535
"outputs": [],
3636
"source": [
@@ -164,7 +164,7 @@
164164
},
165165
{
166166
"cell_type": "code",
167-
"execution_count": 11,
167+
"execution_count": 7,
168168
"metadata": {},
169169
"outputs": [],
170170
"source": [
@@ -188,7 +188,7 @@
188188
},
189189
{
190190
"cell_type": "code",
191-
"execution_count": 12,
191+
"execution_count": 8,
192192
"metadata": {},
193193
"outputs": [
194194
{
@@ -225,7 +225,7 @@
225225
},
226226
{
227227
"cell_type": "code",
228-
"execution_count": 13,
228+
"execution_count": 9,
229229
"metadata": {},
230230
"outputs": [
231231
{
@@ -259,7 +259,7 @@
259259
},
260260
{
261261
"cell_type": "code",
262-
"execution_count": 14,
262+
"execution_count": 10,
263263
"metadata": {},
264264
"outputs": [],
265265
"source": [
@@ -533,15 +533,15 @@
533533
},
534534
{
535535
"cell_type": "code",
536-
"execution_count": 20,
536+
"execution_count": 11,
537537
"metadata": {},
538538
"outputs": [
539539
{
540540
"name": "stdout",
541541
"output_type": "stream",
542542
"text": [
543-
"CPU times: user 3min 41s, sys: 2.12 s, total: 3min 43s\n",
544-
"Wall time: 1min 13s\n"
543+
"CPU times: user 3min 41s, sys: 2.2 s, total: 3min 44s\n",
544+
"Wall time: 1min 21s\n"
545545
]
546546
}
547547
],
@@ -620,7 +620,7 @@
620620
},
621621
{
622622
"cell_type": "code",
623-
"execution_count": 22,
623+
"execution_count": 12,
624624
"metadata": {},
625625
"outputs": [
626626
{
@@ -658,6 +658,51 @@
658658
"print(\"Agreement score (Cohen Kappa): \", cohen_kappa_score(y_test, y_test_pred))"
659659
]
660660
},
661+
{
662+
"cell_type": "code",
663+
"execution_count": 14,
664+
"metadata": {},
665+
"outputs": [
666+
{
667+
"name": "stdout",
668+
"output_type": "stream",
669+
"text": [
670+
"[[1457 66 41 5 55]\n",
671+
" [ 186 259 318 3 217]\n",
672+
" [ 6 43 3195 245 114]\n",
673+
" [ 0 0 34 577 0]\n",
674+
" [ 7 10 146 0 1139]]\n",
675+
" precision recall f1-score support\n",
676+
"\n",
677+
" W 0.88 0.90 0.89 1624\n",
678+
" N1 0.69 0.26 0.38 983\n",
679+
" N2 0.86 0.89 0.87 3603\n",
680+
" N3 0.70 0.94 0.80 611\n",
681+
" REM 0.75 0.87 0.81 1302\n",
682+
"\n",
683+
" accuracy 0.82 8123\n",
684+
" macro avg 0.77 0.77 0.75 8123\n",
685+
"weighted avg 0.81 0.82 0.80 8123\n",
686+
"\n",
687+
"Agreement score (Cohen Kappa): 0.7413141526163325\n"
688+
]
689+
}
690+
],
691+
"source": [
692+
"hmm_model = MultinomialHMM(n_components=N_STAGES)\n",
693+
"\n",
694+
"MODELS_DIR = \"../trained_model\"\n",
695+
"hmm_model.emissionprob_ = np.load(f\"{MODELS_DIR}/HMM_emissionprob.npy\", allow_pickle=True)\n",
696+
"hmm_model.startprob_ = np.load(f\"{MODELS_DIR}/HMM_startprob.npy\", allow_pickle=True)\n",
697+
"hmm_model.transmat_ = np.load(f\"{MODELS_DIR}/HMM_transmat.npy\", allow_pickle=True)\n",
698+
"\n",
699+
"y_test_pred = hmm_model.predict(y_test_pred.reshape(-1, 1))\n",
700+
"\n",
701+
"print(confusion_matrix(y_test, y_test_pred))\n",
702+
"print(classification_report(y_test, y_test_pred, target_names=SLEEP_STAGES_VALUES.keys()))\n",
703+
"print(\"Agreement score (Cohen Kappa): \", cohen_kappa_score(y_test, y_test_pred))"
704+
]
705+
},
661706
{
662707
"cell_type": "markdown",
663708
"metadata": {},

ai/models/model_utils.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ def train_test_split_according_to_age(X, y, use_continuous_age, subjects_test=No
114114
[85,110] # 32 recordings
115115
]
116116
age_categories = np.unique(X[:, AGE_CATEGORY_COL_IDX])
117-
assert subjects_test is None or len(subjects_test) == len(age_categories), "If subjects are specified, they must be specified for all age groups"
118117

119118
if subjects_test is None:
120119
unique_subject_with_age = np.array([

ai/prediction_anonymous.ipynb

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,24 @@
1818
},
1919
{
2020
"cell_type": "code",
21-
"execution_count": 6,
21+
"execution_count": 1,
2222
"metadata": {},
23-
"outputs": [
24-
{
25-
"name": "stdout",
26-
"output_type": "stream",
27-
"text": [
28-
"The autoreload extension is already loaded. To reload it, use:\n",
29-
" %reload_ext autoreload\n"
30-
]
31-
}
32-
],
23+
"outputs": [],
3324
"source": [
3425
"%load_ext autoreload\n",
3526
"%autoreload 2"
3627
]
3728
},
3829
{
3930
"cell_type": "code",
40-
"execution_count": 7,
31+
"execution_count": 11,
4132
"metadata": {},
4233
"outputs": [],
4334
"source": [
4435
"%matplotlib inline\n",
4536
"\n",
4637
"import mne\n",
38+
"import json\n",
4739
"import pandas as pd\n",
4840
"import numpy as np\n",
4941
"import matplotlib.pyplot as plt\n",
@@ -65,7 +57,7 @@
6557
},
6658
{
6759
"cell_type": "code",
68-
"execution_count": 8,
60+
"execution_count": 3,
6961
"metadata": {},
7062
"outputs": [],
7163
"source": [
@@ -81,7 +73,7 @@
8173
},
8274
{
8375
"cell_type": "code",
84-
"execution_count": 9,
76+
"execution_count": 4,
8577
"metadata": {},
8678
"outputs": [
8779
{
@@ -155,7 +147,7 @@
155147
"1 25620.0 1989-04-25 14:50:00 609519000.0 "
156148
]
157149
},
158-
"execution_count": 9,
150+
"execution_count": 4,
159151
"metadata": {},
160152
"output_type": "execute_result"
161153
}
@@ -175,7 +167,7 @@
175167
},
176168
{
177169
"cell_type": "code",
178-
"execution_count": 10,
170+
"execution_count": 5,
179171
"metadata": {},
180172
"outputs": [],
181173
"source": [
@@ -231,7 +223,7 @@
231223
},
232224
{
233225
"cell_type": "code",
234-
"execution_count": 11,
226+
"execution_count": 6,
235227
"metadata": {},
236228
"outputs": [
237229
{
@@ -257,7 +249,7 @@
257249
},
258250
{
259251
"cell_type": "code",
260-
"execution_count": 12,
252+
"execution_count": 7,
261253
"metadata": {},
262254
"outputs": [
263255
{
@@ -282,7 +274,7 @@
282274
},
283275
{
284276
"cell_type": "code",
285-
"execution_count": 13,
277+
"execution_count": 8,
286278
"metadata": {},
287279
"outputs": [
288280
{
@@ -310,7 +302,7 @@
310302
},
311303
{
312304
"cell_type": "code",
313-
"execution_count": 14,
305+
"execution_count": 9,
314306
"metadata": {},
315307
"outputs": [
316308
{
@@ -357,6 +349,48 @@
357349
"print(\"Agreement score (Cohen Kappa): \", cohen_kappa_score(y_physionet, y_electrophysiologist))"
358350
]
359351
},
352+
{
353+
"cell_type": "code",
354+
"execution_count": 12,
355+
"metadata": {},
356+
"outputs": [],
357+
"source": [
358+
"electro_hypno = {\n",
359+
" 'timestamps': [\n",
360+
" int((datetime.fromtimestamp(615514680) + timedelta(seconds=index*EPOCH_DURATION)).timestamp())\n",
361+
" for index, stage in enumerate(y_electrophysiologist)\n",
362+
" ],\n",
363+
" 'stages': [\n",
364+
" str(list(SLEEP_STAGES_VALUES.keys())[list(SLEEP_STAGES_VALUES.values()).index(stage)])\n",
365+
" for index, stage in enumerate(y_electrophysiologist)\n",
366+
" ]\n",
367+
"}\n",
368+
"\n",
369+
"with open('data/electrophysiologist_woman78yo_sleepedf.json', 'w') as fp:\n",
370+
" json.dump(dict(epochs=electro_hypno), fp)"
371+
]
372+
},
373+
{
374+
"cell_type": "code",
375+
"execution_count": 13,
376+
"metadata": {},
377+
"outputs": [],
378+
"source": [
379+
"sleepedf_hypno = {\n",
380+
" 'timestamps': [\n",
381+
" int((datetime.fromtimestamp(615514680) + timedelta(seconds=index*EPOCH_DURATION)).timestamp())\n",
382+
" for index, stage in enumerate(y_physionet)\n",
383+
" ],\n",
384+
" 'stages': [\n",
385+
" str(list(SLEEP_STAGES_VALUES.keys())[list(SLEEP_STAGES_VALUES.values()).index(stage)])\n",
386+
" for index, stage in enumerate(y_physionet)\n",
387+
" ]\n",
388+
"}\n",
389+
"\n",
390+
"with open('data/physionet_woman78yo_sleepedf.json', 'w') as fp:\n",
391+
" json.dump(dict(epochs=sleepedf_hypno), fp)"
392+
]
393+
},
360394
{
361395
"cell_type": "code",
362396
"execution_count": null,

0 commit comments

Comments
 (0)