Importing Data from BIDS

Importing data from BIDS (Brain Imaging Data Structure)

Although there is no universal data storage format for neural data, the BIDS format has seen widespread adoption. This tutorial demonstrates how to easily load data from a BIDS format into a Data object using the load_bids function. Events stored in the BIDS format are naturally segmented to form the "trials" in the Data, facilitating easy data analysis.

Note

Loading data from BIDS requires the additional dependency MNE-BIDS, which is not a hard dependency of naplib-python, so it must be installed separately. You can find installation instructions for MNE-BIDS [here](https://mne.tools/mne-bids/stable/install.html). This tutorial was adapted from a [similar tutorial](https://mne.tools/mne-bids/stable/auto_examples/read_bids_datasets.html#sphx-glr-auto-examples-read-bids-datasets-py) by MNE-BIDS.

Also, this tutorial makes use of an openneuro dataset for illustration purposes. The data downloaded here is about 1.5 GB from one subject.

# Author: Gavin Mischler
#
# License: MIT


import os
from os import path
import openneuro
from mne.datasets import sample
from mne_bids import print_dir_tree
import numpy as np
import matplotlib.pyplot as plt

from naplib.io import load_bids
from naplib.preprocessing import normalize

Download Dataset

dataset = 'ds002778'
subject = 'pd6'

bids_root = path.join(path.dirname(sample.data_path()), dataset)
print(bids_root)
if not path.isdir(bids_root):
    os.makedirs(bids_root)


openneuro.download(dataset=dataset, target_dir=bids_root,
                   include=[f'sub-{subject}'])
Using default location ~/mne_data for sample...
Creating /home/docs/mne_data
Fetching 1 file for the sample dataset ...

  0%|                                              | 0.00/1.65G [00:00<?, ?B/s]
  0%|                                      | 179k/1.65G [00:00<15:46, 1.75MB/s]
  0%|                                     | 1.62M/1.65G [00:00<03:01, 9.11MB/s]
  0%|▏                                    | 7.53M/1.65G [00:00<00:51, 31.8MB/s]
  1%|▎                                    | 12.8M/1.65G [00:00<00:41, 39.8MB/s]
  1%|▍                                    | 18.6M/1.65G [00:00<00:35, 46.3MB/s]
  2%|▌                                    | 26.7M/1.65G [00:00<00:27, 58.3MB/s]
  2%|▊                                    | 34.2M/1.65G [00:00<00:25, 63.7MB/s]
  2%|▉                                    | 40.6M/1.65G [00:00<00:29, 54.6MB/s]
  3%|█                                    | 48.1M/1.65G [00:00<00:26, 60.3MB/s]
  3%|█▏                                   | 54.9M/1.65G [00:01<00:25, 62.4MB/s]
  4%|█▎                                   | 61.3M/1.65G [00:01<00:25, 62.8MB/s]
  4%|█▌                                   | 68.7M/1.65G [00:01<00:23, 66.1MB/s]
  5%|█▋                                   | 76.0M/1.65G [00:01<00:23, 68.1MB/s]
  5%|█▉                                   | 84.3M/1.65G [00:01<00:21, 72.6MB/s]
  6%|██                                   | 92.4M/1.65G [00:01<00:20, 75.0MB/s]
  6%|██▏                                  | 99.9M/1.65G [00:01<00:21, 71.5MB/s]
  6%|██▍                                   | 107M/1.65G [00:01<00:21, 71.7MB/s]
  7%|██▋                                   | 114M/1.65G [00:01<00:21, 71.0MB/s]
  7%|██▊                                   | 121M/1.65G [00:01<00:22, 68.2MB/s]
  8%|██▉                                   | 129M/1.65G [00:02<00:21, 69.7MB/s]
  8%|███▏                                  | 137M/1.65G [00:02<00:20, 72.5MB/s]
  9%|███▎                                  | 144M/1.65G [00:02<00:21, 69.7MB/s]
  9%|███▍                                  | 152M/1.65G [00:02<00:20, 72.5MB/s]
 10%|███▋                                  | 159M/1.65G [00:02<00:20, 71.5MB/s]
 10%|███▊                                  | 166M/1.65G [00:02<00:21, 69.6MB/s]
 11%|████                                  | 174M/1.65G [00:02<00:20, 71.7MB/s]
 11%|████▏                                 | 181M/1.65G [00:02<00:21, 68.5MB/s]
 11%|████▎                                 | 190M/1.65G [00:02<00:20, 72.2MB/s]
 12%|████▌                                 | 197M/1.65G [00:03<00:19, 74.0MB/s]
 12%|████▋                                 | 206M/1.65G [00:03<00:18, 77.7MB/s]
 13%|████▉                                 | 214M/1.65G [00:03<00:18, 77.8MB/s]
 13%|█████                                 | 222M/1.65G [00:03<00:18, 76.3MB/s]
 14%|█████▎                                | 229M/1.65G [00:03<00:18, 75.7MB/s]
 14%|█████▍                                | 237M/1.65G [00:03<00:18, 75.0MB/s]
 15%|█████▌                                | 245M/1.65G [00:03<00:18, 74.1MB/s]
 15%|█████▊                                | 252M/1.65G [00:03<00:19, 70.2MB/s]
 16%|█████▉                                | 259M/1.65G [00:03<00:21, 65.5MB/s]
 16%|██████▏                               | 267M/1.65G [00:04<00:20, 68.0MB/s]
 17%|██████▎                               | 274M/1.65G [00:04<00:19, 70.4MB/s]
 17%|██████▍                               | 282M/1.65G [00:04<00:18, 73.7MB/s]
 18%|██████▋                               | 291M/1.65G [00:04<00:17, 76.9MB/s]
 18%|██████▊                               | 299M/1.65G [00:04<00:17, 77.6MB/s]
 19%|███████                               | 307M/1.65G [00:04<00:16, 79.6MB/s]
 19%|███████▏                              | 315M/1.65G [00:04<00:16, 79.0MB/s]
 20%|███████▍                              | 324M/1.65G [00:04<00:16, 80.2MB/s]
 20%|███████▌                              | 332M/1.65G [00:04<00:17, 75.2MB/s]
 21%|███████▊                              | 340M/1.65G [00:04<00:16, 78.1MB/s]
 21%|███████▉                              | 348M/1.65G [00:05<00:17, 73.1MB/s]
 21%|████████▏                             | 355M/1.65G [00:05<00:17, 72.9MB/s]
 22%|████████▎                             | 363M/1.65G [00:05<00:17, 74.0MB/s]
 22%|████████▌                             | 371M/1.65G [00:05<00:17, 75.2MB/s]
 23%|████████▋                             | 380M/1.65G [00:05<00:16, 78.8MB/s]
 23%|████████▉                             | 388M/1.65G [00:05<00:17, 72.8MB/s]
 24%|█████████                             | 396M/1.65G [00:05<00:16, 74.9MB/s]
 24%|█████████▎                            | 403M/1.65G [00:05<00:16, 73.6MB/s]
 25%|█████████▍                            | 411M/1.65G [00:05<00:17, 72.1MB/s]
 25%|█████████▌                            | 418M/1.65G [00:06<00:17, 71.3MB/s]
 26%|█████████▊                            | 425M/1.65G [00:06<00:18, 67.8MB/s]
 26%|█████████▉                            | 433M/1.65G [00:06<00:17, 71.4MB/s]
 27%|██████████                            | 440M/1.65G [00:06<00:18, 67.1MB/s]
 27%|██████████▎                           | 448M/1.65G [00:06<00:17, 68.7MB/s]
 28%|██████████▍                           | 456M/1.65G [00:06<00:16, 72.8MB/s]
 28%|██████████▋                           | 465M/1.65G [00:06<00:15, 76.8MB/s]
 29%|██████████▊                           | 472M/1.65G [00:06<00:15, 76.8MB/s]
 29%|███████████                           | 481M/1.65G [00:06<00:14, 78.7MB/s]
 30%|███████████▏                          | 489M/1.65G [00:06<00:14, 79.3MB/s]
 30%|███████████▍                          | 497M/1.65G [00:07<00:14, 78.3MB/s]
 31%|███████████▌                          | 505M/1.65G [00:07<00:14, 77.5MB/s]
 31%|███████████▊                          | 512M/1.65G [00:07<00:14, 76.5MB/s]
 31%|███████████▉                          | 520M/1.65G [00:07<00:15, 74.9MB/s]
 32%|████████████▏                         | 528M/1.65G [00:07<00:15, 73.7MB/s]
 32%|████████████▎                         | 536M/1.65G [00:07<00:14, 76.5MB/s]
 33%|████████████▌                         | 544M/1.65G [00:07<00:14, 77.8MB/s]
 33%|████████████▋                         | 552M/1.65G [00:07<00:14, 73.6MB/s]
 34%|████████████▊                         | 559M/1.65G [00:07<00:14, 73.1MB/s]
 34%|█████████████                         | 567M/1.65G [00:07<00:14, 73.3MB/s]
 35%|█████████████▏                        | 574M/1.65G [00:08<00:14, 74.1MB/s]
 35%|█████████████▎                        | 582M/1.65G [00:08<00:15, 71.0MB/s]
 36%|█████████████▌                        | 589M/1.65G [00:08<00:14, 72.8MB/s]
 36%|█████████████▋                        | 597M/1.65G [00:08<00:14, 72.9MB/s]
 37%|█████████████▉                        | 604M/1.65G [00:08<00:14, 73.7MB/s]
 37%|██████████████                        | 612M/1.65G [00:08<00:13, 76.3MB/s]
 38%|██████████████▎                       | 620M/1.65G [00:08<00:13, 76.2MB/s]
 38%|██████████████▍                       | 628M/1.65G [00:08<00:13, 78.1MB/s]
 39%|██████████████▋                       | 637M/1.65G [00:08<00:12, 78.6MB/s]
 39%|██████████████▊                       | 644M/1.65G [00:09<00:20, 49.5MB/s]
 39%|██████████████▉                       | 651M/1.65G [00:09<00:19, 52.4MB/s]
 40%|███████████████▏                      | 659M/1.65G [00:09<00:16, 58.6MB/s]
 40%|███████████████▎                      | 666M/1.65G [00:09<00:15, 62.7MB/s]
 41%|███████████████▍                      | 674M/1.65G [00:09<00:14, 66.8MB/s]
 41%|███████████████▋                      | 681M/1.65G [00:09<00:15, 63.1MB/s]
 42%|███████████████▊                      | 688M/1.65G [00:09<00:15, 64.3MB/s]
 42%|███████████████▉                      | 695M/1.65G [00:09<00:14, 66.3MB/s]
 42%|████████████████▏                     | 702M/1.65G [00:10<00:14, 67.2MB/s]
 43%|████████████████▎                     | 710M/1.65G [00:10<00:13, 71.2MB/s]
 43%|████████████████▌                     | 719M/1.65G [00:10<00:12, 74.4MB/s]
 44%|████████████████▋                     | 727M/1.65G [00:10<00:12, 76.6MB/s]
 44%|████████████████▉                     | 735M/1.65G [00:10<00:11, 77.5MB/s]
 45%|█████████████████                     | 743M/1.65G [00:10<00:17, 51.3MB/s]
 45%|█████████████████▏                    | 750M/1.65G [00:10<00:16, 55.2MB/s]
 46%|█████████████████▍                    | 757M/1.65G [00:10<00:14, 61.0MB/s]
 46%|█████████████████▌                    | 765M/1.65G [00:11<00:13, 64.3MB/s]
 47%|█████████████████▊                    | 773M/1.65G [00:11<00:12, 68.3MB/s]
 47%|█████████████████▉                    | 781M/1.65G [00:11<00:12, 72.0MB/s]
 48%|██████████████████▏                   | 789M/1.65G [00:11<00:11, 72.9MB/s]
 48%|██████████████████▎                   | 797M/1.65G [00:11<00:11, 74.9MB/s]
 49%|██████████████████▍                   | 804M/1.65G [00:11<00:13, 61.8MB/s]
 49%|██████████████████▋                   | 812M/1.65G [00:11<00:12, 65.3MB/s]
 50%|██████████████████▊                   | 819M/1.65G [00:11<00:12, 66.0MB/s]
 50%|██████████████████▉                   | 826M/1.65G [00:11<00:12, 67.5MB/s]
 50%|███████████████████▏                  | 834M/1.65G [00:12<00:11, 72.6MB/s]
 51%|███████████████████▎                  | 842M/1.65G [00:12<00:11, 71.7MB/s]
 51%|███████████████████▌                  | 850M/1.65G [00:12<00:10, 73.4MB/s]
 52%|███████████████████▋                  | 857M/1.65G [00:12<00:11, 69.2MB/s]
 52%|███████████████████▉                  | 866M/1.65G [00:12<00:10, 73.9MB/s]
 53%|████████████████████                  | 874M/1.65G [00:12<00:10, 76.6MB/s]
 53%|████████████████████▎                 | 882M/1.65G [00:12<00:10, 70.8MB/s]
 54%|████████████████████▍                 | 890M/1.65G [00:12<00:10, 73.4MB/s]
 54%|████████████████████▋                 | 898M/1.65G [00:12<00:09, 76.7MB/s]
 55%|████████████████████▊                 | 906M/1.65G [00:12<00:09, 77.4MB/s]
 55%|█████████████████████                 | 914M/1.65G [00:13<00:09, 78.4MB/s]
 56%|█████████████████████▏                | 922M/1.65G [00:13<00:09, 75.8MB/s]
 56%|█████████████████████▍                | 930M/1.65G [00:13<00:09, 74.4MB/s]
 57%|█████████████████████▌                | 937M/1.65G [00:13<00:09, 74.7MB/s]
 57%|█████████████████████▋                | 945M/1.65G [00:13<00:09, 74.3MB/s]
 58%|█████████████████████▉                | 952M/1.65G [00:13<00:09, 71.7MB/s]
 58%|██████████████████████                | 960M/1.65G [00:13<00:09, 72.9MB/s]
 59%|██████████████████████▎               | 968M/1.65G [00:13<00:09, 74.7MB/s]
 59%|██████████████████████▍               | 975M/1.65G [00:13<00:09, 72.6MB/s]
 59%|██████████████████████▌               | 983M/1.65G [00:14<00:09, 70.4MB/s]
 60%|██████████████████████▊               | 990M/1.65G [00:14<00:09, 69.1MB/s]
 60%|██████████████████████▉               | 997M/1.65G [00:14<00:09, 70.4MB/s]
 61%|██████████████████████▍              | 1.00G/1.65G [00:14<00:09, 70.4MB/s]
 61%|██████████████████████▋              | 1.01G/1.65G [00:14<00:08, 71.4MB/s]
 62%|██████████████████████▊              | 1.02G/1.65G [00:14<00:08, 72.7MB/s]
 62%|██████████████████████▉              | 1.03G/1.65G [00:14<00:08, 75.2MB/s]
 63%|███████████████████████▏             | 1.03G/1.65G [00:14<00:08, 71.1MB/s]
 63%|███████████████████████▎             | 1.04G/1.65G [00:14<00:08, 72.5MB/s]
 64%|███████████████████████▍             | 1.05G/1.65G [00:14<00:08, 70.6MB/s]
 64%|███████████████████████▋             | 1.06G/1.65G [00:15<00:09, 64.7MB/s]
 64%|███████████████████████▊             | 1.06G/1.65G [00:15<00:10, 55.9MB/s]
 65%|███████████████████████▉             | 1.07G/1.65G [00:15<00:10, 58.0MB/s]
 65%|████████████████████████▏            | 1.08G/1.65G [00:15<00:08, 64.7MB/s]
 66%|████████████████████████▎            | 1.08G/1.65G [00:15<00:09, 61.4MB/s]
 66%|████████████████████████▍            | 1.09G/1.65G [00:15<00:09, 58.0MB/s]
 67%|████████████████████████▌            | 1.10G/1.65G [00:15<00:08, 63.9MB/s]
 67%|████████████████████████▊            | 1.11G/1.65G [00:15<00:08, 66.4MB/s]
 67%|████████████████████████▉            | 1.11G/1.65G [00:15<00:08, 66.0MB/s]
 68%|█████████████████████████            | 1.12G/1.65G [00:16<00:08, 66.5MB/s]
 68%|█████████████████████████▏           | 1.13G/1.65G [00:16<00:07, 67.4MB/s]
 69%|█████████████████████████▍           | 1.13G/1.65G [00:16<00:07, 66.9MB/s]
 69%|█████████████████████████▌           | 1.14G/1.65G [00:16<00:07, 65.3MB/s]
 69%|█████████████████████████▋           | 1.15G/1.65G [00:16<00:07, 68.4MB/s]
 70%|█████████████████████████▊           | 1.16G/1.65G [00:16<00:07, 70.5MB/s]
 70%|██████████████████████████           | 1.16G/1.65G [00:16<00:06, 72.4MB/s]
 71%|██████████████████████████▏          | 1.17G/1.65G [00:16<00:06, 72.0MB/s]
 71%|██████████████████████████▎          | 1.18G/1.65G [00:17<00:12, 38.7MB/s]
 72%|██████████████████████████▍          | 1.18G/1.65G [00:17<00:16, 27.7MB/s]
 72%|██████████████████████████▋          | 1.19G/1.65G [00:17<00:13, 33.6MB/s]
 73%|██████████████████████████▊          | 1.20G/1.65G [00:17<00:10, 42.1MB/s]
 73%|███████████████████████████          | 1.21G/1.65G [00:17<00:08, 49.9MB/s]
 74%|███████████████████████████▏         | 1.22G/1.65G [00:17<00:07, 58.1MB/s]
 74%|███████████████████████████▎         | 1.22G/1.65G [00:18<00:07, 54.6MB/s]
 74%|███████████████████████████▌         | 1.23G/1.65G [00:18<00:06, 60.4MB/s]
 75%|███████████████████████████▋         | 1.24G/1.65G [00:18<00:06, 61.3MB/s]
 75%|███████████████████████████▉         | 1.25G/1.65G [00:18<00:06, 65.8MB/s]
 76%|████████████████████████████         | 1.25G/1.65G [00:18<00:05, 69.3MB/s]
 76%|████████████████████████████▏        | 1.26G/1.65G [00:18<00:05, 72.1MB/s]
 77%|████████████████████████████▍        | 1.27G/1.65G [00:18<00:05, 74.4MB/s]
 77%|████████████████████████████▌        | 1.28G/1.65G [00:18<00:04, 78.0MB/s]
 78%|████████████████████████████▊        | 1.29G/1.65G [00:18<00:04, 78.7MB/s]
 78%|████████████████████████████▉        | 1.29G/1.65G [00:19<00:04, 76.3MB/s]
 79%|█████████████████████████████▏       | 1.30G/1.65G [00:19<00:04, 77.0MB/s]
 79%|█████████████████████████████▎       | 1.31G/1.65G [00:19<00:04, 78.5MB/s]
 80%|█████████████████████████████▌       | 1.32G/1.65G [00:19<00:04, 79.2MB/s]
 80%|█████████████████████████████▋       | 1.33G/1.65G [00:19<00:04, 77.5MB/s]
 81%|█████████████████████████████▊       | 1.33G/1.65G [00:19<00:03, 79.6MB/s]
 81%|██████████████████████████████       | 1.34G/1.65G [00:19<00:03, 80.8MB/s]
 82%|██████████████████████████████▏      | 1.35G/1.65G [00:19<00:03, 81.7MB/s]
 82%|██████████████████████████████▍      | 1.36G/1.65G [00:19<00:03, 78.5MB/s]
 83%|██████████████████████████████▌      | 1.37G/1.65G [00:19<00:03, 77.8MB/s]
 83%|██████████████████████████████▊      | 1.38G/1.65G [00:20<00:04, 68.2MB/s]
 84%|██████████████████████████████▉      | 1.38G/1.65G [00:20<00:03, 70.3MB/s]
 84%|███████████████████████████████      | 1.39G/1.65G [00:20<00:03, 71.7MB/s]
 85%|███████████████████████████████▎     | 1.40G/1.65G [00:20<00:03, 71.1MB/s]
 85%|███████████████████████████████▍     | 1.40G/1.65G [00:20<00:03, 68.9MB/s]
 85%|███████████████████████████████▌     | 1.41G/1.65G [00:20<00:03, 65.3MB/s]
 86%|███████████████████████████████▊     | 1.42G/1.65G [00:20<00:03, 69.6MB/s]
 86%|███████████████████████████████▉     | 1.43G/1.65G [00:20<00:03, 72.9MB/s]
 87%|████████████████████████████████▏    | 1.44G/1.65G [00:20<00:02, 75.3MB/s]
 87%|████████████████████████████████▎    | 1.44G/1.65G [00:21<00:02, 75.6MB/s]
 88%|████████████████████████████████▍    | 1.45G/1.65G [00:21<00:02, 70.2MB/s]
 88%|████████████████████████████████▋    | 1.46G/1.65G [00:21<00:02, 74.9MB/s]
 89%|████████████████████████████████▊    | 1.47G/1.65G [00:21<00:02, 76.3MB/s]
 89%|█████████████████████████████████    | 1.48G/1.65G [00:21<00:02, 75.3MB/s]
 90%|█████████████████████████████████▏   | 1.48G/1.65G [00:21<00:02, 75.4MB/s]
 90%|█████████████████████████████████▍   | 1.49G/1.65G [00:21<00:02, 71.0MB/s]
 91%|█████████████████████████████████▌   | 1.50G/1.65G [00:21<00:02, 68.9MB/s]
 91%|█████████████████████████████████▋   | 1.51G/1.65G [00:21<00:01, 73.6MB/s]
 92%|█████████████████████████████████▉   | 1.52G/1.65G [00:22<00:01, 77.0MB/s]
 92%|██████████████████████████████████   | 1.52G/1.65G [00:22<00:01, 77.3MB/s]
 93%|██████████████████████████████████▎  | 1.53G/1.65G [00:22<00:01, 78.7MB/s]
 93%|██████████████████████████████████▍  | 1.54G/1.65G [00:22<00:01, 80.9MB/s]
 94%|██████████████████████████████████▋  | 1.55G/1.65G [00:22<00:01, 80.4MB/s]
 94%|██████████████████████████████████▊  | 1.56G/1.65G [00:22<00:01, 80.0MB/s]
 95%|███████████████████████████████████  | 1.56G/1.65G [00:22<00:01, 81.4MB/s]
 95%|███████████████████████████████████▏ | 1.57G/1.65G [00:22<00:00, 83.1MB/s]
 96%|███████████████████████████████████▍ | 1.58G/1.65G [00:22<00:00, 80.9MB/s]
 96%|███████████████████████████████████▌ | 1.59G/1.65G [00:22<00:00, 79.4MB/s]
 97%|███████████████████████████████████▊ | 1.60G/1.65G [00:23<00:00, 81.1MB/s]
 97%|███████████████████████████████████▉ | 1.61G/1.65G [00:23<00:00, 76.6MB/s]
 98%|████████████████████████████████████▏| 1.61G/1.65G [00:23<00:00, 75.9MB/s]
 98%|████████████████████████████████████▎| 1.62G/1.65G [00:23<00:00, 71.8MB/s]
 99%|████████████████████████████████████▍| 1.63G/1.65G [00:23<00:00, 73.8MB/s]
 99%|████████████████████████████████████▋| 1.64G/1.65G [00:23<00:00, 77.1MB/s]
100%|████████████████████████████████████▊| 1.65G/1.65G [00:23<00:00, 79.8MB/s]
  0%|                                              | 0.00/1.65G [00:00<?, ?B/s]
100%|█████████████████████████████████████| 1.65G/1.65G [00:00<00:00, 5.77TB/s]
Download complete in 59s (1576.2 MB)
/home/docs/mne_data/ds002778

👋 Hello! This is openneuro-py 2026.1.0. Great to see you! 🤗

   👉 Please report problems 🤯 and bugs 🪲 at
      https://github.com/hoechenberger/openneuro-py/issues

🌍 Preparing to download ds002778 …

📁 Traversing directories for ds002778 : 0 entities [00:00, ? entities/s]
📁 Traversing directories for ds002778 : 7 entities [00:05,  1.20 entities/s]
📁 Traversing directories for ds002778 : 8 entities [00:06,  1.37 entities/s]
📁 Traversing directories for ds002778 : 10 entities [00:06,  1.88 entities/s]
📁 Traversing directories for ds002778 : 14 entities [00:06,  3.29 entities/s]
📁 Traversing directories for ds002778 : 16 entities [00:06,  3.78 entities/s]
📁 Traversing directories for ds002778 : 17 entities [00:07,  3.83 entities/s]
📁 Traversing directories for ds002778 : 20 entities [00:07,  2.76 entities/s]
📥 Retrieving up to 19 files (5 concurrent downloads).

CHANGES:   0%|          | 0.00/379 [00:00<?, ?B/s]


dataset_description.json:   0%|          | 0.00/354 [00:00<?, ?B/s]


participants.tsv:   0%|          | 0.00/1.62k [00:00<?, ?B/s]


README:   0%|          | 0.00/4.55k [00:00<?, ?B/s]


sub-pd6_ses-off_scans.tsv:   0%|          | 0.00/75.0 [00:00<?, ?B/s]


sub-pd6_ses-off_task-rest_beh.json:   0%|          | 0.00/433 [00:00<?, ?B/s]


sub-pd6_ses-off_task-rest_eeg.bdf:   0%|          | 0.00/11.5M [00:00<?, ?B/s]

sub-pd6_ses-off_task-rest_beh.tsv:   0%|          | 0.00/10.0 [00:00<?, ?B/s]



sub-pd6_ses-off_task-rest_channels.tsv:   0%|          | 0.00/2.22k [00:00<?, ?B/s]


sub-pd6_ses-off_task-rest_eeg.bdf:   5%|▍         | 577k/11.5M [00:00<00:02, 5.57MB/s]

sub-pd6_ses-off_task-rest_eeg.json:   0%|          | 0.00/471 [00:00<?, ?B/s]


sub-pd6_ses-off_task-rest_eeg.bdf:  31%|███       | 3.52M/11.5M [00:00<00:00, 20.0MB/s]

participants.json:   0%|          | 0.00/1.24k [00:00<?, ?B/s]


sub-pd6_ses-off_task-rest_eeg.bdf:  80%|███████▉  | 9.15M/11.5M [00:00<00:00, 37.5MB/s]


sub-pd6_ses-on_scans.tsv:   0%|          | 0.00/74.0 [00:00<?, ?B/s]


sub-pd6_ses-on_task-rest_channels.tsv:   0%|          | 0.00/2.22k [00:00<?, ?B/s]


sub-pd6_ses-on_task-rest_eeg.bdf:   0%|          | 0.00/17.4M [00:00<?, ?B/s]
sub-pd6_ses-on_task-rest_eeg.bdf:   5%|▌         | 900k/17.4M [00:00<00:01, 9.02MB/s]

sub-pd6_ses-on_task-rest_eeg.json:   0%|          | 0.00/471 [00:00<?, ?B/s]



sub-pd6_ses-off_task-rest_events.tsv:   0%|          | 0.00/66.0 [00:00<?, ?B/s]


sub-pd6_ses-on_task-rest_eeg.bdf:  21%|██▏       | 3.70M/17.4M [00:00<00:00, 21.0MB/s]

sub-pd6_ses-on_task-rest_events.tsv:   0%|          | 0.00/51.0 [00:00<?, ?B/s]


sub-pd6_ses-on_task-rest_eeg.bdf:  67%|██████▋   | 11.6M/17.4M [00:00<00:00, 48.9MB/s]


sub-pd6_ses-on_task-rest_beh.json:   0%|          | 0.00/433 [00:00<?, ?B/s]

sub-pd6_ses-on_task-rest_beh.tsv:   0%|          | 0.00/10.0 [00:00<?, ?B/s]


                                                                            ✅ Finished downloading ds002778.

🧠 Please enjoy your brains.

Look at the format of the BIDS file structure

print_dir_tree(bids_root, max_depth=4)
|ds002778/
|--- CHANGES
|--- README
|--- dataset_description.json
|--- participants.json
|--- participants.tsv
|--- sub-pd6/
|------ ses-off/
|--------- sub-pd6_ses-off_scans.tsv
|--------- beh/
|------------ sub-pd6_ses-off_task-rest_beh.json
|------------ sub-pd6_ses-off_task-rest_beh.tsv
|--------- eeg/
|------------ sub-pd6_ses-off_task-rest_channels.tsv
|------------ sub-pd6_ses-off_task-rest_eeg.bdf
|------------ sub-pd6_ses-off_task-rest_eeg.json
|------------ sub-pd6_ses-off_task-rest_events.tsv
|------ ses-on/
|--------- sub-pd6_ses-on_scans.tsv
|--------- beh/
|------------ sub-pd6_ses-on_task-rest_beh.json
|------------ sub-pd6_ses-on_task-rest_beh.tsv
|--------- eeg/
|------------ sub-pd6_ses-on_task-rest_channels.tsv
|------------ sub-pd6_ses-on_task-rest_eeg.bdf
|------------ sub-pd6_ses-on_task-rest_eeg.json
|------------ sub-pd6_ses-on_task-rest_events.tsv

Read file structure into a Data Object

In this task, the stimulus recorded is simply a sudden spike in the 'stim' channels, so the associated events have duration=0. For the sake of this tutorial, we will cut the data into trials based on the 'onset' of one event and ending at the onset of the next. If the durations of the stimulus events were meaningful, we could cut by the 'duration' of each event instead. See the load_bids documentation for more details.

resp_channels = ['Fp1','AF3','F7','F3','FC1','FC5','T7','C3','CP1','CP5','P7',
                 'P3','Pz','PO3','O1','Oz','O2','PO4','P4','P8','CP6','CP2',
                 'C4','T8','FC6','FC2','F4','F8','AF4','Fp2','Fz','Cz']

data = load_bids(root=bids_root, subject=subject, datatype='eeg', task='rest', suffix='eeg', session='off', resp_channels=resp_channels)
Extracting BDF parameters from /home/docs/mne_data/ds002778/sub-pd6/ses-off/eeg/sub-pd6_ses-off_task-rest_eeg.bdf...
Setting channel info structure...
Creating raw.info structure...
Reading events from /home/docs/mne_data/ds002778/sub-pd6/ses-off/eeg/sub-pd6_ses-off_task-rest_events.tsv.
Reading channel info from /home/docs/mne_data/ds002778/sub-pd6/ses-off/eeg/sub-pd6_ses-off_task-rest_channels.tsv.
Not fully anonymizing info - keeping hand, his_id, sex of subject_info
/home/docs/checkouts/readthedocs.org/user_builds/naplib-python/checkouts/latest/naplib/io/load_bids.py:103: RuntimeWarning: Unable to map the following column(s) to to MNE:
gender: f
MMSE: 30
NAART: 42
disease_duration: 8
rl_deficits: L OFF meds, more R ON meds
notes: Used preprocessed data from EEGLAB .mat file instead of raw data for pd on
  raw = read_raw_bids(bids_path=bids_path)
NOTE: pick_channels() is a legacy function. New code should use inst.pick(...).
NOTE: pick_types() is a legacy function. New code should use inst.pick(...).
NOTE: pick_channels() is a legacy function. New code should use inst.pick(...).
NOTE: pick_types() is a legacy function. New code should use inst.pick(...).

  0%|          | 0/2 [00:00<?, ?it/s]
100%|██████████| 2/2 [00:00<00:00, 49.47it/s]

Visualize the Data

Visualize the stims and responses for the first 3 seconds of one of the events

sfreq = int(data[0]['sfreq']) # sampling frequency of the data

# normalize the responses for better visualization
data['resp'] = normalize(data, field='resp')

t = np.linspace(0, 3, 3*sfreq)

fig, axes = plt.subplots(2,1, figsize=(10,6))
axes[0].plot(t, data[1]['stim'][:3*sfreq,:])
axes[0].set_title('Event 1 Stim')

axes[1].plot(t, data[1]['resp'][:3*sfreq,:])
axes[1].set_title('Event 1 Response')
axes[1].set_label('Time (s)')
plt.tight_layout()
plt.show()

# All the metadata has been stored in the mne_info attribute of the Data
print(data.mne_info)
Event 1 Stim, Event 1 Response
<Info | 11 non-empty values
 bads: []
 ch_names: Fp1, AF3, F7, F3, FC1, FC5, T7, C3, CP1, CP5, P7, P3, Pz, PO3, ...
 chs: 32 EEG
 custom_ref_applied: False
 description: Anonymized using a time shift to preserve age at acquisition
 experimenter: mne_anonymize
 highpass: 0.0 Hz
 line_freq: 60.0
 lowpass: 104.0 Hz
 meas_date: 2011-02-18 13:07:18 UTC
 nchan: 32
 projs: []
 sfreq: 512.0 Hz
 subject_info: <subject_info | his_id: sub-pd6, birthday: 1949-02-17, hand: 1>
>

Storing a small portion of data before the onset of the stimulus event

We can use the befaft parameter to store an additional bit of the recording before and/or after each stimulus event region.

befaft = [1,0] # keep 1 second before the stimulus, nothing extra after

data2 = load_bids(root=bids_root, subject=subject, datatype='eeg', task='rest', suffix='eeg', session='off', befaft=befaft, resp_channels=resp_channels)

# Now, when we visualize we have a bit of data from before the stimulus onset

t = np.linspace(-1, 2, 3*sfreq)

data2['resp'] = normalize(data2, field='resp')

fig, axes = plt.subplots(2,1, figsize=(10,6))
axes[0].plot(t, data2[1]['stim'][:3*sfreq,:])
axes[0].set_title('Event 1 Stim')

axes[1].plot(t, data2[1]['resp'][:3*sfreq,:])
axes[1].set_title('Event 1 Response')
axes[1].set_label('Time (s)')
plt.tight_layout()
plt.show()
Event 1 Stim, Event 1 Response
Extracting BDF parameters from /home/docs/mne_data/ds002778/sub-pd6/ses-off/eeg/sub-pd6_ses-off_task-rest_eeg.bdf...
Setting channel info structure...
Creating raw.info structure...
Reading events from /home/docs/mne_data/ds002778/sub-pd6/ses-off/eeg/sub-pd6_ses-off_task-rest_events.tsv.
Reading channel info from /home/docs/mne_data/ds002778/sub-pd6/ses-off/eeg/sub-pd6_ses-off_task-rest_channels.tsv.
Not fully anonymizing info - keeping hand, his_id, sex of subject_info
/home/docs/checkouts/readthedocs.org/user_builds/naplib-python/checkouts/latest/naplib/io/load_bids.py:103: RuntimeWarning: Unable to map the following column(s) to to MNE:
gender: f
MMSE: 30
NAART: 42
disease_duration: 8
rl_deficits: L OFF meds, more R ON meds
notes: Used preprocessed data from EEGLAB .mat file instead of raw data for pd on
  raw = read_raw_bids(bids_path=bids_path)
NOTE: pick_channels() is a legacy function. New code should use inst.pick(...).
NOTE: pick_types() is a legacy function. New code should use inst.pick(...).
NOTE: pick_channels() is a legacy function. New code should use inst.pick(...).
NOTE: pick_types() is a legacy function. New code should use inst.pick(...).

  0%|          | 0/2 [00:00<?, ?it/s]
100%|██████████| 2/2 [00:00<00:00, 49.30it/s]

Total running time of the script: (1 minutes 10.386 seconds)

Gallery generated by Sphinx-Gallery