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.84it/s]

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

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

  2%|▋                                     | 41.0k/2.27M [00:00<00:17, 126kB/s]

  4%|█▋                                     | 101k/2.27M [00:00<00:10, 210kB/s]

  8%|███                                    | 177k/2.27M [00:00<00:06, 301kB/s]

 14%|█████▎                                 | 311k/2.27M [00:00<00:04, 476kB/s]

 16%|██████▎                                | 367k/2.27M [00:01<00:04, 430kB/s]

 20%|███████▊                               | 458k/2.27M [00:01<00:03, 469kB/s]

 24%|█████████▎                             | 540k/2.27M [00:01<00:03, 477kB/s]

 27%|██████████▋                            | 621k/2.27M [00:01<00:03, 481kB/s]

 32%|████████████▎                          | 719k/2.27M [00:01<00:03, 515kB/s]

 35%|█████████████▋                         | 801k/2.27M [00:01<00:02, 509kB/s]

 39%|███████████████▏                       | 883k/2.27M [00:02<00:02, 505kB/s]

 42%|████████████████▌                      | 965k/2.27M [00:02<00:02, 502kB/s]

 48%|██████████████████▎                   | 1.09M/2.27M [00:02<00:02, 587kB/s]

 55%|████████████████████▊                 | 1.24M/2.27M [00:02<00:01, 678kB/s]

 58%|█████████████████████▉                | 1.31M/2.27M [00:02<00:01, 600kB/s]

 62%|███████████████████████▌              | 1.41M/2.27M [00:02<00:01, 593kB/s]

 65%|████████████████████████▌             | 1.47M/2.27M [00:03<00:01, 531kB/s]

 71%|███████████████████████████           | 1.62M/2.27M [00:03<00:01, 636kB/s]

 76%|████████████████████████████▉         | 1.73M/2.27M [00:03<00:00, 653kB/s]

 82%|███████████████████████████████▏      | 1.86M/2.27M [00:03<00:00, 695kB/s]

 89%|█████████████████████████████████▋    | 2.01M/2.27M [00:03<00:00, 754kB/s]

 94%|███████████████████████████████████▊  | 2.14M/2.27M [00:03<00:00, 763kB/s]

 98%|█████████████████████████████████████▏| 2.22M/2.27M [00:04<00:00, 683kB/s]

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


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

  1%|▏                                    | 14.3k/2.13M [00:00<00:30, 70.4kB/s]

  2%|▊                                     | 45.1k/2.13M [00:00<00:15, 132kB/s]

  4%|█▋                                    | 93.2k/2.13M [00:00<00:10, 201kB/s]

 10%|███▊                                   | 208k/2.13M [00:00<00:04, 390kB/s]

 14%|█████▎                                 | 289k/2.13M [00:00<00:04, 426kB/s]

 18%|██████▊                                | 375k/2.13M [00:01<00:03, 458kB/s]

 21%|████████▏                              | 447k/2.13M [00:01<00:03, 453kB/s]

 24%|█████████▍                             | 513k/2.13M [00:01<00:03, 436kB/s]

 28%|██████████▉                            | 595k/2.13M [00:01<00:03, 455kB/s]

 35%|█████████████▌                         | 741k/2.13M [00:01<00:02, 588kB/s]

 39%|███████████████▍                       | 840k/2.13M [00:01<00:02, 589kB/s]

 46%|██████████████████                     | 987k/2.13M [00:02<00:01, 682kB/s]

 50%|███████████████████                   | 1.07M/2.13M [00:02<00:01, 626kB/s]

 55%|████████████████████▊                 | 1.17M/2.13M [00:02<00:01, 617kB/s]

 59%|██████████████████████▎               | 1.25M/2.13M [00:02<00:01, 579kB/s]

 62%|███████████████████████▍              | 1.31M/2.13M [00:02<00:01, 522kB/s]

 66%|█████████████████████████▏            | 1.41M/2.13M [00:02<00:01, 545kB/s]

 70%|██████████████████████████▋           | 1.49M/2.13M [00:03<00:01, 530kB/s]

 77%|█████████████████████████████▎        | 1.64M/2.13M [00:03<00:00, 639kB/s]

 81%|██████████████████████████████▊       | 1.72M/2.13M [00:03<00:00, 596kB/s]

 87%|█████████████████████████████████     | 1.85M/2.13M [00:03<00:00, 654kB/s]

 92%|██████████████████████████████████▊   | 1.95M/2.13M [00:03<00:00, 636kB/s]

 98%|█████████████████████████████████████▏| 2.08M/2.13M [00:03<00:00, 684kB/s]

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

Kalunga2016-WithinSession: 100%|██████████| 2/2 [00:10<00:00,  6.10s/it]
Kalunga2016-WithinSession: 100%|██████████| 2/2 [00:10<00:00,  5.27s/it]
      score      time  samples  ... n_sessions      dataset  pipeline
0  0.251111  0.040635     48.0  ...          1  Kalunga2016       CCA
1  0.331111  0.040015     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 11.947 seconds)

Estimated memory usage: 246 MB

Gallery generated by Sphinx-Gallery