-
Notifications
You must be signed in to change notification settings - Fork 14
/
label_components.py
84 lines (69 loc) · 3.04 KB
/
label_components.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from typing import Optional, Union
import numpy as np
from mne import BaseEpochs
from mne.io import BaseRaw
from mne.preprocessing import ICA
from .features import get_iclabel_features
from .network import run_iclabel
def iclabel_label_components(
inst: Union[BaseRaw, BaseEpochs],
ica: ICA,
inplace: bool = True,
backend: Optional[str] = None,
):
"""Label the provided ICA components with the ICLabel neural network.
ICLabel is designed to classify ICs fitted with an extended infomax ICA
decomposition algorithm on EEG datasets referenced to a common average and
filtered between [1., 100.] Hz. It is possible to run ICLabel on datasets that
do not meet those specification, but the classification performance
might be negatively impacted. Moreover, the ICLabel paper did not study the
effects of these preprocessing steps.
ICLabel uses 3 features:
- Topographic maps, based on the ICA decomposition.
- Power Spectral Density (PSD), based on the ICA decomposition and the
provided instance.
- Autocorrelation, based on the ICA decomposition and the provided
instance.
For more information, see :footcite:t:`iclabel2019`.
Parameters
----------
inst : Raw | Epochs
Instance used to fit the ICA decomposition. The instance should be
referenced to a common average and bandpass filtered between 1 and
100 Hz.
ica : ICA
ICA decomposition of the provided instance. The ICA decomposition
should use the extended infomax method.
inplace : bool
Whether to modify the ``ica`` instance in place by adding the automatic
annotations to the ``labels_`` property. By default True.
backend : None | ``torch`` | ``onnx``
Backend to use to run ICLabel. If None, returns the first available backend in
the order ``torch``, ``onnx``.
Returns
-------
labels_pred_proba : numpy.ndarray of shape (n_components, n_classes)
The estimated corresponding predicted probabilities of output classes
for each independent component. Columns are ordered with 'brain',
'muscle artifact', 'eye blink', 'heart beat', 'line noise',
'channel noise', 'other'.
References
----------
.. footbibliography::
"""
features = get_iclabel_features(inst, ica)
labels_pred_proba = run_iclabel(*features, backend=backend) # type: ignore
if inplace:
from mne_icalabel.config import ICA_LABELS_TO_MNE
ica.labels_scores_ = labels_pred_proba
argmax_labels = np.argmax(labels_pred_proba, axis=1)
# add labels to the ICA instance
for idx, (_, mne_label) in enumerate(ICA_LABELS_TO_MNE.items()):
auto_labels = list(np.argwhere(argmax_labels == idx).flatten())
if mne_label not in ica.labels_:
ica.labels_[mne_label] = auto_labels
continue
for comp in auto_labels:
if comp not in ica.labels_[mne_label]:
ica.labels_[mne_label].append(comp)
return labels_pred_proba