.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_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_dataset_example.py>` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_dataset_example.py: MOABB Dataset Example ======================== In this example, we show how to fetch and prepare a MOABB dataset for usage with Braindecode. .. GENERATED FROM PYTHON SOURCE LINES 7-21 .. code-block:: default # Authors: Lukas Gemein <l.gemein@gmail.com> # Hubert Banville <hubert.jbanville@gmail.com> # Simon Brandt <simonbrandt@protonmail.com> # # License: BSD (3-clause) import matplotlib.pyplot as plt from braindecode.datasets import MOABBDataset from braindecode.preprocessing import \ create_windows_from_events, create_fixed_length_windows from braindecode.preprocessing import preprocess, Preprocessor .. GENERATED FROM PYTHON SOURCE LINES 22-23 First, we create a dataset based on BCIC IV 2a fetched with MOABB, .. GENERATED FROM PYTHON SOURCE LINES 23-25 .. code-block:: default dataset = MOABBDataset(dataset_name="BNCI2014001", subject_ids=[1]) .. GENERATED FROM PYTHON SOURCE LINES 26-27 ds has a pandas DataFrame with additional description of its internal datasets .. GENERATED FROM PYTHON SOURCE LINES 27-29 .. 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 30-33 We can iterate through ds which yields one time point of a continuous signal x, and a target y (which can be None if targets are not defined for the entire continuous signal). .. GENERATED FROM PYTHON SOURCE LINES 33-37 .. code-block:: default for x, y in dataset: print(x.shape, y) break .. rst-class:: sphx-glr-script-out Out: .. code-block:: none (26, 1) None .. GENERATED FROM PYTHON SOURCE LINES 38-40 We can apply preprocessing transforms that are defined in mne and work in-place, such as resampling, bandpass filtering, or electrode selection. .. GENERATED FROM PYTHON SOURCE LINES 40-48 .. code-block:: default preprocessors = [ Preprocessor('pick_types', eeg=True, meg=False, stim=True), Preprocessor('resample', sfreq=100) ] print(dataset.datasets[0].raw.info["sfreq"]) preprocess(dataset, preprocessors) print(dataset.datasets[0].raw.info["sfreq"]) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 250.0 100.0 .. GENERATED FROM PYTHON SOURCE LINES 49-51 We can easily split ds based on a criteria applied to the description DataFrame: .. GENERATED FROM PYTHON SOURCE LINES 51-54 .. code-block:: default subsets = dataset.split("session") print({subset_name: len(subset) for subset_name, subset in subsets.items()}) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none {'session_E': 232164, 'session_T': 232164} .. GENERATED FROM PYTHON SOURCE LINES 55-56 Next, we use a windower to extract events from the dataset based on events: .. GENERATED FROM PYTHON SOURCE LINES 56-61 .. code-block:: default windows_dataset = create_windows_from_events( dataset, trial_start_offset_samples=0, trial_stop_offset_samples=100, window_size_samples=400, window_stride_samples=100, drop_last_window=False) .. GENERATED FROM PYTHON SOURCE LINES 62-66 We can iterate through the windows_ds which yields a window x, a target y, and window_ind (which itself contains ``i_window_in_trial``, ``i_start_in_trial``, and ``i_stop_in_trial``, which are required for combining window predictions in the scorer). .. GENERATED FROM PYTHON SOURCE LINES 66-70 .. code-block:: default for x, y, window_ind in windows_dataset: print(x.shape, y, window_ind) break .. rst-class:: sphx-glr-script-out Out: .. code-block:: none (23, 400) 3 [0, 300, 700] .. GENERATED FROM PYTHON SOURCE LINES 71-72 We visually inspect the windows: .. GENERATED FROM PYTHON SOURCE LINES 72-84 .. code-block:: default max_i = 2 fig, ax_arr = plt.subplots(1, max_i + 1, figsize=(3.5 * (max_i + 1), 3.5), sharex=True, sharey=True) for i, (x, y, window_ind) in enumerate(windows_dataset): ax_arr[i].plot(x.T) ax_arr[i].set_ylim(-4e-5, 4e-5) ax_arr[i].set_title(f"label={y}") if i == max_i: break fig.tight_layout() .. image-sg:: /auto_examples/images/sphx_glr_plot_dataset_example_001.png :alt: label=3, label=3, label=0 :srcset: /auto_examples/images/sphx_glr_plot_dataset_example_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 85-87 Alternatively, we can create evenly spaced ("sliding") windows using a different windower. .. GENERATED FROM PYTHON SOURCE LINES 87-99 .. code-block:: default sliding_windows_dataset = create_fixed_length_windows( dataset, start_offset_samples=0, stop_offset_samples=0, window_size_samples=1200, window_stride_samples=1000, drop_last_window=False) print(len(sliding_windows_dataset)) for x, y, window_ind in sliding_windows_dataset: print(x.shape, y, window_ind) break sliding_windows_dataset.description .. rst-class:: sphx-glr-script-out Out: .. code-block:: none /home/runner/work/braindecode/braindecode/braindecode/preprocessing/windowers.py:597: UserWarning: Meaning of `trial_stop_offset_samples`=0 has changed, use `None` to indicate end of trial/recording. Using `None`. 'Meaning of `trial_stop_offset_samples`=0 has changed, use `None` ' 468 (23, 1200) -1 [0, 0, 1200] .. 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 100-102 Transforms can also be applied on windows in the same way as shown above on continuous data: .. GENERATED FROM PYTHON SOURCE LINES 102-136 .. code-block:: default def crop_windows(windows, start_offset_samples, stop_offset_samples): fs = windows.info["sfreq"] windows.crop(tmin=start_offset_samples / fs, tmax=stop_offset_samples / fs, include_tmax=False) epochs_preprocessors = [ Preprocessor('pick_types', eeg=True, meg=False, stim=False), Preprocessor(crop_windows, apply_on_array=False, start_offset_samples=100, stop_offset_samples=900) ] print(windows_dataset.datasets[0].windows.info["ch_names"], len(windows_dataset.datasets[0].windows.times)) preprocess(windows_dataset, epochs_preprocessors) print(windows_dataset.datasets[0].windows.info["ch_names"], len(windows_dataset.datasets[0].windows.times)) max_i = 2 fig, ax_arr = plt.subplots(1, max_i + 1, figsize=(3.5 * (max_i + 1), 3.5), sharex=True, sharey=True) for i, (x, y, window_ind) in enumerate(windows_dataset): ax_arr[i].plot(x.T) ax_arr[i].set_ylim(-4e-5, 4e-5) ax_arr[i].set_title(f"label={y}") if i == max_i: break fig.tight_layout() plt.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_dataset_example_002.png :alt: label=3, label=3, label=0 :srcset: /auto_examples/images/sphx_glr_plot_dataset_example_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none ['Fz', 'FC3', 'FC1', 'FCz', 'FC2', 'FC4', 'C5', 'C3', 'C1', 'Cz', 'C2', 'C4', 'C6', 'CP3', 'CP1', 'CPz', 'CP2', 'CP4', 'P1', 'Pz', 'P2', 'POz', 'stim'] 400 ['Fz', 'FC3', 'FC1', 'FCz', 'FC2', 'FC4', 'C5', 'C3', 'C1', 'Cz', 'C2', 'C4', 'C6', 'CP3', 'CP1', 'CPz', 'CP2', 'CP4', 'P1', 'Pz', 'P2', 'POz'] 300 .. GENERATED FROM PYTHON SOURCE LINES 137-139 Again, we can easily split windows_ds based on some criteria in the description DataFrame: .. GENERATED FROM PYTHON SOURCE LINES 139-141 .. code-block:: default subsets = windows_dataset.split("session") print({subset_name: len(subset) for subset_name, subset in subsets.items()}) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none {'session_E': 576, 'session_T': 576} .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 8.463 seconds) **Estimated memory usage:** 371 MB .. _sphx_glr_download_auto_examples_plot_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_dataset_example.py <plot_dataset_example.py>` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_dataset_example.ipynb <plot_dataset_example.ipynb>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_