Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MRG: Fix handling of n/a event values when auto-renaming events on read #937

Merged
merged 6 commits into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions .circleci/config.yml
Expand Up @@ -11,7 +11,7 @@ jobs:
# restore cache from last build. Unless __init__.py has changed since then
- restore_cache:
keys:
- data-cache-2-{{ checksum "./mne_bids/__init__.py" }}
- data-cache-3-{{ checksum "./mne_bids/__init__.py" }}

# Also restore pip cache to speed up installations
- restore_cache:
Expand Down Expand Up @@ -48,7 +48,7 @@ jobs:

# Store the data cache
- save_cache:
key: data-cache-2-{{ checksum "./mne_bids/__init__.py" }}
key: data-cache-3-{{ checksum "./mne_bids/__init__.py" }}
paths:
- ~/mne_data

Expand Down
6 changes: 4 additions & 2 deletions doc/whats_new.rst
Expand Up @@ -54,9 +54,11 @@ Bug fixes

- Forcing EDF conversion in :func:`mne_bids.write_raw_bids` properly uses the ``overwrite`` parameter now, by `Adam Li`_ (:gh:`930`)

- :func:`mne_bids.make_report` now correctly handles `participant.tsv` files that only contain a `participant_id` column, by `Simon Kern`_ (:gh:`912`)
- :func:`mne_bids.make_report` now correctly handles ``participant.tsv`` files that only contain a ``participant_id`` column, by `Simon Kern`_ (:gh:`912`)

- :func:`mne_bids.write_raw_bids` doesn't store age, handedness, and sex in `participants.tsv` anymore for empty-room recordings, by `Richard Höchenberger`_ (:gh:`935`)
- :func:`mne_bids.write_raw_bids` doesn't store age, handedness, and sex in ``participants.tsv`` anymore for empty-room recordings, by `Richard Höchenberger`_ (:gh:`935`)

- When :func:`mne_bids.read_raw_bids` automatically creates new hierarchical event names based on event values (in cases where the same ``trial_type`` was assigned to different ``value``s in ``*_events.tsv``), ``'n/a'`` values will now be converted to ``'na'``, by `Richard Höchenberger`_ (:gh:`937`)


:doc:`Find out what was new in previous releases <whats_new_previous_releases>`
Expand Down
4 changes: 3 additions & 1 deletion mne_bids/read.py
Expand Up @@ -416,7 +416,9 @@ def _handle_events_reading(events_fname, raw):
f'The event "{trial_type}" refers to multiple event '
f'values. Creating hierarchical event names.')
for ii in idx:
new_name = f'{trial_type}/{values[ii]}'
value = values[ii]
value = 'na' if value == 'n/a' else value
new_name = f'{trial_type}/{value}'
logger.info(f' Renaming event: {trial_type} -> '
f'{new_name}')
trial_types[ii] = new_name
Expand Down
15 changes: 9 additions & 6 deletions mne_bids/tests/test_read.py
Expand Up @@ -347,21 +347,24 @@ def test_handle_events_reading(tmp_path):
raw = _handle_events_reading(events_fname, raw)
events, event_id = mne.events_from_annotations(raw)

# Test with same `trial_type` referring to different `value`
events = {'onset': [11, 12, 13],
'duration': ['n/a', 'n/a', 'n/a'],
'trial_type': ["event1", "event1", "event2"],
'value': [1, 2, 3]}
# Test with same `trial_type` referring to different `value`:
# The events should be renamed automatically
events = {'onset': [11, 12, 13, 14, 15],
'duration': ['n/a', 'n/a', 'n/a', 'n/a', 'n/a'],
'trial_type': ["event1", "event1", "event2", "event3", "event3"],
'value': [1, 2, 3, 4, 'n/a']}
events_fname = tmp_path / 'bids3' / 'sub-01_task-test_events.json'
events_fname.parent.mkdir()
_to_tsv(events, events_fname)

raw = _handle_events_reading(events_fname, raw)
events, event_id = mne.events_from_annotations(raw)

assert len(events) == 3
assert len(events) == 5
assert 'event1/1' in event_id
assert 'event1/2' in event_id
assert 'event3/4' in event_id
assert 'event3/na' in event_id # 'n/a' value should become 'na'
# The event with unique value mapping should not be renamed
assert 'event2' in event_id

Expand Down