Automatically repair epochsΒΆ

This example demonstrates how to use autoreject to automatically repair epochs.

# Author: Mainak Jas <mainak.jas@telecom-paristech.fr>
#         Denis A. Engemann <denis.engemann@gmail.com>
# License: BSD (3-clause)

Let us first define the parameters. n_interpolates are the \(\rho\) values that we would like autoreject to try and consensus_percs are the \(\kappa\) values that autoreject will try.

Epochs with more than \(\kappa * N\) sensors (\(N\) total sensors) bad are dropped. For the rest of the epochs, the worst \(\rho\) bad sensors (as determined by channel-level thresholds) are interpolated. The exact values of these parameters are not preselected but learned from the data. If the number of bad sensors for a particular trial is less than \(\rho\), all the bad sensors are interpolated.

import numpy as np

n_interpolates = np.array([1, 4, 32])
consensus_percs = np.linspace(0, 1.0, 11)

For the purposes of this example, we shall use the MNE sample dataset. Therefore, let us make some MNE related imports.

import mne  # noqa
from mne.utils import check_random_state  # noqa
from mne.datasets import sample  # noqa

Now, we can import the class required for rejecting and repairing bad epochs. autoreject.compute_thresholds() is a callable which must be provided to the autoreject.AutoReject class for computing the channel-level thresholds.

from autoreject import (AutoReject, set_matplotlib_defaults)  # noqa

Let us now read in the raw fif file for MNE sample dataset.

check_random_state(42)

data_path = sample.data_path()
raw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif'
raw = mne.io.read_raw_fif(raw_fname, preload=True)

Out:

Opening raw data file /home/mainak/Desktop/projects/github_repos/mne-python/examples/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.
Current compensation grade : 0
Reading 0 ... 41699  =      0.000 ...   277.709 secs...

We can then read in the events

event_fname = data_path + ('/MEG/sample/sample_audvis_filt-0-40_raw-'
                           'eve.fif')
event_id = {'Auditory/Left': 1, 'Auditory/Right': 2}
tmin, tmax = -0.2, 0.5

events = mne.read_events(event_fname)

And pick MEG channels for repairing. Currently, autoreject can repair only one channel type at a time.

raw.info['bads'] = []
picks = mne.pick_types(raw.info, meg='grad', eeg=False, stim=False, eog=False,
                       include=[], exclude=[])

Now, we can create epochs. The reject params will be set to None because we do not want epochs to be dropped when instantiating mne.Epochs.

raw.info['projs'] = list()  # remove proj, don't proj while interpolating
epochs = mne.Epochs(raw, events, event_id, tmin, tmax,
                    baseline=(None, 0), reject=None,
                    verbose=False, detrend=0, preload=True)

autoreject.AutoReject internally does cross-validation to determine the optimal values \(\rho^{*}\) and \(\kappa^{*}\)

Note that:class:autoreject.AutoReject by design supports multiple channels. If no picks are passed separate solutions will be computed for each channel type and internally combines. This then readily supports cleaning unseen epochs from the different channel types used during fit. Here we only use a subset of channels to save time.

ar = AutoReject(n_interpolates, consensus_percs, picks=picks,
                thresh_method='random_search', random_state=42)

# Note that fitting and transforming can be done on different compatible
# portions of data if needed.
ar.fit(epochs['Auditory/Left'])
epochs_clean = ar.transform(epochs['Auditory/Left'])
evoked_clean = epochs_clean.average()
evoked = epochs['Auditory/Left'].average()

Out:

Running autoreject on ch_type=grad

