Skip to content

Commit b513184

Browse files
FIX: correct sensor name ordering in plot_topomap when using Info object (#13686)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 0ab3f5f commit b513184

3 files changed

Lines changed: 29 additions & 0 deletions

File tree

doc/changes/dev/13686.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix sensor name ordering in :func:`~mne.viz.plot_topomap` when passing an :class:`~mne.Info` object as ``pos`` with ``names`` argument, by `Famous077`_.

mne/viz/tests/test_topomap.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,3 +993,29 @@ def test_plot_ch_adjacency():
993993
msg = "Editing a 3d adjacency plot is not supported."
994994
with pytest.raises(ValueError, match=msg):
995995
plot_ch_adjacency(info, adj, ch_names, kind="3d", edit=True)
996+
997+
998+
@pytest.mark.parametrize("ch_type", ("mag", "grad"))
999+
def test_plot_topomap_info_names_ordering(ch_type):
1000+
"""Regression test for GH-12700.
1001+
1002+
plot_topomap() must preserve correct sensor name ordering when
1003+
passing an Info object as pos with a names argument.
1004+
This must be tested with MEG data including gradiometers, since the
1005+
bug only affected the gradiometer code path.
1006+
"""
1007+
evoked = read_evokeds(evoked_fname, baseline=(None, 0))[0]
1008+
evoked = evoked.copy().crop(0, 0).pick(picks=ch_type)
1009+
info = evoked.info
1010+
data = evoked.data[:, 0]
1011+
names = info["ch_names"]
1012+
if ch_type == "grad":
1013+
# grad pairs are merged so only every other name is displayed
1014+
expected_names = names[::2]
1015+
else:
1016+
expected_names = names
1017+
im, _ = plot_topomap(data, info, names=names, show=False)
1018+
displayed_names = [t.get_text() for t in im.axes.texts]
1019+
assert displayed_names == list(expected_names), (
1020+
f"Expected {list(expected_names)}, got {displayed_names}"
1021+
)

mne/viz/topomap.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,6 +1292,8 @@ def _plot_topomap(
12921292
pos = _find_topomap_coords(pos, picks=picks[::2], sphere=sphere)
12931293
data, _ = _merge_ch_data(data[picks], ch_type, [])
12941294
data = data.reshape(-1)
1295+
if names is not None:
1296+
names = [names[p] for p in picks[::2]]
12951297
else:
12961298
picks = list(range(data.shape[0]))
12971299
pos = _find_topomap_coords(pos, picks=picks, sphere=sphere)

0 commit comments

Comments
 (0)