Plot channel-level thresholdsΒΆ

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 /Users/mainak/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.
Current compensation grade : 0
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='progressbar')

Out:

/Users/mainak/Documents/github_repos/autoreject/autoreject/utils.py:75: 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"].
  'remove them from epochs.info["bads"].' % n_bads)


[                                        ]   0.00% Computing thresholds ... |
[                                        ]   0.49% Computing thresholds ... /
[                                        ]   0.99% Computing thresholds ... -
[                                        ]   1.48% Computing thresholds ... \
[                                        ]   1.97% Computing thresholds ... |
[                                        ]   2.46% Computing thresholds ... /
[.                                       ]   2.96% Computing thresholds ... -
[.                                       ]   3.45% Computing thresholds ... \
[.                                       ]   3.94% Computing thresholds ... |
[.                                       ]   4.43% Computing thresholds ... /
[.                                       ]   4.93% Computing thresholds ... -
[..                                      ]   5.42% Computing thresholds ... \
[..                                      ]   5.91% Computing thresholds ... |
[..                                      ]   6.40% Computing thresholds ... /
[..                                      ]   6.90% Computing thresholds ... -
[..                                      ]   7.39% Computing thresholds ... \
[...                                     ]   7.88% Computing thresholds ... |
[...                                     ]   8.37% Computing thresholds ... /
[...                                     ]   8.87% Computing thresholds ... -
[...                                     ]   9.36% Computing thresholds ... \
[...                                     ]   9.85% Computing thresholds ... |
[....                                    ]  10.34% Computing thresholds ... /
[....                                    ]  10.84% Computing thresholds ... -
[....                                    ]  11.33% Computing thresholds ... \
[....                                    ]  11.82% Computing thresholds ... |
[....                                    ]  12.32% Computing thresholds ... /
[.....                                   ]  12.81% Computing thresholds ... -
[.....                                   ]  13.30% Computing thresholds ... \
[.....                                   ]  13.79% Computing thresholds ... |
[.....                                   ]  14.29% Computing thresholds ... /
[.....                                   ]  14.78% Computing thresholds ... -
[......                                  ]  15.27% Computing thresholds ... \
[......                                  ]  15.76% Computing thresholds ... |
[......                                  ]  16.26% Computing thresholds ... /
[......                                  ]  16.75% Computing thresholds ... -
[......                                  ]  17.24% Computing thresholds ... \
[.......                                 ]  17.73% Computing thresholds ... |
[.......                                 ]  18.23% Computing thresholds ... /
[.......                                 ]  18.72% Computing thresholds ... -
[.......                                 ]  19.21% Computing thresholds ... \
[.......                                 ]  19.70% Computing thresholds ... |
[........                                ]  20.20% Computing thresholds ... /
[........                                ]  20.69% Computing thresholds ... -
[........                                ]  21.18% Computing thresholds ... \
[........                                ]  21.67% Computing thresholds ... |
[........                                ]  22.17% Computing thresholds ... /
[.........                               ]  22.66% Computing thresholds ... -
[.........                               ]  23.15% Computing thresholds ... \
[.........                               ]  23.65% Computing thresholds ... |
[.........                               ]  24.14% Computing thresholds ... /
[.........                               ]  24.63% Computing thresholds ... -
[..........                              ]  25.12% Computing thresholds ... \
[..........                              ]  25.62% Computing thresholds ... |
[..........                              ]  26.11% Computing thresholds ... /
[..........                              ]  26.60% Computing thresholds ... -
[..........                              ]  27.09% Computing thresholds ... \
[...........                             ]  27.59% Computing thresholds ... |
[...........                             ]  28.08% Computing thresholds ... /
[...........                             ]  28.57% Computing thresholds ... -
[...........                             ]  29.06% Computing thresholds ... \
[...........                             ]  29.56% Computing thresholds ... |
[............                            ]  30.05% Computing thresholds ... /
[............                            ]  30.54% Computing thresholds ... -
[............                            ]  31.03% Computing thresholds ... \
[............                            ]  31.53% Computing thresholds ... |
[............                            ]  32.02% Computing thresholds ... /
[.............                           ]  32.51% Computing thresholds ... -
[.............                           ]  33.00% Computing thresholds ... \
[.............                           ]  33.50% Computing thresholds ... |
[.............                           ]  33.99% Computing thresholds ... /
[.............                           ]  34.48% Computing thresholds ... -
[.............                           ]  34.98% Computing thresholds ... \
[..............                          ]  35.47% Computing thresholds ... |
[..............                          ]  35.96% Computing thresholds ... /
[..............                          ]  36.45% Computing thresholds ... -
[..............                          ]  36.95% Computing thresholds ... \
[..............                          ]  37.44% Computing thresholds ... |
[...............                         ]  37.93% Computing thresholds ... /
[...............                         ]  38.42% Computing thresholds ... -
[...............                         ]  38.92% Computing thresholds ... \
[...............                         ]  39.41% Computing thresholds ... |
[...............                         ]  39.90% Computing thresholds ... /
[................                        ]  40.39% Computing thresholds ... -
[................                        ]  40.89% Computing thresholds ... \
[................                        ]  41.38% Computing thresholds ... |
[................                        ]  41.87% Computing thresholds ... /
[................                        ]  42.36% Computing thresholds ... -
[.................                       ]  42.86% Computing thresholds ... \
[.................                       ]  43.35% Computing thresholds ... |
[.................                       ]  43.84% Computing thresholds ... /
[.................                       ]  44.33% Computing thresholds ... -
[.................                       ]  44.83% Computing thresholds ... \
[..................                      ]  45.32% Computing thresholds ... |
[..................                      ]  45.81% Computing thresholds ... /
[..................                      ]  46.31% Computing thresholds ... -
[..................                      ]  46.80% Computing thresholds ... \
[..................                      ]  47.29% Computing thresholds ... |
[...................                     ]  47.78% Computing thresholds ... /
[...................                     ]  48.28% Computing thresholds ... -
[...................                     ]  48.77% Computing thresholds ... \
[...................                     ]  49.26% Computing thresholds ... |
[...................                     ]  49.75% Computing thresholds ... /
[....................                    ]  50.25% Computing thresholds ... -
[....................                    ]  50.74% Computing thresholds ... \
[....................                    ]  51.23% Computing thresholds ... |
[....................                    ]  51.72% Computing thresholds ... /
[....................                    ]  52.22% Computing thresholds ... -
[.....................                   ]  52.71% Computing thresholds ... \
[.....................                   ]  53.20% Computing thresholds ... |
[.....................                   ]  53.69% Computing thresholds ... /
[.....................                   ]  54.19% Computing thresholds ... -
[.....................                   ]  54.68% Computing thresholds ... \
[......................                  ]  55.17% Computing thresholds ... |
[......................                  ]  55.67% Computing thresholds ... /
[......................                  ]  56.16% Computing thresholds ... -
[......................                  ]  56.65% Computing thresholds ... \
[......................                  ]  57.14% Computing thresholds ... |
[.......................                 ]  57.64% Computing thresholds ... /
[.......................                 ]  58.13% Computing thresholds ... -
[.......................                 ]  58.62% Computing thresholds ... \
[.......................                 ]  59.11% Computing thresholds ... |
[.......................                 ]  59.61% Computing thresholds ... /
[........................                ]  60.10% Computing thresholds ... -
[........................                ]  60.59% Computing thresholds ... \
[........................                ]  61.08% Computing thresholds ... |
[........................                ]  61.58% Computing thresholds ... /
[........................                ]  62.07% Computing thresholds ... -
[.........................               ]  62.56% Computing thresholds ... \
[.........................               ]  63.05% Computing thresholds ... |
[.........................               ]  63.55% Computing thresholds ... /
[.........................               ]  64.04% Computing thresholds ... -
[.........................               ]  64.53% Computing thresholds ... \
[..........................              ]  65.02% Computing thresholds ... |
[..........................              ]  65.52% Computing thresholds ... /
[..........................              ]  66.01% Computing thresholds ... -
[..........................              ]  66.50% Computing thresholds ... \
[..........................              ]  67.00% Computing thresholds ... |
[..........................              ]  67.49% Computing thresholds ... /
[...........................             ]  67.98% Computing thresholds ... -
[...........................             ]  68.47% Computing thresholds ... \
[...........................             ]  68.97% Computing thresholds ... |
[...........................             ]  69.46% Computing thresholds ... /
[...........................             ]  69.95% Computing thresholds ... -
[............................            ]  70.44% Computing thresholds ... \
[............................            ]  70.94% Computing thresholds ... |
[............................            ]  71.43% Computing thresholds ... /
[............................            ]  71.92% Computing thresholds ... -
[............................            ]  72.41% Computing thresholds ... \
[.............................           ]  72.91% Computing thresholds ... |
[.............................           ]  73.40% Computing thresholds ... /
[.............................           ]  73.89% Computing thresholds ... -
[.............................           ]  74.38% Computing thresholds ... \
[.............................           ]  74.88% Computing thresholds ... |
[..............................          ]  75.37% Computing thresholds ... /
[..............................          ]  75.86% Computing thresholds ... -
[..............................          ]  76.35% Computing thresholds ... \
[..............................          ]  76.85% Computing thresholds ... |
[..............................          ]  77.34% Computing thresholds ... /
[...............................         ]  77.83% Computing thresholds ... -
[...............................         ]  78.33% Computing thresholds ... \
[...............................         ]  78.82% Computing thresholds ... |
[...............................         ]  79.31% Computing thresholds ... /
[...............................         ]  79.80% Computing thresholds ... -
[................................        ]  80.30% Computing thresholds ... \
[................................        ]  80.79% Computing thresholds ... |
[................................        ]  81.28% Computing thresholds ... /
[................................        ]  81.77% Computing thresholds ... -
[................................        ]  82.27% Computing thresholds ... \
[.................................       ]  82.76% Computing thresholds ... |
[.................................       ]  83.25% Computing thresholds ... /
[.................................       ]  83.74% Computing thresholds ... -
[.................................       ]  84.24% Computing thresholds ... \
[.................................       ]  84.73% Computing thresholds ... |
[..................................      ]  85.22% Computing thresholds ... /
[..................................      ]  85.71% Computing thresholds ... -
[..................................      ]  86.21% Computing thresholds ... \
[..................................      ]  86.70% Computing thresholds ... |
[..................................      ]  87.19% Computing thresholds ... /
[...................................     ]  87.68% Computing thresholds ... -
[...................................     ]  88.18% Computing thresholds ... \
[...................................     ]  88.67% Computing thresholds ... |
[...................................     ]  89.16% Computing thresholds ... /
[...................................     ]  89.66% Computing thresholds ... -
[....................................    ]  90.15% Computing thresholds ... \
[....................................    ]  90.64% Computing thresholds ... |
[....................................    ]  91.13% Computing thresholds ... /
[....................................    ]  91.63% Computing thresholds ... -
[....................................    ]  92.12% Computing thresholds ... \
[.....................................   ]  92.61% Computing thresholds ... |
[.....................................   ]  93.10% Computing thresholds ... /
[.....................................   ]  93.60% Computing thresholds ... -
[.....................................   ]  94.09% Computing thresholds ... \
[.....................................   ]  94.58% Computing thresholds ... |
[......................................  ]  95.07% Computing thresholds ... /
[......................................  ]  95.57% Computing thresholds ... -
[......................................  ]  96.06% Computing thresholds ... \
[......................................  ]  96.55% Computing thresholds ... |
[......................................  ]  97.04% Computing thresholds ... /
[....................................... ]  97.54% Computing thresholds ... -
[....................................... ]  98.03% Computing thresholds ... \
[....................................... ]  98.52% Computing thresholds ... |
[....................................... ]  99.01% Computing thresholds ... /
[....................................... ]  99.51% Computing thresholds ... -
[........................................] 100.00% Computing thresholds ... \

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()
../_images/sphx_glr_plot_channel_thresholds_001.png

Out:

/Users/mainak/Documents/github_repos/autoreject/examples/plot_channel_thresholds.py:79: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
  plt.show()

Total running time of the script: ( 0 minutes 40.709 seconds)

Gallery generated by Sphinx-Gallery

Fork me on GitHub