Coder Social home page Coder Social logo

Labels for Epoch objects about mne-icalabel HOT 9 CLOSED

mne-tools avatar mne-tools commented on September 15, 2024
Labels for Epoch objects

from mne-icalabel.

Comments (9)

mscheltienne avatar mscheltienne commented on September 15, 2024 1

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.

from mne-icalabel.

sourestdeeds avatar sourestdeeds commented on September 15, 2024 1

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

from mne-icalabel.

mscheltienne avatar mscheltienne commented on September 15, 2024

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?

from mne-icalabel.

mscheltienne avatar mscheltienne commented on September 15, 2024

Looks similar to the issue in #38

from mne-icalabel.

sourestdeeds avatar sourestdeeds commented on September 15, 2024

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)

from mne-icalabel.

mscheltienne avatar mscheltienne commented on September 15, 2024

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):

index = index.T.astype(int)

# 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")

from mne-icalabel.

mscheltienne avatar mscheltienne commented on September 15, 2024

Am I correct in assuming the sampling frequency of your data is 128 Hz?

from mne-icalabel.

sourestdeeds avatar sourestdeeds commented on September 15, 2024

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)

from mne-icalabel.

mscheltienne avatar mscheltienne commented on September 15, 2024

@sourestdeeds It's fixed on main and will be fixed in release >= 0.2.0.

from mne-icalabel.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.