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

.. only:: html

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

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

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

.. _sphx_glr_auto_examples_plot_load_save_datasets.py:


Load and save dataset example
=============================

In this example, we show how to load and save braindecode datasets.

.. GENERATED FROM PYTHON SOURCE LINES 7-18

.. code-block:: default


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

    from braindecode.datasets.moabb import MOABBDataset
    from braindecode.datautil.preprocess import preprocess, MNEPreproc
    from braindecode.datautil.serialization import load_concat_dataset
    from braindecode.datautil.windowers import create_windows_from_events









.. GENERATED FROM PYTHON SOURCE LINES 19-20

First, we load some dataset using MOABB.

.. GENERATED FROM PYTHON SOURCE LINES 20-25

.. 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 26-28

We can apply preprocessing steps to the dataset. It is also possible to skip
this step and not apply any preprocessing.

.. GENERATED FROM PYTHON SOURCE LINES 28-33

.. code-block:: default

    preprocess(
        concat_ds=ds,
        preprocessors=[MNEPreproc(fn='resample', sfreq=10)]
    )





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

 Out:

 .. code-block:: none

    /home/robintibor/work/braindecode-test/braindecode/braindecode/braindecode/datautil/preprocess.py:87: UserWarning: MNEPreproc is deprecated. Use Preprocessor with `apply_on_array=False` instead.
      warn('MNEPreproc is deprecated. Use Preprocessor with '
    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]
    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 34-40

We save the dataset to a an existing directory. It will create a '.fif' file 
for every dataset in the concat dataset. Additionally it will create two 
JSON files, the first holding the description of the dataset, the second 
holding the name of the target. If you want to store to the same directory 
several times, for example due to trying different preprocessing, you can 
choose to overwrite the existing files.

.. GENERATED FROM PYTHON SOURCE LINES 40-45

.. code-block:: default

    ds.save(
        path='./',
        overwrite=False,
    )



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

.. code-block:: pytb

    Traceback (most recent call last):
      File "/home/robintibor/work/braindecode-test/braindecode/braindecode/examples/plot_load_save_datasets.py", line 40, in <module>
        ds.save(
      File "/home/robintibor/work/braindecode-test/braindecode/braindecode/braindecode/datasets/base.py", line 266, in save
        raise FileExistsError(
    FileExistsError: ./description.json or ./target_name.json exist in ./.




.. GENERATED FROM PYTHON SOURCE LINES 46-52

We load the saved dataset from a directory. Signals can be preloaded in 
compliance with mne. Optionally, only specific '.fif' files can be loaded 
by specifying their ids. The target name can be changed, if the dataset 
supports it (TUHAbnormal for example supports 'pathological', 'age', and 
'gender'. If you stored a preprocessed version with target 'pathological' 
it is possible to change the target upon loading).

.. GENERATED FROM PYTHON SOURCE LINES 52-59

.. code-block:: default

    ds_loaded = load_concat_dataset(
        path='./',
        preload=True,
        ids_to_load=[1,3],
        target_name=None,
    )


.. GENERATED FROM PYTHON SOURCE LINES 60-62

The serialization utility also supports WindowsDatasets, so we create 
compute windows next.

.. GENERATED FROM PYTHON SOURCE LINES 62-68

.. code-block:: default

    windows_ds = create_windows_from_events(
        concat_ds=ds_loaded,
        trial_start_offset_samples=0,
        trial_stop_offset_samples=0,
    )


.. GENERATED FROM PYTHON SOURCE LINES 69-75

Again, we save the dataset to an existing directory. It will create a 
'-epo.fif' file for every dataset in the concat dataset. Additionally it 
will create a JSON file holding the description of the dataset. If you 
want to store to the same directory several times, for example due to 
trying different windowing parameters, you can choose to overwrite the 
existing files.

.. GENERATED FROM PYTHON SOURCE LINES 75-80

.. code-block:: default

    windows_ds.save(
        path='./',
        overwrite=True,
    )


.. GENERATED FROM PYTHON SOURCE LINES 81-84

Load the saved dataset from a directory. Signals can be preloaded in 
 compliance with mne. Optionally, only specific '-epo.fif' files can be 
loaded by specifying their ids.

.. GENERATED FROM PYTHON SOURCE LINES 84-90

.. code-block:: default

    windows_ds_loaded = load_concat_dataset(
        path='./',
        preload=False,
        ids_to_load=[0],
        target_name=None,
    )


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

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


.. _sphx_glr_download_auto_examples_plot_load_save_datasets.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_load_save_datasets.py <plot_load_save_datasets.py>`



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

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


.. only:: html

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

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