Convert MNE sample data to BIDS format

This example demonstrates how to convert your existing files into a BIDS-compatible folder.

# Authors: Mainak Jas <mainak.jas@telecom-paristech.fr>
#          Alexandre Gramfort <alexandre.gramfort@telecom-paristech.fr>
#          Teon Brooks <teon.brooks@gmail.com>

# License: BSD (3-clause)

Let us import mne_bids

import os.path as op

import mne
from mne.datasets import sample

from mne_bids import write_raw_bids
from mne_bids.utils import print_dir_tree

And define the paths and event_id dictionary.

data_path = sample.data_path()
event_id = {'Auditory/Left': 1, 'Auditory/Right': 2, 'Visual/Left': 3,
            'Visual/Right': 4, 'Smiley': 5, 'Button': 32}

raw_fname = op.join(data_path, 'MEG', 'sample', 'sample_audvis_raw.fif')
events_data = op.join(data_path, 'MEG', 'sample', 'sample_audvis_raw-eve.fif')
output_path = op.join(data_path, '..', 'MNE-sample-data-bids')

Finally, we specify the raw_file and events_data

raw = mne.io.read_raw_fif(raw_fname)
bids_basename = 'sub-01_ses-01_task-audiovisual_run-01'
write_raw_bids(raw, bids_basename, output_path, events_data=events_data,
               event_id=event_id, overwrite=True)

Out:

Opening raw data file /home/stefanappelhoff/mne_data/MNE-sample-data/MEG/sample/sample_audvis_raw.fif...
    Read a total of 3 projection items:
        PCA-v1 (1 x 102)  idle
        PCA-v2 (1 x 102)  idle
        PCA-v3 (1 x 102)  idle
    Range : 25800 ... 192599 =     42.956 ...   320.670 secs
Ready.
Current compensation grade : 0
Opening raw data file /home/stefanappelhoff/mne_data/MNE-sample-data/MEG/sample/sample_audvis_raw.fif...
    Read a total of 3 projection items:
        PCA-v1 (1 x 102)  idle
        PCA-v2 (1 x 102)  idle
        PCA-v3 (1 x 102)  idle
    Range : 25800 ... 192599 =     42.956 ...   320.670 secs
Ready.
Current compensation grade : 0

Writing '/home/stefanappelhoff/mne_data/MNE-sample-data/../MNE-sample-data-bids/participants.tsv'...

   age group participant_id  sex
0  NaN   NaN         sub-01  NaN

Writing '/home/stefanappelhoff/mne_data/MNE-sample-data/../MNE-sample-data-bids/sub-01/ses-01/sub-01_ses-01_scans.tsv'...

                                            filename             acq_time
0  meg/sub-01_ses-01_task-audiovisual_run-01_meg.fif  2002-12-03T20:01:10

Writing '/home/stefanappelhoff/mne_data/MNE-sample-data/../MNE-sample-data-bids/sub-01/ses-01/meg/sub-01_ses-01_task-audiovisual_run-01_events.tsv'...

      onset  duration      trial_type  event_value  event_sample
0  3.624618       0.0  Auditory/Right            2          2177
1  4.237323       0.0     Visual/Left            3          2545
2  4.946596       0.0   Auditory/Left            1          2971
3  5.692499       0.0    Visual/Right            4          3419
4  6.413426       0.0  Auditory/Right            2          3852

Writing '/home/stefanappelhoff/mne_data/MNE-sample-data/../MNE-sample-data-bids/dataset_description.json'...

{
    "Name": " ",
    "BIDSVersion": "1.1.1 (draft)"
}
/home/stefanappelhoff/miniconda3/envs/mne_bids1/lib/python3.6/site-packages/mne_bids-0.1-py3.6.egg/mne_bids/mne_bids.py:431: UserWarning: No line frequency found, defaulting to 50 Hz
  warn('No line frequency found, defaulting to 50 Hz')
Reading 0 ... 166799  =      0.000 ...   277.714 secs...

Writing '/home/stefanappelhoff/mne_data/MNE-sample-data/../MNE-sample-data-bids/sub-01/ses-01/meg/sub-01_ses-01_task-audiovisual_run-01_meg.json'...

