plot_topo doesn't work for custom MEG layout

Open
#11,732 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
python

Research direction

Start by running the provided reproduction with mne.channels.generate_2d_layout and simulated_evoked.plot_topo, comparing channel names with and without spaces. Trace the plot_topo and generate_2d_layout entry points to find where custom layout names are matched; done means the spaced-name layout produces the same non-empty figure as the continuous-name layout.

Written by the indexing model from the issue text.

Description

BUG VIZ
Description of the problem

The issue is described in:
https://mne.discourse.group/t/converting-gradiometers-to-virtual-magnetometers-for-neuromag-122/7056/6?u=dasdiptyajit

In short:
During creating 2D custom layout, the API mne.channels.generate_2d_layout expects a continuous strings (e.g., without a space) for a channel name. For example, if the channel name is MAG 000 instead of MAG000, API plot_topo doesn’t work (i.e., creates an empty figure)

Steps to reproduce
"""
Test case: create custom layout and plot topoplot of evoked data
"""

# modules
import os
import numpy as np
import mne

# Create simulated evoked with 5 magnetometers
n_channels = 5
sampling_freq = 1000  # in Hertz
ch_names = [f"MAG{n:03}" for n in range(n_channels)]
ch_types = ["mag"] * n_channels
info = mne.create_info(ch_names, ch_types=ch_types, sfreq=sampling_freq)
times = np.linspace(0, 0.5, sampling_freq, endpoint=False)
data = np.random.rand(n_channels, len(times)) * 1e-12  # mag channels scale
simulated_evoked = mne.EvokedArray(data, info)
simulated_evoked.filter(1., 40.)


# define meg sample data
sample_data_folder = mne.datasets.sample.data_path()
sample_data_fname = os.path.join(sample_data_folder, "MEG", "sample", 'sample_audvis-ave.fif')
evk_vec_mag = mne.read_evokeds(sample_data_fname)[0]
evk_vec_mag = evk_vec_mag.copy().pick_types(meg='mag')

# create a custom layout for simulated evoked,
# we will first extract xy coordinates from sample data

chs = evk_vec_mag.info["chs"][:n_channels]  # extract only 5 channels positions
xy_pos = np.empty((len(chs), 2))
for ci, ch in enumerate(chs):
    xy_pos[ci] = ch["loc"][:2]

# we need picks indices for 2D layout
picks = mne.pick_channels(ch_names=info["ch_names"], include=[]).tolist()

# create layout with original channel names and plot topomap
custom_lay1 = mne.channels.generate_2d_layout(xy=xy_pos, ch_names=ch_names, ch_indices=picks,
                                              name='custom_layout1', normalize=True)
custom_lay1.plot()
simulated_evoked.plot_topo(layout=custom_lay1)

# channel names with spaces # error case
ch_names_spaces = [f"MAG {n:03}_v" for n in range(n_channels)]  # error occurs

# create layout with modified channel names and plot topomap
custom_lay2 = mne.channels.generate_2d_layout(xy=xy_pos, ch_names=ch_names_spaces, ch_indices=picks,
                                              name='custom_layout2', normalize=True)
custom_lay2.plot()
simulated_evoked.plot_topo(layout=custom_lay2)
Link to data

No response

Expected results

simulated_evoked.plot_topo(layout=custom_lay2) should return similar results as in simulated_evoked.plot_topo(layout=custom_lay1)

Actual results

simulated_evoked.plot_topo(layout=custom_lay2) returns an empty figure.

Additional information

Step1: Simulates some evoked data for magnetometers (channel locations are missing)
Setp2: Extracts xy coordinates from MNE example data to create a layout for simulated data
Step3: tests the plot_topo API with 2 custom layouts. (one with original channel names and another with spaces)

Dominant language
Python
Stars
3.5k
Forks
1.6k
Avg merge
1d 6h
Merged PRs (30d)
100

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from mne-tools/mne-python

All issues in mne-tools/mne-python

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.