Skip to content

Commit

Permalink
Fix cleaning of channel names which can create duplicate for non vect…
Browse files Browse the repository at this point in the history
…orview or CTF systems (mne-tools#12489)
  • Loading branch information
Mathieu Scheltienne authored Mar 11, 2024
1 parent d7fdcb0 commit 9006789
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
1 change: 1 addition & 0 deletions doc/changes/devel/12489.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix cleaning of channel names for non vectorview or CTF dataset including whitespaces or dash in their channel names, by `Mathieu Scheltienne`_.
11 changes: 6 additions & 5 deletions mne/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,8 @@ def running_subprocess(command, after="wait", verbose=None, *args, **kwargs):
def _clean_names(names, remove_whitespace=False, before_dash=True):
"""Remove white-space on topo matching.
This function handles different naming
conventions for old VS new VectorView systems (`remove_whitespace`).
Also it allows to remove system specific parts in CTF channel names
This function handles different naming conventions for old VS new VectorView systems
(`remove_whitespace`) and removes system specific parts in CTF channel names
(`before_dash`).
Usage
Expand All @@ -281,7 +280,6 @@ def _clean_names(names, remove_whitespace=False, before_dash=True):
# for CTF
ch_names = _clean_names(epochs.ch_names, before_dash=True)
"""
cleaned = []
for name in names:
Expand All @@ -292,7 +290,10 @@ def _clean_names(names, remove_whitespace=False, before_dash=True):
if name.endswith("_v"):
name = name[:-2]
cleaned.append(name)

if len(set(cleaned)) != len(names):
# this was probably not a VectorView or CTF dataset, and we now broke the
# dataset by creating duplicates, so let's use the original channel names.
return names
return cleaned


Expand Down
15 changes: 14 additions & 1 deletion mne/utils/tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import pytest

import mne
from mne.utils import catch_logging, run_subprocess, sizeof_fmt
from mne.utils import _clean_names, catch_logging, run_subprocess, sizeof_fmt


def test_sizeof_fmt():
Expand Down Expand Up @@ -144,3 +144,16 @@ def remove_traceback(log):
other = stdout
assert std == want
assert other == ""


def test_clean_names():
"""Test cleaning names on OPM dataset.
This channel name list is a subset from a user OPM dataset reported on the forum
https://mne.discourse.group/t/error-when-trying-to-plot-projectors-ssp/8456
where the function _clean_names ended up creating a duplicate channel name L108_bz.
"""
ch_names = ["R305_bz-s2", "L108_bz-s77", "R112_bz-s109", "L108_bz-s110"]
ch_names_clean = _clean_names(ch_names, before_dash=True)
assert ch_names == ch_names_clean
assert len(set(ch_names_clean)) == len(ch_names_clean)

0 comments on commit 9006789

Please sign in to comment.