Skip to content

Commit 18999ca

Browse files
Deprecate write_raw_bids()'s events_data parameter in favor of "events" (#1054)
* Deprecate write_raw_bids()'s events_data parameter in favor of "events" For more consistency with MNE-Python. This is WIP. WDYT? * Fix a few tests * Fix examples * Update changelog * Additional changelog entry * Fix CLI * Better backward-compat * Fix CLI * Add test * Fix test * Raise ValueError instead of RuntimeError * Schedule removal for MNE-BIDS 0.14
1 parent 7607a0e commit 18999ca

18 files changed

+174
-104
lines changed

doc/whats_new.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ Detailed list of changes
5454
🧐 API and behavior changes
5555
^^^^^^^^^^^^^^^^^^^^^^^^^^^
5656

57+
- :func:`~mne_bids.write_raw_bids` now expects all but the first four parameters to be passed as keyword arguments, by `Richard Höchenberger`_ (:gh:`1054`)
58+
59+
- The ``events_data`` parameter of :func:`~mne_bids.write_raw_bids` has been deprecated in favor of a new parameter named ``events``. This ensures more consistency between the MNE-BIDS and MNE-Python APIs. You may continue using the ``events_data`` parameter for now, but a ``FutureWarning`` will be raised. ``events_data`` will be removed in MNE-BIDS 0.14, by `Richard Höchenberger`_ (:gh:`1054`)
60+
5761
- In many places, we used to infer the ``datatype`` of a :class:`~mne_bids.BIDSPath` from the ``suffix``, if not explicitly provided. However, this has lead to trouble in certain edge cases. In an effort to reduce the amount of implicit behavior in MNE-BIDS, we now require users to explicitly specify a ``datatype`` whenever the invoked functions or methods expect one, by `Richard Höchenberger`_ (:gh:`1030`)
5862

5963
- :func:`mne_bids.make_dataset_description` now accepts keyword arguments only, and can now also write the following metadata: ``HEDVersion``, ``EthicsApprovals``, ``GeneratedBy``, and ``SourceDatasets``, by `Stefan Appelhoff`_ (:gh:`406`)
@@ -62,6 +66,8 @@ Detailed list of changes
6266

6367
- :func:`mne_bids.print_dir_tree` now raises a :py:class:`FileNotFoundError` instead of a :py:class:`ValueError` if the directory does not exist, by `Richard Höchenberger`_ (:gh:`1013`)
6468

69+
- Passing only one of ``events`` and ``event_id`` to :func:`~mne_bids.write_raw_bids` now raises a ``ValueError`` instead of a ``RuntimeError``, by `Richard Höchenberger`_ (:gh:`1054`)
70+
6571
🛠 Requirements
6672
^^^^^^^^^^^^^^^
6773

examples/anonymize_dataset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575

7676
# Write experimental MEG data, fine-calibration and crosstalk files
7777
write_raw_bids(
78-
raw=raw, bids_path=bids_path, events_data=events_path, event_id=event_id,
78+
raw=raw, bids_path=bids_path, events=events_path, event_id=event_id,
7979
empty_room=bids_path_er, verbose=False
8080
)
8181
write_meg_calibration(cal_path, bids_path=bids_path, verbose=False)

examples/convert_empty_room.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@
5959
shutil.rmtree(bids_root)
6060

6161
# %%
62-
# Specify the raw_file and events_data and run the BIDS conversion, and write
63-
# the BIDS data.
62+
# Specify the raw file and write the BIDS data.
6463

6564
raw = mne.io.read_raw_fif(raw_fname)
6665
raw.info['line_freq'] = 60 # specify power line frequency as required by BIDS

examples/convert_mne_sample.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
# Now we can read the MNE sample data. We define an `event_id` based on our
4545
# knowledge of the data, to give meaning to events in the data.
4646
#
47-
# With `raw_fname` and `events_data`, we determine where to get the sample data
47+
# With `raw_fname` and `events`, we determine where to get the sample data
4848
# from. `output_path` determines where we will write the BIDS conversion to.
4949

5050
data_path = sample.data_path()
@@ -53,7 +53,7 @@
5353

5454
raw_fname = op.join(data_path, 'MEG', 'sample', 'sample_audvis_raw.fif')
5555
er_fname = op.join(data_path, 'MEG', 'sample', 'ernoise_raw.fif') # empty room
56-
events_data = op.join(data_path, 'MEG', 'sample', 'sample_audvis_raw-eve.fif')
56+
events_fname = op.join(data_path, 'MEG', 'sample', 'sample_audvis_raw-eve.fif')
5757
output_path = op.join(data_path, '..', 'MNE-sample-data-bids')
5858

