Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"arrays used as indices must be of integer (or boolean) type" #126

Closed
sjyzhu opened this issue Mar 16, 2023 · 14 comments · Fixed by #181
Closed

"arrays used as indices must be of integer (or boolean) type" #126

sjyzhu opened this issue Mar 16, 2023 · 14 comments · Fixed by #181

Comments

@sjyzhu
Copy link

sjyzhu commented Mar 16, 2023

Describe the bug

When I was running "label_components", an error occurred:
"arrays used as indices must be of integer (or boolean) type"

Steps to reproduce

IDE: jupyter notebook
python 3.9.12
mne 1.3.1
mne-iclabel 0.4

raw_filt = raw_filt.filter(l_freq=1, h_freq=None)
ica = mne.preprocessing.ICA(n_components=8, method='picard', max_iter=10000) 
ica.fit(raw_filt)

ic_labels = label_components(raw_filt, ica, method="iclabel")

Actual results

Snipaste_2023-03-16_22-22-55

@adam2392
Copy link
Member

Can you use the version on main?

Also, the exemplar = np.mean(data_[similar_pts], axis=0) line is nowhere in mne-icalabel I think? Is there a missing error message?

@mscheltienne
Copy link
Member

Indeed a couple of missing error message, can you share the full traceback? Found the lines here

exemplar = np.mean(data_[similar_pts], axis=0)

Also, could you save the ICA decomposition with ica.save and share the ICA decomposition and the Raw file (you can save it with raw_filt.save)?

@mscheltienne
Copy link
Member

Arf, the traceback is incomplete because it's a Jupyter notebook, sneaky ... on the left side..

@sjyzhu
Copy link
Author

sjyzhu commented Mar 16, 2023

Thanks for your reply!
Below is the complete traceback
error

@sjyzhu
Copy link
Author

sjyzhu commented Mar 16, 2023

Can you use the version on main?

Sorry, I don't know what main means... = =

@sjyzhu
Copy link
Author

sjyzhu commented Mar 16, 2023

Also, could you save the ICA decomposition with ica.save and share the ICA decomposition and the Raw file (you can save it with raw_filt.save)?

I'm not sure if I can upload raw files, because these are from confidential patient data.

@mscheltienne
Copy link
Member

mscheltienne commented Mar 16, 2023

main means the not-yet-released code, i.e. the version available as of today on the GitHub repository. You can install it in an environment by doing pip install git+https://github.com/mne-tools/mne-icalabel. This is to eliminate the possibility of a bug that has already been fixed but has not yet been released.

I'm not sure if I can upload raw files, because these are from confidential patient data.

Unlucky, as it's difficult to debug if we can not reproduce the error.
Pinging @yjmantilla who authored the gdatav4 translation, by any chance does this error look familiar to you?

If you don't mind, I would like to give you a couple of short code snippets that I'd like you to run and return the output here to help us debug the issue further without providing us with the RAW/ICA files. Would that be OK with you?
Do you only use Jupyter or also other IDEs, and especially do you know how to use breakpoints and debug mode?

@sjyzhu
Copy link
Author

sjyzhu commented Mar 17, 2023

main means the not-yet-released code, i.e. the version available as of today on the GitHub repository. You can install it in an environment by doing pip install git+https://github.com/mne-tools/mne-icalabel. This is to eliminate the possibility of a bug that has already been fixed but has not yet been released.

The development version has been installed, and the same bug appeared.

If you don't mind, I would like to give you a couple of short code snippets that I'd like you to run and return the output here to help us debug the issue further without providing us with the RAW/ICA files. Would that be OK with you? Do you only use Jupyter or also other IDEs, and especially do you know how to use breakpoints and debug mode?

OK, I can debug locally through your code.
Is it possible to debug using .py files through vscode?

@mscheltienne
Copy link
Member

Thanks for the help, yes you could debug using VSCode, but if you are not familiar with breakpoints and debug mode, there is no gain compare to Jupyter.

In the following part, raw designs the Raw recording used to fit the ICA and ica the ICA decomposition. Here are the code snippet:


First let's get some information on your Raw recording, especially about the channels:

  1. MNE information

Paste in this thread the printed information:

from mne import sys_info
print (mne.sys_info())
  1. raw.info

Paste in this thread the printed information:

# remove identification information if present
raw.info["description"] = ""
raw.info["device_info"] = dict()
raw.info["experimenter"] = ""
raw.info["subject_info"] = dict()
raw.info["temp"] = None
# display information
print (raw.info)
  1. Montage

Paste in this thread the figure returned by:

