Note
Click here to download the full example code
This example demonstrates how to use autoreject
to find
channel-wise thresholds.
# Author: Mainak Jas <mainak.jas@telecom-paristech.fr>
# License: BSD-3-Clause
Let us first load the raw data using mne.io.read_raw_fif()
.
import mne
from mne import io
from mne.datasets import sample
data_path = sample.data_path()
raw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif'
raw = io.read_raw_fif(raw_fname, preload=True)
Out:
Opening raw data file /home/stefanappelhoff/mne_data/MNE-sample-data/MEG/sample/sample_audvis_filt-0-40_raw.fif...
Read a total of 4 projection items:
PCA-v1 (1 x 102) idle
PCA-v2 (1 x 102) idle
PCA-v3 (1 x 102) idle
Average EEG reference (1 x 60) idle
Range : 6450 ... 48149 = 42.956 ... 320.665 secs
Ready.
Reading 0 ... 41699 = 0.000 ... 277.709 secs...
We can extract the events (or triggers) for epoching our signal.
event_fname = data_path + ('/MEG/sample/sample_audvis_filt-0-40_raw-'
'eve.fif')
event_id = {'Auditory/Left': 1}
tmin, tmax = -0.2, 0.5
events = mne.read_events(event_fname)
Now that we have the events, we can extract the trials for the selection
of channels defined by picks
.
epochs = mne.Epochs(raw, events, event_id, tmin, tmax,
baseline=(None, 0),
reject=None, verbose=False, preload=True)
picks = mne.pick_types(epochs.info, meg='grad', eeg=False, stim=False,
eog=False, exclude='bads')
Now, we compute the channel-level thresholds using
autoreject.compute_thresholds()
. The method parameter will determine
how we will search for thresholds over a range of potential candidates.
import numpy as np # noqa
from autoreject import compute_thresholds # noqa
# Get a dictionary of rejection thresholds
threshes = compute_thresholds(epochs, picks=picks, method='random_search',
random_state=42, augment=False,
verbose=True)
Out:
/home/stefanappelhoff/Desktop/bids/autoreject/autoreject/utils.py:66: UserWarning: 2 channels are marked as bad. These will be ignored. If you want them to be considered by autoreject please remove them from epochs.info["bads"].
warnings.warn(
/home/stefanappelhoff/miniconda3/envs/autoreject/lib/python3.8/site-packages/mne/externals/tqdm/__init__.py:5: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
if LooseVersion(__version__) < LooseVersion('4.36'):
/home/stefanappelhoff/miniconda3/envs/autoreject/lib/python3.8/site-packages/mne/externals/tqdm/__init__.py:5: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
if LooseVersion(__version__) < LooseVersion('4.36'):
0%| | Computing thresholds ... : 0/203 [00:00<?, ?it/s]
0%| | Computing thresholds ... : 1/203 [00:00<00:34, 5.78it/s]
1%| | Computing thresholds ... : 2/203 [00:00<00:31, 6.44it/s]
1%|1 | Computing thresholds ... : 3/203 [00:00<00:29, 6.74it/s]
2%|1 | Computing thresholds ... : 4/203 [00:00<00:29, 6.79it/s]
2%|2 | Computing thresholds ... : 5/203 [00:00<00:28, 6.96it/s]
3%|2 | Computing thresholds ... : 6/203 [00:00<00:27, 7.07it/s]
3%|3 | Computing thresholds ... : 7/203 [00:00<00:27, 7.15it/s]
4%|3 | Computing thresholds ... : 8/203 [00:01<00:26, 7.29it/s]
4%|4 | Computing thresholds ... : 9/203 [00:01<00:26, 7.38it/s]
5%|4 | Computing thresholds ... : 10/203 [00:01<00:25, 7.51it/s]
5%|5 | Computing thresholds ... : 11/203 [00:01<00:25, 7.60it/s]
6%|5 | Computing thresholds ... : 12/203 [00:01<00:24, 7.67it/s]
6%|6 | Computing thresholds ... : 13/203 [00:01<00:24, 7.73it/s]
7%|6 | Computing thresholds ... : 14/203 [00:01<00:24, 7.80it/s]
7%|7 | Computing thresholds ... : 15/203 [00:01<00:24, 7.78it/s]
8%|7 | Computing thresholds ... : 16/203 [00:02<00:23, 7.81it/s]
8%|8 | Computing thresholds ... : 17/203 [00:02<00:23, 7.78it/s]
9%|8 | Computing thresholds ... : 18/203 [00:02<00:23, 7.83it/s]
9%|9 | Computing thresholds ... : 19/203 [00:02<00:23, 7.82it/s]
10%|9 | Computing thresholds ... : 20/203 [00:02<00:23, 7.74it/s]
10%|# | Computing thresholds ... : 21/203 [00:02<00:23, 7.70it/s]
11%|# | Computing thresholds ... : 22/203 [00:02<00:23, 7.66it/s]
11%|#1 | Computing thresholds ... : 23/203 [00:03<00:23, 7.65it/s]
12%|#1 | Computing thresholds ... : 24/203 [00:03<00:23, 7.65it/s]
12%|#2 | Computing thresholds ... : 25/203 [00:03<00:23, 7.64it/s]
13%|#2 | Computing thresholds ... : 26/203 [00:03<00:23, 7.63it/s]
13%|#3 | Computing thresholds ... : 27/203 [00:03<00:22, 7.70it/s]
14%|#3 | Computing thresholds ... : 28/203 [00:03<00:22, 7.71it/s]
14%|#4 | Computing thresholds ... : 29/203 [00:03<00:22, 7.72it/s]
15%|#4 | Computing thresholds ... : 30/203 [00:03<00:22, 7.76it/s]
15%|#5 | Computing thresholds ... : 31/203 [00:04<00:22, 7.79it/s]
16%|#5 | Computing thresholds ... : 32/203 [00:04<00:21, 7.82it/s]
16%|#6 | Computing thresholds ... : 33/203 [00:04<00:21, 7.85it/s]
17%|#6 | Computing thresholds ... : 34/203 [00:04<00:21, 7.85it/s]
17%|#7 | Computing thresholds ... : 35/203 [00:04<00:21, 7.87it/s]
18%|#7 | Computing thresholds ... : 36/203 [00:04<00:21, 7.90it/s]
18%|#8 | Computing thresholds ... : 37/203 [00:04<00:20, 7.93it/s]
19%|#8 | Computing thresholds ... : 38/203 [00:04<00:20, 7.95it/s]
19%|#9 | Computing thresholds ... : 39/203 [00:04<00:20, 7.97it/s]
20%|#9 | Computing thresholds ... : 40/203 [00:05<00:20, 7.99it/s]
20%|## | Computing thresholds ... : 41/203 [00:05<00:20, 8.01it/s]
21%|## | Computing thresholds ... : 42/203 [00:05<00:20, 8.02it/s]
21%|##1 | Computing thresholds ... : 43/203 [00:05<00:19, 8.03it/s]
22%|##1 | Computing thresholds ... : 44/203 [00:05<00:19, 8.05it/s]
22%|##2 | Computing thresholds ... : 45/203 [00:05<00:19, 8.03it/s]
23%|##2 | Computing thresholds ... : 46/203 [00:05<00:19, 8.01it/s]
23%|##3 | Computing thresholds ... : 47/203 [00:05<00:19, 8.02it/s]
24%|##3 | Computing thresholds ... : 48/203 [00:06<00:19, 8.01it/s]
24%|##4 | Computing thresholds ... : 49/203 [00:06<00:19, 8.01it/s]
25%|##4 | Computing thresholds ... : 50/203 [00:06<00:19, 8.02it/s]
25%|##5 | Computing thresholds ... : 51/203 [00:06<00:19, 8.00it/s]
26%|##5 | Computing thresholds ... : 52/203 [00:06<00:18, 7.97it/s]
26%|##6 | Computing thresholds ... : 53/203 [00:06<00:18, 8.00it/s]
27%|##6 | Computing thresholds ... : 54/203 [00:06<00:18, 8.00it/s]
27%|##7 | Computing thresholds ... : 55/203 [00:06<00:18, 8.02it/s]
28%|##7 | Computing thresholds ... : 56/203 [00:07<00:18, 8.04it/s]
28%|##8 | Computing thresholds ... : 57/203 [00:07<00:18, 8.03it/s]
29%|##8 | Computing thresholds ... : 58/203 [00:07<00:18, 8.04it/s]
29%|##9 | Computing thresholds ... : 59/203 [00:07<00:17, 8.02it/s]
30%|##9 | Computing thresholds ... : 60/203 [00:07<00:17, 8.04it/s]
30%|### | Computing thresholds ... : 61/203 [00:07<00:17, 8.05it/s]
31%|### | Computing thresholds ... : 62/203 [00:07<00:17, 8.06it/s]
31%|###1 | Computing thresholds ... : 63/203 [00:07<00:17, 8.08it/s]
32%|###1 | Computing thresholds ... : 64/203 [00:08<00:17, 8.07it/s]
32%|###2 | Computing thresholds ... : 65/203 [00:08<00:17, 8.08it/s]
33%|###2 | Computing thresholds ... : 66/203 [00:08<00:16, 8.09it/s]
33%|###3 | Computing thresholds ... : 67/203 [00:08<00:16, 8.09it/s]
33%|###3 | Computing thresholds ... : 68/203 [00:08<00:16, 8.09it/s]
34%|###3 | Computing thresholds ... : 69/203 [00:08<00:16, 8.10it/s]
34%|###4 | Computing thresholds ... : 70/203 [00:08<00:16, 8.09it/s]
35%|###4 | Computing thresholds ... : 71/203 [00:08<00:16, 8.10it/s]
35%|###5 | Computing thresholds ... : 72/203 [00:09<00:16, 8.11it/s]
36%|###5 | Computing thresholds ... : 73/203 [00:09<00:16, 8.11it/s]
36%|###6 | Computing thresholds ... : 74/203 [00:09<00:15, 8.12it/s]
37%|###6 | Computing thresholds ... : 75/203 [00:09<00:15, 8.12it/s]
37%|###7 | Computing thresholds ... : 76/203 [00:09<00:15, 8.13it/s]
38%|###7 | Computing thresholds ... : 77/203 [00:09<00:15, 8.13it/s]
38%|###8 | Computing thresholds ... : 78/203 [00:09<00:15, 8.11it/s]
39%|###8 | Computing thresholds ... : 79/203 [00:09<00:15, 8.13it/s]
39%|###9 | Computing thresholds ... : 80/203 [00:10<00:15, 8.15it/s]
40%|###9 | Computing thresholds ... : 81/203 [00:10<00:14, 8.15it/s]
40%|#### | Computing thresholds ... : 82/203 [00:10<00:14, 8.15it/s]
41%|#### | Computing thresholds ... : 83/203 [00:10<00:14, 8.16it/s]
41%|####1 | Computing thresholds ... : 84/203 [00:10<00:14, 8.15it/s]
42%|####1 | Computing thresholds ... : 85/203 [00:10<00:14, 8.15it/s]
42%|####2 | Computing thresholds ... : 86/203 [00:10<00:14, 8.17it/s]
43%|####2 | Computing thresholds ... : 87/203 [00:10<00:14, 8.16it/s]
43%|####3 | Computing thresholds ... : 88/203 [00:11<00:14, 8.14it/s]
44%|####3 | Computing thresholds ... : 89/203 [00:11<00:14, 8.13it/s]
44%|####4 | Computing thresholds ... : 90/203 [00:11<00:13, 8.10it/s]
45%|####4 | Computing thresholds ... : 91/203 [00:11<00:13, 8.11it/s]
45%|####5 | Computing thresholds ... : 92/203 [00:11<00:13, 8.11it/s]
46%|####5 | Computing thresholds ... : 93/203 [00:11<00:13, 8.11it/s]
46%|####6 | Computing thresholds ... : 94/203 [00:11<00:13, 8.11it/s]
47%|####6 | Computing thresholds ... : 95/203 [00:11<00:13, 8.13it/s]
47%|####7 | Computing thresholds ... : 96/203 [00:11<00:13, 8.14it/s]
48%|####7 | Computing thresholds ... : 97/203 [00:12<00:13, 8.14it/s]
48%|####8 | Computing thresholds ... : 98/203 [00:12<00:12, 8.08it/s]
49%|####8 | Computing thresholds ... : 99/203 [00:12<00:12, 8.09it/s]
49%|####9 | Computing thresholds ... : 100/203 [00:12<00:12, 8.10it/s]
50%|####9 | Computing thresholds ... : 101/203 [00:12<00:12, 8.11it/s]
50%|##### | Computing thresholds ... : 102/203 [00:12<00:12, 8.12it/s]
51%|##### | Computing thresholds ... : 103/203 [00:12<00:12, 8.07it/s]
51%|#####1 | Computing thresholds ... : 104/203 [00:13<00:12, 8.08it/s]
52%|#####1 | Computing thresholds ... : 105/203 [00:13<00:12, 8.09it/s]
52%|#####2 | Computing thresholds ... : 106/203 [00:13<00:11, 8.09it/s]
53%|#####2 | Computing thresholds ... : 107/203 [00:13<00:11, 8.09it/s]
53%|#####3 | Computing thresholds ... : 108/203 [00:13<00:11, 8.02it/s]
54%|#####3 | Computing thresholds ... : 109/203 [00:13<00:11, 8.01it/s]
54%|#####4 | Computing thresholds ... : 110/203 [00:13<00:11, 8.02it/s]
55%|#####4 | Computing thresholds ... : 111/203 [00:13<00:11, 8.03it/s]
55%|#####5 | Computing thresholds ... : 112/203 [00:14<00:11, 8.04it/s]
56%|#####5 | Computing thresholds ... : 113/203 [00:14<00:11, 8.05it/s]
56%|#####6 | Computing thresholds ... : 114/203 [00:14<00:11, 8.07it/s]
57%|#####6 | Computing thresholds ... : 115/203 [00:14<00:10, 8.07it/s]
57%|#####7 | Computing thresholds ... : 116/203 [00:14<00:10, 8.09it/s]
58%|#####7 | Computing thresholds ... : 117/203 [00:14<00:10, 8.07it/s]
58%|#####8 | Computing thresholds ... : 118/203 [00:14<00:10, 8.05it/s]
59%|#####8 | Computing thresholds ... : 119/203 [00:14<00:10, 8.04it/s]
59%|#####9 | Computing thresholds ... : 120/203 [00:15<00:10, 8.01it/s]
60%|#####9 | Computing thresholds ... : 121/203 [00:15<00:10, 8.00it/s]
60%|###### | Computing thresholds ... : 122/203 [00:15<00:10, 8.00it/s]
61%|###### | Computing thresholds ... : 123/203 [00:15<00:09, 8.01it/s]
61%|######1 | Computing thresholds ... : 124/203 [00:15<00:09, 8.02it/s]
62%|######1 | Computing thresholds ... : 125/203 [00:15<00:09, 8.01it/s]
62%|######2 | Computing thresholds ... : 126/203 [00:15<00:09, 8.00it/s]
63%|######2 | Computing thresholds ... : 127/203 [00:15<00:09, 8.02it/s]
63%|######3 | Computing thresholds ... : 128/203 [00:16<00:09, 8.03it/s]
64%|######3 | Computing thresholds ... : 129/203 [00:16<00:09, 8.03it/s]
64%|######4 | Computing thresholds ... : 130/203 [00:16<00:09, 8.04it/s]
65%|######4 | Computing thresholds ... : 131/203 [00:16<00:08, 8.05it/s]
65%|######5 | Computing thresholds ... : 132/203 [00:16<00:08, 8.06it/s]
66%|######5 | Computing thresholds ... : 133/203 [00:16<00:08, 7.99it/s]
66%|######6 | Computing thresholds ... : 134/203 [00:16<00:08, 8.01it/s]
67%|######6 | Computing thresholds ... : 135/203 [00:16<00:08, 8.00it/s]
67%|######6 | Computing thresholds ... : 136/203 [00:17<00:08, 8.00it/s]
67%|######7 | Computing thresholds ... : 137/203 [00:17<00:08, 8.00it/s]
68%|######7 | Computing thresholds ... : 138/203 [00:17<00:08, 7.99it/s]
68%|######8 | Computing thresholds ... : 139/203 [00:17<00:08, 7.98it/s]
69%|######8 | Computing thresholds ... : 140/203 [00:17<00:07, 7.98it/s]
69%|######9 | Computing thresholds ... : 141/203 [00:17<00:07, 7.98it/s]
70%|######9 | Computing thresholds ... : 142/203 [00:17<00:07, 7.97it/s]
70%|####### | Computing thresholds ... : 143/203 [00:17<00:07, 7.98it/s]
71%|####### | Computing thresholds ... : 144/203 [00:18<00:07, 7.99it/s]
71%|#######1 | Computing thresholds ... : 145/203 [00:18<00:07, 7.99it/s]
72%|#######1 | Computing thresholds ... : 146/203 [00:18<00:07, 7.95it/s]
72%|#######2 | Computing thresholds ... : 147/203 [00:18<00:07, 7.93it/s]
73%|#######2 | Computing thresholds ... : 148/203 [00:18<00:06, 7.86it/s]
73%|#######3 | Computing thresholds ... : 149/203 [00:18<00:06, 7.82it/s]
74%|#######3 | Computing thresholds ... : 150/203 [00:18<00:06, 7.77it/s]
74%|#######4 | Computing thresholds ... : 151/203 [00:19<00:06, 7.60it/s]
75%|#######4 | Computing thresholds ... : 152/203 [00:19<00:06, 7.63it/s]
75%|#######5 | Computing thresholds ... : 153/203 [00:19<00:06, 7.58it/s]
76%|#######5 | Computing thresholds ... : 154/203 [00:19<00:06, 7.57it/s]
76%|#######6 | Computing thresholds ... : 155/203 [00:19<00:06, 7.59it/s]
77%|#######6 | Computing thresholds ... : 156/203 [00:19<00:06, 7.60it/s]
77%|#######7 | Computing thresholds ... : 157/203 [00:19<00:06, 7.62it/s]
78%|#######7 | Computing thresholds ... : 158/203 [00:19<00:05, 7.61it/s]
78%|#######8 | Computing thresholds ... : 159/203 [00:20<00:05, 7.63it/s]
79%|#######8 | Computing thresholds ... : 160/203 [00:20<00:05, 7.66it/s]
79%|#######9 | Computing thresholds ... : 161/203 [00:20<00:05, 7.67it/s]
80%|#######9 | Computing thresholds ... : 162/203 [00:20<00:05, 7.69it/s]
80%|######## | Computing thresholds ... : 163/203 [00:20<00:05, 7.69it/s]
81%|######## | Computing thresholds ... : 164/203 [00:20<00:05, 7.71it/s]
81%|########1 | Computing thresholds ... : 165/203 [00:20<00:04, 7.72it/s]
82%|########1 | Computing thresholds ... : 166/203 [00:20<00:04, 7.75it/s]
82%|########2 | Computing thresholds ... : 167/203 [00:21<00:04, 7.74it/s]
83%|########2 | Computing thresholds ... : 168/203 [00:21<00:04, 7.44it/s]
83%|########3 | Computing thresholds ... : 169/203 [00:21<00:04, 7.46it/s]
84%|########3 | Computing thresholds ... : 170/203 [00:21<00:04, 7.49it/s]
84%|########4 | Computing thresholds ... : 171/203 [00:21<00:04, 7.51it/s]
85%|########4 | Computing thresholds ... : 172/203 [00:21<00:04, 7.51it/s]
85%|########5 | Computing thresholds ... : 173/203 [00:21<00:04, 7.46it/s]
86%|########5 | Computing thresholds ... : 174/203 [00:22<00:03, 7.44it/s]
86%|########6 | Computing thresholds ... : 175/203 [00:22<00:03, 7.46it/s]
87%|########6 | Computing thresholds ... : 176/203 [00:22<00:03, 7.50it/s]
87%|########7 | Computing thresholds ... : 177/203 [00:22<00:03, 7.49it/s]
88%|########7 | Computing thresholds ... : 178/203 [00:22<00:03, 7.49it/s]
88%|########8 | Computing thresholds ... : 179/203 [00:22<00:03, 7.51it/s]
89%|########8 | Computing thresholds ... : 180/203 [00:22<00:03, 7.53it/s]
89%|########9 | Computing thresholds ... : 181/203 [00:23<00:02, 7.55it/s]
90%|########9 | Computing thresholds ... : 182/203 [00:23<00:02, 7.56it/s]
90%|######### | Computing thresholds ... : 183/203 [00:23<00:02, 7.54it/s]
91%|######### | Computing thresholds ... : 184/203 [00:23<00:02, 7.56it/s]
91%|#########1| Computing thresholds ... : 185/203 [00:23<00:02, 7.57it/s]
92%|#########1| Computing thresholds ... : 186/203 [00:23<00:02, 7.46it/s]
92%|#########2| Computing thresholds ... : 187/203 [00:23<00:02, 7.49it/s]
93%|#########2| Computing thresholds ... : 188/203 [00:23<00:02, 7.49it/s]
93%|#########3| Computing thresholds ... : 189/203 [00:24<00:01, 7.52it/s]
94%|#########3| Computing thresholds ... : 190/203 [00:24<00:01, 7.55it/s]
94%|#########4| Computing thresholds ... : 191/203 [00:24<00:01, 7.57it/s]
95%|#########4| Computing thresholds ... : 192/203 [00:24<00:01, 7.59it/s]
95%|#########5| Computing thresholds ... : 193/203 [00:24<00:01, 7.60it/s]
96%|#########5| Computing thresholds ... : 194/203 [00:24<00:01, 7.59it/s]
96%|#########6| Computing thresholds ... : 195/203 [00:24<00:01, 7.60it/s]
97%|#########6| Computing thresholds ... : 196/203 [00:24<00:00, 7.62it/s]
97%|#########7| Computing thresholds ... : 197/203 [00:25<00:00, 7.47it/s]
98%|#########7| Computing thresholds ... : 198/203 [00:25<00:00, 7.03it/s]
98%|#########8| Computing thresholds ... : 199/203 [00:25<00:00, 6.98it/s]
99%|#########8| Computing thresholds ... : 200/203 [00:25<00:00, 7.02it/s]
99%|#########9| Computing thresholds ... : 201/203 [00:25<00:00, 6.98it/s]
100%|#########9| Computing thresholds ... : 202/203 [00:26<00:00, 7.02it/s]
100%|##########| Computing thresholds ... : 203/203 [00:26<00:00, 7.04it/s]
100%|##########| Computing thresholds ... : 203/203 [00:26<00:00, 7.76it/s]
Finally, let us plot a histogram of the channel-level thresholds to verify that the thresholds are indeed different for different sensors.
import matplotlib.pyplot as plt # noqa
from autoreject import set_matplotlib_defaults # noqa
set_matplotlib_defaults(plt)
unit = r'fT/cm'
scaling = 1e13
plt.figure(figsize=(6, 5))
plt.tick_params(axis='x', which='both', bottom='off', top='off')
plt.tick_params(axis='y', which='both', left='off', right='off')
plt.hist(scaling * np.array(list(threshes.values())), 30,
color='g', alpha=0.4)
plt.xlabel('Threshold (%s)' % unit)
plt.ylabel('Number of sensors')
plt.xlim((100, 950))
plt.tight_layout()
plt.show()
Total running time of the script: ( 0 minutes 26.868 seconds)