forked from NeuroTechX/moabb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_cross_session_multiple_datasets.py
118 lines (97 loc) · 3.65 KB
/
plot_cross_session_multiple_datasets.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"""
==================================
Cross-Session on Multiple Datasets
==================================
This example shows how to perform a cross-session analysis on two MI datasets
using a CSP+LDA pipeline
The cross session evaluation context will evaluate performance using a leave
one session out cross-validation. For each session in the dataset, a model
is trained on every other session and performance are evaluated on the current
session.
"""
# Authors: Sylvain Chevallier <[email protected]>
#
# License: BSD (3-clause)
import warnings
import matplotlib.pyplot as plt
import seaborn as sns
from mne.decoding import CSP
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
from sklearn.pipeline import make_pipeline
import moabb
from moabb.datasets import BNCI2014001, Zhou2016
from moabb.evaluations import CrossSessionEvaluation
from moabb.paradigms import LeftRightImagery
warnings.simplefilter(action="ignore", category=FutureWarning)
warnings.simplefilter(action="ignore", category=RuntimeWarning)
moabb.set_log_level("info")
###############################################################################
# Loading dataset
# ---------------
#
# Load 2 subjects of BNCI 2014-004 and Zhou2016 datasets, with 2 session each
subj = [1, 2]
datasets = [Zhou2016(), BNCI2014001()]
for d in datasets:
d.subject_list = subj
###############################################################################
# Choose paradigm
# ---------------
#
# We select the paradigm MI, applying a bandpass filter (8-35 Hz) on
# the data and we will keep only left and right hand motor imagery
paradigm = LeftRightImagery(fmin=8, fmax=35)
##############################################################################
# Create pipelines
# ----------------
#
# Use the Common Spatial Patterns with 8 components and a Linear Discriminant
# Analysis classifier.
pipeline = {}
pipeline["CSP+LDA"] = make_pipeline(CSP(n_components=8), LDA())
##############################################################################
# Get data (optional)
# -------------------
#
# To get access to the EEG signals downloaded from the dataset, you could
# use `dataset.get_data(subjects=[subject_id])` to obtain the EEG under
# an MNE format, stored in a dictionary of sessions and runs.
# Otherwise, `paradigm.get_data(dataset=dataset, subjects=[subject_id])`
# allows to obtain the EEG data in scikit format, the labels and the meta
# information. The data are preprocessed according to the paradigm
# requirements.
# X_all, labels_all, meta_all = [], [], []
# for d in datasets:
# # sessions = d.get_data(subjects=[2])
# X, labels, meta = paradigm.get_data(dataset=d, subjects=[2])
# X_all.append(X)
# labels_all.append(labels)
# meta_all.append(meta)
##############################################################################
# Evaluation
# ----------
#
# The evaluation will return a dataframe containing a single AUC score for
# each subject / session of the dataset, and for each pipeline.
overwrite = True # set to True if we want to overwrite cached results
evaluation = CrossSessionEvaluation(
paradigm=paradigm, datasets=datasets, suffix="examples", overwrite=overwrite
)
results = evaluation.process(pipeline)
print(results.head())
results.replace(["session_E", "session_T"], ["session_0", "session_1"], inplace=True)
##############################################################################
# Plot Results
# ----------------
#
# Here we plot the results, indicating the score for each session and subject
sns.catplot(
data=results,
x="session",
y="score",
hue="subject",
col="dataset",
kind="bar",
palette="viridis",
)
plt.show()