Skip to content

Instantly share code, notes, and snippets.

@teonbrooks
Created February 5, 2014 21:06
Show Gist options
  • Save teonbrooks/8833099 to your computer and use it in GitHub Desktop.
Save teonbrooks/8833099 to your computer and use it in GitHub Desktop.
Checking Bad Channels
def check_bad_chs(self, threshold=0.05, reject=3e-12, n_chan=5):
"""
Check for flat-line channels or channels that repeatedly exceeded
threshold.
"""
ds = self.load_events(drop_bad_chs=False)
ds = ds[ds['experiment'] == 'fixation']
threshold = ds.n_cases * threshold
epochs = E.load.fiff.mne_epochs(ds, tmin=-.2, tmax=.6,
drop_bad_chs=False, verbose=False,
baseline=(None, 0), preload=True,
reject={'mag': reject})
if epochs.drop_log:
bads = E.Factor(sum(epochs.drop_log, []))
bads = E.table.frequencies(bads)
bads = bads[bads['n'] > threshold]['cell'].as_labels()
else:
bads = []
picks = mne.fiff.pick_types(epochs.info, exclude=[])
data = epochs.get_data()[:, picks, :]
flats = []
diffs = np.diff(data) == 0
for epoch in diffs:
# channels flat > 50% time period
flats.append(np.where(np.mean(epoch, 1) >= .5)[0])
flats = np.unique(np.hstack(flats))
flats = ['MEG %03d' % (x + 1) for x in flats]
bad_chs = np.unique(np.hstack((bads, flats)).ravel())
if len(bad_chs) > n_chan:
drop = 1
else:
drop = 0
with open(self.get('bads-file'), 'w') as FILE:
import datetime
date = datetime.datetime.now().ctime()
FILE.write('# Log of bad channels for %s written on %s\n\n'
% (self.get('subject'), date))
FILE.write('bads=%s\n' % bad_chs)
FILE.write('drop=%s' % drop)
return bad_chs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment