.. 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 7-15

.. code-block:: default


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

    from braindecode.datasets import MOABBDataset
    from braindecode.preprocessing import create_windows_from_events








.. GENERATED FROM PYTHON SOURCE LINES 16-17

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

.. GENERATED FROM PYTHON SOURCE LINES 17-19

.. code-block:: default

    dataset = 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 20-21

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

.. GENERATED FROM PYTHON SOURCE LINES 21-23

.. code-block:: default

    dataset.description






.. raw:: html

    <div class="output_subarea output_html rendered_html output_result">
    <div>
    <style scoped>
        .dataframe tbody tr th:only-of-type {
            vertical-align: middle;
        }

        .dataframe tbody tr th {
            vertical-align: top;
        }

        .dataframe thead th {
            text-align: right;
        }
    </style>
    <table border="1" class="dataframe">
      <thead>
        <tr style="text-align: right;">
          <th></th>
          <th>subject</th>
          <th>session</th>
          <th>run</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <th>0</th>
          <td>1</td>
          <td>session_T</td>
          <td>run_0</td>
        </tr>
        <tr>
          <th>1</th>
          <td>1</td>
          <td>session_T</td>
          <td>run_1</td>
        </tr>
        <tr>
          <th>2</th>
          <td>1</td>
          <td>session_T</td>
          <td>run_2</td>
        </tr>
        <tr>
          <th>3</th>
          <td>1</td>
          <td>session_T</td>
          <td>run_3</td>
        </tr>
        <tr>
          <th>4</th>
          <td>1</td>
          <td>session_T</td>
          <td>run_4</td>
        </tr>
        <tr>
          <th>5</th>
          <td>1</td>
          <td>session_T</td>
          <td>run_5</td>
        </tr>
        <tr>
          <th>6</th>
          <td>1</td>
          <td>session_E</td>
          <td>run_0</td>
        </tr>
        <tr>
          <th>7</th>
          <td>1</td>
          <td>session_E</td>
          <td>run_1</td>
        </tr>
        <tr>
          <th>8</th>
          <td>1</td>
          <td>session_E</td>
          <td>run_2</td>
        </tr>
        <tr>
          <th>9</th>
          <td>1</td>
          <td>session_E</td>
          <td>run_3</td>
        </tr>
        <tr>
          <th>10</th>
          <td>1</td>
          <td>session_E</td>
          <td>run_4</td>
        </tr>
        <tr>
          <th>11</th>
          <td>1</td>
          <td>session_E</td>
          <td>run_5</td>
        </tr>
      </tbody>
    </table>
    </div>
    </div>
    <br />
    <br />

.. GENERATED FROM PYTHON SOURCE LINES 24-27

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 27-31

.. code-block:: default

    splits = dataset.split("run")
    print(splits)
    splits["run_4"].description





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

 Out:

 .. code-block:: none

    {'run_0': <braindecode.datasets.base.BaseConcatDataset object at 0x7f7490b1f210>, 'run_1': <braindecode.datasets.base.BaseConcatDataset object at 0x7f7490e93810>, 'run_2': <braindecode.datasets.base.BaseConcatDataset object at 0x7f7490b21890>, 'run_3': <braindecode.datasets.base.BaseConcatDataset object at 0x7f7490b25410>, 'run_4': <braindecode.datasets.base.BaseConcatDataset object at 0x7f7490bc2950>, 'run_5': <braindecode.datasets.base.BaseConcatDataset object at 0x7f748c1a5f50>}


.. raw:: html

    <div class="output_subarea output_html rendered_html output_result">
    <div>
    <style scoped>
        .dataframe tbody tr th:only-of-type {
            vertical-align: middle;
        }

        .dataframe tbody tr th {
            vertical-align: top;
        }

        .dataframe thead th {
            text-align: right;
        }
    </style>
    <table border="1" class="dataframe">
      <thead>
        <tr style="text-align: right;">
          <th></th>
          <th>subject</th>
          <th>session</th>
          <th>run</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <th>0</th>
          <td>1</td>
          <td>session_T</td>
          <td>run_4</td>
        </tr>
        <tr>
          <th>1</th>
          <td>1</td>
          <td>session_E</td>
          <td>run_4</td>
        </tr>
      </tbody>
    </table>
    </div>
    </div>
    <br />
    <br />

