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

Add the code from PR "Add support for MNE-ICALabel #812" #1018

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
44 changes: 44 additions & 0 deletions mne_bids_pipeline/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

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 param

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?

Copy link
Member

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 (as None is what we use now), and people wanting to use icalabel would need to set it to 100.

```
"""

icalabel_include: Annotated[
Copy link
Member

Choose a reason for hiding this comment

The 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 ica_ so this should be

Suggested change
icalabel_include: Annotated[
ica_icalabel_include: Annotated[

Sequence[
Literal[
"brain",
"muscle artifact",
"eye blink",
"heart beat",
"line noise",
"channel noise",
"other",
]
],
Len(1, 7),
Copy link
Member

Choose a reason for hiding this comment

The 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 unique pydantic workaround

pydantic/pydantic-core#820 (comment)

If you don't want to implement it here maybe add it as a # TODO: comment in the ICA preprocessing scripts somewhere? (Don't add it in this file as it would make it messier.)

Choose a reason for hiding this comment

The 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

Copy link
Member

Choose a reason for hiding this comment

The 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?

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 ["eye blink", "eye blink"]). But what you have here is already good enough!

] = ["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"
Expand Down
17 changes: 17 additions & 0 deletions mne_bids_pipeline/_config_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,21 @@ def _check_config(config: SimpleNamespace, config_path: PathLike | None) -> None
f"but got shape {destination.shape}"
)

# From: https://github.com/mne-tools/mne-bids-pipeline/pull/812
# MNE-ICALabel
if config.ica_use_icalabel:
if config.ica_l_freq != 1.0 or config.h_freq != 100.0:
raise ValueError(
f"When using MNE-ICALabel, you must set ica_l_freq=1 and h_freq=100, "
f"but got: ica_l_freq={config.ica_l_freq} and h_freq={config.h_freq}"
)

if config.eeg_reference != "average":
raise ValueError(
f'When using MNE-ICALabel, you must set eeg_reference="average", but '
f"got: eeg_reference={config.eeg_reference}"
)


def _default_factory(key: str, val: Any) -> Any:
# convert a default to a default factory if needed, having an explicit
Expand All @@ -350,6 +365,8 @@ def _default_factory(key: str, val: Any) -> Any:
{"custom": (8, 24.0, 40)}, # decoding_csp_freqs
["evoked"], # inverse_targets
[4, 8, 16], # autoreject_n_interpolate
# ["brain", "muscle artifact", "eye blink", "heart beat", "line noise", "channel noise", "other"], # icalabel_include
["brain", "other"],
]

def default_factory() -> Any:
Expand Down
23 changes: 22 additions & 1 deletion mne_bids_pipeline/steps/preprocessing/_06a1_fit_ica.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An equivalent check should probably live up in _config_import.py

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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
Loading
Loading