Skip to content

Instantly share code, notes, and snippets.

@dtxe
Created March 28, 2023 14:23
Show Gist options
  • Save dtxe/d76c127c6234fc0364637e66e2a67a33 to your computer and use it in GitHub Desktop.
Save dtxe/d76c127c6234fc0364637e66e2a67a33 to your computer and use it in GitHub Desktop.
MNE interpolate new scalp channels
import mne
mneraw = mne.read_raw_edf('file.edf')
electrodes = ['Fpz', 'Cz']
# if electrodes to plot are not available, try to interpolate them
electrodes_not_in_list = [e for e in electrodes if e not in mneraw.ch_names]
if len(electrodes_not_in_list) > 0:
try:
mneraw = mne.add_reference_channels(mneraw, ref_channels=electrodes_not_in_list, copy=False)
scalpmontage = mne.channels.make_standard_montage('standard_1020')
mneraw = mneraw.set_montage(None) # remove montage
mneraw = mneraw.set_montage(scalpmontage, on_missing='warn') # re-add montage for Fpz electrode
# for e in electrodes_not_in_list:
mneraw.info['bads'].extend(electrodes_not_in_list)
mneraw = mneraw.interpolate_bads(reset_bads=True)
except Exception as ex:
logging.info('Could not add ' + repr(electrodes_not_in_list) + ' and interpolate: ' + repr(ex))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment