-
Notifications
You must be signed in to change notification settings - Fork 1
/
02_fit_ica.py
73 lines (60 loc) · 2.39 KB
/
02_fit_ica.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
"""
================================================
Decompose EEG signal into independent components
================================================
Authors: José C. García Alanis <[email protected]>
License: BSD (3-clause)
"""
from mne import open_report
from mne.io import read_raw_fif
from mne.preprocessing import ICA
# All parameters are defined in config.py
from config import fname, parser, n_jobs
# check if NVIDIA CUDA GPU processing should be used
if n_jobs == 'cuda':
from mne.utils import set_config
set_config('MNE_USE_CUDA', 'true')
# Handle command line arguments
args = parser.parse_args()
subject = args.subject
print('Fitting ICA for subject %s' % subject)
###############################################################################
# 1) Import the output from previous processing step
input_file = fname.output(processing_step='repair_bads',
subject=subject,
file_type='raw.fif')
raw = read_raw_fif(input_file, preload=True)
# filter data to remove drifts
raw_filt = raw.copy().filter(l_freq=1.0, h_freq=None, n_jobs=n_jobs)
###############################################################################
# 2) Set ICA parameters
n_components = 20
method = 'infomax'
reject = dict(eeg=250e-6)
###############################################################################
# 3) Fit ICA
ica = ICA(n_components=n_components,
method=method,
fit_params=dict(extended=True))
ica.fit(raw_filt,
reject=reject,
reject_by_annotation=True)
###############################################################################
# 4) Plot ICA components
ica_fig = ica.plot_components(picks=range(0, 20), show=False)
###############################################################################
# 5) Save ICA solution
# output path
output_path = fname.output(processing_step='fit_ica',
subject=subject,
file_type='ica.fif')
# save file
ica.save(output_path)
###############################################################################
# 6) Create HTML report
with open_report(fname.report(subject=subject)[0]) as report:
report.add_figs_to_section(ica_fig, 'ICA solution',
section='ICA',
replace=True)
report.save(fname.report(subject=subject)[1], overwrite=True,
open_browser=False)