How to deal with "Data and pos need to be of same length." error while using mne.viz.plot_topomap

  • MNE-Python version: 0.22.0
  • operating system: Ubuntu 20.04.02

I am following this tutorial link.

I don’t get an error while using the sample data. But when I am using my data, and selecting grad channels for the normalization I am getting error in the following line

plot_topomap(alpha_pw, raw.info, cmap=cm.viridis, contours=0);

ValueError: Data and pos need to be of same length. Got data of length 101, pos of length 100

Here the alpha_pw contains PSD for selected channels in the alpha band. raw is modified with pick channels, to select only grad.

Help of any kind is appreciated.

If it works for the sample data but fails with your own data, it will be very difficult for anyone to help you debug if they don’t have the data that causes the error. Can you share a link to a file that causes the ValueError, and a code sample that is complete (someone can copy/paste in its entirety and get the same ValueError you see) but also as minimal as possible (all extraneous steps removed)?

1 Like

Might not be able to share the data.
What causes this error? The operations done on raw are using mne functions. POS data is modified by pick_channels.
If I were to debug it myself, where should I start?

Not knowing any dimensions of your data this is a total guess but: might you have rejected a channel somewhere along the way?

1 Like

To debug this yourself, try to execute your line/steps one at a time and systematically check the dimension of your data to find where one channel is dropped. To check the dimension, you can get the numpy array and check with .shape. To get the numpy array, you can used e.g. raw.get_data(picks='all').

OK, then what about sharing a minimal, complete example at least? If it’s an error in your code, we might be able to spot it if we could see your code.

The first 2 arguments to plot_topomap are data and pos. They need to be the same length, and apparently in your data they are not the same length.

Thanks to everyone.
Your replies helped me debug it. I have been rejecting bad channels based on my initial analysis. And was rejecting two unpaired grad channels. I have rewritten to reject both grad channels, if one of the pair is found bad. That solved the issue.