Closed sourestdeeds closed 2 years ago
Hello, thank you for reporting the issue! but I can not reproduce based on your description..
from mne import make_fixed_length_epochs
from mne.datasets import sample
from mne.io import read_raw
from mne.preprocessing import ICA
from mne_icalabel import label_components
# load raw instance
directory = sample.data_path() / "MEG" / "sample"
fname = "sample_audvis_filt-0-40_raw.fif"
raw = read_raw(directory / fname, preload=False)
raw.pick_types(eeg=True)
raw.crop(tmin=0, tmax=20)
raw.load_data()
# create epochs
epochs = make_fixed_length_epochs(raw, duration=1., preload=True)
# fit an ICA on epochs
ica = ICA(n_components=5, method="picard")
ica.fit(epochs)
# run ICLabel
label_components(epochs, ica, method="iclabel")
Outputs:
>>> {'y_pred_proba': array([0.96021754, 0.4062748 , 0.92534626, 0.95375717, 0.5524776 ],
dtype=float32),
'labels': ['eye blink', 'other', 'brain', 'brain', 'other']}
Could you try to reproduce the issue by using a dataset from the MNE sample dataset as I did above, or could you share a minimally reproducible example with the attached files that raises the error?
Looks similar to the issue in #38
If it helps I'm having a look at the line this is caused by. The shape of my raw and epochs are
epochs.get_data().shape
(909, 122, 513)
raw.get_data().shape
(122, 584600)
The index variable calculated in _eeg_rpsd_constants
for raw and epochs is
# raw
(128, 9133)
# epochs
(128, 7)
It's difficult to debug if I can not reproduce the error. Could you share some script and files to reproduce it?
For the shapes, with the example from the MNE sample dataset, I have:
epochs.get_data().shape
>>> (20, 59, 150)
raw.get_data().shape
>>> (59, 3004)
Which looks similar to you, assuming you do have 122 EEG channels and 909 epochs.
And for the index variables in _eeg_rpsd_constants
, breaking on line 274 (executing it): https://github.com/mne-tools/mne-icalabel/blob/ecc6a917e9540acdaf148845a145c882fcfc3b26/mne_icalabel/iclabel/features.py#L274
# with inst as Epochs
>>> (150, 1)
# with inst as Raw
>>> (150, 39)
It's difficult to say if it's normal or not as it depends on the number of samples in your data and on the sampling frequency.
What happens if you try to re-fit a fresh ICA instance on the epochs?
from mne.preprocessing import ICA
from mne_icalabel import label_components
ica = ICA(n_components=5, method="picard") # just to speed-up
ica.fit(epochs)
label_components(raw, ica, method="iclabel")
Am I correct in assuming the sampling frequency of your data is 128 Hz?
Yeah, I downsampled the raw to 128Hz and then went
raw.resample(128)
raw.notch_filter(freqs=None, method='spectrum_fit', n_jobs=12)
raw.filter(7, 35, n_jobs=12)
raw.set_eeg_reference()
events, event_ids = mne.events_from_annotations(
raw, verbose = False)
epochs = mne.Epochs(
raw=raw,
events=events,
event_id=event_ids,
preload=True,
tmin=0,
tmax=4,
baseline=None,
event_repeated='merge',
)
ica = mne.preprocessing.ICA(
method='picard', fit_params=dict(extended=True, ortho=False)
)
ica.fit(epochs)
# ICA Labels
ic_labels = label_components(raw, ica, method='iclabel') # epochs object causes error
labels = ic_labels["labels"]
exclude_idx = [idx for idx, label in enumerate(labels)
if label not in ["brain"]]
print(f"Excluding these ICA components: {exclude_idx}")
# Apply ICA
ica.plot_overlay(epochs.average(), exclude=exclude_idx)
ica.apply(epochs, exclude=exclude_idx)
Thank you! I managed to reproduce it with this sample, changing the duration of epochs to 4 seconds:
from mne import make_fixed_length_epochs
from mne.datasets import sample
from mne.io import read_raw
from mne.preprocessing import ICA
from mne_icalabel import label_components
# load raw instance
directory = sample.data_path() / "MEG" / "sample"
fname = "sample_audvis_filt-0-40_raw.fif"
raw = read_raw(directory / fname, preload=False)
raw.pick_types(eeg=True)
raw.crop(tmin=0, tmax=20)
raw.load_data()
# create epochs
epochs = make_fixed_length_epochs(raw, duration=4., preload=True)
# fit an ICA on epochs
ica = ICA(n_components=5, method="picard")
ica.fit(epochs)
# run ICLabel
label_components(epochs, ica, method="iclabel")
I'll have a look.
@sourestdeeds It's fixed on main and will be fixed in release >= 0.2.0
.
Thats amazing! Thanks so much, my automated workflow is working super well now.
Hopefully i can use this to get some classification results on my super noisy data
Describe the bug
Upon fitting with ICA on an epochs instance, i pass in the object (along with a fitted ica instance on the same epoch object) using
The error generated is
If I instead use the following raw instance to classify the epochs it works, is this intended? Essentially I'm performing ICA after epoching, so I'm not sure how using the fitted ica on epoch data and then using the raw to compare this with is correct.
Perhaps I have some funky rank deficiency going on somewhere from interpolation or something?