-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluator.py
More file actions
825 lines (721 loc) · 36.2 KB
/
Copy pathevaluator.py
File metadata and controls
825 lines (721 loc) · 36.2 KB
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
import numpy as np
import pandas as pd
from sklearn.metrics import accuracy_score, balanced_accuracy_score, \
f1_score, precision_score, recall_score, confusion_matrix, ConfusionMatrixDisplay
import matplotlib.pyplot as plt
from sklearn.model_selection import StratifiedGroupKFold, StratifiedKFold, LeaveOneGroupOut, train_test_split
from scipy.stats import ttest_ind
from tqdm import tqdm
import os
import json
import warnings
SEED = 2022
def evaluator(y_pred, y_true, verbose=False):
"""Returns evaluation metric scores"""
accuracy = accuracy_score(y_pred=y_pred, y_true=y_true)
balanced_accuracy = balanced_accuracy_score(y_pred=y_pred, y_true=y_true)
f1 = f1_score(y_pred=y_pred, y_true=y_true, average='weighted')
recall = recall_score(y_pred=y_pred, y_true=y_true, average='weighted')
precision = precision_score(y_pred=y_pred, y_true=y_true, average='weighted')
confusion = confusion_matrix(y_pred=y_pred, y_true=y_true)
# display scores
if verbose:
ConfusionMatrixDisplay(confusion_matrix=confusion, display_labels=[False, True]).plot(cmap=plt.cm.Blues)
plt.title('Physical fatigue')
print(f'accuracy: {accuracy}\n'
f'balanced accuracy: {balanced_accuracy}\n'
f'f1 (weighted): {f1}\n'
f'recall (weighted): {recall}\n'
f'precision (weighted): {precision}')
return {'accuracy': accuracy,
'balanced_accuracy': balanced_accuracy,
'f1': f1,
'recall': recall,
'precision': precision}
def aggregate_by_day(values, indices, metadata):
"""
Aggregates values along individual days
:param values: e.g. labels or label predictions
:param indices: indices of values w.r.t dataset
:param metadata: metadata for FULL(!) dataset
:return:
"""
# day information for each segment
metadata_selection = np.array(metadata)[indices]
days_selection = [(meta['date'], meta['subjectID']) for meta in
metadata_selection] # note that same date can be used by different subjects
# assign values to corresponding day
values_daily = {key: [] for key in set(days_selection)}
for key, pred in zip(days_selection, values):
values_daily[key].append(pred)
return values_daily
# TODO: check for multiclass predictions
def daily_majority_vote(y_pred_segments, indices, metadata):
"""Predicts majority class from segement predictions"""
# aggregate by day
y_pred_daily = aggregate_by_day(y_pred_segments, indices, metadata)
# majority vote
for day, day_preds in y_pred_daily.items():
if len(day_preds) == 1:
y_pred_daily[day] = day_preds[0]
else:
county_by_label = np.bincount(day_preds)
y_pred_daily[day] = np.argmax(county_by_label)
return y_pred_daily
def agreements(y_pred_segments, indices, metadata):
"""Calculates percentage of agreements between predictions of same-day segments"""
# aggregate by day
y_pred_daily = aggregate_by_day(y_pred_segments, indices, metadata)
# agreements by day
agreements = {key: 0 for key in y_pred_daily.keys()}
for day, day_preds in y_pred_daily.items():
if len(day_preds) == 1:
agreements[day] = 1 # only one value -> agrees by default
else:
agreements[day] = int(np.sum(day_preds) == len(day_preds) or np.sum(day_preds) == 0)
return np.mean(list(agreements.values()))
# TODO: check if assumption correct, that we do not need X data to create the splits
# TODO: currently only working for binary classification -> make for multiclass
def stratified_group_k_fold(path, groups, model, images, folds=5, verbose=True, variable=0) -> dict:
"""
Calculates stratified group k-fold for a specified model
:param path: path to folder where data is stored
:param groups: subjectID for all datapoints in dataset
:param model: model implementing train, fit, predict
:param folds: k folds
:param verbose: whether to print mean test set metrics
:param images: whether to use images or statistical features
:param variable: which variable to use (0 -> phF or 1 -> MF)
:return: dictionary with test set metrics for each fold
"""
assert variable in (0, 1)
# calculate data size
if images:
N = sum([1 for p in os.listdir(path) if (p[:14] == 'feature_vector' and p[:19] != 'feature_vector_stat')])
else:
N = sum([1 for p in os.listdir(path) if p[:19] == 'feature_vector_stat'])
# load labels (we need them for stratification)
y = np.empty(N, dtype=int)
for i in range(N):
if images:
y[i] = np.load(path + f'/labels{i}.npy', allow_pickle=True)[variable] # TODO: multiclass
else:
y[i] = np.load(path + f'/labels_stat{i}.npy', allow_pickle=True)[variable] # TODO: multiclass
# CV
cv = StratifiedGroupKFold(n_splits=folds, shuffle=True, random_state=SEED)
scores_cv = []
data_indices = np.arange(N)
print(f'Starting stratified group {folds}-fold for {["physical fatigue", "mental fatigue"][variable]}')
with tqdm(total=folds) as pbar:
for i, (train_indices, test_indices) in enumerate(cv.split(X=data_indices, y=y, groups=groups)):
# test labels
y_test = y[test_indices]
# training
try:
model.reset()
except AttributeError:
pass # model has no trainable parameters
model.fit(train_indices)
# predict
y_pred = model.predict(test_indices)
# evaluate
scores = evaluator(y_pred, y_test, verbose=False)
scores_cv.append(scores)
# for progress bar
pbar.update(1)
pbar.set_description(f' Fold {i + 1} F1: {scores["f1"]}')
# print (if verbose==True)
if verbose:
print('Performance model:')
metrics = scores_cv[0].keys()
for metric in metrics:
mean = np.mean([scores_cv_i[metric] for scores_cv_i in scores_cv])
std = np.std([scores_cv_i[metric] for scores_cv_i in scores_cv])
print(f' {metric}: {round(mean, 3)} +- {round(std, 3)} \n')
return scores_cv
# TODO: check if assumption correct, that we do not need X data to create the splits
# TODO: currently only working for binary classification -> make for multiclass
def stratified_k_fold(path, model, images, folds=5, verbose=True, variable=0) -> dict:
"""
Calculates stratified k-fold for a specified model
:param path: path to folder where data is stored
:param model: model implementing train, fit, predict
:param folds: k folds
:param verbose: whether to print mean test set metrics
:param images: whether to use images or statistical features
:param variable: which variable to use (0 -> phF or 1 -> MF)
:return: dictionary with test set metrics for each fold
"""
assert variable in (0, 1)
# calculate data size
if images:
N = sum([1 for p in os.listdir(path) if (p[:14] == 'feature_vector' and p[:19] != 'feature_vector_stat')])
else:
N = sum([1 for p in os.listdir(path) if p[:19] == 'feature_vector_stat'])
# load labels (we need them for stratification)
y = np.empty(N, dtype=int)
for i in range(N):
if images:
y[i] = np.load(path + f'/labels{i}.npy', allow_pickle=True)[variable] # TODO: multiclass
else:
y[i] = np.load(path + f'/labels_stat{i}.npy', allow_pickle=True)[variable] # TODO: multiclass
# CV
cv = StratifiedKFold(n_splits=folds, shuffle=True, random_state=SEED)
scores_cv = []
data_indices = np.arange(N)
print(f'Starting stratified {folds}-fold for {["physical fatigue", "mental fatigue"][variable]}')
with tqdm(total=folds) as pbar:
for i, (train_indices, test_indices) in enumerate(cv.split(X=data_indices, y=y)):
# test labels
y_test = y[test_indices]
# training
try:
model.reset()
except AttributeError:
pass # model has no trainable parameters
model.fit(train_indices)
# predict
y_pred = model.predict(test_indices)
# evaluate
scores = evaluator(y_pred, y_test, verbose=False)
scores_cv.append(scores)
# for progress bar
pbar.update(1)
pbar.set_description(f' Fold {i + 1} F1: {scores["f1"]}')
# print (if verbose==True)
if verbose:
print('Performance model:')
metrics = scores_cv[0].keys()
for metric in metrics:
mean = np.mean([scores_cv_i[metric] for scores_cv_i in scores_cv])
std = np.std([scores_cv_i[metric] for scores_cv_i in scores_cv])
print(f' {metric}: {round(mean, 3)} +- {round(std, 3)} \n')
return scores_cv
# TODO: check if assumption correct, that we do not need X data to create the splits
# TODO: currently only working for binary classification -> make for multiclass
def leave_one_subject_out(path, groups, model, images, verbose=True, variable=0) -> dict:
"""
Calculates LOSO for a specified model
:param path: path to folder where data is stored
:param groups: subjectID for all datapoints in dataset
:param model: model implementing train, fit, predict
:param verbose: whether to print mean test set metrics
:param images: whether to use images or statistical features
:param variable: which variable to use (0 -> phF or 1 -> MF)
:return: dictionary with test set metrics for each fold
"""
assert variable in (0, 1)
# calculate data size
if images:
N = sum([1 for p in os.listdir(path) if (p[:14] == 'feature_vector' and p[:19] != 'feature_vector_stat')])
else:
N = sum([1 for p in os.listdir(path) if p[:19] == 'feature_vector_stat'])
# load labels (we need them for stratification)
y = np.empty(N, dtype=int)
for i in range(N):
if images:
y[i] = np.load(path + f'/labels{i}.npy', allow_pickle=True)[variable] # TODO: multiclass
else:
y[i] = np.load(path + f'/labels_stat{i}.npy', allow_pickle=True)[variable] # TODO: multiclass
# CV
cv = LeaveOneGroupOut()
scores_cv = []
data_indices = np.arange(N)
print(f'Starting leave-one-subject-out for {["physical fatigue", "mental fatigue"][variable]}')
with tqdm(total=len(np.unique(groups))) as pbar:
for i, (train_indices, test_indices) in enumerate(cv.split(X=data_indices, y=y, groups=groups)):
# test labels
y_test = y[test_indices]
# training
try:
model.reset()
except AttributeError:
pass # model has no trainable parameters
model.fit(train_indices)
# predict
y_pred = model.predict(test_indices)
# evaluate
scores = evaluator(y_pred, y_test, verbose=False)
scores_cv.append(scores)
# for progress bar
pbar.update(1)
pbar.set_description(f' Fold {i + 1} F1: {scores["f1"]}')
# print (if verbose==True)
if verbose:
print('Performance model:')
metrics = scores_cv[0].keys()
for metric in metrics:
mean = np.mean([scores_cv_i[metric] for scores_cv_i in scores_cv])
std = np.std([scores_cv_i[metric] for scores_cv_i in scores_cv])
print(f' {metric}: {round(mean, 3)} +- {round(std, 3)} \n')
return scores_cv
# TODO: check if assumption correct, that we do not need X data to create the splits
# TODO: currently only working for binary classification -> make for multiclass
def stratified_train_test(path, model, test_size, images, verbose=True, variable=0) -> dict:
"""
Calculates performance of specified model under one stratified train/test split
:param path: path to folder where data is stored
:param model: model implementing train, fit, predict
:param test_size: size of test set (between 0 to 1)
:param verbose: whether to print mean test set metrics
:param images: whether to use images or statistical features
:param variable: which variable to use (0 -> phF or 1 -> MF)
:return: dictionary with test set metrics for each fold
"""
assert variable in (0, 1)
# calculate data size
if images:
N = sum([1 for p in os.listdir(path) if (p[:14] == 'feature_vector' and p[:19] != 'feature_vector_stat')])
else:
N = sum([1 for p in os.listdir(path) if p[:19] == 'feature_vector_stat'])
# load labels (we need them for stratification)
y = np.empty(N, dtype=int)
for i in range(N):
if images:
y[i] = np.load(path + f'/labels{i}.npy', allow_pickle=True)[variable] # TODO: multiclass
else:
y[i] = np.load(path + f'/labels_stat{i}.npy', allow_pickle=True)[variable] # TODO: multiclass
# train/test split
data_indices = np.arange(N)
train_indices, test_indices, y_train, y_test = train_test_split(data_indices, y, test_size=test_size, shuffle=True,
random_state=SEED, stratify=y)
print(f'Starting stratified train/test for {["physical fatigue", "mental fatigue"][variable]}')
# training
model.fit(train_indices)
# predict
y_pred = model.predict(test_indices)
# evaluate
scores = evaluator(y_pred, y_test, verbose=False)
# print (if verbose==True)
if verbose:
print('Performance model:')
for metric, score in scores.items():
print(f' {metric}: {round(score, 3)} +- {round(score, 3)} \n')
return scores
def scores_tables():
"""Prints out all scores in LaTeX tables"""
phF = []
MF = []
# not very pretty but works
with warnings.catch_warnings():
warnings.filterwarnings('ignore')
score_path = './Scores/relevant'
for path in os.listdir(score_path):
# separate tables for phF/MF
table_phF = {'accuracy': [],
'balanced_accuracy': [],
'f1': [],
'recall': [],
'precision': []}
table_MF = {'accuracy': [],
'balanced_accuracy': [],
'f1': [],
'recall': [],
'precision': []}
for model in os.listdir(score_path + '/' + path):
full_path = score_path + '/' + path + '/' + model
# load scores for model
with open(full_path) as f:
scores = f.read()
scores = scores.replace('\'', '\"')
#scores = scores.replace(', None', '')
scores = json.loads(scores)
# phF/MF
for variable in (0, 1):
# aggregate all metrics of different folds for specific model
aggregation = {'accuracy': [],
'balanced_accuracy': [],
'f1': [],
'recall': [],
'precision': []}
for fold in scores[variable]:
for metric in aggregation.keys():
aggregation[metric].append(fold[metric])
for metric in aggregation.keys():
mean = np.round(np.mean(aggregation[metric]), 3)
std = np.round(np.std(aggregation[metric]), 3)
entry = f'{mean} ± {std}'
# put mean +- std of scores into table for each model
if variable == 0:
table_phF[metric].append(entry)
else:
table_MF[metric].append(entry)
# prettier metric names
table_phF = {'Accuracy': table_phF['accuracy'],
'Balanced accuracy': table_phF['balanced_accuracy'],
'F1-score': table_phF['f1'],
'Recall': table_phF['recall'],
'Precision': table_phF['precision']}
table_MF = {'Accuracy': table_MF['accuracy'],
'Balanced accuracy': table_MF['balanced_accuracy'],
'F1-score': table_MF['f1'],
'Recall': table_MF['recall'],
'Precision': table_MF['precision']}
caption_phF = {'loso': 'LOSO (Physical fatigue)',
'strat_5_fold': 'Stratified 5-fold (Physical fatigue)',
'strat_group_5_fold': 'Stratified group 5-fold (Physical fatigue)'}[path]
caption_MF = {'loso': 'LOSO (Mental fatigue)',
'strat_5_fold': 'Stratified 5-fold (Mental fatigue)',
'strat_group_5_fold': 'Stratified group 5-fold (Mental fatigue)'}[path]
# to latex
"""models = [{'cnn.txt': 'CNN',
'cnn2.txt': 'CNN (2nd run)',
'majority_voting.txt': 'Majority Voting',
'random_guess.txt': 'Random Guess',
'xgboost.txt': 'XGBoost',
'random_forest.txt': 'Random Forest'}[model] for model in os.listdir(score_path + '/' + path)]"""
models = [model.capitalize().replace('Cnn', 'CNN').replace('Xgboost', 'XGBoost').
replace('_', ' ').replace('.txt', '').replace('forest', 'Forest').replace('guess', 'Guess')
for model in os.listdir(score_path + '/' + path)]
models = pd.Series(models)
# pHF
df = pd.DataFrame(table_phF)
df = df.set_index([models])
# make largest value bold
for column_name in list(df.columns):
index_max = np.argmax([float(entry.split('±')[0]) for entry in df[column_name].to_numpy()])
entry = df[column_name].iloc[index_max]
entry = 'BOLD{' + entry + '}'
df[column_name].iloc[index_max] = entry
phF.append(df.to_latex(index=True,
bold_rows=True,
caption=caption_phF,
column_format='llllll',
position='H'))
# MF
df = pd.DataFrame(table_MF)
df = df.set_index([models])
# make largest value bold
for column_name in list(df.columns):
index_max = np.argmax([float(entry.split('±')[0]) for entry in df[column_name].to_numpy()])
entry = df[column_name].iloc[index_max]
entry = 'BOLD{' + entry + '}'
df[column_name].iloc[index_max] = entry
MF.append(df.to_latex(index=True,
bold_rows=True,
caption=caption_MF,
column_format='llllll',
position='H'))
# print
for entry in phF:
print(entry.replace('BOLD\\', r'\textbf').replace('\}', '}'))
for entry in MF:
print(entry.replace('BOLD\\', r'\textbf').replace('\}', '}'))
def p_value_tables(model_names_first=None, model_names_second=None):
"""
Calculates statistical performance significance compared to baselines
:param model_names_first: list of models names to use for comparison (incl. '.txt')
:param model_names_second: list of models names to use for comparison (incl. '.txt')
:return:
"""
phF = []
MF = []
# not very pretty but works
with warnings.catch_warnings():
warnings.filterwarnings('ignore')
score_path = './Scores'
for path in os.listdir(score_path):
# store scores for each model
models = {}
for model in os.listdir(score_path + '/' + path):
full_path = score_path + '/' + path + '/' + model
# load scores for model
with open(full_path) as f:
scores = f.read()
scores = json.loads(scores.replace('\'', '\"'))
# phF/MF
for variable in (0, 1):
# aggregate all metrics of different folds for specific model
aggregation = {'accuracy': [],
'balanced_accuracy': [],
'f1': [],
'recall': [],
'precision': []}
for fold in scores[variable]:
for metric in aggregation.keys():
aggregation[metric].append(fold[metric])
# store scores in models
if variable == 0:
models[model] = {variable: aggregation}
else:
models[model][variable] = aggregation
# build tables
caption_phF = {'loso': 'LOSO - p-values (Physical fatigue)',
'strat_5_fold': 'Stratified 5-fold - p-values (Physical fatigue)',
'strat_group_5_fold': 'Stratified group 5-fold - p-values (Physical fatigue)'}[path]
caption_MF = {'loso': 'LOSO - p-values (Mental fatigue)',
'strat_5_fold': 'Stratified 5-fold - p-values (Mental fatigue)',
'strat_group_5_fold': 'Stratified group 5-fold - p-values (Mental fatigue)'}[path]
# phF
models_name = [model for model in os.listdir(score_path + '/' + path)]
baselines_name = ['majority_voting.txt', 'random_guess.txt']
non_baselines_name = [model_name for model_name in models_name if
model_name not in baselines_name]
if model_names_first is not None:
non_baselines_name = [model_name for model_name in model_names_first]
if model_names_second is not None:
baselines_name = [model_name for model_name in model_names_second]
n_models = len(non_baselines_name) * len(baselines_name)
df = pd.DataFrame({'accuracy': [pd.NA]*n_models,
'balanced_accuracy': [pd.NA]*n_models,
'f1': [pd.NA]*n_models,
'recall': [pd.NA]*n_models,
'precision': [pd.NA]*n_models})
df = df.set_index(pd.Series([non_baseline.replace('.txt', '') +
'/' + baseline.replace('.txt', '')
for non_baseline in non_baselines_name
for baseline in baselines_name]))
# calculate p-value
for metric in list(aggregation.keys()):
i = 0
for non_baseline in non_baselines_name:
for baseline in baselines_name:
baseline_scores = models[baseline][0][metric]
model_scores = models[non_baseline][0][metric]
_, p_value = ttest_ind(baseline_scores, model_scores, equal_var=False)
df[metric].iloc[i] = round(p_value, 3)
i += 1
# make value bold if p < 0.05
for column_name in list(df.columns):
for row in range(df.shape[0]):
entry = df[column_name].iloc[row]
if float(entry) < 0.05:
entry = 'BOLD{' + str(entry) + '}'
df[column_name].iloc[row] = entry
# prettier names
df = df.rename(columns={"accuracy": "Accuracy", "balanced_accuracy": "Balanced accuracy",
"f1": "F1-score", "recall": "Recall", "precision": 'Precision'})
df = df.set_index(
pd.Series(
[str(n).capitalize().replace('Cnn', 'CNN').replace('Xgboost', 'XGBoost').
replace('_', ' ').replace('.txt', '').replace('forest', 'Forest').
replace('guess', 'Guess').replace('majority', 'Majority').replace('random', 'Random').
replace('voting', 'Voting')
for n in list(df.index)]
)
)
# to latex
phF.append(df.to_latex(index=True,
bold_rows=True,
caption=caption_phF,
column_format='llllll',
position='H'))
# MF
models_name = [model for model in os.listdir(score_path + '/' + path)]
baselines_name = ['majority_voting.txt', 'random_guess.txt']
non_baselines_name = [model_name for model_name in models_name if
model_name not in baselines_name]
if model_names_first is not None:
non_baselines_name = [model_name for model_name in model_names_first]
if model_names_second is not None:
baselines_name = [model_name for model_name in model_names_second]
n_models = len(non_baselines_name) * len(baselines_name)
df = pd.DataFrame({'accuracy': [pd.NA]*n_models,
'balanced_accuracy': [pd.NA]*n_models,
'f1': [pd.NA]*n_models,
'recall': [pd.NA]*n_models,
'precision': [pd.NA]*n_models})
df = df.set_index(pd.Series([non_baseline.replace('.txt', '') +
'/' + baseline.replace('.txt', '')
for non_baseline in non_baselines_name
for baseline in baselines_name]))
# calculate p-value
for metric in list(aggregation.keys()):
i = 0
for non_baseline in non_baselines_name:
for baseline in baselines_name:
baseline_scores = models[baseline][1][metric]
model_scores = models[non_baseline][1][metric]
_, p_value = ttest_ind(baseline_scores, model_scores, equal_var=False)
df[metric].iloc[i] = round(p_value, 3)
i += 1
# make value bold if p < 0.05
for column_name in list(df.columns):
for row in range(df.shape[0]):
entry = df[column_name].iloc[row]
if float(entry) < 0.05:
entry = 'BOLD{' + str(entry) + '}'
df[column_name].iloc[row] = entry
# prettier names
df = df.rename(columns={"accuracy": "Accuracy", "balanced_accuracy": "Balanced accuracy",
"f1": "F1-score", "recall": "Recall", "precision": 'Precision'})
df = df.set_index(
pd.Series(
[str(n).capitalize().replace('Cnn', 'CNN').replace('Xgboost', 'XGBoost').
replace('_', ' ').replace('.txt', '').replace('forest', 'Forest').
replace('guess', 'Guess').replace('majority', 'Majority').replace('random', 'Random').
replace('voting', 'Voting')
for n in list(df.index)]
)
)
# to latex
MF.append(df.to_latex(index=True,
bold_rows=True,
caption=caption_MF,
column_format='llllll',
position='H'))
# print
for entry in phF:
print(entry.replace('BOLD\\', r'\textbf').replace('\}', '}'))
for entry in MF:
print(entry.replace('BOLD\\', r'\textbf').replace('\}', '}'))
def p_value_tables_stars(model_names_first=None, model_names_second=None):
"""
Calculates statistical performance significance compared to baselines
:param model_names_first: list of models names to use for comparison (incl. '.txt')
:param model_names_second: list of models names to use for comparison (incl. '.txt')
:return:
"""
def starify(p):
if p > 0.05:
return ''
if p > 0.01:
return '*'
if p > 0.001:
return '**'
return '***'
phF = []
MF = []
# not very pretty but works
with warnings.catch_warnings():
warnings.filterwarnings('ignore')
score_path = './Scores'
for path in os.listdir(score_path):
# store scores for each model
models = {}
for model in os.listdir(score_path + '/' + path):
full_path = score_path + '/' + path + '/' + model
# load scores for model
with open(full_path) as f:
scores = f.read()
scores = json.loads(scores.replace('\'', '\"'))
# phF/MF
for variable in (0, 1):
# aggregate all metrics of different folds for specific model
aggregation = {'accuracy': [],
'balanced_accuracy': [],
'f1': [],
'recall': [],
'precision': []}
for fold in scores[variable]:
for metric in aggregation.keys():
aggregation[metric].append(fold[metric])
# store scores in models
if variable == 0:
models[model] = {variable: aggregation}
else:
models[model][variable] = aggregation
# build tables
caption_phF = {'loso': 'LOSO - p-values (Physical fatigue)',
'strat_5_fold': 'Stratified 5-fold - p-values (Physical fatigue)',
'strat_group_5_fold': 'Stratified group 5-fold - p-values (Physical fatigue)'}[path]
caption_MF = {'loso': 'LOSO - p-values (Mental fatigue)',
'strat_5_fold': 'Stratified 5-fold - p-values (Mental fatigue)',
'strat_group_5_fold': 'Stratified group 5-fold - p-values (Mental fatigue)'}[path]
# phF
models_name = [model for model in os.listdir(score_path + '/' + path)]
baselines_name = ['majority_voting.txt', 'random_guess.txt']
non_baselines_name = [model_name for model_name in models_name if
model_name not in baselines_name]
if model_names_first is not None:
non_baselines_name = [model_name for model_name in model_names_first]
if model_names_second is not None:
baselines_name = [model_name for model_name in model_names_second]
n_models = len(non_baselines_name) * len(baselines_name)
df = pd.DataFrame({'accuracy': [pd.NA]*n_models,
'balanced_accuracy': [pd.NA]*n_models,
'f1': [pd.NA]*n_models,
'recall': [pd.NA]*n_models,
'precision': [pd.NA]*n_models})
df = df.set_index(pd.Series([non_baseline.replace('.txt', '') +
'/' + baseline.replace('.txt', '')
for non_baseline in non_baselines_name
for baseline in baselines_name]))
# calculate p-value
for metric in list(aggregation.keys()):
i = 0
for non_baseline in non_baselines_name:
for baseline in baselines_name:
baseline_scores = models[baseline][0][metric]
model_scores = models[non_baseline][0][metric]
_, p_value = ttest_ind(baseline_scores, model_scores, equal_var=False)
df[metric].iloc[i] = starify(p_value)
i += 1
# prettier names
df = df.rename(columns={"accuracy": "Accuracy", "balanced_accuracy": "Balanced accuracy",
"f1": "F1-score", "recall": "Recall", "precision": 'Precision'})
df = df.set_index(
pd.Series(
[str(n).capitalize().replace('Cnn', 'CNN').replace('Xgboost', 'XGBoost').
replace('_', ' ').replace('.txt', '').replace('forest', 'Forest').
replace('guess', 'Guess').replace('majority', 'Majority').replace('random', 'Random').
replace('voting', 'Voting')
for n in list(df.index)]
)
)
# to latex
phF.append(df.to_latex(index=True,
bold_rows=True,
caption=caption_phF,
column_format='llllll',
position='H'))
# MF
models_name = [model for model in os.listdir(score_path + '/' + path)]
baselines_name = ['majority_voting.txt', 'random_guess.txt']
non_baselines_name = [model_name for model_name in models_name if
model_name not in baselines_name]
if model_names_first is not None:
non_baselines_name = [model_name for model_name in model_names_first]
if model_names_second is not None:
baselines_name = [model_name for model_name in model_names_second]
n_models = len(non_baselines_name) * len(baselines_name)
df = pd.DataFrame({'accuracy': [pd.NA]*n_models,
'balanced_accuracy': [pd.NA]*n_models,
'f1': [pd.NA]*n_models,
'recall': [pd.NA]*n_models,
'precision': [pd.NA]*n_models})
df = df.set_index(pd.Series([non_baseline.replace('.txt', '') +
'/' + baseline.replace('.txt', '')
for non_baseline in non_baselines_name
for baseline in baselines_name]))
# calculate p-value
for metric in list(aggregation.keys()):
i = 0
for non_baseline in non_baselines_name:
for baseline in baselines_name:
baseline_scores = models[baseline][1][metric]
model_scores = models[non_baseline][1][metric]
_, p_value = ttest_ind(baseline_scores, model_scores, equal_var=False)
df[metric].iloc[i] = starify(p_value)
i += 1
# prettier names
df = df.rename(columns={"accuracy": "Accuracy", "balanced_accuracy": "Balanced accuracy",
"f1": "F1-score", "recall": "Recall", "precision": 'Precision'})
df = df.set_index(
pd.Series(
[str(n).capitalize().replace('Cnn', 'CNN').replace('Xgboost', 'XGBoost').
replace('_', ' ').replace('.txt', '').replace('forest', 'Forest').
replace('guess', 'Guess').replace('majority', 'Majority').replace('random', 'Random').
replace('voting', 'Voting')
for n in list(df.index)]
)
)
# to latex
MF.append(df.to_latex(index=True,
bold_rows=True,
caption=caption_MF,
column_format='llllll',
position='H'))
# print
for entry in phF:
print(entry.replace('BOLD\\', r'\textbf').replace('\}', '}'))
for entry in MF:
print(entry.replace('BOLD\\', r'\textbf').replace('\}', '}'))
if __name__ == '__main__':
scores_tables()
print(r'\newpage')
p_value_tables()
##p_value_tables_stars()
print(r'\newpage')
p_value_tables(model_names_first=['cnn.txt'],
model_names_second=['cnn (transformer imp.).txt'])