Tutorial 5: Creating a dataset class#

# Author: Gregoire Cattan
#
# https://github.com/plcrodrigues/Workshop-MOABB-BCI-Graz-2019

from pyriemann.classification import MDM
from pyriemann.estimation import ERPCovariances
from sklearn.pipeline import make_pipeline

from moabb.datasets import Cattan2019_VR
from moabb.datasets.braininvaders import BI2014a
from moabb.datasets.compound_dataset import CompoundDataset
from moabb.datasets.utils import blocks_reps
from moabb.evaluations import WithinSessionEvaluation
from moabb.paradigms.p300 import P300

Initialization#

This tutorial illustrates how to use the CompoundDataset to: 1) Select a few subjects/sessions/runs in an existing dataset 2) Merge two CompoundDataset into a new one 3) … and finally use this new dataset on a pipeline (this steps is not specific to CompoundDataset)

Let’s define a paradigm and a pipeline for evaluation first.

paradigm = P300()
pipelines = {}
pipelines["MDM"] = make_pipeline(ERPCovariances(estimator="lwf"), MDM(metric="riemann"))

Creation a selection of subject#

We are going to great two CompoundDataset, namely CustomDataset1 & 2. A CompoundDataset accepts a subjects_list of subjects. It is a list of tuple. A tuple contains 4 values:

  • the original dataset

  • the subject number to select

  • the sessions. It can be:

    • a session name (‘0’)

    • a list of sessions ([‘0’, ‘1’])

    • None to select all the sessions attributed to a subject

  • the runs. As for sessions, it can be a single run name, a list or None` (to select all runs).

class CustomDataset1(CompoundDataset):
    def __init__(self):
        biVR = Cattan2019_VR(virtual_reality=True, screen_display=True)
        runs = blocks_reps([0, 2], [0, 1, 2, 3, 4], biVR.n_repetitions)
        subjects_list = [
            (biVR, 1, "0VR", runs),
            (biVR, 2, "0VR", runs),
        ]
        CompoundDataset.__init__(
            self,
            subjects_list=subjects_list,
            code="CustomDataset1",
            interval=[0, 1.0],
        )


class CustomDataset2(CompoundDataset):
    def __init__(self):
        bi2014 = BI2014a()
        subjects_list = [
            (bi2014, 4, None, None),
            (bi2014, 7, None, None),
        ]
        CompoundDataset.__init__(
            self,
            subjects_list=subjects_list,
            code="CustomDataset2",
            interval=[0, 1.0],
        )

Merging the datasets#

We are now going to merge the two CompoundDataset into a single one. The implementation is straight forward. Instead of providing a list of subjects, you should provide a list of CompoundDataset. subjects_list = [CustomDataset1(), CustomDataset2()]

class CustomDataset3(CompoundDataset):
    def __init__(self):
        subjects_list = [CustomDataset1(), CustomDataset2()]
        CompoundDataset.__init__(
            self,
            subjects_list=subjects_list,
            code="CustomDataset3",
            interval=[0, 1.0],
        )

Evaluate and display#

Let’s use a WithinSessionEvaluation to evaluate our new dataset. If you already new how to do this, nothing changed: The CompoundDataset can be used as a normal dataset.

datasets = [CustomDataset3()]
evaluation = WithinSessionEvaluation(
    paradigm=paradigm, datasets=datasets, overwrite=False, suffix="newdataset"
)
scores = evaluation.process(pipelines)

print(scores)
CustomDataset3-WithinSession:   0%|          | 0/4 [00:00<?, ?it/s]
CustomDataset3-WithinSession:  25%|██▌       | 1/4 [00:06<00:20,  6.98s/it]
CustomDataset3-WithinSession:  50%|█████     | 2/4 [00:13<00:13,  6.81s/it]

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

  0%|                                     | 13.3k/46.4M [00:00<07:52, 98.2kB/s]

  0%|                                      | 84.0k/46.4M [00:00<02:46, 277kB/s]

  1%|▎                                      | 368k/46.4M [00:00<00:53, 860kB/s]

  2%|▊                                    | 1.01M/46.4M [00:00<00:19, 2.33MB/s]

  5%|█▋                                   | 2.16M/46.4M [00:00<00:09, 4.86MB/s]

  9%|███▏                                 | 4.05M/46.4M [00:00<00:04, 8.83MB/s]

 16%|██████                               | 7.58M/46.4M [00:00<00:02, 16.4MB/s]

 23%|████████▌                            | 10.7M/46.4M [00:01<00:01, 20.7MB/s]

 31%|███████████▎                         | 14.2M/46.4M [00:01<00:01, 24.9MB/s]

 38%|██████████████▏                      | 17.9M/46.4M [00:01<00:01, 28.4MB/s]

 46%|████████████████▉                    | 21.2M/46.4M [00:01<00:00, 30.0MB/s]

 54%|███████████████████▊                 | 24.9M/46.4M [00:01<00:00, 31.9MB/s]

 61%|██████████████████████▍              | 28.1M/46.4M [00:01<00:00, 32.0MB/s]

 68%|█████████████████████████            | 31.5M/46.4M [00:01<00:00, 32.4MB/s]

 76%|███████████████████████████▉         | 35.1M/46.4M [00:01<00:00, 33.6MB/s]

 83%|██████████████████████████████▋      | 38.5M/46.4M [00:01<00:00, 33.5MB/s]

 91%|█████████████████████████████████▋   | 42.2M/46.4M [00:01<00:00, 34.7MB/s]

 98%|████████████████████████████████████▍| 45.7M/46.4M [00:02<00:00, 34.3MB/s]

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

CustomDataset3-WithinSession:  75%|███████▌  | 3/4 [00:29<00:10, 10.71s/it]

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

  0%|                                      | 13.3k/74.3M [00:00<10:29, 118kB/s]

  0%|                                      | 84.0k/74.3M [00:00<04:14, 292kB/s]

  0%|▏                                      | 313k/74.3M [00:00<01:15, 978kB/s]

  1%|▍                                     | 784k/74.3M [00:00<00:43, 1.68MB/s]

  2%|▉                                    | 1.76M/74.3M [00:00<00:19, 3.80MB/s]

  5%|█▋                                   | 3.50M/74.3M [00:00<00:09, 7.52MB/s]

  9%|███▍                                 | 6.99M/74.3M [00:00<00:04, 15.2MB/s]

 14%|█████▏                               | 10.4M/74.3M [00:01<00:03, 20.7MB/s]

 19%|██████▊                              | 13.8M/74.3M [00:01<00:02, 24.3MB/s]

 24%|████████▊                            | 17.6M/74.3M [00:01<00:01, 28.5MB/s]

 29%|██████████▌                          | 21.3M/74.3M [00:01<00:01, 30.6MB/s]

 33%|████████████▍                        | 24.9M/74.3M [00:01<00:01, 32.1MB/s]

 38%|██████████████                       | 28.2M/74.3M [00:01<00:01, 32.5MB/s]

 42%|███████████████▋                     | 31.6M/74.3M [00:01<00:01, 31.9MB/s]

 47%|█████████████████▌                   | 35.2M/74.3M [00:01<00:01, 33.2MB/s]

 52%|███████████████████▎                 | 38.8M/74.3M [00:01<00:01, 33.7MB/s]

 57%|█████████████████████                | 42.4M/74.3M [00:01<00:00, 34.3MB/s]

 62%|██████████████████████▊              | 45.8M/74.3M [00:02<00:00, 34.0MB/s]

 67%|████████████████████████▌            | 49.4M/74.3M [00:02<00:00, 34.5MB/s]

 71%|██████████████████████████▎          | 52.9M/74.3M [00:02<00:00, 34.5MB/s]

 76%|████████████████████████████▏        | 56.5M/74.3M [00:02<00:00, 34.7MB/s]

 81%|█████████████████████████████▉       | 60.0M/74.3M [00:02<00:00, 34.8MB/s]

 85%|███████████████████████████████▌     | 63.5M/74.3M [00:02<00:00, 34.4MB/s]

 90%|█████████████████████████████████▎   | 67.0M/74.3M [00:02<00:00, 34.5MB/s]

 95%|███████████████████████████████████  | 70.5M/74.3M [00:02<00:00, 34.6MB/s]

100%|████████████████████████████████████▊| 74.0M/74.3M [00:02<00:00, 34.2MB/s]

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

CustomDataset3-WithinSession: 100%|██████████| 4/4 [00:56<00:00, 17.46s/it]
CustomDataset3-WithinSession: 100%|██████████| 4/4 [00:56<00:00, 14.21s/it]
      score      time  samples  ... n_sessions         dataset  pipeline
0  0.635000  0.389871    120.0  ...          1  CustomDataset3       MDM
1  0.600000  0.359119    120.0  ...          1  CustomDataset3       MDM
2  0.638159  2.193877    768.0  ...          1  CustomDataset3       MDM
3  0.542359  4.396564   1356.0  ...          1  CustomDataset3       MDM

[4 rows x 9 columns]

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

Estimated memory usage: 469 MB

Gallery generated by Sphinx-Gallery