[                                        ] 0.49% Creating augmented epochs |
[                                        ] 0.98% Creating augmented epochs /
[                                        ] 1.47% Creating augmented epochs -
[                                        ] 1.96% Creating augmented epochs \
[                                        ] 2.45% Creating augmented epochs |
[.                                       ] 2.94% Creating augmented epochs /
[.                                       ] 3.43% Creating augmented epochs -
[.                                       ] 3.92% Creating augmented epochs \
[.                                       ] 4.41% Creating augmented epochs |
[.                                       ] 4.90% Creating augmented epochs /
[..                                      ] 5.39% Creating augmented epochs -
[..                                      ] 5.88% Creating augmented epochs \
[..                                      ] 6.37% Creating augmented epochs |
[..                                      ] 6.86% Creating augmented epochs /
[..                                      ] 7.35% Creating augmented epochs -
[...                                     ] 7.84% Creating augmented epochs \
[...                                     ] 8.33% Creating augmented epochs |
[...                                     ] 8.82% Creating augmented epochs /
[...                                     ] 9.31% Creating augmented epochs -
[...                                     ] 9.80% Creating augmented epochs \
[....                                    ] 10.29% Creating augmented epochs |
[....                                    ] 10.78% Creating augmented epochs /
[....                                    ] 11.27% Creating augmented epochs -
[....                                    ] 11.76% Creating augmented epochs \
[....                                    ] 12.25% Creating augmented epochs |
[.....                                   ] 12.75% Creating augmented epochs /
[.....                                   ] 13.24% Creating augmented epochs -
[.....                                   ] 13.73% Creating augmented epochs \
[.....                                   ] 14.22% Creating augmented epochs |
[.....                                   ] 14.71% Creating augmented epochs /
[......                                  ] 15.20% Creating augmented epochs -
[......                                  ] 15.69% Creating augmented epochs \
[......                                  ] 16.18% Creating augmented epochs |
[......                                  ] 16.67% Creating augmented epochs /
[......                                  ] 17.16% Creating augmented epochs -
[.......                                 ] 17.65% Creating augmented epochs \
[.......                                 ] 18.14% Creating augmented epochs |
[.......                                 ] 18.63% Creating augmented epochs /
[.......                                 ] 19.12% Creating augmented epochs -
[.......                                 ] 19.61% Creating augmented epochs \
[........                                ] 20.10% Creating augmented epochs |
[........                                ] 20.59% Creating augmented epochs /
[........                                ] 21.08% Creating augmented epochs -
[........                                ] 21.57% Creating augmented epochs \
[........                                ] 22.06% Creating augmented epochs |
[.........                               ] 22.55% Creating augmented epochs /
[.........                               ] 23.04% Creating augmented epochs -
[.........                               ] 23.53% Creating augmented epochs \
[.........                               ] 24.02% Creating augmented epochs |
[.........                               ] 24.51% Creating augmented epochs /
[..........                              ] 25.00% Creating augmented epochs -
[..........                              ] 25.49% Creating augmented epochs \
[..........                              ] 25.98% Creating augmented epochs |
[..........                              ] 26.47% Creating augmented epochs /
[..........                              ] 26.96% Creating augmented epochs -
[..........                              ] 27.45% Creating augmented epochs \
[...........                             ] 27.94% Creating augmented epochs |
[...........                             ] 28.43% Creating augmented epochs /
[...........                             ] 28.92% Creating augmented epochs -
[...........                             ] 29.41% Creating augmented epochs \
[...........                             ] 29.90% Creating augmented epochs |
[............                            ] 30.39% Creating augmented epochs /
[............                            ] 30.88% Creating augmented epochs -
[............                            ] 31.37% Creating augmented epochs \
[............                            ] 31.86% Creating augmented epochs |
[............                            ] 32.35% Creating augmented epochs /
[.............                           ] 32.84% Creating augmented epochs -
[.............                           ] 33.33% Creating augmented epochs \
[.............                           ] 33.82% Creating augmented epochs |
[.............                           ] 34.31% Creating augmented epochs /
[.............                           ] 34.80% Creating augmented epochs -
[..............                          ] 35.29% Creating augmented epochs \
[..............                          ] 35.78% Creating augmented epochs |
[..............                          ] 36.27% Creating augmented epochs /
[..............                          ] 36.76% Creating augmented epochs -
[..............                          ] 37.25% Creating augmented epochs \
[...............                         ] 37.75% Creating augmented epochs |
[...............                         ] 38.24% Creating augmented epochs /
[...............                         ] 38.73% Creating augmented epochs -
[...............                         ] 39.22% Creating augmented epochs \
[...............                         ] 39.71% Creating augmented epochs |
[................                        ] 40.20% Creating augmented epochs /
[................                        ] 40.69% Creating augmented epochs -
[................                        ] 41.18% Creating augmented epochs \
[................                        ] 41.67% Creating augmented epochs |
[................                        ] 42.16% Creating augmented epochs /
[.................                       ] 42.65% Creating augmented epochs -
[.................                       ] 43.14% Creating augmented epochs \
[.................                       ] 43.63% Creating augmented epochs |
[.................                       ] 44.12% Creating augmented epochs /
[.................                       ] 44.61% Creating augmented epochs -
[..................                      ] 45.10% Creating augmented epochs \
[..................                      ] 45.59% Creating augmented epochs |
[..................                      ] 46.08% Creating augmented epochs /
[..................                      ] 46.57% Creating augmented epochs -
[..................                      ] 47.06% Creating augmented epochs \
[...................                     ] 47.55% Creating augmented epochs |
[...................                     ] 48.04% Creating augmented epochs /
[...................                     ] 48.53% Creating augmented epochs -
[...................                     ] 49.02% Creating augmented epochs \
[...................                     ] 49.51% Creating augmented epochs |
[....................                    ] 50.00% Creating augmented epochs /
[....................                    ] 50.49% Creating augmented epochs -
[....................                    ] 50.98% Creating augmented epochs \
[....................                    ] 51.47% Creating augmented epochs |
[....................                    ] 51.96% Creating augmented epochs /
[....................                    ] 52.45% Creating augmented epochs -
[.....................                   ] 52.94% Creating augmented epochs \
[.....................                   ] 53.43% Creating augmented epochs |
[.....................                   ] 53.92% Creating augmented epochs /
[.....................                   ] 54.41% Creating augmented epochs -
[.....................                   ] 54.90% Creating augmented epochs \
[......................                  ] 55.39% Creating augmented epochs |
[......................                  ] 55.88% Creating augmented epochs /
[......................                  ] 56.37% Creating augmented epochs -
[......................                  ] 56.86% Creating augmented epochs \
[......................                  ] 57.35% Creating augmented epochs |
[.......................                 ] 57.84% Creating augmented epochs /
[.......................                 ] 58.33% Creating augmented epochs -
[.......................                 ] 58.82% Creating augmented epochs \
[.......................                 ] 59.31% Creating augmented epochs |
[.......................                 ] 59.80% Creating augmented epochs /
[........................                ] 60.29% Creating augmented epochs -
[........................                ] 60.78% Creating augmented epochs \
[........................                ] 61.27% Creating augmented epochs |
[........................                ] 61.76% Creating augmented epochs /
[........................                ] 62.25% Creating augmented epochs -
[.........................               ] 62.75% Creating augmented epochs \
[.........................               ] 63.24% Creating augmented epochs |
[.........................               ] 63.73% Creating augmented epochs /
[.........................               ] 64.22% Creating augmented epochs -
[.........................               ] 64.71% Creating augmented epochs \
[..........................              ] 65.20% Creating augmented epochs |
[..........................              ] 65.69% Creating augmented epochs /
[..........................              ] 66.18% Creating augmented epochs -
[..........................              ] 66.67% Creating augmented epochs \
[..........................              ] 67.16% Creating augmented epochs |
[...........................             ] 67.65% Creating augmented epochs /
[...........................             ] 68.14% Creating augmented epochs -
[...........................             ] 68.63% Creating augmented epochs \
[...........................             ] 69.12% Creating augmented epochs |
[...........................             ] 69.61% Creating augmented epochs /
[............................            ] 70.10% Creating augmented epochs -
[............................            ] 70.59% Creating augmented epochs \
[............................            ] 71.08% Creating augmented epochs |
[............................            ] 71.57% Creating augmented epochs /
[............................            ] 72.06% Creating augmented epochs -
[.............................           ] 72.55% Creating augmented epochs \
[.............................           ] 73.04% Creating augmented epochs |
[.............................           ] 73.53% Creating augmented epochs /
[.............................           ] 74.02% Creating augmented epochs -
[.............................           ] 74.51% Creating augmented epochs \
[..............................          ] 75.00% Creating augmented epochs |
[..............................          ] 75.49% Creating augmented epochs /
[..............................          ] 75.98% Creating augmented epochs -
[..............................          ] 76.47% Creating augmented epochs \
[..............................          ] 76.96% Creating augmented epochs |
[..............................          ] 77.45% Creating augmented epochs /
[...............................         ] 77.94% Creating augmented epochs -
[...............................         ] 78.43% Creating augmented epochs \
[...............................         ] 78.92% Creating augmented epochs |
[...............................         ] 79.41% Creating augmented epochs /
[...............................         ] 79.90% Creating augmented epochs -
[................................        ] 80.39% Creating augmented epochs \
[................................        ] 80.88% Creating augmented epochs |
[................................        ] 81.37% Creating augmented epochs /
[................................        ] 81.86% Creating augmented epochs -
[................................        ] 82.35% Creating augmented epochs \
[.................................       ] 82.84% Creating augmented epochs |
[.................................       ] 83.33% Creating augmented epochs /
[.................................       ] 83.82% Creating augmented epochs -
[.................................       ] 84.31% Creating augmented epochs \
[.................................       ] 84.80% Creating augmented epochs |
[..................................      ] 85.29% Creating augmented epochs /
[..................................      ] 85.78% Creating augmented epochs -
[..................................      ] 86.27% Creating augmented epochs \
[..................................      ] 86.76% Creating augmented epochs |
[..................................      ] 87.25% Creating augmented epochs /
[...................................     ] 87.75% Creating augmented epochs -
[...................................     ] 88.24% Creating augmented epochs \
[...................................     ] 88.73% Creating augmented epochs |
[...................................     ] 89.22% Creating augmented epochs /
[...................................     ] 89.71% Creating augmented epochs -
[....................................    ] 90.20% Creating augmented epochs \
[....................................    ] 90.69% Creating augmented epochs |
[....................................    ] 91.18% Creating augmented epochs /
[....................................    ] 91.67% Creating augmented epochs -
[....................................    ] 92.16% Creating augmented epochs \
[.....................................   ] 92.65% Creating augmented epochs |
[.....................................   ] 93.14% Creating augmented epochs /
[.....................................   ] 93.63% Creating augmented epochs -
[.....................................   ] 94.12% Creating augmented epochs \
[.....................................   ] 94.61% Creating augmented epochs |
[......................................  ] 95.10% Creating augmented epochs /
[......................................  ] 95.59% Creating augmented epochs -
[......................................  ] 96.08% Creating augmented epochs \
[......................................  ] 96.57% Creating augmented epochs |
[......................................  ] 97.06% Creating augmented epochs /
[....................................... ] 97.55% Creating augmented epochs -
[....................................... ] 98.04% Creating augmented epochs \
[....................................... ] 98.53% Creating augmented epochs |
[....................................... ] 99.02% Creating augmented epochs /
[....................................... ] 99.51% Creating augmented epochs -
[........................................] 100.00% Creating augmented epochs \ Computing thresholds ...
[Parallel(n_jobs=1)]: Done   1 out of   1 | elapsed:    0.3s remaining:    0.0s
[Parallel(n_jobs=1)]: Done   2 out of   2 | elapsed:    0.6s remaining:    0.0s
[Parallel(n_jobs=1)]: Done   3 out of   3 | elapsed:    0.9s remaining:    0.0s
[Parallel(n_jobs=1)]: Done   4 out of   4 | elapsed:    1.2s remaining:    0.0s
[Parallel(n_jobs=1)]: Done   5 out of   5 | elapsed:    1.5s remaining:    0.0s
[Parallel(n_jobs=1)]: Done   6 out of   6 | elapsed:    1.8s remaining:    0.0s
[Parallel(n_jobs=1)]: Done   7 out of   7 | elapsed:    2.1s remaining:    0.0s
[Parallel(n_jobs=1)]: Done   8 out of   8 | elapsed:    2.4s remaining:    0.0s
[Parallel(n_jobs=1)]: Done   9 out of   9 | elapsed:    2.6s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  10 out of  10 | elapsed:    2.9s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  11 out of  11 | elapsed:    3.2s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  12 out of  12 | elapsed:    3.5s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  13 out of  13 | elapsed:    3.8s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  14 out of  14 | elapsed:    4.1s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  15 out of  15 | elapsed:    4.4s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  16 out of  16 | elapsed:    4.6s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  17 out of  17 | elapsed:    4.9s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  18 out of  18 | elapsed:    5.2s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  19 out of  19 | elapsed:    5.5s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  20 out of  20 | elapsed:    5.8s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  21 out of  21 | elapsed:    6.1s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  22 out of  22 | elapsed:    6.3s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  23 out of  23 | elapsed:    6.6s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  24 out of  24 | elapsed:    6.9s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  25 out of  25 | elapsed:    7.2s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  26 out of  26 | elapsed:    7.5s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  27 out of  27 | elapsed:    7.7s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  28 out of  28 | elapsed:    8.0s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  29 out of  29 | elapsed:    8.3s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  30 out of  30 | elapsed:    8.6s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  31 out of  31 | elapsed:    8.9s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  32 out of  32 | elapsed:    9.2s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  33 out of  33 | elapsed:    9.5s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  34 out of  34 | elapsed:    9.7s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  35 out of  35 | elapsed:   10.0s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  36 out of  36 | elapsed:   10.3s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  37 out of  37 | elapsed:   10.6s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  38 out of  38 | elapsed:   10.8s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  39 out of  39 | elapsed:   11.1s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  40 out of  40 | elapsed:   11.4s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  41 out of  41 | elapsed:   11.7s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  42 out of  42 | elapsed:   12.0s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  43 out of  43 | elapsed:   12.2s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  44 out of  44 | elapsed:   12.6s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  45 out of  45 | elapsed:   12.8s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  46 out of  46 | elapsed:   13.1s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  47 out of  47 | elapsed:   13.4s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  48 out of  48 | elapsed:   13.7s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  49 out of  49 | elapsed:   13.9s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  50 out of  50 | elapsed:   14.2s remaining:    0.0s
[Parallel(n_jobs=1)]: Done 204 out of 204 | elapsed:  1.0min finished

