Created
January 22, 2015 14:17
-
-
Save dengemann/c473fb26ca4cf5451a80 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| from scipy.stats import zscore | |
| import mne | |
| from mne.fixes import nanmean | |
| from mne.utils import logger | |
| def detect_bad_channels(raw, picks=None, thresh=3): | |
| """Detect bad channels | |
| """ | |
| if picks is None: | |
| picks = mne.pick_types(raw.info, meg=False, eeg=True, exclude=[]) | |
| x, _ = raw[picks] | |
| ch_names = [raw.ch_names[k] for k in picks] | |
| corrx = nanmean( | |
| np.ma.masked_array(np.corrcoef(x), np.identity(len(x), dtype=bool)), | |
| axis=0) | |
| bad_corr = np.where(np.abs(zscore(corrx)) > thresh)[0] | |
| logger.info('Bad by correlation:\n' + | |
| '\n\t'.join([ch_names[k] for k in bad_corr])) | |
| bad_var = np.where(np.abs(zscore(np.var(x, axis=1))) > thresh)[0] | |
| logger.info('Bad by variance:\n' | |
| '\n\t'.join([ch_names[k] for k in bad_var])) | |
| bad_idx = np.unique(np.concatenate((bad_corr, bad_var))) | |
| return [ch_names[k] for k in bad_idx] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment