.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_tutorials/tutorial_4_adding_a_dataset.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_tutorials_tutorial_4_adding_a_dataset.py: ==================================== Tutorial 4: Creating a dataset class ==================================== .. GENERATED FROM PYTHON SOURCE LINES 6-23 .. code-block:: default # Authors: Pedro L. C. Rodrigues, Sylvain Chevallier # # https://github.com/plcrodrigues/Workshop-MOABB-BCI-Graz-2019 import mne import numpy as np from pyriemann.classification import MDM from pyriemann.estimation import Covariances from scipy.io import loadmat, savemat from sklearn.pipeline import make_pipeline from moabb.datasets import download as dl from moabb.datasets.base import BaseDataset from moabb.evaluations import WithinSessionEvaluation from moabb.paradigms import LeftRightImagery .. GENERATED FROM PYTHON SOURCE LINES 24-33 Creating some Data ------------------ To illustrate the creation of a dataset class in MOABB, we first create an example dataset saved in .mat file. It contains a single fake recording on 8 channels lasting for 150 seconds (sampling frequency 256 Hz). We have included the script that creates this dataset and have uploaded it online. The fake dataset is available on the `Zenodo website `_ .. GENERATED FROM PYTHON SOURCE LINES 33-70 .. code-block:: default def create_example_dataset(): """Create a fake example for a dataset""" sfreq = 256 t_recording = 150 t_trial = 1 # duration of a trial intertrial = 2 # time between end of a trial and the next one n_chan = 8 x = np.zeros((n_chan + 1, t_recording * sfreq)) # electrodes + stimulus stim = np.zeros(t_recording * sfreq) t_offset = 1.0 # offset where the trials start n_trials = 40 rep = np.linspace(0, 4 * t_trial, t_trial * sfreq) signal = np.sin(2 * np.pi / t_trial * rep) for n in range(n_trials): label = n % 2 + 1 # alternate between class 0 and class 1 tn = int(t_offset * sfreq + n * (t_trial + intertrial) * sfreq) stim[tn] = label noise = 0.1 * np.random.randn(n_chan, len(signal)) x[:-1, tn : (tn + t_trial * sfreq)] = label * signal + noise x[-1, :] = stim return x, sfreq # Create the fake data for subject in [1, 2, 3]: x, fs = create_example_dataset() filename = "subject_" + str(subject).zfill(2) + ".mat" mdict = {} mdict["x"] = x mdict["fs"] = fs savemat(filename, mdict) .. GENERATED FROM PYTHON SOURCE LINES 71-86 Creating a Dataset Class ------------------------ We will create now a dataset class using the fake data simulated with the code from above. For this, we first need to import the right classes from MOABB: - ``dl`` is a very useful script that downloads automatically a dataset online if it is not yet available in the user's computer. The script knows where to download the files because we create a global variable telling the URL where to fetch the data. - ``BaseDataset`` is the basic class that we overload to create our dataset. The global variable with the dataset's URL should specify an online repository where all the files are stored. .. GENERATED FROM PYTHON SOURCE LINES 86-89 .. code-block:: default ExampleDataset_URL = "https://sandbox.zenodo.org/record/369543/files/" .. GENERATED FROM PYTHON SOURCE LINES 90-96 The ``ExampleDataset`` needs to implement only 3 functions: - ``__init__`` for indicating the parameter of the dataset - ``_get_single_subject_data`` to define how to process the data once they have been downloaded - ``data_path`` to define how the data are downloaded. .. GENERATED FROM PYTHON SOURCE LINES 96-145 .. code-block:: default class ExampleDataset(BaseDataset): """ Dataset used to exemplify the creation of a dataset class in MOABB. The data samples have been simulated and has no physiological meaning whatsoever. """ def __init__(self): super().__init__( subjects=[1, 2, 3], sessions_per_subject=1, events={"left_hand": 1, "right_hand": 2}, code="Example dataset", interval=[0, 0.75], paradigm="imagery", doi="", ) def _get_single_subject_data(self, subject): """return data for a single subject""" file_path_list = self.data_path(subject) data = loadmat(file_path_list[0]) x = data["x"] fs = data["fs"] ch_names = ["ch" + str(i) for i in range(8)] + ["stim"] ch_types = ["eeg" for i in range(8)] + ["stim"] info = mne.create_info(ch_names, fs, ch_types) raw = mne.io.RawArray(x, info) sessions = {} sessions["session_1"] = {} sessions["session_1"]["run_1"] = raw return sessions def data_path( self, subject, path=None, force_update=False, update_path=None, verbose=None ): """Download the data from one subject""" if subject not in self.subject_list: raise (ValueError("Invalid subject number")) url = "{:s}subject_0{:d}.mat".format(ExampleDataset_URL, subject) path = dl.data_dl(url, "ExampleDataset") return [path] # it has to return a list .. GENERATED FROM PYTHON SOURCE LINES 146-151 Using the ExampleDataset ------------------------ Now that the `ExampleDataset` is defined, it could be instanciated directly. The rest of the code follows the steps described in the previous tutorials. .. GENERATED FROM PYTHON SOURCE LINES 151-166 .. code-block:: default dataset = ExampleDataset() paradigm = LeftRightImagery() X, labels, meta = paradigm.get_data(dataset=dataset, subjects=[1]) evaluation = WithinSessionEvaluation( paradigm=paradigm, datasets=dataset, overwrite=False, suffix="newdataset" ) pipelines = {} pipelines["MDM"] = make_pipeline(Covariances("oas"), MDM(metric="riemann")) scores = evaluation.process(pipelines) print(scores) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 0%| | 0.00/2.77M [00:00`_. # noqa: E501 You could then follow the instructions on `how to contribute `_ # noqa: E501 .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 7.175 seconds) .. _sphx_glr_download_auto_tutorials_tutorial_4_adding_a_dataset.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: tutorial_4_adding_a_dataset.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: tutorial_4_adding_a_dataset.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_