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

.. only:: html

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

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

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

.. _sphx_glr_auto_examples_plot_custom_dataset_example.py:


Custom Dataset Example
======================

This example shows how to convert data X and y as numpy arrays to a braindecode
compatible data format.

.. GENERATED FROM PYTHON SOURCE LINES 8-17

.. code-block:: default


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

    import mne

    from braindecode.datasets import create_from_X_y








.. GENERATED FROM PYTHON SOURCE LINES 18-19

To set up the example, we first fetch some data using mne:

.. GENERATED FROM PYTHON SOURCE LINES 19-34

.. code-block:: default


    # 5, 6, 7, 10, 13, 14 are codes for executed and imagined hands/feet
    subject_id = 22
    event_codes = [5, 6, 9, 10, 13, 14]
    # event_codes = [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

    # This will download the files if you don't have them yet,
    # and then return the paths to the files.
    physionet_paths = mne.datasets.eegbci.load_data(
        subject_id, event_codes, update_path=False)

    # Load each of the files
    parts = [mne.io.read_raw_edf(path, preload=True, stim_channel='auto')
             for path in physionet_paths]





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

 Out:

 .. code-block:: none

    Extracting EDF parameters from /home/runner/mne_data/MNE-eegbci-data/files/eegmmidb/1.0.0/S022/S022R05.edf...
    EDF file detected
    Setting channel info structure...
    Creating raw.info structure...
    Reading 0 ... 19999  =      0.000 ...   124.994 secs...
    Extracting EDF parameters from /home/runner/mne_data/MNE-eegbci-data/files/eegmmidb/1.0.0/S022/S022R06.edf...
    EDF file detected
    Setting channel info structure...
    Creating raw.info structure...
    Reading 0 ... 19999  =      0.000 ...   124.994 secs...
    Extracting EDF parameters from /home/runner/mne_data/MNE-eegbci-data/files/eegmmidb/1.0.0/S022/S022R09.edf...
    EDF file detected
    Setting channel info structure...
    Creating raw.info structure...
    Reading 0 ... 19999  =      0.000 ...   124.994 secs...
    Extracting EDF parameters from /home/runner/mne_data/MNE-eegbci-data/files/eegmmidb/1.0.0/S022/S022R10.edf...
    EDF file detected
    Setting channel info structure...
    Creating raw.info structure...
    Reading 0 ... 19999  =      0.000 ...   124.994 secs...
    Extracting EDF parameters from /home/runner/mne_data/MNE-eegbci-data/files/eegmmidb/1.0.0/S022/S022R13.edf...
    EDF file detected
    Setting channel info structure...
    Creating raw.info structure...
    Reading 0 ... 19999  =      0.000 ...   124.994 secs...
    Extracting EDF parameters from /home/runner/mne_data/MNE-eegbci-data/files/eegmmidb/1.0.0/S022/S022R14.edf...
    EDF file detected
    Setting channel info structure...
    Creating raw.info structure...
    Reading 0 ... 19999  =      0.000 ...   124.994 secs...




.. GENERATED FROM PYTHON SOURCE LINES 35-38

We take the required data, targets and additional information sampling
frequency and channel names from the loaded data. Note that this data and
information can originate from any source.

.. GENERATED FROM PYTHON SOURCE LINES 38-43

.. code-block:: default

    X = [raw.get_data() for raw in parts]
    y = event_codes
    sfreq = parts[0].info["sfreq"]
    ch_names = parts[0].info["ch_names"]








.. GENERATED FROM PYTHON SOURCE LINES 44-45

Convert to data format compatible with skorch and braindecode:

.. GENERATED FROM PYTHON SOURCE LINES 45-53

