<All picks must be < n_channels (231), got 231> Error received when trying to epoch RawArray

  • MNE version: 1.4.2
  • operating system: macOS 12

Created a raw array using some data I have and constructed the info object to go with it. When trying to epoch the data afterwords I was given the error “All picks must be < n_channels (231), got 231”. I have used MNE for experimental data before and never had an issue of using all the channels before, is there a step I’m missing or must I remove some channels to continue?

Hello,

Can you share the concerned code snippet and the full traceback?
You are selecting one more channel than you actually have.

Mathieu

I do have 231 channels, but I may be calling more somehow.

Code:

import mne
import pickle
import numpy as np
from lfpykit.eegmegcalc import NYHeadModel
import os

# Import head model for EEG lead locations

nyhead = NYHeadModel(nyhead_file=os.getenv('NP_LFPYKIT_HEAD_FILE', None))

# Load pkl file

file = open('/Users/scottmcelroy/A1_scz/A1_sim_data/v34_batch53_0_0_data.pkl', 'rb')
data = pickle.load(file)

# Set dipole location

nyhead.set_dipole_pos('parietal_lobe')
nyhead.find_closest_electrode()

# Convert sim data to be in EEG format

M = nyhead.get_transformation_matrix()
timeRange = [0, 5000]
timeSteps = [int(timeRange[0] / 0.05), int(timeRange[1] / 0.05)]

# Set params to be readable by MNE

sfreq = 2500
times = np.arange(0, 5000, 0.05)
ch_types = ['eeg']*231
ch_names = []
p = data['simData']['dipoleSum']
p = np.array(p).T
p = p[:, timeSteps[0]:timeSteps[1]]
t = np.arange(timeRange[0], timeRange[1], 0.05)

# Rotate dipole to head surface and convert to appropriate units

p = nyhead.rotate_dipole_to_surface_normal(p)
eeg = M @ p * 1e3  # [mV] -> [uV] unit conversion

# Set channel names

for x in range(0, 231):
    ch_names = ch_names + [str(x)]


# Create raw.info for MNE

info = mne.create_info(ch_names=ch_names, sfreq=sfreq, ch_types=ch_types)
info['description'] = 'sim_data_test'

# Create MNE Raw Array

raw = mne.io.RawArray(eeg, info)
raw.load_data()
epochs=mne.EpochsArray(raw, info)

Traceback:

runfile('/Users/scottmcelroy/GitHub/EEG_schizophrenia/sim_psd.py', wdir='/Users/scottmcelroy/GitHub/EEG_schizophrenia')
Creating RawArray with float64 data, n_channels=231, n_times=1500
    Range : 0 ... 1499 =      0.000 ...     0.075 secs
Ready.
Traceback (most recent call last):
  File "/Users/scottmcelroy/anaconda3/envs/dev/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3508, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-46-15f331849e66>", line 1, in <module>
    runfile('/Users/scottmcelroy/GitHub/EEG_schizophrenia/sim_psd.py', wdir='/Users/scottmcelroy/GitHub/EEG_schizophrenia')
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_bundle/pydev_umd.py", line 198, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/scottmcelroy/GitHub/EEG_schizophrenia/sim_psd.py", line 65, in <module>
    epochs=mne.EpochsArray(raw, info)
  File "<decorator-gen-290>", line 12, in __init__
  File "/Users/scottmcelroy/anaconda3/envs/dev/lib/python3.10/site-packages/mne/epochs.py", line 3261, in __init__
    dtype = np.complex128 if np.any(np.iscomplex(data)) else np.float64
  File "/Users/scottmcelroy/anaconda3/envs/dev/lib/python3.10/site-packages/numpy/lib/type_check.py", line 239, in iscomplex
    ax = asanyarray(x)
  File "/Users/scottmcelroy/anaconda3/envs/dev/lib/python3.10/site-packages/mne/io/base.py", line 834, in __getitem__
    return self._getitem(item)
  File "/Users/scottmcelroy/anaconda3/envs/dev/lib/python3.10/site-packages/mne/io/base.py", line 837, in _getitem
    sel, start, stop = self._parse_get_set_params(item)
  File "/Users/scottmcelroy/anaconda3/envs/dev/lib/python3.10/site-packages/mne/io/base.py", line 770, in _parse_get_set_params
    sel = _picks_to_idx(self.info, item[0])
  File "/Users/scottmcelroy/anaconda3/envs/dev/lib/python3.10/site-packages/mne/io/pick.py", line 1350, in _picks_to_idx
    raise ValueError(
ValueError: All picks must be < n_channels (231), got 231

So the traceback shows that the error originates from epochs = mne.EpochsArray(raw, info). But I don’t understand that part.

raw = mne.io.RawArray(eeg, info)
raw.load_data()
epochs=mne.EpochsArray(raw, info)

You create a raw from the (assumed) numpy array eeg. It should be of shape (n_channels, n_times), can you do print (eeg.shape) to confirm?
Then you call raw.load_data(), which does nothing here as a RawArray is preloaded by definition.
Then you create epochs through EpochsArray not from a numpy array as it’s suppose to but from raw. IMO, that last line can not work. EpochsArray is designed to create epochs from a numpy array of shape (n_epochs, n_channels, n_times).

You have 2 main ways to create epochs:

  • from a raw (continuous recording) by calling mne.Epochs. In this case, the measurement information (sensor name, types, location, …) is taken from raw.info.
  • from a numpy array by calling mne.EpochsArray (all the XXXArray class take a numpy array as input). In this case, the measurement information is not known and has to be provided separately as you do.

Mathieu

1 Like

eeg.shape is the correct shape. It looks that using EpochsArray is the issue, I thought because I had used an RawArray I would want to continue using EpochsArray. Using mne.Epochs seems to work, thank you!