-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbasic_example.py
102 lines (76 loc) · 2.69 KB
/
basic_example.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
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
from virtualreality.dataset import VirtualReality
from virtualreality.utilities import get_block_repetition
import numpy as np
import mne
import moabb
import pandas as pd
from moabb.paradigms import P300
from pyriemann.estimation import ERPCovariances
from pyriemann.classification import MDM
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import KFold
from sklearn.externals import joblib
from tqdm import tqdm
"""
=============================
Classification of the trials
=============================
This example shows how to extract the epochs from the dataset of a given
subject and then classify them using Riemannian Geometry framework for BCI.
We compare the scores in the VR and PC conditions.
"""
# Authors: Pedro Rodrigues <pedro.rodrigues01@gmail.com>
#
# License: BSD (3-clause)
import warnings
warnings.filterwarnings("ignore")
# create dataset
dataset = VirtualReality()
# get the paradigm
paradigm = P300()
# loop to get scores for each subject
nsubjects = 5
df = {}
for tmax in [0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]:
paradigm.tmax = tmax
scores = []
for subject in tqdm(dataset.subject_list[:nsubjects]):
scores_subject = [subject]
for condition in ['VR', 'PC']:
# define the dataset instance
if condition == 'VR':
dataset.VR = True
dataset.PC = False
elif condition == 'PC':
dataset.VR = False
dataset.PC = True
# get the epochs and labels
X, labels, meta = paradigm.get_data(dataset, subjects=[subject])
labels = LabelEncoder().fit_transform(labels)
kf = KFold(n_splits = 6)
repetitions = [1, 2]
auc = []
blocks = np.arange(1, 12+1)
for train_idx, test_idx in kf.split(np.arange(12)):
# split in training and testing blocks
X_training, labels_training, _ = get_block_repetition(X, labels, meta, blocks[train_idx], repetitions)
X_test, labels_test, _ = get_block_repetition(X, labels, meta, blocks[test_idx], repetitions)
# estimate the extended ERP covariance matrices with Xdawn
dict_labels = {'Target':1, 'NonTarget':0}
erpc = ERPCovariances(classes=[dict_labels['Target']], estimator='lwf')
erpc.fit(X_training, labels_training)
covs_training = erpc.transform(X_training)
covs_test = erpc.transform(X_test)
# get the AUC for the classification
clf = MDM()
clf.fit(covs_training, labels_training)
labels_pred = clf.predict(covs_test)
auc.append(roc_auc_score(labels_test, labels_pred))
# stock scores
scores_subject.append(np.mean(auc))
scores.append(scores_subject)
# print results
df[tmax] = pd.DataFrame(scores, columns=['subject', 'VR', 'PC'])
filename = './results.pkl'
joblib.dump(df, filename)