From 93b08b4fc20eb80c94074ac6eed0a99968992ca2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20H=C3=B6chenberger?= Date: Tue, 26 Sep 2023 11:36:52 +0200 Subject: [PATCH 1/3] Correctly handle reading when BIDS channel order differs from Raw Fixes #1170 --- mne_bids/read.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/mne_bids/read.py b/mne_bids/read.py index a0ab4962e..f00de6f26 100644 --- a/mne_bids/read.py +++ b/mne_bids/read.py @@ -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 = { From f16f8aad57b301dce0147a80e14a537d85e3a08f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20H=C3=B6chenberger?= Date: Tue, 26 Sep 2023 15:21:30 +0200 Subject: [PATCH 2/3] Add changelog entry --- doc/whats_new.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/whats_new.rst b/doc/whats_new.rst index ae01b4e66..1daa65260 100644 --- a/doc/whats_new.rst +++ b/doc/whats_new.rst @@ -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 @@ -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 ` From 3c9fbafbe27804c019622c46e2c5278776cee56d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20H=C3=B6chenberger?= Date: Tue, 26 Sep 2023 15:46:56 +0200 Subject: [PATCH 3/3] Add test --- mne_bids/tests/test_read.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/mne_bids/tests/test_read.py b/mne_bids/tests/test_read.py index 39fb1ece6..1537fdb50 100644 --- a/mne_bids/tests/test_read.py +++ b/mne_bids/tests/test_read.py @@ -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 @@ -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):