
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/plot_split_dataset.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        Click :ref:`here <sphx_glr_download_auto_examples_plot_split_dataset.py>`
        to download the full example code

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_plot_split_dataset.py:

Split Dataset Example
========================

In this example, we show multiple ways of how to split datasets.

.. GENERATED FROM PYTHON SOURCE LINES 6-16

.. code-block:: default


    # Authors: Lukas Gemein <l.gemein@gmail.com>
    #
    # License: BSD (3-clause)

    from IPython.display import display

    from braindecode.datasets import MOABBDataset
    from braindecode.datautil.windowers import create_windows_from_events








.. GENERATED FROM PYTHON SOURCE LINES 17-18

First, we create a dataset based on BCIC IV 2a fetched with MOABB,

.. GENERATED FROM PYTHON SOURCE LINES 18-20

.. code-block:: default

    ds = MOABBDataset(dataset_name="BNCI2014001", subject_ids=[1])





.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    48 events found
    Event IDs: [1 2 3 4]
    48 events found
    Event IDs: [1 2 3 4]
    48 events found
    Event IDs: [1 2 3 4]
    48 events found
    Event IDs: [1 2 3 4]
    48 events found
    Event IDs: [1 2 3 4]
    48 events found
    Event IDs: [1 2 3 4]
    48 events found
    Event IDs: [1 2 3 4]
    48 events found
    Event IDs: [1 2 3 4]
    48 events found
    Event IDs: [1 2 3 4]
    48 events found
    Event IDs: [1 2 3 4]
    48 events found
    Event IDs: [1 2 3 4]
    48 events found
    Event IDs: [1 2 3 4]




.. GENERATED FROM PYTHON SOURCE LINES 21-22

ds has a pandas DataFrame with additional description of its internal datasets

.. GENERATED FROM PYTHON SOURCE LINES 22-24

.. code-block:: default

    display(ds.description)





.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

        subject    session    run
    0         1  session_T  run_0
    1         1  session_T  run_1
    2         1  session_T  run_2
    3         1  session_T  run_3
    4         1  session_T  run_4
    5         1  session_T  run_5
    6         1  session_E  run_0
    7         1  session_E  run_1
    8         1  session_E  run_2
    9         1  session_E  run_3
    10        1  session_E  run_4
    11        1  session_E  run_5




.. GENERATED FROM PYTHON SOURCE LINES 25-28

We can split the dataset based on the info in the description, for example
based on different runs. The returned dictionary will have string keys
corresponding to unique entries in the description DataFrame column

.. GENERATED FROM PYTHON SOURCE LINES 28-32

.. code-block:: default

    splits = ds.split("run")
    display(splits)
    display(splits["run_4"].description)





.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    {'run_0': <braindecode.datasets.base.BaseConcatDataset object at 0x7fd78eaa8070>, 'run_1': <braindecode.datasets.base.BaseConcatDataset object at 0x7fd78c2068b0>, 'run_2': <braindecode.datasets.base.BaseConcatDataset object at 0x7fd78c206a30>, 'run_3': <braindecode.datasets.base.BaseConcatDataset object at 0x7fd78c2067c0>, 'run_4': <braindecode.datasets.base.BaseConcatDataset object at 0x7fd78c206c40>, 'run_5': <braindecode.datasets.base.BaseConcatDataset object at 0x7fd78c206df0>}
       subject    session    run
    0        1  session_T  run_4
    1        1  session_E  run_4




.. GENERATED FROM PYTHON SOURCE LINES 33-36

We can also split the dataset based on a list of integers corresponding to
rows in the description. In this case, the returned dictionary will have
'0' as the only key

.. GENERATED FROM PYTHON SOURCE LINES 36-40

.. code-block:: default

    splits = ds.split([0, 1, 5])
    display(splits)
    display(splits["0"].description)





.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    {'0': <braindecode.datasets.base.BaseConcatDataset object at 0x7fd78c206a90>}
       subject    session    run
    0        1  session_T  run_0
    1        1  session_T  run_1
    2        1  session_T  run_5




.. GENERATED FROM PYTHON SOURCE LINES 41-45

If we want multiple splits based on indices, we can also specify a list of
list of integers. In this case, the dictionary will have string keys
representing the id of the dataset split in the order of the given list of
integers

.. GENERATED FROM PYTHON SOURCE LINES 45-49

.. code-block:: default

    splits = ds.split([[0, 1, 5], [2, 3, 4], [6, 7, 8, 9, 10, 11]])
    display(splits)
    display(splits["2"].description)





.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    {'0': <braindecode.datasets.base.BaseConcatDataset object at 0x7fd78eaa8070>, '1': <braindecode.datasets.base.BaseConcatDataset object at 0x7fd78c206fa0>, '2': <braindecode.datasets.base.BaseConcatDataset object at 0x7fd78c206df0>}
       subject    session    run
    0        1  session_E  run_0
    1        1  session_E  run_1
    2        1  session_E  run_2
    3        1  session_E  run_3
    4        1  session_E  run_4
    5        1  session_E  run_5




.. GENERATED FROM PYTHON SOURCE LINES 50-51

Similarly, we can split datasets after creating windows

.. GENERATED FROM PYTHON SOURCE LINES 51-59

.. code-block:: default

    windows = create_windows_from_events(
        ds, trial_start_offset_samples=0, trial_stop_offset_samples=0)
    splits = windows.split("run")
    display(splits)
    splits = windows.split([4, 8])
    display(splits)
    splits = windows.split([[4, 8], [5, 9, 11]])
    display(splits)