[                                        ] 1.39% Repairing epochs |
[.                                       ] 2.78% Repairing epochs /
[.                                       ] 4.17% Repairing epochs -
[..                                      ] 5.56% Repairing epochs \
[..                                      ] 6.94% Repairing epochs |
[...                                     ] 8.33% Repairing epochs /
[...                                     ] 9.72% Repairing epochs -
[....                                    ] 11.11% Repairing epochs \
[.....                                   ] 12.50% Repairing epochs |
[.....                                   ] 13.89% Repairing epochs /
[......                                  ] 15.28% Repairing epochs -
[......                                  ] 16.67% Repairing epochs \
[.......                                 ] 18.06% Repairing epochs |
[.......                                 ] 19.44% Repairing epochs /
[........                                ] 20.83% Repairing epochs -
[........                                ] 22.22% Repairing epochs \
[.........                               ] 23.61% Repairing epochs |
[..........                              ] 25.00% Repairing epochs /
[..........                              ] 26.39% Repairing epochs -
[...........                             ] 27.78% Repairing epochs \
[...........                             ] 29.17% Repairing epochs |
[............                            ] 30.56% Repairing epochs /
[............                            ] 31.94% Repairing epochs -
[.............                           ] 33.33% Repairing epochs \
[.............                           ] 34.72% Repairing epochs |
[..............                          ] 36.11% Repairing epochs /
[...............                         ] 37.50% Repairing epochs -
[...............                         ] 38.89% Repairing epochs \
[................                        ] 40.28% Repairing epochs |
[................                        ] 41.67% Repairing epochs /
[.................                       ] 43.06% Repairing epochs -
[.................                       ] 44.44% Repairing epochs \
[..................                      ] 45.83% Repairing epochs |
[..................                      ] 47.22% Repairing epochs /
[...................                     ] 48.61% Repairing epochs -
[....................                    ] 50.00% Repairing epochs \
[....................                    ] 51.39% Repairing epochs |
[.....................                   ] 52.78% Repairing epochs /
[.....................                   ] 54.17% Repairing epochs -
[......................                  ] 55.56% Repairing epochs \
[......................                  ] 56.94% Repairing epochs |
[.......................                 ] 58.33% Repairing epochs /
[.......................                 ] 59.72% Repairing epochs -
[........................                ] 61.11% Repairing epochs \
[.........................               ] 62.50% Repairing epochs |
[.........................               ] 63.89% Repairing epochs /
[..........................              ] 65.28% Repairing epochs -
[..........................              ] 66.67% Repairing epochs \
[...........................             ] 68.06% Repairing epochs |
[...........................             ] 69.44% Repairing epochs /
[............................            ] 70.83% Repairing epochs -
[............................            ] 72.22% Repairing epochs \
[.............................           ] 73.61% Repairing epochs |
[..............................          ] 75.00% Repairing epochs /
[..............................          ] 76.39% Repairing epochs -
[...............................         ] 77.78% Repairing epochs \
[...............................         ] 79.17% Repairing epochs |
[................................        ] 80.56% Repairing epochs /
[................................        ] 81.94% Repairing epochs -
[.................................       ] 83.33% Repairing epochs \
[.................................       ] 84.72% Repairing epochs |
[..................................      ] 86.11% Repairing epochs /
[...................................     ] 87.50% Repairing epochs -
[...................................     ] 88.89% Repairing epochs \
[....................................    ] 90.28% Repairing epochs |
[....................................    ] 91.67% Repairing epochs /
[.....................................   ] 93.06% Repairing epochs -
[.....................................   ] 94.44% Repairing epochs \
[......................................  ] 95.83% Repairing epochs |
[......................................  ] 97.22% Repairing epochs /
[....................................... ] 98.61% Repairing epochs -
[........................................] 100.00% Repairing epochs \
[                                        ] 1.39% Repairing epochs |
[.                                       ] 2.78% Repairing epochs /
[.                                       ] 4.17% Repairing epochs -
[..                                      ] 5.56% Repairing epochs \
[..                                      ] 6.94% Repairing epochs |
[...                                     ] 8.33% Repairing epochs /
[...                                     ] 9.72% Repairing epochs -
[....                                    ] 11.11% Repairing epochs \
[.....                                   ] 12.50% Repairing epochs |
[.....                                   ] 13.89% Repairing epochs /
[......                                  ] 15.28% Repairing epochs -
[......                                  ] 16.67% Repairing epochs \
[.......                                 ] 18.06% Repairing epochs |
[.......                                 ] 19.44% Repairing epochs /
[........                                ] 20.83% Repairing epochs -
[........                                ] 22.22% Repairing epochs \
[.........                               ] 23.61% Repairing epochs |
[..........                              ] 25.00% Repairing epochs /
[..........                              ] 26.39% Repairing epochs -
[...........                             ] 27.78% Repairing epochs \
[...........                             ] 29.17% Repairing epochs |
[............                            ] 30.56% Repairing epochs /
[............                            ] 31.94% Repairing epochs -
[.............                           ] 33.33% Repairing epochs \
[.............                           ] 34.72% Repairing epochs |
[..............                          ] 36.11% Repairing epochs /
[...............                         ] 37.50% Repairing epochs -
[...............                         ] 38.89% Repairing epochs \
[................                        ] 40.28% Repairing epochs |
[................                        ] 41.67% Repairing epochs /
[.................                       ] 43.06% Repairing epochs -
[.................                       ] 44.44% Repairing epochs \
[..................                      ] 45.83% Repairing epochs |
[..................                      ] 47.22% Repairing epochs /
[...................                     ] 48.61% Repairing epochs -
[....................                    ] 50.00% Repairing epochs \
[....................                    ] 51.39% Repairing epochs |
[.....................                   ] 52.78% Repairing epochs /
[.....................                   ] 54.17% Repairing epochs -
[......................                  ] 55.56% Repairing epochs \
[......................                  ] 56.94% Repairing epochs |
[.......................                 ] 58.33% Repairing epochs /
[.......................                 ] 59.72% Repairing epochs -
[........................                ] 61.11% Repairing epochs \
[.........................               ] 62.50% Repairing epochs |
[.........................               ] 63.89% Repairing epochs /
[..........................              ] 65.28% Repairing epochs -
[..........................              ] 66.67% Repairing epochs \
[...........................             ] 68.06% Repairing epochs |
[...........................             ] 69.44% Repairing epochs /
[............................            ] 70.83% Repairing epochs -
[............................            ] 72.22% Repairing epochs \
[.............................           ] 73.61% Repairing epochs |
[..............................          ] 75.00% Repairing epochs /
[..............................          ] 76.39% Repairing epochs -
[...............................         ] 77.78% Repairing epochs \
[...............................         ] 79.17% Repairing epochs |
[................................        ] 80.56% Repairing epochs /
[................................        ] 81.94% Repairing epochs -
[.................................       ] 83.33% Repairing epochs \
[.................................       ] 84.72% Repairing epochs |
[..................................      ] 86.11% Repairing epochs /
[...................................     ] 87.50% Repairing epochs -
[...................................     ] 88.89% Repairing epochs \
[....................................    ] 90.28% Repairing epochs |
[....................................    ] 91.67% Repairing epochs /
[.....................................   ] 93.06% Repairing epochs -
[.....................................   ] 94.44% Repairing epochs \
[......................................  ] 95.83% Repairing epochs |
[......................................  ] 97.22% Repairing epochs /
[....................................... ] 98.61% Repairing epochs -
[........................................] 100.00% Repairing epochs \
[....                                    ] 10.00% Fold |
[........                                ] 20.00% Fold /
[............                            ] 30.00% Fold -
[................                        ] 40.00% Fold \
[....................                    ] 50.00% Fold |
[........................                ] 60.00% Fold /
[............................            ] 70.00% Fold -
[................................        ] 80.00% Fold \
[....................................    ] 90.00% Fold |
[........................................] 100.00% Fold /
[.............                           ] 33.33% n_interp |
[                                        ] 1.39% Repairing epochs |
[.                                       ] 2.78% Repairing epochs /
[.                                       ] 4.17% Repairing epochs -
[..                                      ] 5.56% Repairing epochs \
[..                                      ] 6.94% Repairing epochs |
[...                                     ] 8.33% Repairing epochs /
[...                                     ] 9.72% Repairing epochs -
[....                                    ] 11.11% Repairing epochs \
[.....                                   ] 12.50% Repairing epochs |
[.....                                   ] 13.89% Repairing epochs /
[......                                  ] 15.28% Repairing epochs -
[......                                  ] 16.67% Repairing epochs \
[.......                                 ] 18.06% Repairing epochs |
[.......                                 ] 19.44% Repairing epochs /
[........                                ] 20.83% Repairing epochs -
[........                                ] 22.22% Repairing epochs \
[.........                               ] 23.61% Repairing epochs |
[..........                              ] 25.00% Repairing epochs /
[..........                              ] 26.39% Repairing epochs -
[...........                             ] 27.78% Repairing epochs \
[...........                             ] 29.17% Repairing epochs |
[............                            ] 30.56% Repairing epochs /
[............                            ] 31.94% Repairing epochs -
[.............                           ] 33.33% Repairing epochs \
[.............                           ] 34.72% Repairing epochs |
[..............                          ] 36.11% Repairing epochs /
[...............                         ] 37.50% Repairing epochs -
[...............                         ] 38.89% Repairing epochs \
[................                        ] 40.28% Repairing epochs |
[................                        ] 41.67% Repairing epochs /
[.................                       ] 43.06% Repairing epochs -
[.................                       ] 44.44% Repairing epochs \
[..................                      ] 45.83% Repairing epochs |
[..................                      ] 47.22% Repairing epochs /
[...................                     ] 48.61% Repairing epochs -
[....................                    ] 50.00% Repairing epochs \
[....................                    ] 51.39% Repairing epochs |
[.....................                   ] 52.78% Repairing epochs /
[.....................                   ] 54.17% Repairing epochs -
[......................                  ] 55.56% Repairing epochs \
[......................                  ] 56.94% Repairing epochs |
[.......................                 ] 58.33% Repairing epochs /
[.......................                 ] 59.72% Repairing epochs -
[........................                ] 61.11% Repairing epochs \
[.........................               ] 62.50% Repairing epochs |
[.........................               ] 63.89% Repairing epochs /
[..........................              ] 65.28% Repairing epochs -
[..........................              ] 66.67% Repairing epochs \
[...........................             ] 68.06% Repairing epochs |
[...........................             ] 69.44% Repairing epochs /
[............................            ] 70.83% Repairing epochs -
[............................            ] 72.22% Repairing epochs \
[.............................           ] 73.61% Repairing epochs |
[..............................          ] 75.00% Repairing epochs /
[..............................          ] 76.39% Repairing epochs -
[...............................         ] 77.78% Repairing epochs \
[...............................         ] 79.17% Repairing epochs |
[................................        ] 80.56% Repairing epochs /
[................................        ] 81.94% Repairing epochs -
[.................................       ] 83.33% Repairing epochs \
[.................................       ] 84.72% Repairing epochs |
[..................................      ] 86.11% Repairing epochs /
[...................................     ] 87.50% Repairing epochs -
[...................................     ] 88.89% Repairing epochs \
[....................................    ] 90.28% Repairing epochs |
[....................................    ] 91.67% Repairing epochs /
[.....................................   ] 93.06% Repairing epochs -
[.....................................   ] 94.44% Repairing epochs \
[......................................  ] 95.83% Repairing epochs |
[......................................  ] 97.22% Repairing epochs /
[....................................... ] 98.61% Repairing epochs -
[........................................] 100.00% Repairing epochs \
[....                                    ] 10.00% Fold |
[........                                ] 20.00% Fold /
[............                            ] 30.00% Fold -
[................                        ] 40.00% Fold \
[....................                    ] 50.00% Fold |
[........................                ] 60.00% Fold /
[............................            ] 70.00% Fold -
[................................        ] 80.00% Fold \
[....................................    ] 90.00% Fold |
[........................................] 100.00% Fold /
[..........................              ] 66.67% n_interp /
[                                        ] 1.39% Repairing epochs |
[.                                       ] 2.78% Repairing epochs /
[.                                       ] 4.17% Repairing epochs -
[..                                      ] 5.56% Repairing epochs \
[..                                      ] 6.94% Repairing epochs |
[...                                     ] 8.33% Repairing epochs /
[...                                     ] 9.72% Repairing epochs -
[....                                    ] 11.11% Repairing epochs \
[.....                                   ] 12.50% Repairing epochs |
[.....                                   ] 13.89% Repairing epochs /
[......                                  ] 15.28% Repairing epochs -
[......                                  ] 16.67% Repairing epochs \
[.......                                 ] 18.06% Repairing epochs |
[.......                                 ] 19.44% Repairing epochs /
[........                                ] 20.83% Repairing epochs -
[........                                ] 22.22% Repairing epochs \
[.........                               ] 23.61% Repairing epochs |
[..........                              ] 25.00% Repairing epochs /
[..........                              ] 26.39% Repairing epochs -
[...........                             ] 27.78% Repairing epochs \
[...........                             ] 29.17% Repairing epochs |
[............                            ] 30.56% Repairing epochs /
[............                            ] 31.94% Repairing epochs -
[.............                           ] 33.33% Repairing epochs \
[.............                           ] 34.72% Repairing epochs |
[..............                          ] 36.11% Repairing epochs /
[...............                         ] 37.50% Repairing epochs -
[...............                         ] 38.89% Repairing epochs \
[................                        ] 40.28% Repairing epochs |
[................                        ] 41.67% Repairing epochs /
[.................                       ] 43.06% Repairing epochs -
[.................                       ] 44.44% Repairing epochs \
[..................                      ] 45.83% Repairing epochs |
[..................                      ] 47.22% Repairing epochs /
[...................                     ] 48.61% Repairing epochs -
[....................                    ] 50.00% Repairing epochs \
[....................                    ] 51.39% Repairing epochs |
[.....................                   ] 52.78% Repairing epochs /
[.....................                   ] 54.17% Repairing epochs -
[......................                  ] 55.56% Repairing epochs \
[......................                  ] 56.94% Repairing epochs |
[.......................                 ] 58.33% Repairing epochs /
[.......................                 ] 59.72% Repairing epochs -
[........................                ] 61.11% Repairing epochs \
[.........................               ] 62.50% Repairing epochs |
[.........................               ] 63.89% Repairing epochs /
[..........................              ] 65.28% Repairing epochs -
[..........................              ] 66.67% Repairing epochs \
[...........................             ] 68.06% Repairing epochs |
[...........................             ] 69.44% Repairing epochs /
[............................            ] 70.83% Repairing epochs -
[............................            ] 72.22% Repairing epochs \
[.............................           ] 73.61% Repairing epochs |
[..............................          ] 75.00% Repairing epochs /
[..............................          ] 76.39% Repairing epochs -
[...............................         ] 77.78% Repairing epochs \
[...............................         ] 79.17% Repairing epochs |
[................................        ] 80.56% Repairing epochs /
[................................        ] 81.94% Repairing epochs -
[.................................       ] 83.33% Repairing epochs \
[.................................       ] 84.72% Repairing epochs |
[..................................      ] 86.11% Repairing epochs /
[...................................     ] 87.50% Repairing epochs -
[...................................     ] 88.89% Repairing epochs \
[....................................    ] 90.28% Repairing epochs |
[....................................    ] 91.67% Repairing epochs /
[.....................................   ] 93.06% Repairing epochs -
[.....................................   ] 94.44% Repairing epochs \
[......................................  ] 95.83% Repairing epochs |
[......................................  ] 97.22% Repairing epochs /
[....................................... ] 98.61% Repairing epochs -
[........................................] 100.00% Repairing epochs \
[....                                    ] 10.00% Fold |
[........                                ] 20.00% Fold /
[............                            ] 30.00% Fold -
[................                        ] 40.00% Fold \
[....................                    ] 50.00% Fold |
[........................                ] 60.00% Fold /
[............................            ] 70.00% Fold -
[................................        ] 80.00% Fold \
[....................................    ] 90.00% Fold |
[........................................] 100.00% Fold /
[........................................] 100.00% n_interp -