5959
# %%
@@ -99,7 +99,7 @@
9999
write_raw_bids(
100100
raw=raw,
101101
bids_path=bids_path,
102-
events_data=events_data,
102+
events=events_fname,
103103
event_id=event_id,
104104
empty_room=raw_er,
105105
overwrite=True

examples/convert_mri_and_trans.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
event_id = {'Auditory/Left': 1, 'Auditory/Right': 2, 'Visual/Left': 3,
6262
'Visual/Right': 4, 'Smiley': 5, 'Button': 32}
6363
raw_fname = op.join(data_path, 'MEG', 'sample', 'sample_audvis_raw.fif')
64-
events_data = op.join(data_path, 'MEG', 'sample', 'sample_audvis_raw-eve.fif')
64+
events_fname = op.join(data_path, 'MEG', 'sample', 'sample_audvis_raw-eve.fif')
6565
output_path = op.abspath(op.join(data_path, '..', 'MNE-sample-data-bids'))
6666
fs_subjects_dir = op.join(data_path, 'subjects') # FreeSurfer subjects dir
6767

@@ -87,7 +87,7 @@
8787
run = '01'
8888
bids_path = BIDSPath(subject=sub, session=ses, task=task,
8989
run=run, root=output_path)
90-
write_raw_bids(raw, bids_path, events_data=events_data,
90+
write_raw_bids(raw, bids_path, events=events_fname,
9191
event_id=event_id, overwrite=True)
9292

9393
# %%

examples/mark_bad_channels.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757

5858
raw = mne.io.read_raw_fif(raw_fname, verbose=False)
5959
raw.info['line_freq'] = 60 # Specify power line frequency as required by BIDS.
60-
write_raw_bids(raw, bids_path=bids_path, events_data=events_fname,
60+
write_raw_bids(raw, bids_path=bids_path, events=events_fname,
6161
event_id=event_id, overwrite=True, verbose=False)
6262

6363
# %%

mne_bids/commands/mne_bids_raw_to_bids.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ def run():
3737
parser.add_option('--acq', dest='acq',
3838
help='acquisition parameter for this dataset')
3939
parser.add_option('--events_data', dest='events_data',
40+
help='Deprecated. Pass --events instead.')
41+
parser.add_option('--events', dest='events',
4042
help='events file (events.tsv)')
4143
parser.add_option('--event_id', dest='event_id',
4244
help='event id dict', metavar='eid')
@@ -80,9 +82,10 @@ def run():
8082
if opt.line_freq is not None:
8183
line_freq = None if opt.line_freq == "None" else opt.line_freq
8284
raw.info['line_freq'] = line_freq
85+
8386
write_raw_bids(raw, bids_path, event_id=opt.event_id,
84-
events_data=opt.events_data, overwrite=opt.overwrite,
85-
verbose=True)
87+
events=opt.events, overwrite=opt.overwrite,
88+
events_data=opt.events_data, verbose=True)
8689

8790

8891
if __name__ == '__main__':

mne_bids/commands/tests/test_cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,8 @@ def test_count_events(tmp_path):
257257
'visual/right': 4, 'face': 5, 'button': 32}
258258

259259
bids_path = BIDSPath(subject='01', root=output_path, task='foo')
260-
write_raw_bids(raw, bids_path, events, event_id, overwrite=True,
261-
verbose=False)
260+
write_raw_bids(raw, bids_path, events=events, event_id=event_id,
261+
overwrite=True, verbose=False)
262262

263263
with ArgvSetter(('--bids_root', output_path)):
264264
mne_bids_count_events.run()

mne_bids/inspect.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def _save_annotations(*, annotations, bids_path):
239239
raw = read_raw_bids(bids_path=bids_path, extra_params=extra_params,
240240
verbose='warning')
241241
raw.set_annotations(annotations)
242-
events, durs, descrs = _read_events(events_data=None, event_id=None,
242+
events, durs, descrs = _read_events(events=None, event_id=None,
243243
bids_path=bids_path, raw=raw)
244244

245245
# Write sidecar – or remove it if no events are left.

mne_bids/read.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ def _read_raw(raw_path, electrode=None, hsp=None, hpi=None,
7474
return raw
7575

7676

77-
def _read_events(events_data, event_id, raw, bids_path=None):
77+
def _read_events(events, event_id, raw, bids_path=None):
7878
"""Retrieve events (for use in *_events.tsv) from FIFF/array & Annotations.
7979
8080
Parameters
8181
----------
82-
events_data : path-like | np.ndarray | None
82+
events : path-like | np.ndarray | None
8383
If a string, a path to an events file. If an array, an MNE events array
8484
(shape n_events, 3). If None, events will be generated from
8585
``raw.annotations``.
@@ -107,19 +107,19 @@ def _read_events(events_data, event_id, raw, bids_path=None):
107107
the values to the event IDs.
108108
109109
"""
110-
# get events from events_data
111-
if isinstance(events_data, np.ndarray):
112-
if events_data.ndim != 2:
110+
# retrieve events
111+
if isinstance(events, np.ndarray):
112+
if events.ndim != 2:
113113
raise ValueError('Events must have two dimensions, '
114-
f'found {events_data.ndim}')
115-
if events_data.shape[1] != 3:
114+
f'found {events.ndim}')
115+
if events.shape[1] != 3:
116116
raise ValueError('Events must have second dimension of length 3, '
117-
f'found {events_data.shape[1]}')
118-
events = events_data
119-
elif events_data is None:
117+
f'found {events.shape[1]}')
118+
events = events
119+
elif events is None:
120120
events = np.empty(shape=(0, 3), dtype=int)
121121
else:
122-
events = read_events(events_data).astype(int)
122+
events = read_events(events).astype(int)
123123

124124
if events.size > 0:
125125
# Only keep events for which we have an ID <> description mapping.
@@ -129,7 +129,7 @@ def _read_events(events_data, event_id, raw, bids_path=None):
129129
f'No description was specified for the following event(s): '
130130
f'{", ".join([str(x) for x in sorted(ids_without_desc)])}. '
131131
f'Please add them to the event_id dictionary, or drop them '
132-
f'from the events_data array.'
132+
f'from the events array.'
133133
)
134134
del ids_without_desc
135135
mask = [e in list(event_id.values()) for e in events[:, 2]]
@@ -175,7 +175,7 @@ def _read_events(events_data, event_id, raw, bids_path=None):
175175
)
176176
):
177177
warn('No events found or provided. Please add annotations to the raw '
178-
'data, or provide the events_data and event_id parameters. For '
178+
'data, or provide the events and event_id parameters. For '
179179
'resting state data, BIDS recommends naming the task using '
180180
'labels beginning with "rest".')
181181

0 commit comments

Comments
 (0)