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

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.

paradigm = SSVEP(fmin=10, fmax=40, n_classes=3)

Create Pipelines#

Use a Canonical Correlation Analysis classifier

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]No hdf5_path provided, models will not be saved.

Kalunga2016-WithinSession:  50%|█████     | 1/2 [00:00<00:00,  1.83it/s]

  0%|                                              | 0.00/2.27M [00:00<?, ?B/s]

  1%|▏                                     | 12.3k/2.27M [00:00<00:22, 103kB/s]

  4%|█▍                                    | 89.1k/2.27M [00:00<00:07, 310kB/s]

 11%|████▍                                  | 255k/2.27M [00:00<00:03, 585kB/s]

 21%|████████▏                              | 478k/2.27M [00:00<00:02, 824kB/s]

 29%|███████████▏                          | 667k/2.27M [00:00<00:01, 1.07MB/s]

 35%|█████████████▌                         | 788k/2.27M [00:00<00:01, 904kB/s]

 43%|████████████████▋                      | 969k/2.27M [00:01<00:01, 915kB/s]

 51%|███████████████████▏                  | 1.15M/2.27M [00:01<00:01, 921kB/s]

 61%|██████████████████████▋              | 1.39M/2.27M [00:01<00:00, 1.03MB/s]

 70%|█████████████████████████▉           | 1.59M/2.27M [00:01<00:00, 1.03MB/s]

 80%|█████████████████████████████▌       | 1.82M/2.27M [00:01<00:00, 1.08MB/s]

 89%|████████████████████████████████▊    | 2.02M/2.27M [00:02<00:00, 1.06MB/s]

 99%|████████████████████████████████████▌| 2.24M/2.27M [00:02<00:00, 1.10MB/s]

  0%|                                              | 0.00/2.27M [00:00<?, ?B/s]
100%|█████████████████████████████████████| 2.27M/2.27M [00:00<00:00, 10.5GB/s]


  0%|                                              | 0.00/2.13M [00:00<?, ?B/s]

  1%|▎                                     | 14.3k/2.13M [00:00<00:16, 125kB/s]

  5%|██                                     | 116k/2.13M [00:00<00:04, 411kB/s]

 24%|████████▉                             | 503k/2.13M [00:00<00:01, 1.22MB/s]

 69%|█████████████████████████▍           | 1.46M/2.13M [00:00<00:00, 3.50MB/s]

  0%|                                              | 0.00/2.13M [00:00<?, ?B/s]
100%|█████████████████████████████████████| 2.13M/2.13M [00:00<00:00, 6.36GB/s]
No hdf5_path provided, models will not be saved.

Kalunga2016-WithinSession: 100%|██████████| 2/2 [00:05<00:00,  2.93s/it]
Kalunga2016-WithinSession: 100%|██████████| 2/2 [00:05<00:00,  2.58s/it]
      score      time  samples  ... n_sessions      dataset  pipeline
0  0.248889  0.040808     48.0  ...          1  Kalunga2016       CCA
1  0.335556  0.039719     48.0  ...          1  Kalunga2016       CCA

[2 rows x 9 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")
plot within session ssvep
<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()
plot within session ssvep

Total running time of the script: (0 minutes 6.495 seconds)

Estimated memory usage: 246 MB

Gallery generated by Sphinx-Gallery