-
Notifications
You must be signed in to change notification settings - Fork 69
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
Add the code from PR "Add support for MNE-ICALabel #812" #1018
base: main
Are you sure you want to change the base?
Changes from all commits
2922224
3338807
4bf3976
8eaf750
8ca9132
5b4d14e
fd304e1
ede148b
7d2a729
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1376,19 +1376,63 @@ | |||||
`1` or `None` to not perform any decimation. | ||||||
""" | ||||||
|
||||||
ica_use_ecg_detection: bool = True | ||||||
""" | ||||||
Whether to use the MNE ECG detection on the ICA components. | ||||||
""" | ||||||
|
||||||
ica_ecg_threshold: float = 0.1 | ||||||
""" | ||||||
The cross-trial phase statistics (CTPS) threshold parameter used for detecting | ||||||
ECG-related ICs. | ||||||
""" | ||||||
|
||||||
ica_use_eog_detection: bool = True | ||||||
""" | ||||||
Whether to use the MNE EOG detection on the ICA components. | ||||||
""" | ||||||
|
||||||
ica_eog_threshold: float = 3.0 | ||||||
""" | ||||||
The threshold to use during automated EOG classification. Lower values mean | ||||||
that more ICs will be identified as EOG-related. If too low, the | ||||||
false-alarm rate increases dramatically. | ||||||
""" | ||||||
|
||||||
|
||||||
# From: https://github.com/mne-tools/mne-bids-pipeline/pull/812 | ||||||
ica_use_icalabel: bool = False | ||||||
""" | ||||||
Whether to use MNE-ICALabel to automatically label ICA components. Only available for | ||||||
EEG data. | ||||||
!!! info | ||||||
Using MNE-ICALabel mandates that you also set: | ||||||
```python | ||||||
eeg_reference = "average" | ||||||
ica_l_freq = 1 | ||||||
h_freq = 100 | ||||||
``` | ||||||
""" | ||||||
|
||||||
icalabel_include: Annotated[ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think to be consistent all ICA-related stuff should be prefixed by
Suggested change
|
||||||
Sequence[ | ||||||
Literal[ | ||||||
"brain", | ||||||
"muscle artifact", | ||||||
"eye blink", | ||||||
"heart beat", | ||||||
"line noise", | ||||||
"channel noise", | ||||||
"other", | ||||||
] | ||||||
], | ||||||
Len(1, 7), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we are really going for broke on this one, we should use the pydantic/pydantic-core#820 (comment) If you don't want to implement it here maybe add it as a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dont think I can really follow you there. Maybe something got lost, but what does "going for broke" mean? @jschepers I dont actually see where we really select components to be excluded, but in their tutorial it is also quite confusing. I remember we looked at it, but it was some time ago. E.g. in eeglab you specify "remove muscle if probability >80%" and similar. How is this done here, do you remember? Else I will try to give this another spin in debug mode There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Sorry it's just an idiom -- in this case I mean if you want to put forth potentially a lot of effort to try to come up with a more complete/cool solution, you could. What we want here in not just a list with length between 1 and 7 with elements from a set of possible choices, but rather that they are unique elements (i.e., a user shouldn't put in |
||||||
] = ["brain", "other"] | ||||||
""" | ||||||
Which independent components (ICs) to keep based on the labels given by ICLabel. | ||||||
Possible labels are "brain", "muscle artifact", "eye blink", "heart beat", "line noise", "channel noise", "other". | ||||||
""" | ||||||
|
||||||
# ### Amplitude-based artifact rejection | ||||||
# | ||||||
# ???+ info "Good Practice / Advice" | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,6 +79,14 @@ def run_ica( | |
"""Run ICA.""" | ||
import matplotlib.pyplot as plt | ||
|
||
if cfg.ica_use_icalabel: | ||
# The ICALabel network was trained on extended-Infomax ICA decompositions fit | ||
# on data flltered between 1 and 100 Hz. | ||
assert cfg.ica_algorithm in ["picard-extended_infomax", "extended_infomax"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An equivalent check should probably live up in |
||
assert cfg.ica_l_freq == 1.0 | ||
assert cfg.h_freq == 100.0 | ||
assert cfg.eeg_reference == "average" | ||
|
||
raw_fnames = [in_files.pop(f"raw_run-{run}") for run in cfg.runs] | ||
out_files = dict() | ||
bids_basename = raw_fnames[0].copy().update(processing=None, split=None, run=None) | ||
|
@@ -164,7 +172,18 @@ def run_ica( | |
|
||
# Set an EEG reference | ||
if "eeg" in cfg.ch_types: | ||
projection = True if cfg.eeg_reference == "average" else False | ||
if cfg.ica_use_icalabel: | ||
assert cfg.eeg_reference == "average" | ||
projection = False # Avg. ref. needs to be applied for MNE-ICALabel | ||
elif cfg.eeg_reference == "average": | ||
projection = True | ||
else: | ||
projection = False | ||
|
||
if not projection: | ||
msg = "Applying average reference to EEG epochs used for ICA fitting." | ||
logger.info(**gen_log_kwargs(message=msg)) | ||
|
||
epochs.set_eeg_reference(cfg.eeg_reference, projection=projection) | ||
|
||
ar_reject_log = ar_n_interpolate_ = None | ||
|
@@ -338,10 +357,12 @@ def get_config( | |
ica_max_iterations=config.ica_max_iterations, | ||
ica_decim=config.ica_decim, | ||
ica_reject=config.ica_reject, | ||
ica_use_icalabel=config.ica_use_icalabel, | ||
autoreject_n_interpolate=config.autoreject_n_interpolate, | ||
random_state=config.random_state, | ||
ch_types=config.ch_types, | ||
l_freq=config.l_freq, | ||
h_freq=config.h_freq, | ||
epochs_decim=config.epochs_decim, | ||
raw_resample_sfreq=config.raw_resample_sfreq, | ||
event_repeated=config.event_repeated, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might want to add a
ica_h_freq
paramThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that would be a breaking change then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nah doesn't have to be -- we can use
ice_h_freq=None
as the default (asNone
is what we use now), and people wanting to use icalabel would need to set it to 100.