Note
Go to the end to download the full example code.
Within Session SSVEP#
This Example shows how to perform a within-session SSVEP analysis on the MAMEM dataset 3, using a CCA pipeline.
The within-session evaluation assesses the performance of a classification pipeline using a 5-fold cross-validation. The reported metric (here, accuracy) is the average of all fold.
# Authors: Sylvain Chevallier <sylvain.chevallier@uvsq.fr>
#
# License: BSD (3-clause)
import warnings
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.pipeline import make_pipeline
import moabb
from moabb.datasets import Kalunga2016
from moabb.evaluations import WithinSessionEvaluation
from moabb.paradigms import SSVEP
from moabb.pipelines import SSVEP_CCA
warnings.simplefilter(action="ignore", category=FutureWarning)
warnings.simplefilter(action="ignore", category=RuntimeWarning)
moabb.set_log_level("info")
Loading Dataset#
Load 2 subjects of Kalunga2016 dataset
subj = [1, 3]
dataset = Kalunga2016()
dataset.subject_list = subj
Choose Paradigm#
We select the paradigm SSVEP, applying a bandpass filter (3-15 Hz) on the data and we keep only the first 3 classes, that is stimulation frequency of 13Hz, 17Hz and 21Hz.
Create Pipelines#
Use a Canonical Correlation Analysis classifier
interval = dataset.interval
freqs = paradigm.used_events(dataset)
pipeline = {}
pipeline["CCA"] = make_pipeline(SSVEP_CCA(n_harmonics=3))
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 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. In paradigm.get_data, the EEG are preprocessed according to the paradigm requirement.
# sessions = dataset.get_data(subjects=[3])
# X, labels, meta = paradigm.get_data(dataset=dataset, subjects=[3])
Evaluation#
The evaluation will return a DataFrame containing a single AUC score for each subject and pipeline.
overwrite = True # set to True if we want to overwrite cached results
evaluation = WithinSessionEvaluation(
paradigm=paradigm, datasets=dataset, suffix="examples", overwrite=overwrite
)
results = evaluation.process(pipeline)
print(results.head())
Kalunga2016-WithinSession: 0%| | 0/2 [00:00<?, ?it/s]
Kalunga2016-WithinSession: 50%|█████ | 1/2 [00:09<00:09, 9.50s/it]
0%| | 0.00/2.27M [00:00<?, ?B/s]
1%|▏ | 13.3k/2.27M [00:00<00:22, 101kB/s]
2%|▋ | 41.0k/2.27M [00:00<00:12, 178kB/s]
4%|█▌ | 96.3k/2.27M [00:00<00:06, 313kB/s]
6%|██▌ | 146k/2.27M [00:00<00:05, 362kB/s]
9%|███▍ | 197k/2.27M [00:00<00:05, 389kB/s]
12%|████▋ | 276k/2.27M [00:00<00:04, 493kB/s]
15%|██████ | 350k/2.27M [00:00<00:03, 541kB/s]
19%|███████▌ | 438k/2.27M [00:00<00:03, 611kB/s]
25%|█████████▊ | 569k/2.27M [00:01<00:02, 776kB/s]
31%|████████████ | 700k/2.27M [00:01<00:01, 888kB/s]
35%|█████████████▋ | 799k/2.27M [00:01<00:01, 878kB/s]
40%|███████████████▋ | 913k/2.27M [00:01<00:01, 913kB/s]
44%|████████████████▉ | 1.01M/2.27M [00:01<00:01, 895kB/s]
48%|██████████████████▍ | 1.10M/2.27M [00:01<00:01, 861kB/s]
55%|████████████████████▋ | 1.24M/2.27M [00:01<00:01, 966kB/s]
61%|██████████████████████▌ | 1.39M/2.27M [00:01<00:00, 1.06MB/s]
68%|████████████████████████▉ | 1.53M/2.27M [00:01<00:00, 1.12MB/s]
74%|███████████████████████████▍ | 1.68M/2.27M [00:02<00:00, 1.17MB/s]
79%|█████████████████████████████▎ | 1.80M/2.27M [00:02<00:00, 1.13MB/s]
84%|███████████████████████████████▏ | 1.91M/2.27M [00:02<00:00, 1.08MB/s]
89%|████████████████████████████████▉ | 2.02M/2.27M [00:02<00:00, 1.04MB/s]
94%|██████████████████████████████████▌ | 2.13M/2.27M [00:02<00:00, 1.00MB/s]
98%|█████████████████████████████████████▏| 2.23M/2.27M [00:02<00:00, 963kB/s]
0%| | 0.00/2.27M [00:00<?, ?B/s]
100%|█████████████████████████████████████| 2.27M/2.27M [00:00<00:00, 11.3GB/s]
0%| | 0.00/2.13M [00:00<?, ?B/s]
1%|▏ | 12.3k/2.13M [00:00<00:28, 74.0kB/s]
2%|▋ | 39.9k/2.13M [00:00<00:13, 157kB/s]
4%|█▋ | 91.1k/2.13M [00:00<00:07, 290kB/s]
10%|███▋ | 203k/2.13M [00:00<00:03, 552kB/s]
20%|███████▊ | 435k/2.13M [00:00<00:01, 1.07MB/s]
42%|███████████████▉ | 893k/2.13M [00:00<00:00, 2.06MB/s]
85%|███████████████████████████████▍ | 1.81M/2.13M [00:00<00:00, 3.98MB/s]
0%| | 0.00/2.13M [00:00<?, ?B/s]
100%|█████████████████████████████████████| 2.13M/2.13M [00:00<00:00, 8.18GB/s]
Kalunga2016-WithinSession: 100%|██████████| 2/2 [00:23<00:00, 12.23s/it]
Kalunga2016-WithinSession: 100%|██████████| 2/2 [00:23<00:00, 11.82s/it]
score time ... pipeline codecarbon_task_name
0 0.333333 0.001203 ... CCA 21d8ae72-63c1-49b1-a43d-29e26f152aff
1 0.337778 0.001123 ... CCA 9fccf3ef-4f80-4e85-9678-2f8fd7dc6693
[2 rows x 11 columns]
Plot Results#
Here we plot the results, indicating the score for each subject
plt.figure()
sns.barplot(data=results, y="score", x="session", hue="subject", palette="viridis")

<Axes: xlabel='session', ylabel='score'>
And the computation time in seconds
plt.figure()
ax = sns.barplot(data=results, y="time", x="session", hue="subject", palette="Reds")
ax.set_ylabel("Time (s)")
plt.show()

Total running time of the script: (0 minutes 25.908 seconds)
Estimated memory usage: 318 MB