Estimated consensus=0.30 and n_interpolate=4

[                                        ] 1.39% Repairing epochs |
[.                                       ] 2.78% Repairing epochs /
[.                                       ] 4.17% Repairing epochs -
[..                                      ] 5.56% Repairing epochs \
[..                                      ] 6.94% Repairing epochs |
[...                                     ] 8.33% Repairing epochs /
[...                                     ] 9.72% Repairing epochs -
[....                                    ] 11.11% Repairing epochs \
[.....                                   ] 12.50% Repairing epochs |
[.....                                   ] 13.89% Repairing epochs /
[......                                  ] 15.28% Repairing epochs -
[......                                  ] 16.67% Repairing epochs \
[.......                                 ] 18.06% Repairing epochs |
[.......                                 ] 19.44% Repairing epochs /
[........                                ] 20.83% Repairing epochs -
[........                                ] 22.22% Repairing epochs \
[.........                               ] 23.61% Repairing epochs |
[..........                              ] 25.00% Repairing epochs /
[..........                              ] 26.39% Repairing epochs -
[...........                             ] 27.78% Repairing epochs \
[...........                             ] 29.17% Repairing epochs |
[............                            ] 30.56% Repairing epochs /
[............                            ] 31.94% Repairing epochs -
[.............                           ] 33.33% Repairing epochs \
[.............                           ] 34.72% Repairing epochs |
[..............                          ] 36.11% Repairing epochs /
[...............                         ] 37.50% Repairing epochs -
[...............                         ] 38.89% Repairing epochs \
[................                        ] 40.28% Repairing epochs |
[................                        ] 41.67% Repairing epochs /
[.................                       ] 43.06% Repairing epochs -
[.................                       ] 44.44% Repairing epochs \
[..................                      ] 45.83% Repairing epochs |
[..................                      ] 47.22% Repairing epochs /
[...................                     ] 48.61% Repairing epochs -
[....................                    ] 50.00% Repairing epochs \
[....................                    ] 51.39% Repairing epochs |
[.....................                   ] 52.78% Repairing epochs /
[.....................                   ] 54.17% Repairing epochs -
[......................                  ] 55.56% Repairing epochs \
[......................                  ] 56.94% Repairing epochs |
[.......................                 ] 58.33% Repairing epochs /
[.......................                 ] 59.72% Repairing epochs -
[........................                ] 61.11% Repairing epochs \
[.........................               ] 62.50% Repairing epochs |
[.........................               ] 63.89% Repairing epochs /
[..........................              ] 65.28% Repairing epochs -
[..........................              ] 66.67% Repairing epochs \
[...........................             ] 68.06% Repairing epochs |
[...........................             ] 69.44% Repairing epochs /
[............................            ] 70.83% Repairing epochs -
[............................            ] 72.22% Repairing epochs \
[.............................           ] 73.61% Repairing epochs |
[..............................          ] 75.00% Repairing epochs /
[..............................          ] 76.39% Repairing epochs -
[...............................         ] 77.78% Repairing epochs \
[...............................         ] 79.17% Repairing epochs |
[................................        ] 80.56% Repairing epochs /
[................................        ] 81.94% Repairing epochs -
[.................................       ] 83.33% Repairing epochs \
[.................................       ] 84.72% Repairing epochs |
[..................................      ] 86.11% Repairing epochs /
[...................................     ] 87.50% Repairing epochs -
[...................................     ] 88.89% Repairing epochs \
[....................................    ] 90.28% Repairing epochs |
[....................................    ] 91.67% Repairing epochs /
[.....................................   ] 93.06% Repairing epochs -
[.....................................   ] 94.44% Repairing epochs \
[......................................  ] 95.83% Repairing epochs |
[......................................  ] 97.22% Repairing epochs /
[....................................... ] 98.61% Repairing epochs -
[........................................] 100.00% Repairing epochs \

