Note
Go to the end to download the full example code.
Load and save dataset example#
In this example, we show how to load and save braindecode datasets.
# Authors: Lukas Gemein <l.gemein@gmail.com>
#
# License: BSD (3-clause)
import tempfile
from braindecode.datasets import MOABBDataset
from braindecode.preprocessing import preprocess, Preprocessor
from braindecode.datautil import load_concat_dataset
from braindecode.preprocessing import create_windows_from_events
First, we load some dataset using MOABB.
dataset = MOABBDataset(
dataset_name="BNCI2014001",
subject_ids=[1],
)
BNCI2014001 has been renamed to BNCI2014_001. BNCI2014001 will be removed in version 1.1.
The dataset class name 'BNCI2014001' must be an abbreviation of its code 'BNCI2014-001'. See moabb.datasets.base.is_abbrev for more information.
/home/runner/.local/lib/python3.10/site-packages/urllib3/connectionpool.py:1064: InsecureRequestWarning: Unverified HTTPS request is being made to host 'lampx.tugraz.at'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
warnings.warn(
0%| | 0.00/42.8M [00:00<?, ?B/s]
0%| | 8.19k/42.8M [00:00<08:48, 81.0kB/s]
0%| | 32.8k/42.8M [00:00<04:04, 175kB/s]
0%| | 96.3k/42.8M [00:00<01:53, 378kB/s]
0%|▏ | 209k/42.8M [00:00<01:04, 662kB/s]
1%|▍ | 432k/42.8M [00:00<00:35, 1.21MB/s]
2%|▊ | 889k/42.8M [00:00<00:18, 2.31MB/s]
4%|█▌ | 1.80M/42.8M [00:00<00:09, 4.45MB/s]
8%|███▏ | 3.62M/42.8M [00:00<00:04, 8.69MB/s]
16%|█████▊ | 6.74M/42.8M [00:00<00:02, 15.5MB/s]
23%|████████▍ | 9.75M/42.8M [00:01<00:01, 19.5MB/s]
30%|███████████ | 12.8M/42.8M [00:01<00:01, 22.5MB/s]
37%|█████████████▋ | 15.8M/42.8M [00:01<00:01, 24.5MB/s]
44%|████████████████▎ | 18.8M/42.8M [00:01<00:00, 26.1MB/s]
51%|██████████████████▉ | 22.0M/42.8M [00:01<00:00, 27.4MB/s]
58%|█████████████████████▌ | 24.9M/42.8M [00:01<00:00, 27.3MB/s]
65%|████████████████████████▏ | 28.0M/42.8M [00:01<00:00, 27.9MB/s]
72%|██████████████████████████▋ | 30.9M/42.8M [00:01<00:00, 28.2MB/s]
79%|█████████████████████████████▎ | 33.9M/42.8M [00:01<00:00, 28.5MB/s]
86%|███████████████████████████████▉ | 36.9M/42.8M [00:01<00:00, 28.4MB/s]
93%|██████████████████████████████████▌ | 39.9M/42.8M [00:02<00:00, 28.7MB/s]
42.8MB [00:02, 28.3MB/s]
0%| | 0.00/42.8M [00:00<?, ?B/s]
100%|██████████████████████████████████████| 42.8M/42.8M [00:00<00:00, 255GB/s]
/home/runner/.local/lib/python3.10/site-packages/urllib3/connectionpool.py:1064: InsecureRequestWarning: Unverified HTTPS request is being made to host 'lampx.tugraz.at'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
warnings.warn(
0%| | 0.00/43.8M [00:00<?, ?B/s]
0%| | 8.19k/43.8M [00:00<09:03, 80.5kB/s]
0%| | 32.8k/43.8M [00:00<04:11, 174kB/s]
0%| | 96.3k/43.8M [00:00<01:56, 376kB/s]
0%|▏ | 209k/43.8M [00:00<01:06, 659kB/s]
1%|▍ | 432k/43.8M [00:00<00:36, 1.20MB/s]
2%|▊ | 889k/43.8M [00:00<00:18, 2.30MB/s]
4%|█▌ | 1.80M/43.8M [00:00<00:09, 4.44MB/s]
8%|███ | 3.62M/43.8M [00:00<00:04, 8.66MB/s]
14%|█████▎ | 6.33M/43.8M [00:00<00:02, 14.1MB/s]
21%|███████▉ | 9.41M/43.8M [00:01<00:01, 19.0MB/s]
28%|██████████▍ | 12.3M/43.8M [00:01<00:01, 21.8MB/s]
35%|████████████▊ | 15.1M/43.8M [00:01<00:01, 23.4MB/s]
41%|███████████████▎ | 18.1M/43.8M [00:01<00:01, 25.0MB/s]
48%|█████████████████▊ | 21.0M/43.8M [00:01<00:00, 26.0MB/s]
55%|████████████████████▎ | 24.0M/43.8M [00:01<00:00, 26.9MB/s]
62%|██████████████████████▉ | 27.1M/43.8M [00:01<00:00, 27.9MB/s]
69%|█████████████████████████▍ | 30.0M/43.8M [00:01<00:00, 28.0MB/s]
76%|███████████████████████████▉ | 33.1M/43.8M [00:01<00:00, 28.6MB/s]
82%|██████████████████████████████▍ | 36.0M/43.8M [00:01<00:00, 27.7MB/s]
89%|█████████████████████████████████ | 39.0M/43.8M [00:02<00:00, 28.2MB/s]
96%|███████████████████████████████████▍ | 42.0M/43.8M [00:02<00:00, 28.3MB/s]
0%| | 0.00/43.8M [00:00<?, ?B/s]
100%|██████████████████████████████████████| 43.8M/43.8M [00:00<00:00, 239GB/s]
48 events found on stim channel stim
Event IDs: [1 2 3 4]
48 events found on stim channel stim
Event IDs: [1 2 3 4]
48 events found on stim channel stim
Event IDs: [1 2 3 4]
48 events found on stim channel stim
Event IDs: [1 2 3 4]
48 events found on stim channel stim
Event IDs: [1 2 3 4]
48 events found on stim channel stim
Event IDs: [1 2 3 4]
48 events found on stim channel stim
Event IDs: [1 2 3 4]
48 events found on stim channel stim
Event IDs: [1 2 3 4]
48 events found on stim channel stim
Event IDs: [1 2 3 4]
48 events found on stim channel stim
Event IDs: [1 2 3 4]
48 events found on stim channel stim
Event IDs: [1 2 3 4]
48 events found on stim channel stim
Event IDs: [1 2 3 4]
We can apply preprocessing steps to the dataset. It is also possible to skip this step and not apply any preprocessing.
preprocess(concat_ds=dataset, preprocessors=[Preprocessor(fn="resample", sfreq=10)])
48 events found on stim channel stim
Event IDs: [1 2 3 4]
48 events found on stim channel stim
Event IDs: [1 2 3 4]
48 events found on stim channel stim
Event IDs: [1 2 3 4]
48 events found on stim channel stim
Event IDs: [1 2 3 4]
48 events found on stim channel stim
Event IDs: [1 2 3 4]
48 events found on stim channel stim
Event IDs: [1 2 3 4]
48 events found on stim channel stim
Event IDs: [1 2 3 4]
48 events found on stim channel stim
Event IDs: [1 2 3 4]
48 events found on stim channel stim
Event IDs: [1 2 3 4]
48 events found on stim channel stim
Event IDs: [1 2 3 4]
48 events found on stim channel stim
Event IDs: [1 2 3 4]
48 events found on stim channel stim
Event IDs: [1 2 3 4]
48 events found on stim channel stim
Event IDs: [1 2 3 4]
48 events found on stim channel stim
Event IDs: [1 2 3 4]
48 events found on stim channel stim
Event IDs: [1 2 3 4]
48 events found on stim channel stim
Event IDs: [1 2 3 4]
48 events found on stim channel stim
Event IDs: [1 2 3 4]
48 events found on stim channel stim
Event IDs: [1 2 3 4]
48 events found on stim channel stim
Event IDs: [1 2 3 4]
48 events found on stim channel stim
Event IDs: [1 2 3 4]
48 events found on stim channel stim
Event IDs: [1 2 3 4]
48 events found on stim channel stim
Event IDs: [1 2 3 4]
48 events found on stim channel stim
Event IDs: [1 2 3 4]
48 events found on stim channel stim
Event IDs: [1 2 3 4]
<braindecode.datasets.moabb.MOABBDataset object at 0x7fe7ea646800>
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.
tmpdir = tempfile.mkdtemp() # write in a temporary directory
dataset.save(
path=tmpdir,
overwrite=False,
)
Writing /tmp/tmpfrg05y_e/0/0-raw.fif
Closing /tmp/tmpfrg05y_e/0/0-raw.fif
[done]
Writing /tmp/tmpfrg05y_e/1/1-raw.fif
Closing /tmp/tmpfrg05y_e/1/1-raw.fif
[done]
Writing /tmp/tmpfrg05y_e/2/2-raw.fif
Closing /tmp/tmpfrg05y_e/2/2-raw.fif
[done]
Writing /tmp/tmpfrg05y_e/3/3-raw.fif
Closing /tmp/tmpfrg05y_e/3/3-raw.fif
[done]
Writing /tmp/tmpfrg05y_e/4/4-raw.fif
Closing /tmp/tmpfrg05y_e/4/4-raw.fif
[done]
Writing /tmp/tmpfrg05y_e/5/5-raw.fif
Closing /tmp/tmpfrg05y_e/5/5-raw.fif
[done]
Writing /tmp/tmpfrg05y_e/6/6-raw.fif
Closing /tmp/tmpfrg05y_e/6/6-raw.fif
[done]
Writing /tmp/tmpfrg05y_e/7/7-raw.fif
Closing /tmp/tmpfrg05y_e/7/7-raw.fif
[done]
Writing /tmp/tmpfrg05y_e/8/8-raw.fif
Closing /tmp/tmpfrg05y_e/8/8-raw.fif
[done]
Writing /tmp/tmpfrg05y_e/9/9-raw.fif
Closing /tmp/tmpfrg05y_e/9/9-raw.fif
[done]
Writing /tmp/tmpfrg05y_e/10/10-raw.fif
Closing /tmp/tmpfrg05y_e/10/10-raw.fif
[done]
Writing /tmp/tmpfrg05y_e/11/11-raw.fif
Closing /tmp/tmpfrg05y_e/11/11-raw.fif
[done]
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).
dataset_loaded = load_concat_dataset(
path=tmpdir,
preload=True,
ids_to_load=[1, 3],
target_name=None,
)
Opening raw data file /tmp/tmpfrg05y_e/1/1-raw.fif...
Range : 0 ... 3868 = 0.000 ... 386.800 secs
Ready.
Reading 0 ... 3868 = 0.000 ... 386.800 secs...
Opening raw data file /tmp/tmpfrg05y_e/3/3-raw.fif...
Range : 0 ... 3868 = 0.000 ... 386.800 secs
Ready.
Reading 0 ... 3868 = 0.000 ... 386.800 secs...
The serialization utility also supports WindowsDatasets, so we create compute windows next.
windows_dataset = create_windows_from_events(
concat_ds=dataset_loaded,
trial_start_offset_samples=0,
trial_stop_offset_samples=0,
)
windows_dataset.description
Used Annotations descriptions: ['feet', 'left_hand', 'right_hand', 'tongue']
Used Annotations descriptions: ['feet', 'left_hand', 'right_hand', 'tongue']
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.
windows_dataset.save(
path=tmpdir,
overwrite=True,
)
Writing /tmp/tmpfrg05y_e/0/0-raw.fif
Closing /tmp/tmpfrg05y_e/0/0-raw.fif
[done]
Writing /tmp/tmpfrg05y_e/1/1-raw.fif
Closing /tmp/tmpfrg05y_e/1/1-raw.fif
[done]
/home/runner/work/braindecode/braindecode/braindecode/datasets/base.py:777: UserWarning: The number of saved datasets (2) does not match the number of existing subdirectories (12). You may now encounter a mix of differently preprocessed datasets!
warnings.warn(
/home/runner/work/braindecode/braindecode/braindecode/datasets/base.py:788: UserWarning: Chosen directory /tmp/tmpfrg05y_e contains other subdirectories or files ['5', '4', '2', '9', '3', '6', '7', '11', '10', '8'].
warnings.warn(
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.
windows_dataset_loaded = load_concat_dataset(
path=tmpdir,
preload=False,
ids_to_load=[0],
target_name=None,
)
windows_dataset_loaded.description
Opening raw data file /tmp/tmpfrg05y_e/0/0-raw.fif...
Range : 0 ... 3868 = 0.000 ... 386.800 secs
Ready.
Total running time of the script: (0 minutes 9.798 seconds)
Estimated memory usage: 947 MB