raw.get_montage().plot()
  1. icawinv

Return the file icawinv.npy:

import numpy as np


# Compute icawinv
n_components = ica.n_components_
s = np.sqrt(ica.pca_explained_variance_)[:n_components]
u = ica.unmixing_matrix_ / s
v = ica.pca_components_[:n_components, :]
weights = (u * s) @ v
icawinv = np.linalg.pinv(weights)
del weights
# sanity-check
assert icawinv.shape[-1] == ica.n_components_
assert icawinv.ndim == 2
np.save("icawinv.npy", icawinv, allow_pickle=False)
  1. picks of the topoplot feature

Paste in this thread the printed information:

print (ica.ch_names)
  1. EEG channels coordinates in spherical coordinate system

Return the file rd.npy and th.npy:

import numpy as np

from mne_icalabel.iclabel.utils import _mne_to_eeglab_locs


rd, th = _mne_to_eeglab_locs(raw, ica.ch_names)
np.save("rd.npy", rd, allow_pickle=False)
np.save("th.npy", th, allow_pickle=False)

That should be enough for debug and for me to reproduce the error locally.

@sjyzhu
Copy link
Author

sjyzhu commented Apr 7, 2023

Thanks for the help, yes you could debug using VSCode, but if you are not familiar with breakpoints and debug mode, there is no gain compare to Jupyter.

Sorry for not getting back to you for such a long time.
After changing a batch of data, the issue did not occur again. Therefore, it should have been a problem with that particular batch of data.
Thank you very much for your patient guidance !

@sjyzhu sjyzhu closed this as completed Apr 7, 2023
@GABowers
Copy link

I'm getting this error. Test file is attached, and the test script is here:

import mne, mne_icalabel

if __name__ == "__main__":
    fn = './test_raw.fif'
    raw = mne.io.read_raw_fif(fn, preload=True)
    standard = mne.channels.make_standard_montage(kind = 'biosemi64')
    raw.set_montage(standard, on_missing='ignore')
    raw.set_eeg_reference(['LMA', 'RMA'])
    raw.filter(l_freq=1, h_freq=100, picks=None)
    ica = mne.preprocessing.ICA(n_components=0.999999, method='infomax', fit_params=dict(extended=True), max_iter='auto', random_state=1)
    ica.fit(raw, picks=['eeg'])
    ic_labels = mne_icalabel.label_components(raw, ica, method='iclabel')
    labels = ic_labels["labels"]
    exclude_idx = [idx for idx, label in enumerate(labels) if label not in ["brain", "other"]]
    ica.exclude = exclude_idx

test_raw.zip

@mscheltienne mscheltienne reopened this Apr 27, 2024
@mscheltienne
Copy link
Member

Thanks for the reproducible example, it makes debugging way simpler. The error occurs because of a nan in the x and y variables here:

myepsx = np.spacing(0.5 * (np.max(x) - np.min(x))) ** (1 / 3)
myepsy = np.spacing(0.5 * (np.max(y) - np.min(y))) ** (1 / 3)

This is due to raw.set_montage(standard, on_missing='ignore'), which ignored missing entries. But we actually need a montage with ALL electrodes coordinates to compute the topographic map feature. We could ignore missing channels in the interpolation function, further down the road, but I think we should also issue a warning further up the road. Here is a corrected code snippet for your file:

from mne.io import read_raw_fif
from mne.preprocessing import ICA
from mne_icalabel import label_components

fname = r"C:\Users\scheltie\Downloads\test_raw.fif"

raw = read_raw_fif(fname, preload=True)
raw.rename_channels({"Afz": "AFz"})
raw.set_montage("biosemi64")
raw.set_eeg_reference(["LMA", "RMA"])
raw.filter(l_freq=1, h_freq=100, picks=None)
raw.drop_channels(["LMA", "RMA"])
ica = ICA(
    n_components=0.999999,
    method="infomax",
    fit_params=dict(extended=True),
    max_iter="auto",
    random_state=1,
)
ica.fit(raw, picks=["eeg"])
ic_labels = label_components(raw, ica, method="iclabel")

I'll think about it and open a PR to close this issue.

@GABowers
Copy link

Okay great, thanks! So in general, I should try to set the montage without ignoring anything?

It looks like you drop the mastoids before ICA. Any particular reason? I tried it with and without that and it works either way.

@mscheltienne
Copy link
Member

mscheltienne commented Apr 27, 2024

Exactly, in this case to do so, one channel had to be renamed to match the name in the montage.

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 a pull request may close this issue.

4 participants