.. GENERATED FROM PYTHON SOURCE LINES 32-35

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 35-39

.. code-block:: default

    splits = dataset.split([0, 1, 5])
    print(splits)
    splits["0"].description





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

 Out:

 .. code-block:: none

    {'0': <braindecode.datasets.base.BaseConcatDataset object at 0x7f748c6bbb90>}


.. raw:: html

    <div class="output_subarea output_html rendered_html output_result">
    <div>
    <style scoped>
        .dataframe tbody tr th:only-of-type {
            vertical-align: middle;
        }

        .dataframe tbody tr th {
            vertical-align: top;
        }

        .dataframe thead th {
            text-align: right;
        }
    </style>
    <table border="1" class="dataframe">
      <thead>
        <tr style="text-align: right;">
          <th></th>
          <th>subject</th>
          <th>session</th>
          <th>run</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <th>0</th>
          <td>1</td>
          <td>session_T</td>
          <td>run_0</td>
        </tr>
        <tr>
          <th>1</th>
          <td>1</td>
          <td>session_T</td>
          <td>run_1</td>
        </tr>
        <tr>
          <th>2</th>
          <td>1</td>
          <td>session_T</td>
          <td>run_5</td>
        </tr>
      </tbody>
    </table>
    </div>
    </div>
    <br />
    <br />

.. GENERATED FROM PYTHON SOURCE LINES 40-44

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 44-48

.. code-block:: default

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





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

 Out:

 .. code-block:: none

    {'0': <braindecode.datasets.base.BaseConcatDataset object at 0x7f7490b25410>, '1': <braindecode.datasets.base.BaseConcatDataset object at 0x7f7490b49e90>, '2': <braindecode.datasets.base.BaseConcatDataset object at 0x7f748ee50590>}


.. raw:: html

    <div class="output_subarea output_html rendered_html output_result">
    <div>
    <style scoped>
        .dataframe tbody tr th:only-of-type {
            vertical-align: middle;
        }

        .dataframe tbody tr th {
            vertical-align: top;
        }

        .dataframe thead th {
            text-align: right;
        }
    </style>
    <table border="1" class="dataframe">
      <thead>
        <tr style="text-align: right;">
          <th></th>
          <th>subject</th>
          <th>session</th>
          <th>run</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <th>0</th>
          <td>1</td>
          <td>session_E</td>
          <td>run_0</td>
        </tr>
        <tr>
          <th>1</th>
          <td>1</td>
          <td>session_E</td>
          <td>run_1</td>
        </tr>
        <tr>
          <th>2</th>
          <td>1</td>
          <td>session_E</td>
          <td>run_2</td>
        </tr>
        <tr>
          <th>3</th>
          <td>1</td>
          <td>session_E</td>
          <td>run_3</td>
        </tr>
        <tr>
          <th>4</th>
          <td>1</td>
          <td>session_E</td>
          <td>run_4</td>
        </tr>
        <tr>
          <th>5</th>
          <td>1</td>
          <td>session_E</td>
          <td>run_5</td>
        </tr>
      </tbody>
    </table>
    </div>
    </div>
    <br />
    <br />

.. GENERATED FROM PYTHON SOURCE LINES 49-51

If we want to split based on a list of indices but you want to specify
the keys in the output dictionary you can pass a dict as:

.. GENERATED FROM PYTHON SOURCE LINES 51-57