{
    "TaskName": "audiovisual",
    "Manufacturer": "Elekta",
    "PowerLineFrequency": 50,
    "SamplingFrequency": 600.614990234375,
    "SoftwareFilters": "n/a",
    "RecordingDuration": 277.7136813300495,
    "RecordingType": "continuous",
    "DewarPosition": "n/a",
    "DigitizedLandmarks": false,
    "DigitizedHeadPoints": false,
    "MEGChannelCount": 306,
    "MEGREFChannelCount": 0,
    "EEGChannelCount": 60,
    "EOGChannelCount": 1,
    "ECGChannelCount": 0,
    "EMGChannelCount": 0,
    "MiscChannelCount": 0,
    "TriggerChannelCount": 9
}

Writing '/home/stefanappelhoff/mne_data/MNE-sample-data/../MNE-sample-data-bids/sub-01/ses-01/meg/sub-01_ses-01_task-audiovisual_run-01_channels.tsv'...

       name           type units         description  sampling_frequency  \
0  MEG 0113  MEGGRADPLANAR   T/m  Planar Gradiometer           600.61499
1  MEG 0112  MEGGRADPLANAR   T/m  Planar Gradiometer           600.61499
2  MEG 0111         MEGMAG     T        Magnetometer           600.61499
3  MEG 0122  MEGGRADPLANAR   T/m  Planar Gradiometer           600.61499
4  MEG 0123  MEGGRADPLANAR   T/m  Planar Gradiometer           600.61499

   low_cutoff  high_cutoff status
0         0.1     172.1763   good
1         0.1     172.1763   good
2         0.1     172.1763   good
3         0.1     172.1763   good
4         0.1     172.1763   good
Copying data files to /home/stefanappelhoff/mne_data/MNE-sample-data/../MNE-sample-data-bids/sub-01/ses-01/meg/sub-01_ses-01_task-audiovisual_run-01_meg.fif
/home/stefanappelhoff/Desktop/bids/mne-bids/examples/convert_mne_sample.py:44: RuntimeWarning: This filename (/home/stefanappelhoff/mne_data/MNE-sample-data/../MNE-sample-data-bids/sub-01/ses-01/meg/sub-01_ses-01_task-audiovisual_run-01_meg.fif) does not conform to MNE naming conventions. All raw files should end with raw.fif, raw_sss.fif, raw_tsss.fif, raw.fif.gz, raw_sss.fif.gz or raw_tsss.fif.gz
  event_id=event_id, overwrite=True)
Overwriting existing file.
Writing /home/stefanappelhoff/mne_data/MNE-sample-data-bids/sub-01/ses-01/meg/sub-01_ses-01_task-audiovisual_run-01_meg.fif
Closing /home/stefanappelhoff/mne_data/MNE-sample-data-bids/sub-01/ses-01/meg/sub-01_ses-01_task-audiovisual_run-01_meg.fif [done]

'/home/stefanappelhoff/mne_data/MNE-sample-data/../MNE-sample-data-bids'

Now let’s see the structure of the BIDS folder we created.

print_dir_tree(output_path)

Out:

|------------------ MNE-sample-data-bids
|--------------------- participants.tsv
|--------------------- dataset_description.json
|--------------------- participants.json
|--------------------- sub-01
|------------------------ ses-01
|--------------------------- sub-01_ses-01_scans.tsv
|--------------------------- meg
|------------------------------ sub-01_ses-01_coordsystem.json
|------------------------------ sub-01_ses-01_task-audiovisual_run-01_meg.json
|------------------------------ sub-01_ses-01_task-audiovisual_run-01_channels.tsv
|------------------------------ sub-01_ses-01_task-audiovisual_run-01_meg.fif
|------------------------------ sub-01_ses-01_task-audiovisual_run-01_events.tsv
|--------------------------- anat
|------------------------------ sub-01_ses-01_T1w.nii.gz
|------------------------------ sub-01_ses-01_T1w.json

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

Gallery generated by Sphinx-Gallery