Now, we will manually mark the bad channels just for plotting.

evoked.info['bads'] = ['MEG 2443']
evoked_clean.info['bads'] = ['MEG 2443']

Let us plot the results.

import matplotlib.pyplot as plt  # noqa
set_matplotlib_defaults(plt)

fig, axes = plt.subplots(2, 1, figsize=(6, 6))

for ax in axes:
    ax.tick_params(axis='x', which='both', bottom='off', top='off')
    ax.tick_params(axis='y', which='both', left='off', right='off')

ylim = dict(grad=(-170, 200))
evoked.pick_types(meg='grad', exclude=[])
evoked.plot(exclude=[], axes=axes[0], ylim=ylim, show=False)
axes[0].set_title('Before autoreject')
evoked_clean.pick_types(meg='grad', exclude=[])
evoked_clean.plot(exclude=[], axes=axes[1], ylim=ylim)
axes[1].set_title('After autoreject')
plt.tight_layout()
../_images/sphx_glr_plot_auto_repair_001.png

To top things up, we can also visualize the bad sensors for each trial using a heatmap.

ar.get_reject_log(epochs['Auditory/Left']).plot()
../_images/sphx_glr_plot_auto_repair_002.png

Total running time of the script: ( 3 minutes 22.938 seconds)

Generated by Sphinx-Gallery

Fork me on GitHub