[074d3d]: / examples / preprocessing / ica_comparison.py

Download this file

77 lines (56 with data), 1.5 kB

 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
"""
.. _ex-ica-comp:
===========================================
Compare the different ICA algorithms in MNE
===========================================
Different ICA algorithms are fit to raw MEG data, and the corresponding maps
are displayed.
"""
# Authors: Pierre Ablin <pierreablin@gmail.com>
#
# License: BSD-3-Clause
# Copyright the MNE-Python contributors.
# %%
from time import time
import mne
from mne.datasets import sample
from mne.preprocessing import ICA
print(__doc__)
# %%
# Read and preprocess the data. Preprocessing consists of:
#
# - MEG channel selection
# - 1-30 Hz band-pass filter
data_path = sample.data_path()
meg_path = data_path / "MEG" / "sample"
raw_fname = meg_path / "sample_audvis_filt-0-40_raw.fif"
raw = mne.io.read_raw_fif(raw_fname).crop(0, 60).pick("meg").load_data()
reject = dict(mag=5e-12, grad=4000e-13)
raw.filter(1, 30, fir_design="firwin")
# %%
# Define a function that runs ICA on the raw MEG data and plots the components
def run_ica(method, fit_params=None):
ica = ICA(
n_components=20,
method=method,
fit_params=fit_params,
max_iter="auto",
random_state=0,
)
t0 = time()
ica.fit(raw, reject=reject)
fit_time = time() - t0
title = f"ICA decomposition using {method} (took {fit_time:.1f}s)"
ica.plot_components(title=title)
# %%
# FastICA
run_ica("fastica")
# %%
# Picard
run_ica("picard")
# %%
# Infomax
run_ica("infomax")
# %%
# Extended Infomax
run_ica("infomax", fit_params=dict(extended=True))