-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Plot ica comparison #13215
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
base: main
Are you sure you want to change the base?
Plot ica comparison #13215
Changes from 14 commits
9b76bc0
dc72b25
f4b0bd8
feaa0b6
ad7b1a1
a513556
d94f2a5
bc55115
dae4983
422710c
f029801
4ca2431
71ec45e
b1920f8
3e2dc76
ad522e8
77f1f3a
55c8f13
9214d09
149ed7f
47ab1e6
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
Extend :ref:`ex-ica-comp` example on comparing ICA algorithms with clean vs noisy MEG data, by :newcontrib:`Ganasekhar Kalla`. |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -1,50 +1,72 @@ | ||||
""" | ||||
.. _ex-ica-comp: | ||||
|
||||
=========================================== | ||||
Compare the different ICA algorithms in MNE | ||||
=========================================== | ||||
=========================================================== | ||||
Compare the performance of different ICA algorithms in MNE | ||||
=========================================================== | ||||
|
||||
Different ICA algorithms are fit to raw MEG data, and the corresponding maps | ||||
are displayed. | ||||
This example compares various ICA algorithms (FastICA, Picard, Infomax, | ||||
Extended Infomax) on the same raw MEG data. For each algorithm: | ||||
|
||||
- The ICA fit time (speed) is shown | ||||
- All components (up to 20) are visualized | ||||
- The EOG-related component from each method is detected and compared | ||||
- Comparison on clean vs noisy data is done | ||||
|
||||
Note: In typical preprocessing, only one ICA algorithm is used. | ||||
This example is for educational purposes. | ||||
""" | ||||
# Authors: Pierre Ablin <[email protected]> | ||||
# Ganasekhar Kalla <[email protected]> | ||||
# | ||||
# License: BSD-3-Clause | ||||
# Copyright the MNE-Python contributors. | ||||
|
||||
# %% | ||||
|
||||
from pathlib import Path | ||||
from time import time | ||||
|
||||
import numpy as np | ||||
|
||||
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" | ||||
# Load sample dataset | ||||
data_path = Path(sample.data_path()) | ||||
|
||||
raw_file = data_path / "MEG" / "sample" / "sample_audvis_raw.fif" | ||||
raw = mne.io.read_raw_fif(raw_file, preload=True) | ||||
raw.pick_types(meg=True, eeg=False, eog=True) | ||||
raw.crop(0, 60) | ||||
|
||||
|
||||
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") | ||||
# Copy for clean and noisy | ||||
|
||||
raw_clean = raw.copy() | ||||
raw_noisy = raw_clean.copy() | ||||
|
||||
noise = 1e-12 * np.random.randn(*raw_noisy._data.shape) | ||||
|
||||
raw_noisy._data += noise | ||||
|
||||
# Rejection thresholds | ||||
reject_clean = dict(mag=5e-12, grad=4000e-13) | ||||
reject_noisy = dict(mag=1e-11, grad=8000e-13) | ||||
|
||||
|
||||
# %% | ||||
# Define a function that runs ICA on the raw MEG data and plots the components | ||||
|
||||
|
||||
def run_ica(method, fit_params=None): | ||||
# Run ICA | ||||
def run_ica(raw_input, method, fit_params=None, reject=None): | ||||
print(f"\nRunning ICA with: {method}") | ||||
ica = ICA( | ||||
n_components=20, | ||||
method=method, | ||||
|
@@ -53,24 +75,82 @@ def run_ica(method, fit_params=None): | |||
random_state=0, | ||||
) | ||||
t0 = time() | ||||
ica.fit(raw, reject=reject) | ||||
ica.fit(raw_input, reject=reject) | ||||
fit_time = time() - t0 | ||||
title = f"ICA decomposition using {method} (took {fit_time:.1f}s)" | ||||
print(f"Fitting ICA took {fit_time:.1f}s.") | ||||
|
||||
# Updated code with broken long line | ||||
|
# Updated code with broken long line |
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.
Please do not remove previous authors. I've already fixed that for you.