MNE Dataset ExampleΒΆ

This example shows how to convert data from mne.Raws or mne.Epochs to a braindecode compatible data format.

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

import mne

from braindecode.datautil import (
    create_from_mne_raw, create_from_mne_epochs)

First, fetch some data using mne:

# 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]

Out:

Using default location ~/mne_data for EEGBCI...
Using default location ~/mne_data for EEGBCI...
Using default location ~/mne_data for EEGBCI...
Using default location ~/mne_data for EEGBCI...
Using default location ~/mne_data for EEGBCI...
Using default location ~/mne_data for EEGBCI...
Extracting EDF parameters from /home/circleci/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/circleci/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/circleci/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/circleci/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/circleci/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/circleci/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...

Convert mne.RawArrays to a compatible data format:

descriptions = [{"event_code": code, "subject": subject_id}
                for code in event_codes]
windows_datasets = create_from_mne_raw(
    parts,
    trial_start_offset_samples=0,
    trial_stop_offset_samples=0,
    window_size_samples=500,
    window_stride_samples=500,
    drop_last_window=False,
    descriptions=descriptions,
)

Out:

Used Annotations descriptions: ['T0', 'T1', 'T2']
60 matching events found
No baseline correction applied
Adding metadata with 4 columns
0 projection items activated
Loading data for 60 events and 500 original time points ...
0 bad epochs dropped
Used Annotations descriptions: ['T0', 'T1', 'T2']
60 matching events found
No baseline correction applied
Adding metadata with 4 columns
0 projection items activated
Loading data for 60 events and 500 original time points ...
0 bad epochs dropped
Used Annotations descriptions: ['T0', 'T1', 'T2']
60 matching events found
No baseline correction applied
Adding metadata with 4 columns
0 projection items activated
Loading data for 60 events and 500 original time points ...
0 bad epochs dropped
Used Annotations descriptions: ['T0', 'T1', 'T2']
60 matching events found
No baseline correction applied
Adding metadata with 4 columns
0 projection items activated
Loading data for 60 events and 500 original time points ...
0 bad epochs dropped
Used Annotations descriptions: ['T0', 'T1', 'T2']
60 matching events found
No baseline correction applied
Adding metadata with 4 columns
0 projection items activated
Loading data for 60 events and 500 original time points ...
0 bad epochs dropped
Used Annotations descriptions: ['T0', 'T1', 'T2']
60 matching events found
No baseline correction applied
Adding metadata with 4 columns
0 projection items activated
Loading data for 60 events and 500 original time points ...
0 bad epochs dropped

If trials were already cut beforehand and are available as mne.Epochs:

list_of_epochs = [mne.Epochs(raw, [[0, 0, 0]], tmin=0, baseline=None)
                  for raw in parts]
windows_datasets = create_from_mne_epochs(
    list_of_epochs,
    window_size_samples=50,
    window_stride_samples=50,
    drop_last_window=False
)

Out:

1 matching events found
No baseline correction applied
Not setting metadata
0 projection items activated
1 matching events found
No baseline correction applied
Not setting metadata
0 projection items activated
1 matching events found
No baseline correction applied
Not setting metadata
0 projection items activated
1 matching events found
No baseline correction applied
Not setting metadata
0 projection items activated
1 matching events found
No baseline correction applied
Not setting metadata
0 projection items activated
1 matching events found
No baseline correction applied
Not setting metadata
0 projection items activated
Creating RawArray with float64 data, n_channels=64, n_times=81
    Range : 0 ... 80 =      0.000 ...     0.500 secs
Ready.
2 matching events found
No baseline correction applied
Adding metadata with 4 columns
0 projection items activated
Loading data for 2 events and 50 original time points ...
0 bad epochs dropped
Creating RawArray with float64 data, n_channels=64, n_times=81
    Range : 0 ... 80 =      0.000 ...     0.500 secs
Ready.
2 matching events found
No baseline correction applied
Adding metadata with 4 columns
0 projection items activated
Loading data for 2 events and 50 original time points ...
0 bad epochs dropped
Creating RawArray with float64 data, n_channels=64, n_times=81
    Range : 0 ... 80 =      0.000 ...     0.500 secs
Ready.
2 matching events found
No baseline correction applied
Adding metadata with 4 columns
0 projection items activated
Loading data for 2 events and 50 original time points ...
0 bad epochs dropped
Creating RawArray with float64 data, n_channels=64, n_times=81
    Range : 0 ... 80 =      0.000 ...     0.500 secs
Ready.
2 matching events found
No baseline correction applied
Adding metadata with 4 columns
0 projection items activated
Loading data for 2 events and 50 original time points ...
0 bad epochs dropped
Creating RawArray with float64 data, n_channels=64, n_times=81
    Range : 0 ... 80 =      0.000 ...     0.500 secs
Ready.
2 matching events found
No baseline correction applied
Adding metadata with 4 columns
0 projection items activated
Loading data for 2 events and 50 original time points ...
0 bad epochs dropped
Creating RawArray with float64 data, n_channels=64, n_times=81
    Range : 0 ... 80 =      0.000 ...     0.500 secs
Ready.
2 matching events found
No baseline correction applied
Adding metadata with 4 columns
0 projection items activated
Loading data for 2 events and 50 original time points ...
0 bad epochs dropped

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

Estimated memory usage: 68 MB

Gallery generated by Sphinx-Gallery