.. code-block:: default

    splits = dataset.split(
        {"train": [0, 1, 5], "valid": [2, 3, 4], "test": [6, 7, 8, 9, 10, 11]}
    )
    print(splits)
    splits["test"].description





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

 Out:

 .. code-block:: none

    {'train': <braindecode.datasets.base.BaseConcatDataset object at 0x7f748c1a5f50>, 'valid': <braindecode.datasets.base.BaseConcatDataset object at 0x7f748ee506d0>, 'test': <braindecode.datasets.base.BaseConcatDataset object at 0x7f748ee50450>}


.. raw:: html

    <div class="output_subarea output_html rendered_html output_result">
    <div>
    <style scoped>
        .dataframe tbody tr th:only-of-type {
            vertical-align: middle;
        }

        .dataframe tbody tr th {
            vertical-align: top;
        }

        .dataframe thead th {
            text-align: right;
        }
    </style>
    <table border="1" class="dataframe">
      <thead>
        <tr style="text-align: right;">
          <th></th>
          <th>subject</th>
          <th>session</th>
          <th>run</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <th>0</th>
          <td>1</td>
          <td>session_E</td>
          <td>run_0</td>
        </tr>
        <tr>
          <th>1</th>
          <td>1</td>
          <td>session_E</td>
          <td>run_1</td>
        </tr>
        <tr>
          <th>2</th>
          <td>1</td>
          <td>session_E</td>
          <td>run_2</td>
        </tr>
        <tr>
          <th>3</th>
          <td>1</td>
          <td>session_E</td>
          <td>run_3</td>
        </tr>
        <tr>
          <th>4</th>
          <td>1</td>
          <td>session_E</td>
          <td>run_4</td>
        </tr>
        <tr>
          <th>5</th>
          <td>1</td>
          <td>session_E</td>
          <td>run_5</td>
        </tr>
      </tbody>
    </table>
    </div>
    </div>
    <br />
    <br />

.. GENERATED FROM PYTHON SOURCE LINES 58-59

Similarly, we can split datasets after creating windows

.. GENERATED FROM PYTHON SOURCE LINES 59-64

.. code-block:: default

    windows = create_windows_from_events(
        dataset, trial_start_offset_samples=0, trial_stop_offset_samples=0)
    splits = windows.split("run")
    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 0x7f7490b8f110>, 'run_1': <braindecode.datasets.base.BaseConcatDataset object at 0x7f748c19ef10>, 'run_2': <braindecode.datasets.base.BaseConcatDataset object at 0x7f748ee22910>, 'run_3': <braindecode.datasets.base.BaseConcatDataset object at 0x7f748ee22350>, 'run_4': <braindecode.datasets.base.BaseConcatDataset object at 0x7f7490b853d0>, 'run_5': <braindecode.datasets.base.BaseConcatDataset object at 0x7f7490b40450>}



.. GENERATED FROM PYTHON SOURCE LINES 65-68

.. code-block:: default

    splits = windows.split([4, 8])
    splits





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

 Out:

 .. code-block:: none


    {'0': <braindecode.datasets.base.BaseConcatDataset object at 0x7f7490e80d90>}



.. GENERATED FROM PYTHON SOURCE LINES 69-72

.. code-block:: default

    splits = windows.split([[4, 8], [5, 9, 11]])
    splits





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

 Out:

 .. code-block:: none


    {'0': <braindecode.datasets.base.BaseConcatDataset object at 0x7f7490e93810>, '1': <braindecode.datasets.base.BaseConcatDataset object at 0x7f7490b1f210>}



.. GENERATED FROM PYTHON SOURCE LINES 73-75

.. code-block:: default

    splits = windows.split(dict(train=[4, 8], test=[5, 9, 11]))
    splits




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

 Out:

 .. code-block:: none


    {'train': <braindecode.datasets.base.BaseConcatDataset object at 0x7f748c19e6d0>, 'test': <braindecode.datasets.base.BaseConcatDataset object at 0x7f748c19eb10>}




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

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

**Estimated memory usage:**  354 MB


.. _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>`_