-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassification_scores.py
73 lines (56 loc) · 2.05 KB
/
classification_scores.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
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import StratifiedKFold, cross_val_score
from sklearn.preprocessing import LabelEncoder
from pyriemann.classification import MDM
from pyriemann.estimation import ERPCovariances
from braininvaders2012.dataset import BrainInvaders2012
from tqdm import tqdm
import numpy as np
import mne
import joblib
"""
=============================
Classification of the trials
=============================
This example shows how to extract the epochs from the dataset of a given
subject and then classify them using Machine Learning techniques using
Riemannian Geometry.
"""
# Authors: Pedro Rodrigues <pedro.rodrigues01@gmail.com>
#
# License: BSD (3-clause)
import warnings
warnings.filterwarnings("ignore")
# define the dataset instance
dataset = BrainInvaders2012(Training=True)
scr = {}
# get the data from subject of interest
for subject in dataset.subject_list:
data = dataset._get_single_subject_data(subject)
raw = data['session_1']['run_training']
# filter data and resample
fmin = 1
fmax = 24
raw.filter(fmin, fmax, verbose=False)
# detect the events and cut the signal into epochs
events = mne.find_events(raw=raw, shortest_event=1, verbose=False)
event_id = {'NonTarget': 1, 'Target': 2}
epochs = mne.Epochs(raw, events, event_id, tmin=0.0, tmax=1.0, baseline=None, verbose=False, preload=True)
epochs.pick_types(eeg=True)
# get trials and labels
X = epochs.get_data()
y = events[:, -1]
y = LabelEncoder().fit_transform(y)
# cross validation
skf = StratifiedKFold(n_splits=5)
clf = make_pipeline(ERPCovariances(estimator='lwf', classes=[1]), MDM())
scr[subject] = cross_val_score(clf, X, y, cv=skf, scoring='roc_auc').mean()
# print results of classification
print('subject', subject)
print('mean AUC :', scr[subject])
#####
filename = './classification_scores.pkl'
joblib.dump(scr, filename)
with open('classification_scores.txt', 'w') as the_file:
for subject in scr.keys():
the_file.write('subject ' + str(subject).zfill(2) + ' :' + ' {:.2f}'.format(scr[subject]) + '\n')