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: Correctly handle reading when BIDS channel order differs from Raw #1171

Merged
merged 3 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ The following authors contributed for the first time. Thank you so much! 🤩

The following authors had contributed before. Thank you for sticking around! 🤘

* `Richard Höchenberger`_
* `Stefan Appelhoff`_

Detailed list of changes
Expand All @@ -44,7 +45,7 @@ Detailed list of changes
🪲 Bug fixes
^^^^^^^^^^^^

- nothing yet
- Fix reading when the channel order differs between ``*_channels.tsv`` and the raw data file, which would previously throw an error, by `Richard Höchenberger`_ (:gh:`1171`)

:doc:`Find out what was new in previous releases <whats_new_previous_releases>`

Expand Down
4 changes: 1 addition & 3 deletions mne_bids/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,9 +642,7 @@ def _handle_channels_reading(channels_fname, raw):
f"set channel names."
)
else:
for bids_ch_name, raw_ch_name in zip(ch_names_tsv, raw.ch_names.copy()):
if bids_ch_name != raw_ch_name:
raw.rename_channels({raw_ch_name: bids_ch_name})
raw.rename_channels(dict(zip(raw.ch_names, ch_names_tsv)))

# Set the channel types in the raw data according to channels.tsv
channel_type_bids_mne_map_available_channels = {
Expand Down
15 changes: 14 additions & 1 deletion mne_bids/tests/test_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -1258,7 +1258,7 @@ def test_ignore_exclude_param(tmp_path):
@pytest.mark.filterwarnings(warning_str["channel_unit_changed"])
@testing.requires_testing_data
def test_channels_tsv_raw_mismatch(tmp_path):
"""Test behavior when channels.tsv contains channels not found in raw."""
"""Test mismatch between channels in channels.tsv and raw."""
bids_path = _bids_path.copy().update(root=tmp_path, datatype="meg", task="rest")

# Remove one channel from the raw data without updating channels.tsv
Expand Down Expand Up @@ -1311,6 +1311,19 @@ def test_channels_tsv_raw_mismatch(tmp_path):
):
read_raw_bids(bids_path)

# Test mismatched channel ordering between channels.tsv and raw
raw = _read_raw_fif(raw_fname, verbose=False)
write_raw_bids(raw, bids_path=bids_path, overwrite=True, verbose=False)

ch_names_orig = raw.ch_names.copy()
ch_names_new = ch_names_orig.copy()
ch_names_new[1], ch_names_new[0] = ch_names_new[0], ch_names_new[1]
raw.reorder_channels(ch_names_new)
raw.save(raw_path, overwrite=True)

raw = read_raw_bids(bids_path)
assert raw.ch_names == ch_names_orig


@testing.requires_testing_data
def test_file_not_found(tmp_path):
Expand Down