Ecog: Locating electrodes error

Hello everyone,

I am having issues with locating electrodes in my ecog data, following the tutorial in MNE’s webpage.

First of all, MNE and OS versions are:

- MNE version: e.g. 0.24.1
- operating system: Ubuntu 24

And the code I am using is:

    raw = mne.io.read_raw_cnt(
        '/place/where/ecog/is/located/ecog_file.cnt')
    # estimate head->mri transform
    CT = nibabel.load('/place/where/CT/file/is/CT_file.nii')
    T1 = nibabel.load('/place/where/T1/is/fsaverage/subject/mri/T1.mgz')
    reg_affine, _ = mne.transforms.compute_volume_registration(
        CT, T1, pipeline='rigids')
    CT_aligned = mne.transforms.apply_volume_registration(CT, T1, reg_affine)

    subj_trans = mne.coreg.estimate_head_mri_t('subject', '/place/where/fsaverage/is/fsaverage', verbose= 5)

    gui = mne.gui.locate_ieeg(raw.info, subj_trans, CT_aligned, subject='subject', subjects_dir= 'place/where/fsaverage/is/fsaverage', verbose= 5)

However, I get an error message at the line where variable “gui” is, as follows:

  gui = mne.gui.locate_ieeg(raw.info, subj_trans, CT_aligned, subject='subject', subjects_dir= 'place/where/fsaverage/is/fsaverage', verbose= 5)
  File "<decorator-gen-567>", line 22, in locate_ieeg
  File "/home/beyond/.conda/envs/Ecog_EEG_MEG/lib/python3.9/site-packages/mne/gui/__init__.py", line 319, in locate_ieeg
    gui = IntracranialElectrodeLocator(
  File "/home/beyond/.conda/envs/Ecog_EEG_MEG/lib/python3.9/site-packages/mne/gui/_ieeg_locate_gui.py", line 168, in __init__
    self._plot_images()
  File "/home/beyond/.conda/envs/Ecog_EEG_MEG/lib/python3.9/site-packages/mne/gui/_ieeg_locate_gui.py", line 300, in _plot_images
    ct_data = np.take(self._ct_data, self._current_slice[axis],
  File "<__array_function__ internals>", line 180, in take
  File "/home/beyond/.conda/envs/Ecog_EEG_MEG/lib/python3.9/site-packages/numpy/core/fromnumeric.py", line 190, in take
    return _wrapfunc(a, 'take', indices, axis=axis, out=out, mode=mode)
  File "/home/beyond/.conda/envs/Ecog_EEG_MEG/lib/python3.9/site-packages/numpy/core/fromnumeric.py", line 57, in _wrapfunc
    return bound(*args, **kwds)
IndexError: index -806 is out of bounds for axis 0 with size 256

The main error message is: IndexError: index -806 is out of bounds for axis 0 with size 256

Anyone has any idea?

Thank you for your time.

Needle

Hi, It seems that there are already some coordinates in your raw data. subj_trans is the transform matrix from head coordinate to mri(aka the Surface RAS) coordinate, which means the coordinates inside ECoG data(your raw) should be in “head” coordinates, this is required by Neuromag.
To be short, you need to make sure the coordinate frame of your raw is the head coordinate frame.
If you don’t want this, you could clear the montage in your raw after loading the ECoG.
Maybe this would help.

raw.set_montage(None)
1 Like

Hello Barry,

thank you very much for your answer.

However, I may be getting this wrong but please correct me if I am, I would suspect that if that were the case as you say, if I would do rawfile.get_montage(), I would get something. Except I do not, the result is “None”.

If I am correct then the issue is something else am I right?

Thank you

Yes, you’re right.
check

raw.info['chs']

again, and see whether the loc element is Nan.

1 Like

Thank you for the prompt reply.

Good one on
raw.info['chs']

It seems I do have loc, yet the loc array, for each item has always the last 9 values as Nan, as you can see here as an example:

'loc': array([-0.93406069, 0.26896548, 0.23492166, nan, nan, nan, nan, nan, nan, nan, nan, nan]

Could this be the root of the issue?

I suppose only if you do raw.set_montage(), then you would get the montage by calling raw.get_montage().
Otherwise, you need to check raw.info[‘chs’].
In my opinion, this would be 99.99% the cause.
This is my output.


So still, try raw.set_montage(None) and check raw.info[‘chs’] again.

1 Like

Also, you might need to ask the clinicians why the data has this info.
I’m not familiar with ECoG so just a suggection.

1 Like

I had a meeting, apologies for tardy answer on my part.

I see, so in your case, since you are still to set the montage with the normal process as described i the MNE tutorial, you get all Nan, as all it is still to be defined.

You are right, I could eitehr set all to Nan with
raw.set_montage()
Or I could ask the clinicians why it is only half filled from the start.

I should probably follow the second solution.

I shall do that now and get back to you in case the problem continues. If it is resolved, I shall point you as the solver of this problem.

1 Like

I am back, I checked with them.

It is something their devices do, “sometimes”, which means, they do not know why, but it should not be there.

I will set this to Nan as you suggested. I checked and it works.

I will run my pipeline, which takes sometime and check back with you in case you were right and so on.

Seems it worked in the sense that, it runs now until the gui step to locate the electrodes, but the gui opens for a second as a black window, then quickly closes.

Not sure if related to anything we have done here, but I will check it for myself for now I suppose, as this is not related to this specific issue anymore.

Thank you Barry for your resolution, it worked indeed.

Bravo, thank you

How do you run the code?
In the terminal and run python main.py?
Or in jupyter notebook?
Also, you could save the aligned CT in Nifti format so you don’t need to do the alignment every time.
My solution is to align all the patients’ T1 and CT and save them using a script, then do locating.
You could save the aligned CT by

nib.save(ct_aligned, 'aligned_CT.nii')

I run in Pycharm.

Probably right, I am testing it, I forgot to save it. Good suggestion, will do