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:07<00:22,  7.39s/it]
CustomDataset3-WithinSession:  50%|█████     | 2/4 [00:14<00:14,  7.24s/it]

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

  0%|                                     | 12.3k/46.4M [00:00<08:47, 88.0kB/s]

  0%|                                      | 38.9k/46.4M [00:00<04:35, 168kB/s]

  0%|                                      | 95.2k/46.4M [00:00<02:27, 313kB/s]

  0%|▏                                      | 210k/46.4M [00:00<01:18, 585kB/s]

  1%|▎                                     | 439k/46.4M [00:00<00:41, 1.10MB/s]

  2%|▋                                     | 864k/46.4M [00:00<00:22, 2.01MB/s]

  4%|█▍                                   | 1.75M/46.4M [00:00<00:11, 3.92MB/s]

  8%|██▊                                  | 3.53M/46.4M [00:00<00:05, 7.72MB/s]

 15%|█████▌                               | 7.01M/46.4M [00:01<00:02, 15.0MB/s]

 20%|███████▍                             | 9.30M/46.4M [00:01<00:02, 16.7MB/s]

 28%|██████████▎                          | 12.9M/46.4M [00:01<00:01, 21.5MB/s]

 33%|████████████▍                        | 15.5M/46.4M [00:01<00:01, 22.0MB/s]

 42%|███████████████▌                     | 19.5M/46.4M [00:01<00:01, 26.0MB/s]

 49%|██████████████████                   | 22.7M/46.4M [00:01<00:00, 26.2MB/s]

 56%|████████████████████▊                | 26.1M/46.4M [00:01<00:00, 27.4MB/s]

 63%|███████████████████████▍             | 29.5M/46.4M [00:01<00:00, 28.2MB/s]

 72%|██████████████████████████▌          | 33.3M/46.4M [00:01<00:00, 30.0MB/s]

 80%|█████████████████████████████▍       | 37.0M/46.4M [00:02<00:00, 30.9MB/s]

 86%|███████████████████████████████▉     | 40.1M/46.4M [00:02<00:00, 30.0MB/s]

 94%|██████████████████████████████████▉  | 43.8M/46.4M [00:02<00:00, 30.9MB/s]

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

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

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

  0%|                                     | 12.3k/74.3M [00:00<13:33, 91.3kB/s]

  0%|                                      | 41.0k/74.3M [00:00<06:54, 179kB/s]

  0%|                                      | 97.3k/74.3M [00:00<03:53, 317kB/s]

  0%|                                       | 209k/74.3M [00:00<02:09, 572kB/s]

  1%|▏                                     | 426k/74.3M [00:00<01:10, 1.04MB/s]

  1%|▍                                     | 884k/74.3M [00:00<00:36, 2.04MB/s]

  2%|▉                                    | 1.78M/74.3M [00:00<00:18, 3.93MB/s]

  5%|█▊                                   | 3.58M/74.3M [00:00<00:09, 7.68MB/s]

 10%|███▌                                 | 7.15M/74.3M [00:01<00:04, 15.0MB/s]

 15%|█████▌                               | 11.2M/74.3M [00:01<00:03, 20.9MB/s]

 20%|███████▍                             | 15.0M/74.3M [00:01<00:02, 22.5MB/s]

 25%|█████████                            | 18.3M/74.3M [00:01<00:02, 24.4MB/s]

 29%|██████████▊                          | 21.8M/74.3M [00:01<00:02, 26.1MB/s]

 34%|████████████▋                        | 25.4M/74.3M [00:01<00:01, 27.7MB/s]

 39%|██████████████▍                      | 28.9M/74.3M [00:01<00:01, 28.5MB/s]

 44%|████████████████                     | 32.3M/74.3M [00:01<00:01, 29.1MB/s]

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

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

 57%|████████████████████▉                | 42.1M/74.3M [00:02<00:01, 29.1MB/s]

 61%|██████████████████████▋              | 45.6M/74.3M [00:02<00:00, 29.6MB/s]

 65%|████████████████████████▏            | 48.6M/74.3M [00:02<00:00, 28.3MB/s]

 70%|█████████████████████████▊           | 51.8M/74.3M [00:02<00:00, 28.3MB/s]

 75%|███████████████████████████▋         | 55.5M/74.3M [00:02<00:00, 29.5MB/s]

 79%|█████████████████████████████▏       | 58.7M/74.3M [00:02<00:00, 29.0MB/s]

 83%|██████████████████████████████▋      | 61.7M/74.3M [00:02<00:00, 28.1MB/s]

 88%|████████████████████████████████▌    | 65.3M/74.3M [00:03<00:00, 29.3MB/s]

 92%|██████████████████████████████████   | 68.4M/74.3M [00:03<00:00, 28.6MB/s]

 96%|███████████████████████████████████▌ | 71.4M/74.3M [00:03<00:00, 27.6MB/s]

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

CustomDataset3-WithinSession: 100%|██████████| 4/4 [00:57<00:00, 17.62s/it]
CustomDataset3-WithinSession: 100%|██████████| 4/4 [00:57<00:00, 14.38s/it]
      score      time  samples  ... n_sessions         dataset  pipeline
0  0.530000  0.360842    120.0  ...          1  CustomDataset3       MDM
1  0.557500  0.352545    120.0  ...          1  CustomDataset3       MDM
2  0.619863  2.003942    768.0  ...          1  CustomDataset3       MDM
3  0.539310  4.412965   1356.0  ...          1  CustomDataset3       MDM

[4 rows x 9 columns]

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

Estimated memory usage: 611 MB

Gallery generated by Sphinx-Gallery