.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    Used Annotations descriptions: ['feet', 'left_hand', 'right_hand', 'tongue']
    Adding metadata with 4 columns
    Replacing existing metadata with 4 columns
    48 matching events found
    No baseline correction applied
    0 projection items activated
    Loading data for 48 events and 1000 original time points ...
    0 bad epochs dropped
    Used Annotations descriptions: ['feet', 'left_hand', 'right_hand', 'tongue']
    Adding metadata with 4 columns
    Replacing existing metadata with 4 columns
    48 matching events found
    No baseline correction applied
    0 projection items activated
    Loading data for 48 events and 1000 original time points ...
    0 bad epochs dropped
    Used Annotations descriptions: ['feet', 'left_hand', 'right_hand', 'tongue']
    Adding metadata with 4 columns
    Replacing existing metadata with 4 columns
    48 matching events found
    No baseline correction applied
    0 projection items activated
    Loading data for 48 events and 1000 original time points ...
    0 bad epochs dropped
    Used Annotations descriptions: ['feet', 'left_hand', 'right_hand', 'tongue']
    Adding metadata with 4 columns
    Replacing existing metadata with 4 columns
    48 matching events found
    No baseline correction applied
    0 projection items activated
    Loading data for 48 events and 1000 original time points ...
    0 bad epochs dropped
    Used Annotations descriptions: ['feet', 'left_hand', 'right_hand', 'tongue']
    Adding metadata with 4 columns
    Replacing existing metadata with 4 columns
    48 matching events found
    No baseline correction applied
    0 projection items activated
    Loading data for 48 events and 1000 original time points ...
    0 bad epochs dropped
    Used Annotations descriptions: ['feet', 'left_hand', 'right_hand', 'tongue']
    Adding metadata with 4 columns
    Replacing existing metadata with 4 columns
    48 matching events found
    No baseline correction applied
    0 projection items activated
    Loading data for 48 events and 1000 original time points ...
    0 bad epochs dropped
    Used Annotations descriptions: ['feet', 'left_hand', 'right_hand', 'tongue']
    Adding metadata with 4 columns
    Replacing existing metadata with 4 columns
    48 matching events found
    No baseline correction applied
    0 projection items activated
    Loading data for 48 events and 1000 original time points ...
    0 bad epochs dropped
    Used Annotations descriptions: ['feet', 'left_hand', 'right_hand', 'tongue']
    Adding metadata with 4 columns
    Replacing existing metadata with 4 columns
    48 matching events found
    No baseline correction applied
    0 projection items activated
    Loading data for 48 events and 1000 original time points ...
    0 bad epochs dropped
    Used Annotations descriptions: ['feet', 'left_hand', 'right_hand', 'tongue']
    Adding metadata with 4 columns
    Replacing existing metadata with 4 columns
    48 matching events found
    No baseline correction applied
    0 projection items activated
    Loading data for 48 events and 1000 original time points ...
    0 bad epochs dropped
    Used Annotations descriptions: ['feet', 'left_hand', 'right_hand', 'tongue']
    Adding metadata with 4 columns
    Replacing existing metadata with 4 columns
    48 matching events found
    No baseline correction applied
    0 projection items activated
    Loading data for 48 events and 1000 original time points ...
    0 bad epochs dropped
    Used Annotations descriptions: ['feet', 'left_hand', 'right_hand', 'tongue']
    Adding metadata with 4 columns
    Replacing existing metadata with 4 columns
    48 matching events found
    No baseline correction applied
    0 projection items activated
    Loading data for 48 events and 1000 original time points ...
    0 bad epochs dropped
    Used Annotations descriptions: ['feet', 'left_hand', 'right_hand', 'tongue']
    Adding metadata with 4 columns
    Replacing existing metadata with 4 columns
    48 matching events found
    No baseline correction applied
    0 projection items activated
    Loading data for 48 events and 1000 original time points ...
    0 bad epochs dropped
    {'run_0': <braindecode.datasets.base.BaseConcatDataset object at 0x7fd78ea9ae80>, 'run_1': <braindecode.datasets.base.BaseConcatDataset object at 0x7fd78eaa82e0>, 'run_2': <braindecode.datasets.base.BaseConcatDataset object at 0x7fd78c1be160>, 'run_3': <braindecode.datasets.base.BaseConcatDataset object at 0x7fd78c1be4c0>, 'run_4': <braindecode.datasets.base.BaseConcatDataset object at 0x7fd78d2bcbb0>, 'run_5': <braindecode.datasets.base.BaseConcatDataset object at 0x7fd78d2ce850>}
    {'0': <braindecode.datasets.base.BaseConcatDataset object at 0x7fd78c1cc250>}
    {'0': <braindecode.datasets.base.BaseConcatDataset object at 0x7fd78d2cefa0>, '1': <braindecode.datasets.base.BaseConcatDataset object at 0x7fd78d2ceeb0>}





.. rst-class:: sphx-glr-timing

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


.. _sphx_glr_download_auto_examples_plot_split_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: plot_split_dataset.py <plot_split_dataset.py>`



  .. container:: sphx-glr-download sphx-glr-download-jupyter

     :download:`Download Jupyter notebook: plot_split_dataset.ipynb <plot_split_dataset.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
