mne_bids.write_raw_bids¶
-
mne_bids.write_raw_bids(raw, bids_path, events_data=None, event_id=None, anonymize=None, overwrite=False, verbose=True)[source]¶ Save raw data to a BIDS-compliant folder structure.
Warning
The original file is simply copied over if the original file format is BIDS-supported for that datatype. Otherwise, this function will convert to a BIDS-supported file format while warning the user. For EEG and iEEG data, conversion will be to BrainVision format; for MEG, conversion will be to FIFF.
mne-bidswill infer the manufacturer information from the file extension. If your file format is non-standard for the manufacturer, please update the manufacturer field in the sidecars manually.
- Parameters
- rawinstance of mne.io.Raw
The raw data. It must be an instance of
mne.io.Raw. The data should not be loaded from disk, i.e.,raw.preloadmust beFalse.- bids_pathBIDSPath
The file to write. The
mne_bids.BIDSPathinstance passed here must have the.rootattribute set. If the.datatypeattribute is not set, it will be inferred from the recording data type found inraw. Example:bids_path = BIDSPath(subject='01', session='01', task='testing', acquisition='01', run='01', root='/data/BIDS')
This will write the following files in the correct subfolder
root:sub-01_ses-01_task-testing_acq-01_run-01_meg.fif sub-01_ses-01_task-testing_acq-01_run-01_meg.json sub-01_ses-01_task-testing_acq-01_run-01_channels.tsv sub-01_ses-01_task-testing_acq-01_run-01_coordsystem.json
and the following one if
events_datais notNone:sub-01_ses-01_task-testing_acq-01_run-01_events.tsv
and add a line to the following files:
participants.tsv scans.tsv
Note that the data type is automatically inferred from the raw object, as well as the extension. Data with MEG and other electrophysiology data in the same file will be stored as
'meg'.- events_datapath-like | array | None
Use this parameter to specify events to write to the
*_events.tsvsidecar file, additionally to the object’smne.Annotations(which are always written). If a path, specifies the location of an MNE events file. If an array, the MNE events array (shape:(n_events, 3)). If a path or an array andraw.annotationsexist, the union ofevent_dataandraw.annotationswill be written. Corresponding descriptions for all event IDs (listed in the third column of the MNE events array) must be specified via theevent_idparameter; otherwise, an exception is raised. IfNone, events will only be inferred from the the raw object’smne.Annotations.Note
If
not None, writes the union ofevents_dataandraw.annotations. If you wish to only writeraw.annotations, passevents_data=None. If you want to exclude the events inraw.annotationsfrom being written, callraw.set_annotations(None)before invoking this function.Note
Descriptions of all event IDs must be specified via the
event_idparameter.- event_iddict | None
Descriptions of all event IDs, if you passed
events_data. The descriptions will be written to thetrial_typecolumn in*_events.tsv. The dictionary keys correspond to the event descriptions and the values to the event IDs. You must specify a description for all event IDs inevents_data.- anonymizedict | None
If None (default), no anonymization is performed. If a dictionary, data will be anonymized depending on the dictionary keys:
daysbackis a required key,keep_hisis optional.daysbackintNumber of days by which to move back the recording date in time. In studies with multiple subjects the relative recording date differences between subjects can be kept by using the same number of
daysbackfor all subject anonymizations.daysbackshould be great enough to shift the date prior to 1925 to conform with BIDS anonymization rules.keep_hisboolIf
False(default), all subject information next to the recording date will be overwritten as well. If True, keep subject information apart from the recording date.
- overwritebool
Whether to overwrite existing files or data in files. Defaults to
False.If
True, any existing files with the same BIDS parameters will be overwritten with the exception of the*_participants.tsvand*_scans.tsvfiles. For these files, parts of pre-existing data that match the current data will be replaced. For*_participants.tsv, specifically, age, sex and hand fields will be overwritten, while any manually added fields inparticipants.jsonandparticipants.tsvby a user will be retained. IfFalse, no existing data will be overwritten or replaced.- verbosebool
If
True, this will print a snippet of the sidecar files. Otherwise, no content will be printed.
- Returns
- bids_pathBIDSPath
The path of the created data file.
Notes
You should ensure that
raw.info['subject_info']andraw.info['meas_date']are set to proper (not-None) values to allow for the correct computation of each participant’s age when creating*_participants.tsv.This function will convert existing
mne.Annotationsfromraw.annotationsto events. Additionally, any events supplied viaevents_datawill be written too. To avoid writing of annotations, remove them from the raw file viaraw.set_annotations(None)before invokingwrite_raw_bids.To write events encoded in a
STIMchannel, you first need to create the events array manually and pass it to this function:See the documentation of
mne.find_eventsfor more information on event extraction fromSTIMchannels.