Skip to content

Commit

Permalink
MRG: Fix handling of n/a event values when auto-renaming events on re…
Browse files Browse the repository at this point in the history
…ad (#937)

* Fix handling of n/a event values when auto-renaming events on read

Fixes #936

* Add tests

* Update changelog

* Nuke Circle cache

* Fix tests?

* Nuke Circle cache again…
  • Loading branch information
hoechenberger committed Jan 7, 2022
1 parent a497390 commit 0473a3e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
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-4-{{ 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-4-{{ checksum "./mne_bids/__init__.py" }}
paths:
- ~/mne_data

Expand Down
6 changes: 4 additions & 2 deletions doc/whats_new.rst
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ filterwarnings =
ignore:Function mark_bad_channels is deprecated.*:DeprecationWarning
# This is for Python 3.10+ and MNE <1.0
ignore:The distutils package is deprecated.*:DeprecationWarning
# Python 3.10+ and NumPy 1.22 (and maybe also newer NumPy versions?)
ignore:.*distutils\.sysconfig module is deprecated.*:DeprecationWarning

[pydocstyle]
convention = pep257
Expand Down

0 comments on commit 0473a3e

Please sign in to comment.