.. code-block:: default

    windows_dataset = create_from_X_y(
        X, y, drop_last_window=False, sfreq=sfreq, ch_names=ch_names,
        window_stride_samples=500,
        window_size_samples=500,
    )

    windows_dataset.description  # look as dataset description





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

 Out:

 .. code-block:: none

    Creating RawArray with float64 data, n_channels=64, n_times=20000
        Range : 0 ... 19999 =      0.000 ...   124.994 secs
    Ready.
    Creating RawArray with float64 data, n_channels=64, n_times=20000
        Range : 0 ... 19999 =      0.000 ...   124.994 secs
    Ready.
    Creating RawArray with float64 data, n_channels=64, n_times=20000
        Range : 0 ... 19999 =      0.000 ...   124.994 secs
    Ready.
    Creating RawArray with float64 data, n_channels=64, n_times=20000
        Range : 0 ... 19999 =      0.000 ...   124.994 secs
    Ready.
    Creating RawArray with float64 data, n_channels=64, n_times=20000
        Range : 0 ... 19999 =      0.000 ...   124.994 secs
    Ready.
    Creating RawArray with float64 data, n_channels=64, n_times=20000
        Range : 0 ... 19999 =      0.000 ...   124.994 secs
    Ready.
    Adding metadata with 4 columns
    Replacing existing metadata with 4 columns
    40 matching events found
    No baseline correction applied
    0 projection items activated
    Loading data for 40 events and 500 original time points ...
    0 bad epochs dropped
    Adding metadata with 4 columns
    Replacing existing metadata with 4 columns
    40 matching events found
    No baseline correction applied
    0 projection items activated
    Loading data for 40 events and 500 original time points ...
    0 bad epochs dropped
    Adding metadata with 4 columns
    Replacing existing metadata with 4 columns
    40 matching events found
    No baseline correction applied
    0 projection items activated
    Loading data for 40 events and 500 original time points ...
    0 bad epochs dropped
    Adding metadata with 4 columns
    Replacing existing metadata with 4 columns
    40 matching events found
    No baseline correction applied
    0 projection items activated
    Loading data for 40 events and 500 original time points ...
    0 bad epochs dropped
    Adding metadata with 4 columns
    Replacing existing metadata with 4 columns
    40 matching events found
    No baseline correction applied
    0 projection items activated
    Loading data for 40 events and 500 original time points ...
    0 bad epochs dropped
    Adding metadata with 4 columns
    Replacing existing metadata with 4 columns
    40 matching events found
    No baseline correction applied
    0 projection items activated
    Loading data for 40 events and 500 original time points ...
    0 bad epochs dropped


.. 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>target</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <th>0</th>
          <td>5</td>
        </tr>
        <tr>
          <th>1</th>
          <td>6</td>
        </tr>
        <tr>
          <th>2</th>
          <td>9</td>
        </tr>
        <tr>
          <th>3</th>
          <td>10</td>
        </tr>
        <tr>
          <th>4</th>
          <td>13</td>
        </tr>
        <tr>
          <th>5</th>
          <td>14</td>
        </tr>
      </tbody>
    </table>
    </div>
    </div>
    <br />
    <br />

.. GENERATED FROM PYTHON SOURCE LINES 54-55

You can manipulate the dataset

.. GENERATED FROM PYTHON SOURCE LINES 55-57

.. code-block:: default

    print(len(windows_dataset))  # get the number of samples





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

 Out:

 .. code-block:: none

    240




.. GENERATED FROM PYTHON SOURCE LINES 58-59

You can now index the data

.. GENERATED FROM PYTHON SOURCE LINES 59-65

.. code-block:: default

    i = 0
    x_i, y_i, window_ind = windows_dataset[0]
    n_channels, n_times = x_i.shape  # the EEG data
    _, start_ind, stop_ind = window_ind
    print(f"n_channels={n_channels}  -- n_times={n_times} -- y_i={y_i}")
    print(f"start_ind={start_ind} -- stop_ind={stop_ind}")




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

 Out:

 .. code-block:: none

    Loading data for 1 events and 500 original time points ...
    n_channels=64  -- n_times=500 -- y_i=5
    start_ind=0 -- stop_ind=500





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

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

**Estimated memory usage:**  96 MB


.. _sphx_glr_download_auto_examples_plot_custom_dataset_example.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_custom_dataset_example.py <plot_custom_dataset_example.py>`



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

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


.. only:: html

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

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