Note
Go to the end to download the full example code.
Repairing artifacts with ICA automatically using ICLabel Model#
This tutorial covers automatically repairing signals using ICA with the ICLabel model[1], which originates in EEGLab. For conceptual background on ICA, see this scikit-learn tutorial. For a basic understanding of how to use ICA to remove artifacts, see the tutorial in MNE-Python.
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.
Note
This example involves running the ICA Infomax algorithm, which requires scikit-learn to be installed. Please install this optional dependency before running the example.
We begin as always by importing the necessary Python modules and loading some
example data. Because ICA can be computationally
intense, we’ll also crop the data to 60 seconds; and to save ourselves from
repeatedly typing mne.preprocessing
we’ll directly import a few functions
and classes from that submodule.
import mne
from mne.preprocessing import ICA
from mne_icalabel import label_components
sample_data_folder = mne.datasets.sample.data_path() / "MEG" / "sample"
sample_data_raw_file = sample_data_folder / "sample_audvis_raw.fif"
raw = mne.io.read_raw_fif(sample_data_raw_file)
# we'll crop to 60 seconds and drop MEG channels
raw.crop(tmax=60.0).pick_types(eeg=True, stim=True, eog=True)
raw.load_data()
Using default location ~/mne_data for sample...
Creating /home/runner/mne_data
0%| | 0.00/1.65G [00:00<?, ?B/s]
0%| | 2.14M/1.65G [00:00<01:17, 21.3MB/s]
0%|▏ | 5.61M/1.65G [00:00<00:58, 28.3MB/s]
1%|▏ | 10.1M/1.65G [00:00<00:45, 35.9MB/s]
1%|▎ | 14.0M/1.65G [00:00<00:45, 35.8MB/s]
1%|▍ | 18.4M/1.65G [00:00<00:42, 38.5MB/s]
1%|▌ | 22.5M/1.65G [00:00<00:42, 38.1MB/s]
2%|▌ | 26.9M/1.65G [00:00<00:40, 39.9MB/s]
2%|▋ | 30.9M/1.65G [00:00<00:41, 39.0MB/s]
2%|▊ | 35.3M/1.65G [00:00<00:40, 40.4MB/s]
2%|▉ | 39.4M/1.65G [00:01<00:41, 39.2MB/s]
3%|▉ | 43.8M/1.65G [00:01<00:39, 40.8MB/s]
3%|█ | 47.9M/1.65G [00:01<00:40, 39.5MB/s]
3%|█▏ | 52.0M/1.65G [00:01<00:40, 39.7MB/s]
3%|█▎ | 56.2M/1.65G [00:01<00:40, 39.9MB/s]
4%|█▎ | 60.6M/1.65G [00:01<00:38, 40.9MB/s]
4%|█▍ | 64.7M/1.65G [00:01<00:39, 39.7MB/s]
4%|█▌ | 69.1M/1.65G [00:01<00:38, 41.0MB/s]
4%|█▋ | 73.2M/1.65G [00:01<00:39, 39.8MB/s]
5%|█▋ | 77.5M/1.65G [00:01<00:38, 40.5MB/s]
5%|█▊ | 81.7M/1.65G [00:02<00:39, 40.0MB/s]
5%|█▉ | 85.9M/1.65G [00:02<00:38, 40.3MB/s]
5%|██ | 90.1M/1.65G [00:02<00:39, 40.1MB/s]
6%|██ | 94.4M/1.65G [00:02<00:38, 40.8MB/s]
6%|██▏ | 98.6M/1.65G [00:02<00:38, 39.9MB/s]
6%|██▎ | 103M/1.65G [00:02<00:37, 40.8MB/s]
6%|██▍ | 107M/1.65G [00:02<00:38, 40.0MB/s]
7%|██▌ | 111M/1.65G [00:02<00:37, 40.7MB/s]
7%|██▋ | 115M/1.65G [00:02<00:38, 40.0MB/s]
7%|██▊ | 120M/1.65G [00:03<00:37, 40.7MB/s]
7%|██▊ | 124M/1.65G [00:03<00:38, 40.0MB/s]
8%|██▉ | 128M/1.65G [00:03<00:37, 40.7MB/s]
8%|███ | 132M/1.65G [00:03<00:38, 39.9MB/s]
8%|███▏ | 137M/1.65G [00:03<00:37, 40.8MB/s]
9%|███▏ | 141M/1.65G [00:03<00:37, 39.8MB/s]
9%|███▎ | 145M/1.65G [00:03<00:38, 39.4MB/s]
9%|███▍ | 149M/1.65G [00:03<00:39, 38.1MB/s]
9%|███▌ | 153M/1.65G [00:03<00:36, 40.6MB/s]
10%|███▌ | 158M/1.65G [00:03<00:38, 39.0MB/s]
10%|███▋ | 162M/1.65G [00:04<00:38, 38.5MB/s]
10%|███▊ | 167M/1.65G [00:04<00:36, 40.5MB/s]
10%|███▉ | 171M/1.65G [00:04<00:37, 39.1MB/s]
11%|████ | 175M/1.65G [00:04<00:35, 41.2MB/s]
11%|████▏ | 180M/1.65G [00:04<00:37, 39.5MB/s]
11%|████▏ | 184M/1.65G [00:04<00:35, 41.4MB/s]
11%|████▎ | 188M/1.65G [00:04<00:36, 39.8MB/s]
12%|████▍ | 193M/1.65G [00:04<00:35, 41.5MB/s]
12%|████▌ | 197M/1.65G [00:04<00:36, 39.9MB/s]
12%|████▋ | 201M/1.65G [00:05<00:37, 38.9MB/s]
12%|████▋ | 206M/1.65G [00:05<00:35, 40.7MB/s]
13%|████▊ | 210M/1.65G [00:05<00:36, 39.3MB/s]
13%|████▉ | 215M/1.65G [00:05<00:34, 41.2MB/s]
13%|█████ | 219M/1.65G [00:05<00:36, 39.6MB/s]
14%|█████▏ | 223M/1.65G [00:05<00:34, 41.3MB/s]
14%|█████▏ | 228M/1.65G [00:05<00:35, 39.6MB/s]
14%|█████▎ | 232M/1.65G [00:05<00:34, 41.5MB/s]
14%|█████▍ | 236M/1.65G [00:05<00:35, 40.0MB/s]
15%|█████▌ | 240M/1.65G [00:06<00:39, 35.8MB/s]
15%|█████▌ | 244M/1.65G [00:06<00:42, 32.8MB/s]
15%|█████▋ | 249M/1.65G [00:06<00:38, 36.7MB/s]
15%|█████▊ | 253M/1.65G [00:06<00:38, 36.1MB/s]
16%|█████▉ | 257M/1.65G [00:06<00:36, 38.7MB/s]
16%|██████ | 261M/1.65G [00:06<00:36, 37.8MB/s]
16%|██████ | 266M/1.65G [00:06<00:34, 40.0MB/s]
16%|██████▏ | 270M/1.65G [00:06<00:35, 38.6MB/s]
17%|██████▎ | 274M/1.65G [00:06<00:33, 40.8MB/s]
17%|██████▍ | 279M/1.65G [00:07<00:34, 39.3MB/s]
17%|██████▌ | 283M/1.65G [00:07<00:33, 41.2MB/s]
17%|██████▌ | 287M/1.65G [00:07<00:34, 39.5MB/s]
18%|██████▋ | 293M/1.65G [00:07<00:31, 42.8MB/s]
18%|██████▊ | 297M/1.65G [00:07<00:33, 41.0MB/s]
18%|██████▉ | 301M/1.65G [00:07<00:32, 41.6MB/s]
18%|███████ | 305M/1.65G [00:07<00:34, 39.4MB/s]
19%|███████▏ | 310M/1.65G [00:07<00:31, 42.7MB/s]
19%|███████▏ | 315M/1.65G [00:07<00:32, 41.7MB/s]
19%|███████▎ | 319M/1.65G [00:08<00:33, 40.2MB/s]
20%|███████▍ | 324M/1.65G [00:08<00:31, 41.7MB/s]
20%|███████▌ | 328M/1.65G [00:08<00:33, 39.5MB/s]
20%|███████▋ | 332M/1.65G [00:08<00:32, 41.1MB/s]
20%|███████▋ | 337M/1.65G [00:08<00:33, 39.5MB/s]
21%|███████▊ | 341M/1.65G [00:08<00:32, 40.9MB/s]
21%|███████▉ | 345M/1.65G [00:08<00:32, 39.7MB/s]
21%|████████ | 350M/1.65G [00:08<00:31, 41.6MB/s]
21%|████████▏ | 354M/1.65G [00:08<00:32, 39.7MB/s]
22%|████████▏ | 359M/1.65G [00:09<00:33, 39.1MB/s]
22%|████████▎ | 363M/1.65G [00:09<00:31, 40.4MB/s]
22%|████████▍ | 367M/1.65G [00:09<00:32, 39.1MB/s]
23%|████████▌ | 372M/1.65G [00:09<00:30, 42.3MB/s]
23%|████████▋ | 377M/1.65G [00:09<00:31, 40.9MB/s]
23%|████████▊ | 381M/1.65G [00:09<00:30, 41.9MB/s]
23%|████████▊ | 385M/1.65G [00:09<00:31, 40.4MB/s]
24%|████████▉ | 390M/1.65G [00:09<00:30, 41.4MB/s]
24%|█████████ | 394M/1.65G [00:09<00:31, 40.1MB/s]
24%|█████████▏ | 398M/1.65G [00:10<00:30, 41.3MB/s]
24%|█████████▎ | 402M/1.65G [00:10<00:31, 39.9MB/s]
25%|█████████▎ | 407M/1.65G [00:10<00:31, 39.0MB/s]
25%|█████████▍ | 411M/1.65G [00:10<00:37, 33.1MB/s]
25%|█████████▌ | 414M/1.65G [00:10<00:39, 31.6MB/s]
25%|█████████▌ | 417M/1.65G [00:10<00:40, 30.7MB/s]
26%|█████████▋ | 422M/1.65G [00:10<00:36, 33.4MB/s]
26%|█████████▊ | 426M/1.65G [00:10<00:34, 35.7MB/s]
26%|█████████▉ | 430M/1.65G [00:10<00:33, 36.5MB/s]
26%|█████████▉ | 435M/1.65G [00:11<00:31, 38.2MB/s]
27%|██████████ | 439M/1.65G [00:11<00:31, 38.2MB/s]
27%|██████████▏ | 443M/1.65G [00:11<00:30, 39.4MB/s]
27%|██████████▎ | 447M/1.65G [00:11<00:30, 39.0MB/s]
27%|██████████▍ | 452M/1.65G [00:11<00:29, 40.3MB/s]
28%|██████████▍ | 456M/1.65G [00:11<00:30, 39.3MB/s]
28%|██████████▌ | 460M/1.65G [00:11<00:29, 40.3MB/s]
28%|██████████▋ | 464M/1.65G [00:11<00:30, 39.4MB/s]
28%|██████████▊ | 469M/1.65G [00:11<00:28, 41.0MB/s]
29%|██████████▉ | 473M/1.65G [00:11<00:27, 42.9MB/s]
29%|██████████▉ | 478M/1.65G [00:12<00:28, 41.0MB/s]
29%|███████████ | 482M/1.65G [00:12<00:29, 39.8MB/s]
29%|███████████▏ | 487M/1.65G [00:12<00:28, 41.2MB/s]
30%|███████████▎ | 491M/1.65G [00:12<00:29, 39.8MB/s]
30%|███████████▍ | 496M/1.65G [00:12<00:27, 41.6MB/s]
30%|███████████▍ | 500M/1.65G [00:12<00:28, 39.8MB/s]
31%|███████████▌ | 505M/1.65G [00:12<00:26, 43.2MB/s]
31%|███████████▋ | 510M/1.65G [00:12<00:26, 42.8MB/s]
31%|███████████▊ | 515M/1.65G [00:12<00:26, 43.6MB/s]
31%|███████████▉ | 519M/1.65G [00:13<00:27, 41.5MB/s]
32%|████████████ | 523M/1.65G [00:13<00:26, 41.9MB/s]
32%|████████████▏ | 528M/1.65G [00:13<00:27, 40.7MB/s]
32%|████████████▏ | 532M/1.65G [00:13<00:27, 41.4MB/s]
32%|████████████▎ | 536M/1.65G [00:13<00:27, 40.3MB/s]
33%|████████████▍ | 540M/1.65G [00:13<00:27, 40.3MB/s]
33%|████████████▌ | 544M/1.65G [00:13<00:27, 39.9MB/s]
33%|████████████▌ | 548M/1.65G [00:13<00:27, 40.6MB/s]
33%|████████████▋ | 552M/1.65G [00:13<00:31, 35.5MB/s]
34%|████████████▊ | 556M/1.65G [00:14<01:02, 17.6MB/s]
34%|████████████▊ | 559M/1.65G [00:14<01:15, 14.5MB/s]
34%|████████████▉ | 561M/1.65G [00:15<01:24, 12.9MB/s]
34%|████████████▉ | 563M/1.65G [00:15<01:29, 12.2MB/s]
34%|████████████▉ | 564M/1.65G [00:15<01:29, 12.2MB/s]
34%|█████████████ | 566M/1.65G [00:15<01:27, 12.4MB/s]
34%|█████████████ | 569M/1.65G [00:15<01:04, 16.9MB/s]
35%|█████████████▏ | 574M/1.65G [00:15<00:48, 22.5MB/s]
35%|█████████████▎ | 578M/1.65G [00:15<00:40, 26.4MB/s]
35%|█████████████▍ | 582M/1.65G [00:15<00:34, 31.0MB/s]
35%|█████████████▍ | 586M/1.65G [00:15<00:32, 32.4MB/s]
36%|█████████████▌ | 590M/1.65G [00:16<00:30, 35.2MB/s]
36%|█████████████▋ | 595M/1.65G [00:16<00:29, 35.9MB/s]
36%|█████████████▊ | 599M/1.65G [00:16<00:27, 38.0MB/s]
36%|█████████████▊ | 603M/1.65G [00:16<00:28, 37.4MB/s]
37%|█████████████▉ | 607M/1.65G [00:16<00:26, 39.2MB/s]
37%|██████████████ | 612M/1.65G [00:16<00:26, 38.7MB/s]
37%|██████████████▏ | 616M/1.65G [00:16<00:25, 40.2MB/s]
38%|██████████████▎ | 620M/1.65G [00:16<00:26, 39.1MB/s]
38%|██████████████▎ | 625M/1.65G [00:16<00:25, 40.8MB/s]
38%|██████████████▍ | 629M/1.65G [00:17<00:26, 39.1MB/s]
38%|██████████████▌ | 633M/1.65G [00:17<00:25, 39.3MB/s]
39%|██████████████▋ | 637M/1.65G [00:17<00:25, 39.5MB/s]
39%|██████████████▋ | 641M/1.65G [00:17<00:24, 40.6MB/s]
39%|██████████████▊ | 645M/1.65G [00:17<00:25, 39.7MB/s]
39%|██████████████▉ | 650M/1.65G [00:17<00:24, 41.1MB/s]
40%|███████████████ | 654M/1.65G [00:17<00:25, 39.8MB/s]
40%|███████████████▏ | 658M/1.65G [00:17<00:24, 40.9MB/s]
40%|███████████████▏ | 662M/1.65G [00:17<00:24, 39.7MB/s]
40%|███████████████▎ | 667M/1.65G [00:17<00:24, 41.1MB/s]
41%|███████████████▍ | 671M/1.65G [00:18<00:24, 39.7MB/s]
41%|███████████████▌ | 675M/1.65G [00:18<00:23, 41.2MB/s]
41%|███████████████▋ | 680M/1.65G [00:18<00:24, 39.2MB/s]
41%|███████████████▋ | 684M/1.65G [00:18<00:23, 41.0MB/s]
42%|███████████████▊ | 688M/1.65G [00:18<00:24, 39.8MB/s]
42%|███████████████▉ | 693M/1.65G [00:18<00:23, 41.2MB/s]
42%|████████████████ | 697M/1.65G [00:18<00:23, 40.5MB/s]
42%|████████████████ | 701M/1.65G [00:18<00:23, 41.0MB/s]
43%|████████████████▏ | 705M/1.65G [00:18<00:23, 40.2MB/s]
43%|████████████████▎ | 710M/1.65G [00:19<00:23, 40.8MB/s]
43%|████████████████▍ | 714M/1.65G [00:19<00:23, 40.3MB/s]
43%|████████████████▌ | 718M/1.65G [00:19<00:22, 41.1MB/s]
44%|████████████████▌ | 722M/1.65G [00:19<00:22, 40.7MB/s]
44%|████████████████▋ | 726M/1.65G [00:19<00:23, 39.8MB/s]
44%|████████████████▊ | 731M/1.65G [00:19<00:22, 41.6MB/s]
44%|████████████████▉ | 735M/1.65G [00:19<00:23, 39.3MB/s]
45%|█████████████████ | 739M/1.65G [00:19<00:23, 39.0MB/s]
45%|█████████████████ | 744M/1.65G [00:19<00:22, 40.8MB/s]
45%|█████████████████▏ | 748M/1.65G [00:19<00:23, 38.3MB/s]
46%|█████████████████▎ | 752M/1.65G [00:20<00:22, 39.4MB/s]
46%|█████████████████▍ | 756M/1.65G [00:20<00:23, 38.4MB/s]
46%|█████████████████▍ | 761M/1.65G [00:20<00:22, 39.8MB/s]
46%|█████████████████▌ | 765M/1.65G [00:20<00:22, 39.1MB/s]
47%|█████████████████▋ | 769M/1.65G [00:20<00:21, 40.2MB/s]
47%|█████████████████▊ | 773M/1.65G [00:20<00:22, 39.3MB/s]
47%|█████████████████▉ | 778M/1.65G [00:20<00:21, 40.7MB/s]
47%|█████████████████▉ | 782M/1.65G [00:20<00:22, 39.6MB/s]
48%|██████████████████ | 786M/1.65G [00:20<00:21, 39.9MB/s]
48%|██████████████████▏ | 790M/1.65G [00:21<00:21, 39.9MB/s]
48%|██████████████████▎ | 795M/1.65G [00:21<00:20, 40.9MB/s]
48%|██████████████████▎ | 799M/1.65G [00:21<00:21, 39.8MB/s]
49%|██████████████████▍ | 803M/1.65G [00:21<00:20, 41.0MB/s]
49%|██████████████████▌ | 807M/1.65G [00:21<00:21, 39.7MB/s]
49%|██████████████████▋ | 812M/1.65G [00:21<00:20, 40.9MB/s]
49%|██████████████████▊ | 816M/1.65G [00:21<00:21, 39.7MB/s]
50%|██████████████████▊ | 820M/1.65G [00:21<00:20, 41.0MB/s]
50%|██████████████████▉ | 824M/1.65G [00:21<00:20, 39.6MB/s]
50%|███████████████████ | 829M/1.65G [00:21<00:19, 41.3MB/s]
50%|███████████████████▏ | 833M/1.65G [00:22<00:20, 39.7MB/s]
51%|███████████████████▎ | 837M/1.65G [00:22<00:19, 40.8MB/s]
51%|███████████████████▎ | 841M/1.65G [00:22<00:20, 39.5MB/s]
51%|███████████████████▍ | 846M/1.65G [00:22<00:19, 41.1MB/s]
51%|███████████████████▌ | 850M/1.65G [00:22<00:20, 39.5MB/s]
52%|███████████████████▋ | 855M/1.65G [00:22<00:19, 40.7MB/s]
52%|███████████████████▋ | 859M/1.65G [00:22<00:20, 39.4MB/s]
52%|███████████████████▊ | 863M/1.65G [00:22<00:18, 41.6MB/s]
52%|███████████████████▉ | 867M/1.65G [00:22<00:20, 38.9MB/s]
53%|████████████████████ | 872M/1.65G [00:23<00:20, 39.0MB/s]
53%|████████████████████▏ | 877M/1.65G [00:23<00:19, 40.7MB/s]
53%|████████████████████▏ | 881M/1.65G [00:23<00:19, 39.4MB/s]
54%|████████████████████▎ | 885M/1.65G [00:23<00:18, 40.9MB/s]
54%|████████████████████▍ | 890M/1.65G [00:23<00:18, 40.8MB/s]
54%|████████████████████▌ | 894M/1.65G [00:23<00:18, 41.2MB/s]
54%|████████████████████▋ | 898M/1.65G [00:23<00:18, 40.4MB/s]
55%|████████████████████▋ | 902M/1.65G [00:23<00:18, 40.9MB/s]
55%|████████████████████▊ | 907M/1.65G [00:23<00:18, 40.0MB/s]
55%|████████████████████▉ | 911M/1.65G [00:24<00:18, 40.3MB/s]
55%|█████████████████████ | 915M/1.65G [00:24<00:18, 40.0MB/s]
56%|█████████████████████▏ | 919M/1.65G [00:24<00:18, 40.6MB/s]
56%|█████████████████████▏ | 924M/1.65G [00:24<00:18, 39.9MB/s]
56%|█████████████████████▎ | 928M/1.65G [00:24<00:18, 39.9MB/s]
56%|█████████████████████▍ | 932M/1.65G [00:24<00:17, 40.1MB/s]
57%|█████████████████████▌ | 936M/1.65G [00:24<00:17, 40.1MB/s]
57%|█████████████████████▌ | 940M/1.65G [00:24<00:17, 40.1MB/s]
57%|█████████████████████▋ | 945M/1.65G [00:24<00:17, 40.1MB/s]
57%|█████████████████████▊ | 949M/1.65G [00:24<00:17, 40.4MB/s]
58%|█████████████████████▉ | 953M/1.65G [00:25<00:17, 40.1MB/s]
58%|██████████████████████ | 957M/1.65G [00:25<00:17, 40.3MB/s]
58%|██████████████████████ | 962M/1.65G [00:25<00:17, 40.2MB/s]
58%|██████████████████████▏ | 966M/1.65G [00:25<00:17, 39.4MB/s]
59%|██████████████████████▎ | 970M/1.65G [00:25<00:16, 40.4MB/s]
59%|██████████████████████▍ | 974M/1.65G [00:25<00:16, 40.4MB/s]
59%|██████████████████████▍ | 979M/1.65G [00:25<00:16, 40.2MB/s]
59%|██████████████████████▌ | 983M/1.65G [00:25<00:16, 40.2MB/s]
60%|██████████████████████▋ | 987M/1.65G [00:25<00:16, 40.2MB/s]
60%|██████████████████████▊ | 991M/1.65G [00:26<00:16, 40.2MB/s]
60%|██████████████████████▉ | 995M/1.65G [00:26<00:16, 40.3MB/s]
60%|██████████████████████▍ | 1.00G/1.65G [00:26<00:16, 40.2MB/s]
61%|██████████████████████▍ | 1.00G/1.65G [00:26<00:16, 40.4MB/s]
61%|██████████████████████▌ | 1.01G/1.65G [00:26<00:16, 40.2MB/s]
61%|██████████████████████▋ | 1.01G/1.65G [00:26<00:15, 40.4MB/s]
62%|██████████████████████▊ | 1.02G/1.65G [00:26<00:15, 40.1MB/s]
62%|██████████████████████▊ | 1.02G/1.65G [00:26<00:15, 40.8MB/s]
62%|██████████████████████▉ | 1.02G/1.65G [00:26<00:16, 38.4MB/s]
62%|███████████████████████ | 1.03G/1.65G [00:27<00:17, 35.1MB/s]
63%|███████████████████████▏ | 1.03G/1.65G [00:27<00:16, 37.1MB/s]
63%|███████████████████████▏ | 1.04G/1.65G [00:27<00:15, 39.3MB/s]
63%|███████████████████████▎ | 1.04G/1.65G [00:27<00:14, 41.2MB/s]
63%|███████████████████████▍ | 1.05G/1.65G [00:27<00:14, 41.4MB/s]
64%|███████████████████████▌ | 1.05G/1.65G [00:27<00:14, 42.8MB/s]
64%|███████████████████████▋ | 1.06G/1.65G [00:27<00:13, 43.7MB/s]
64%|███████████████████████▋ | 1.06G/1.65G [00:27<00:14, 41.3MB/s]
64%|███████████████████████▊ | 1.06G/1.65G [00:27<00:13, 42.5MB/s]
65%|███████████████████████▉ | 1.07G/1.65G [00:27<00:13, 42.3MB/s]
65%|████████████████████████ | 1.07G/1.65G [00:28<00:13, 43.1MB/s]
65%|████████████████████████▏ | 1.08G/1.65G [00:28<00:14, 41.0MB/s]
66%|████████████████████████▏ | 1.08G/1.65G [00:28<00:13, 42.3MB/s]
66%|████████████████████████▎ | 1.09G/1.65G [00:28<00:13, 40.6MB/s]
66%|████████████████████████▍ | 1.09G/1.65G [00:28<00:13, 42.1MB/s]
66%|████████████████████████▌ | 1.10G/1.65G [00:28<00:13, 39.8MB/s]
67%|████████████████████████▋ | 1.10G/1.65G [00:28<00:13, 39.7MB/s]
67%|████████████████████████▋ | 1.10G/1.65G [00:28<00:13, 40.9MB/s]
67%|████████████████████████▊ | 1.11G/1.65G [00:28<00:13, 39.7MB/s]
67%|████████████████████████▉ | 1.11G/1.65G [00:29<00:13, 40.9MB/s]
68%|█████████████████████████ | 1.12G/1.65G [00:29<00:13, 39.4MB/s]
68%|█████████████████████████ | 1.12G/1.65G [00:29<00:12, 41.2MB/s]
68%|█████████████████████████▏ | 1.13G/1.65G [00:29<00:13, 38.6MB/s]
68%|█████████████████████████▎ | 1.13G/1.65G [00:29<00:12, 42.3MB/s]
69%|█████████████████████████▍ | 1.14G/1.65G [00:29<00:12, 40.7MB/s]
69%|█████████████████████████▌ | 1.14G/1.65G [00:29<00:12, 41.6MB/s]
69%|█████████████████████████▌ | 1.14G/1.65G [00:29<00:12, 40.4MB/s]
69%|█████████████████████████▋ | 1.15G/1.65G [00:29<00:12, 41.5MB/s]
70%|█████████████████████████▊ | 1.15G/1.65G [00:30<00:12, 40.0MB/s]
70%|█████████████████████████▉ | 1.16G/1.65G [00:30<00:12, 41.3MB/s]
70%|█████████████████████████▉ | 1.16G/1.65G [00:30<00:12, 40.1MB/s]
71%|██████████████████████████ | 1.17G/1.65G [00:30<00:11, 40.9MB/s]
71%|██████████████████████████▏ | 1.17G/1.65G [00:30<00:12, 39.7MB/s]
71%|██████████████████████████▎ | 1.17G/1.65G [00:30<00:12, 39.9MB/s]
71%|██████████████████████████▎ | 1.18G/1.65G [00:30<00:12, 39.5MB/s]
72%|██████████████████████████▍ | 1.18G/1.65G [00:30<00:11, 40.8MB/s]
72%|██████████████████████████▌ | 1.19G/1.65G [00:30<00:11, 39.5MB/s]
72%|██████████████████████████▋ | 1.19G/1.65G [00:30<00:11, 41.0MB/s]
72%|██████████████████████████▋ | 1.19G/1.65G [00:31<00:11, 39.6MB/s]
73%|██████████████████████████▊ | 1.20G/1.65G [00:31<00:11, 41.1MB/s]
73%|██████████████████████████▉ | 1.20G/1.65G [00:31<00:11, 39.6MB/s]
73%|███████████████████████████ | 1.21G/1.65G [00:31<00:11, 40.1MB/s]
73%|███████████████████████████ | 1.21G/1.65G [00:31<00:11, 39.7MB/s]
74%|███████████████████████████▏ | 1.22G/1.65G [00:31<00:10, 40.5MB/s]
74%|███████████████████████████▎ | 1.22G/1.65G [00:31<00:11, 39.1MB/s]
74%|███████████████████████████▍ | 1.22G/1.65G [00:31<00:10, 41.0MB/s]
74%|███████████████████████████▌ | 1.23G/1.65G [00:31<00:10, 39.5MB/s]
75%|███████████████████████████▌ | 1.23G/1.65G [00:32<00:10, 41.0MB/s]
75%|███████████████████████████▋ | 1.24G/1.65G [00:32<00:10, 39.7MB/s]
75%|███████████████████████████▊ | 1.24G/1.65G [00:32<00:09, 41.2MB/s]
75%|███████████████████████████▉ | 1.25G/1.65G [00:32<00:10, 39.8MB/s]
76%|███████████████████████████▉ | 1.25G/1.65G [00:32<00:09, 41.2MB/s]
76%|████████████████████████████ | 1.25G/1.65G [00:32<00:10, 39.8MB/s]
76%|████████████████████████████▏ | 1.26G/1.65G [00:32<00:09, 41.3MB/s]
76%|████████████████████████████▎ | 1.26G/1.65G [00:32<00:09, 39.8MB/s]
77%|████████████████████████████▎ | 1.27G/1.65G [00:32<00:09, 41.0MB/s]
77%|████████████████████████████▍ | 1.27G/1.65G [00:32<00:09, 39.5MB/s]
77%|████████████████████████████▌ | 1.28G/1.65G [00:33<00:09, 40.1MB/s]
77%|████████████████████████████▋ | 1.28G/1.65G [00:33<00:09, 39.3MB/s]
78%|████████████████████████████▋ | 1.28G/1.65G [00:33<00:10, 35.8MB/s]
78%|████████████████████████████▊ | 1.29G/1.65G [00:33<00:10, 36.2MB/s]
78%|████████████████████████████▉ | 1.29G/1.65G [00:33<00:09, 37.5MB/s]
78%|█████████████████████████████ | 1.30G/1.65G [00:33<00:09, 39.4MB/s]
79%|█████████████████████████████ | 1.30G/1.65G [00:33<00:09, 38.6MB/s]
79%|█████████████████████████████▏ | 1.30G/1.65G [00:34<00:15, 21.8MB/s]
79%|█████████████████████████████▎ | 1.31G/1.65G [00:34<00:12, 28.4MB/s]
80%|█████████████████████████████▍ | 1.31G/1.65G [00:34<00:10, 33.2MB/s]
80%|█████████████████████████████▌ | 1.32G/1.65G [00:34<00:09, 34.4MB/s]
80%|█████████████████████████████▋ | 1.32G/1.65G [00:34<00:09, 36.1MB/s]
80%|█████████████████████████████▋ | 1.33G/1.65G [00:34<00:08, 37.0MB/s]
81%|█████████████████████████████▊ | 1.33G/1.65G [00:34<00:08, 39.0MB/s]
81%|█████████████████████████████▉ | 1.34G/1.65G [00:34<00:08, 38.3MB/s]
81%|██████████████████████████████ | 1.34G/1.65G [00:34<00:07, 40.1MB/s]
81%|██████████████████████████████ | 1.35G/1.65G [00:35<00:07, 39.0MB/s]
82%|██████████████████████████████▏ | 1.35G/1.65G [00:35<00:07, 40.6MB/s]
82%|██████████████████████████████▎ | 1.35G/1.65G [00:35<00:07, 39.0MB/s]
82%|██████████████████████████████▍ | 1.36G/1.65G [00:35<00:07, 41.4MB/s]
82%|██████████████████████████████▌ | 1.36G/1.65G [00:35<00:07, 39.9MB/s]
83%|██████████████████████████████▌ | 1.37G/1.65G [00:35<00:07, 39.4MB/s]
83%|██████████████████████████████▋ | 1.37G/1.65G [00:35<00:06, 40.6MB/s]
83%|██████████████████████████████▊ | 1.38G/1.65G [00:35<00:06, 43.1MB/s]
84%|██████████████████████████████▉ | 1.38G/1.65G [00:35<00:06, 43.8MB/s]
84%|███████████████████████████████ | 1.39G/1.65G [00:36<00:06, 43.8MB/s]
84%|███████████████████████████████ | 1.39G/1.65G [00:36<00:06, 41.8MB/s]
84%|███████████████████████████████▏ | 1.39G/1.65G [00:36<00:06, 42.4MB/s]
85%|███████████████████████████████▎ | 1.40G/1.65G [00:36<00:06, 40.8MB/s]
85%|███████████████████████████████▍ | 1.40G/1.65G [00:36<00:05, 41.7MB/s]
85%|███████████████████████████████▌ | 1.41G/1.65G [00:36<00:06, 37.8MB/s]
85%|███████████████████████████████▌ | 1.41G/1.65G [00:36<00:06, 39.6MB/s]
86%|███████████████████████████████▋ | 1.42G/1.65G [00:36<00:05, 40.8MB/s]
86%|███████████████████████████████▊ | 1.42G/1.65G [00:36<00:05, 39.6MB/s]
86%|███████████████████████████████▉ | 1.43G/1.65G [00:37<00:05, 40.8MB/s]
86%|███████████████████████████████▉ | 1.43G/1.65G [00:37<00:05, 39.6MB/s]
87%|████████████████████████████████ | 1.43G/1.65G [00:37<00:05, 40.8MB/s]
87%|████████████████████████████████▏ | 1.44G/1.65G [00:37<00:05, 39.7MB/s]
87%|████████████████████████████████▎ | 1.44G/1.65G [00:37<00:05, 40.9MB/s]
88%|████████████████████████████████▍ | 1.45G/1.65G [00:37<00:05, 39.7MB/s]
88%|████████████████████████████████▍ | 1.45G/1.65G [00:37<00:05, 39.3MB/s]
88%|████████████████████████████████▌ | 1.45G/1.65G [00:37<00:04, 40.2MB/s]
88%|████████████████████████████████▋ | 1.46G/1.65G [00:37<00:04, 39.6MB/s]
89%|████████████████████████████████▊ | 1.46G/1.65G [00:37<00:04, 40.5MB/s]
89%|████████████████████████████████▊ | 1.47G/1.65G [00:38<00:04, 39.8MB/s]
89%|████████████████████████████████▉ | 1.47G/1.65G [00:38<00:04, 40.7MB/s]
89%|█████████████████████████████████ | 1.48G/1.65G [00:38<00:04, 39.5MB/s]
90%|█████████████████████████████████▏ | 1.48G/1.65G [00:38<00:04, 40.6MB/s]
90%|█████████████████████████████████▏ | 1.48G/1.65G [00:38<00:04, 39.7MB/s]
90%|█████████████████████████████████▎ | 1.49G/1.65G [00:38<00:04, 40.6MB/s]
90%|█████████████████████████████████▍ | 1.49G/1.65G [00:38<00:04, 39.7MB/s]
91%|█████████████████████████████████▌ | 1.50G/1.65G [00:38<00:03, 40.8MB/s]
91%|█████████████████████████████████▌ | 1.50G/1.65G [00:38<00:03, 39.4MB/s]
91%|█████████████████████████████████▋ | 1.51G/1.65G [00:39<00:03, 40.7MB/s]
91%|█████████████████████████████████▊ | 1.51G/1.65G [00:39<00:03, 39.6MB/s]
92%|█████████████████████████████████▉ | 1.51G/1.65G [00:39<00:03, 40.9MB/s]
92%|█████████████████████████████████▉ | 1.52G/1.65G [00:39<00:03, 39.7MB/s]
92%|██████████████████████████████████ | 1.52G/1.65G [00:39<00:03, 40.7MB/s]
92%|██████████████████████████████████▏ | 1.53G/1.65G [00:39<00:03, 39.3MB/s]
93%|██████████████████████████████████▎ | 1.53G/1.65G [00:39<00:03, 39.9MB/s]
93%|██████████████████████████████████▎ | 1.54G/1.65G [00:39<00:02, 40.8MB/s]
93%|██████████████████████████████████▍ | 1.54G/1.65G [00:39<00:02, 39.5MB/s]
93%|██████████████████████████████████▌ | 1.54G/1.65G [00:39<00:02, 39.7MB/s]
94%|██████████████████████████████████▋ | 1.55G/1.65G [00:40<00:02, 40.1MB/s]
94%|██████████████████████████████████▋ | 1.55G/1.65G [00:40<00:02, 39.7MB/s]
94%|██████████████████████████████████▊ | 1.56G/1.65G [00:40<00:02, 40.0MB/s]
94%|██████████████████████████████████▉ | 1.56G/1.65G [00:40<00:02, 40.2MB/s]
95%|███████████████████████████████████ | 1.56G/1.65G [00:40<00:02, 40.5MB/s]
95%|███████████████████████████████████ | 1.57G/1.65G [00:40<00:02, 40.6MB/s]
95%|███████████████████████████████████▏ | 1.57G/1.65G [00:40<00:01, 40.2MB/s]
95%|███████████████████████████████████▎ | 1.58G/1.65G [00:40<00:01, 41.5MB/s]
96%|███████████████████████████████████▍ | 1.58G/1.65G [00:40<00:01, 40.8MB/s]
96%|███████████████████████████████████▍ | 1.59G/1.65G [00:40<00:01, 40.9MB/s]
96%|███████████████████████████████████▌ | 1.59G/1.65G [00:41<00:01, 40.3MB/s]
96%|███████████████████████████████████▋ | 1.59G/1.65G [00:41<00:01, 40.3MB/s]
97%|███████████████████████████████████▊ | 1.60G/1.65G [00:41<00:01, 39.9MB/s]
97%|███████████████████████████████████▊ | 1.60G/1.65G [00:41<00:01, 39.9MB/s]
97%|███████████████████████████████████▉ | 1.61G/1.65G [00:41<00:01, 39.7MB/s]
97%|████████████████████████████████████ | 1.61G/1.65G [00:41<00:01, 40.0MB/s]
98%|████████████████████████████████████▏| 1.61G/1.65G [00:41<00:00, 39.5MB/s]
98%|████████████████████████████████████▏| 1.62G/1.65G [00:41<00:00, 40.3MB/s]
98%|████████████████████████████████████▎| 1.62G/1.65G [00:41<00:00, 40.9MB/s]
98%|████████████████████████████████████▍| 1.63G/1.65G [00:42<00:00, 40.3MB/s]
99%|████████████████████████████████████▌| 1.63G/1.65G [00:42<00:00, 41.1MB/s]
99%|████████████████████████████████████▌| 1.64G/1.65G [00:42<00:00, 39.2MB/s]
99%|████████████████████████████████████▋| 1.64G/1.65G [00:42<00:00, 38.8MB/s]
99%|████████████████████████████████████▊| 1.64G/1.65G [00:42<00:00, 40.7MB/s]
100%|████████████████████████████████████▉| 1.65G/1.65G [00:42<00:00, 38.3MB/s]
100%|████████████████████████████████████▉| 1.65G/1.65G [00:42<00:00, 40.0MB/s]
0%| | 0.00/1.65G [00:00<?, ?B/s]
100%|█████████████████████████████████████| 1.65G/1.65G [00:00<00:00, 8.26TB/s]
Attempting to create new mne-python configuration file:
/home/runner/.mne/mne-python.json
Download complete in 01m15s (1576.2 MB)
Opening raw data file /home/runner/mne_data/MNE-sample-data/MEG/sample/sample_audvis_raw.fif...
Read a total of 3 projection items:
PCA-v1 (1 x 102) idle
PCA-v2 (1 x 102) idle
PCA-v3 (1 x 102) idle
Range : 25800 ... 192599 = 42.956 ... 320.670 secs
Ready.
NOTE: pick_types() is a legacy function. New code should use inst.pick(...).
Reading 0 ... 36037 = 0.000 ... 60.000 secs...
Note
Before applying ICA (or any artifact repair strategy), be sure to observe the artifacts in your data to make sure you choose the right repair tool. Sometimes the right tool is no tool at all — if the artifacts are small enough you may not even need to repair them to get good analysis results. See Overview of artifact detection for guidance on detecting and visualizing various types of artifact.
Example: EOG and ECG artifact repair#
Visualizing the artifacts#
Let’s begin by visualizing the artifacts that we want to repair. In this dataset they are big enough to see easily in the raw data:
# pick some channels that clearly show heartbeats and blinks
artifact_picks = mne.pick_channels_regexp(raw.ch_names, regexp=r"(EEG 00.)")
raw.plot(order=artifact_picks, n_channels=len(artifact_picks), show_scrollbars=False)
Filtering to remove slow drifts#
Before we run the ICA, an important step is filtering the data to remove
low-frequency drifts, which can negatively affect the quality of the ICA fit.
The slow drifts are problematic because they reduce the independence of the
assumed-to-be-independent sources (e.g., during a slow upward drift, the
neural, heartbeat, blink, and other muscular sources will all tend to have
higher values), making it harder for the algorithm to find an accurate
solution. A high-pass filter with 1 Hz cutoff frequency is recommended.
However, because filtering is a linear operation, the ICA solution found from
the filtered signal can be applied to the unfiltered signal (see
[2] for more information), so we’ll keep a copy of
the unfiltered Raw
object around so we can apply the ICA solution
to it later.
Filtering raw data in 1 contiguous segment
Setting up band-pass filter from 1 - 1e+02 Hz
FIR filter parameters
---------------------
Designing a one-pass, zero-phase, non-causal bandpass filter:
- Windowed time-domain design (firwin) method
- Hamming window with 0.0194 passband ripple and 53 dB stopband attenuation
- Lower passband edge: 1.00
- Lower transition bandwidth: 1.00 Hz (-6 dB cutoff frequency: 0.50 Hz)
- Upper passband edge: 100.00 Hz
- Upper transition bandwidth: 25.00 Hz (-6 dB cutoff frequency: 112.50 Hz)
- Filter length: 1983 samples (3.302 s)
[Parallel(n_jobs=1)]: Done 17 tasks | elapsed: 0.0s
Fitting and plotting the ICA solution#
Now we’re ready to set up and fit the ICA. Since we know (from observing our
raw data) that the EOG and ECG artifacts are fairly strong, we would expect
those artifacts to be captured in the first few dimensions of the PCA
decomposition that happens before the ICA. Therefore, we probably don’t need
a huge number of components to do a good job of isolating our artifacts
(though it is usually preferable to include more components for a more
accurate solution). As a first guess, we’ll run ICA with n_components=15
(use only the first 15 PCA components to compute the ICA decomposition) — a
very small number given that our data has 59 good EEG channels, but with the
advantage that it will run quickly and we will able to tell easily whether it
worked or not (because we already know what the EOG / ECG artifacts should
look like).
ICA fitting is not deterministic (e.g., the components may get a sign flip on different runs, or may not always be returned in the same order), so we’ll also specify a random seed so that we get identical results each time this tutorial is built by our web servers.
Before fitting ICA, we will apply a common average referencing, to comply with the ICLabel requirements.
filt_raw = filt_raw.set_eeg_reference("average")
EEG channel type selected for re-referencing
Applying average reference.
Applying a custom ('EEG',) reference.
We will use the ‘extended infomax’ method for fitting the ICA, to comply with the ICLabel requirements. ICLabel was not tested with other ICA decomposition algorithm, but its performance and accuracy should not be impacted by the algorithm.
Fitting ICA to data using 59 channels (please be patient, this may take a while)
Selecting by number: 15 components
Computing Extended Infomax ICA
Fitting ICA took 5.3s.
Some optional parameters that we could have passed to the
fit
method include decim
(to use only
every Nth sample in computing the ICs, which can yield a considerable
speed-up) and reject
(for providing a rejection dictionary for maximum
acceptable peak-to-peak amplitudes for each channel type, just like we used
when creating epoched data in the Overview of MEG/EEG analysis with MNE-Python tutorial).
Now we can examine the ICs to see what they captured.
plot_sources
will show the time series of the
ICs. Note that in our call to plot_sources
we
can use the original, unfiltered Raw
object:
raw.load_data()
ica.plot_sources(raw, show_scrollbars=False, show=True)
Creating RawArray with float64 data, n_channels=16, n_times=36038
Range : 25800 ... 61837 = 42.956 ... 102.956 secs
Ready.
Here we can pretty clearly see that the first component (ICA000
) captures
the EOG signal quite well (for more info on visually identifying Independent
Components, this EEGLAB tutorial is a good resource). We can also
visualize the scalp field distribution of each component using
plot_components
. These are interpolated based
on the values in the ICA mixing matrix:
ica.plot_components()
# blinks
ica.plot_overlay(raw, exclude=[0], picks="eeg")
Applying ICA to Raw instance
Transforming to ICA space (15 components)
Zeroing out 1 ICA component
Projecting back using 59 PCA components
We can also plot some diagnostics of IC using
plot_properties
:
ica.plot_properties(raw, picks=[0])
Using multitaper spectrum estimation with 7 DPSS windows
Not setting metadata
30 matching events found
No baseline correction applied
0 projection items activated
Selecting ICA components automatically#
Now that we’ve explored what components need to be removed, we can apply the automatic ICA component labeling algorithm, which will assign a probability value for each component being one of:
brain
muscle artifact
eye blink
heart beat
line noise
channel noise
other
The output of the ICLabel label_components
function produces
predicted probability values for each of these classes in that order.
See [1] for full details.
ic_labels = label_components(filt_raw, ica, method="iclabel")
# ICA0 was correctly identified as an eye blink, whereas ICA12 was
# classified as a muscle artifact.
print(ic_labels["labels"])
ica.plot_properties(raw, picks=[0, 12], verbose=False)
['eye blink', 'brain', 'brain', 'brain', 'brain', 'brain', 'muscle artifact', 'muscle artifact', 'other', 'brain', 'muscle artifact', 'muscle artifact', 'muscle artifact', 'muscle artifact', 'eye blink']
Extract Labels and Reconstruct Raw Data#
We can extract the labels of each component and exclude non-brain classified components, keeping ‘brain’ and ‘other’. “Other” is a catch-all that for non-classifiable components. We will stay on the side of caution and assume we cannot blindly remove these.
labels = ic_labels["labels"]
exclude_idx = [
idx for idx, label in enumerate(labels) if label not in ["brain", "other"]
]
print(f"Excluding these ICA components: {exclude_idx}")
Excluding these ICA components: [0, 6, 7, 10, 11, 12, 13, 14]
Now that the exclusions have been set, we can reconstruct the sensor signals
with artifacts removed using the apply
method
(remember, we’re applying the ICA solution from the filtered data to the
original unfiltered signal). Plotting the original raw data alongside the
reconstructed data shows that the heartbeat and blink artifacts are repaired.
# ica.apply() changes the Raw object in-place, so let's make a copy first:
reconst_raw = raw.copy()
ica.apply(reconst_raw, exclude=exclude_idx)
raw.plot(order=artifact_picks, n_channels=len(artifact_picks), show_scrollbars=False)
reconst_raw.plot(
order=artifact_picks, n_channels=len(artifact_picks), show_scrollbars=False
)
del reconst_raw
Applying ICA to Raw instance
Transforming to ICA space (15 components)
Zeroing out 8 ICA components
Projecting back using 59 PCA components
References#
Total running time of the script: (1 minutes 34.581 seconds)
Estimated memory usage: 489 MB