Note
Go to the end to download the full example code.
Select Electrodes and Resampling#
Within paradigm, it is possible to restrict analysis only to a subset of electrodes and to resample to a specific sampling rate. There is also a utility function to select common electrodes shared between datasets. This tutorial demonstrates how to use this functionality.
# Authors: Sylvain Chevallier <sylvain.chevallier@uvsq.fr>
#
# License: BSD (3-clause)
import matplotlib.pyplot as plt
from mne.decoding import CSP
from pyriemann.estimation import Covariances
from pyriemann.tangentspace import TangentSpace
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
from sklearn.linear_model import LogisticRegression as LR
from sklearn.pipeline import make_pipeline
import moabb.analysis.plotting as moabb_plt
from moabb.datasets import BNCI2014_001, Zhou2016
from moabb.datasets.utils import find_intersecting_channels
from moabb.evaluations import WithinSessionEvaluation
from moabb.paradigms import LeftRightImagery
Datasets#
Load 2 subjects of BNCI 2014-004 and Zhou2016 datasets, with 2 sessions each
subj = [1, 2]
datasets = [Zhou2016(), BNCI2014_001()]
for d in datasets:
d.subject_list = subj
Paradigm#
Restrict further analysis to specified channels, here C3, C4, and Cz. Also, use a specific resampling. In this example, all datasets are set to 200 Hz.
paradigm = LeftRightImagery(channels=["C3", "C4", "Cz"], resample=200.0)
Evaluation#
The evaluation is conducted on with CSP+LDA, only on the 3 electrodes, with a sampling rate of 200 Hz.
evaluation = WithinSessionEvaluation(paradigm=paradigm, datasets=datasets)
csp_lda = make_pipeline(CSP(n_components=2), LDA())
ts_lr = make_pipeline(
Covariances(estimator="oas"), TangentSpace(metric="riemann"), LR(C=1.0)
)
results = evaluation.process({"csp+lda": csp_lda, "ts+lr": ts_lr})
print(results.head())
Zhou2016-WithinSession: 0%| | 0/2 [00:00<?, ?it/s]
Zhou2016-WithinSession: 50%|█████ | 1/2 [00:38<00:38, 38.98s/it]
Zhou2016-WithinSession: 100%|██████████| 2/2 [01:17<00:00, 38.78s/it]
Zhou2016-WithinSession: 100%|██████████| 2/2 [01:17<00:00, 38.81s/it]
BNCI2014-001-WithinSession: 0%| | 0/2 [00:00<?, ?it/s]
BNCI2014-001-WithinSession: 50%|█████ | 1/2 [00:27<00:27, 27.62s/it]
BNCI2014-001-WithinSession: 100%|██████████| 2/2 [00:55<00:00, 27.59s/it]
BNCI2014-001-WithinSession: 100%|██████████| 2/2 [00:55<00:00, 27.60s/it]
score time ... pipeline codecarbon_task_name
0 0.856313 0.037852 ... ts+lr 6693e60e-adbe-40c3-9c2a-084eb4c7b8d7
1 0.920000 0.032828 ... ts+lr 3723767e-205b-4454-b45b-c10057c72d36
2 0.946000 0.032568 ... ts+lr 65275037-16a9-4ecd-a582-81d705bab8f6
3 0.918000 0.032623 ... ts+lr c3a21f72-c70c-4532-b9eb-057e633d7774
4 0.839506 0.029705 ... ts+lr c3b0786d-ae01-4885-829a-e556bb52d16e
[5 rows x 11 columns]
Electrode Selection#
It is possible to select the electrodes that are shared by all datasets using the find_intersecting_channels function. Datasets that have 0 overlap with others are discarded. It returns the set of common channels, as well as the list of datasets with valid channels.
electrodes, datasets = find_intersecting_channels(datasets)
evaluation = WithinSessionEvaluation(
paradigm=paradigm, datasets=datasets, overwrite=True, suffix="resample"
)
results = evaluation.process({"csp+lda": csp_lda, "ts+lr": ts_lr})
print(results.head())
Zhou2016-WithinSession: 0%| | 0/2 [00:00<?, ?it/s]
Zhou2016-WithinSession: 50%|█████ | 1/2 [00:38<00:38, 38.87s/it]
Zhou2016-WithinSession: 100%|██████████| 2/2 [01:17<00:00, 38.67s/it]
Zhou2016-WithinSession: 100%|██████████| 2/2 [01:17<00:00, 38.70s/it]
BNCI2014-001-WithinSession: 0%| | 0/2 [00:00<?, ?it/s]
BNCI2014-001-WithinSession: 50%|█████ | 1/2 [00:27<00:27, 27.60s/it]
BNCI2014-001-WithinSession: 100%|██████████| 2/2 [00:55<00:00, 27.59s/it]
BNCI2014-001-WithinSession: 100%|██████████| 2/2 [00:55<00:00, 27.59s/it]
score time ... pipeline codecarbon_task_name
0 0.860101 0.038367 ... ts+lr 5cbd3949-ba85-4a22-a935-8e5527e847be
1 0.930000 0.033058 ... ts+lr b76fbd26-ee16-4850-a89a-7296f14b501c
2 0.950000 0.032593 ... ts+lr e244d24f-b770-4f2f-9e93-760b7e18353b
3 0.912000 0.032709 ... ts+lr aa9373ce-59e3-4149-98a1-ba310700b7bf
4 0.809877 0.030288 ... ts+lr c3eff376-c894-4e3c-9620-b93d8dd7ecd1
[5 rows x 11 columns]
Plot Results#
Compare the obtained results with the two pipelines, CSP+LDA and logistic regression computed in the tangent space of the covariance matrices.
fig = moabb_plt.paired_plot(results, "csp+lda", "ts+lr")
plt.show()

Total running time of the script: (4 minutes 34.105 seconds)
Estimated memory usage: 769 MB