Skip to content
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
```
"""

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!

Copy link
Member

Choose a reason for hiding this comment

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

Okay I finally came back to this! Things seem to be working at my end.

FYI some of the components have a kind of low probability... looked like in testing at one point one of the things labeled as blink had a probability of like 0.35 or so. So not sure if you want to add a threshold as suggested by @behinger above. But in the meantime I think this is working as intended, @behinger @jschepers feel free to try it and look etc!

] = ["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"]
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