Skip to content

FIX: correct sensor name ordering in plot_topomap when using Info object#13686

Open
Famous077 wants to merge 21 commits intomne-tools:mainfrom
Famous077:fix/plot-topomap-sensor-names
Open

FIX: correct sensor name ordering in plot_topomap when using Info object#13686
Famous077 wants to merge 21 commits intomne-tools:mainfrom
Famous077:fix/plot-topomap-sensor-names

Conversation

@Famous077
Copy link
Contributor

@Famous077 Famous077 commented Feb 25, 2026

Reference issue (if any)

What does this implement/fix?

--> When passing an Info object as pos to mne.viz.plot_topomap() with the
names argument, the sensor names were not reordered to match the
reordered channel positions. This caused a mismatch between displayed
names and their actual positions on the topomap.

The fix reorders the names array to match the picks ordering when:

  • Regular channels: names reordered to match picks
  • Gradiometer pairs: names reordered to match picks[::2]

Additional information

--> The data values were always correct , only the displayed sensor names
were mismatched. The fix is minimal and targeted, only affecting the
names reordering when pos is an Info object.

: This PR was developed with the assistance of an AI coding assistant for guidance on code structure, docstring formatting, and implementation approach.

@Famous077
Copy link
Contributor Author

Famous077 commented Feb 25, 2026

Hi @agramfort , Would appreciate your review when you get a chance. Happy to update based on your feedbac.k. Thank you

Copy link
Member

@larsoner larsoner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add/update some test that would fail on main but pass on this PR?

Also can you please disclose AI usage for this PR?

@@ -0,0 +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 <https://github.com/Famous077>`__. No newline at end of file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need this file, the other is enough

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HI @larsoner , Thank you for the review. I have addressed all the feedbacks:

-Removed the duplicate changelog file 12700.bugfix.rst as suggested
-Updated 13686.bugfix.rst to use :newcontrib: format
-Added regression test test_plot_topomap_info_names_ordering that verifies correct sensor name ordering when passing an Info object as pos with a names argument

Please let me know if anything else needs to be changed.

@Famous077 Famous077 requested a review from larsoner March 2, 2026 19:48
Comment on lines +998 to +1002
import numpy as np

from mne import create_info
from mne.channels import make_standard_montage
from mne.viz import plot_topomap
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of these should be nested (see how we don't nest things in other tests unless they are non-required dependencies)

@Famous077
Copy link
Contributor Author

@larsoner Update done. Can you review it again ? Waiting for you feedbacks.

@Famous077 Famous077 requested a review from larsoner March 5, 2026 02:53
# This must not raise and must display correct sensor names
fig, _ = plot_topomap(data, info, names=names, show=False)
assert fig is not None
plt.close("all")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this, we auto-close mpl figures at the end of all tests

Suggested change
plt.close("all")


# This must not raise and must display correct sensor names
fig, _ = plot_topomap(data, info, names=names, show=False)
assert fig is not None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to go the extra mile, you could look at fig.axes[0].texts and .get_text() from the objects and make sure the names match the expected ones!

@Famous077 Famous077 requested a review from larsoner March 5, 2026 19:15
@Famous077
Copy link
Contributor Author

Hi @larsoner, changes have been made according to your suggestions. let me know if anything else needs to be implement. Waiting for you feedback.

@Famous077
Copy link
Contributor Author

Hi @larsoner, I have been working on the 4 failing CI jobs:

  • All imports in my test are already at module level — no nested imports.
  • My new test test_plot_topomap_info_names_ordering passes locally.
  • The 2 failing tests (test_plot_topomap_basic, test_plot_topomap_nirs_ica) also fail on main without my changes, which confirm that they are pre-existing issues unrelated to this PR.

Please let me know if anything else needs to be addressed. Thank you

@tsbinns
Copy link
Contributor

tsbinns commented Mar 6, 2026

@Famous077 The logs show only test_plot_topomap_basic, but that it not failing on main: https://github.com/mne-tools/mne-python/actions/runs/22636326990

It is failing because maptlotlib raises a warning about too many figures being opened at once, and in our docs build, warnings are treated as errors.

This comment about not needing to close figures only applies at the end of tests, but may be required between plotting calls within tests to prevent too many figures existing.

Please revert all the plt.close("all") statements you dropped from the otherwise-untouched existing tests in 14833a9.

@Famous077
Copy link
Contributor Author

Famous077 commented Mar 6, 2026

Hi @tsbinns @drammock , Changes have been done as per your suggestion . Can you review it whenever you get time. Waiting for your feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants