Skip to content

Instantly share code, notes, and snippets.

@larsoner
Last active November 18, 2019 17:59
Show Gist options
  • Save larsoner/f30e6b3b2f4e7c7979abbd367ea7ee54 to your computer and use it in GitHub Desktop.
Save larsoner/f30e6b3b2f4e7c7979abbd367ea7ee54 to your computer and use it in GitHub Desktop.
import os
import numpy as np
import matplotlib.pyplot as plt
import mne
data_path = mne.datasets.sample.data_path()
# Closes #3987, #4880, #5190, #5472, #6304
# Places where we use _auto/_find_topomap_coords that probably need checking:
#
# mne.channels.channels._compute_ch_connectivity (should be okay, Delaunay)
# mne.viz.layout.find_layout (to_sphere=False)
# mne.viz.layout.make_eeg_layout
# mne.viz.layout._pair_grad_sensors
# mne.viz.evoked._handle_spatial_colors
#
# Need to check:
# mne.viz.plot_layout
###############################################################################
# Case 1: picking channels shouldn't change locations: plot_sensors (#6304)
# SetChannelsMixin.plot_sensors
# mne.viz.utils.plot_sensors
# mne.channels.layout._auto_topomap_coords
# This should plot in physical units, head should have radius 0.095
# It should be this way for both to_sphere=True (default) and to_sphere=False
# """
fname = os.path.join(data_path, 'MEG', 'sample', 'sample_audvis_raw.fif')
raw = mne.io.read_raw_fif(fname)
raw.crop(tmax=60).load_data()
for to_sphere in (True, False):
raw.plot_sensors(ch_type='eeg', show_names=True, to_sphere=to_sphere,
sphere='auto', verbose=True)
midline_ch_names = ['EEG {:03}'.format(n) for n in (2, 12, 30, 48, 58, 60)]
midline = raw.copy().pick_channels(midline_ch_names)
midline.plot_sensors(show_names=True, to_sphere=to_sphere, sphere='auto')
# """
###############################################################################
# Case 2: inset plots should show EEG sensors in correct locations (#6304)
# """
events = mne.find_events(raw)
epochs = mne.Epochs(raw, events)
evoked = epochs.average()
evoked.plot(spatial_colors=True)
# """
###############################################################################
# Case 3: picking channels shouldn't change locations: montage plotting (#5190)
# Case 4: Electrode positions should be more like FT (#4880)
# https://user-images.githubusercontent.com/3467229/35116071-e6d53434-fc68-11e7-87e2-d80f0e6c44c5.png # noqa
# """
chan_selection = [
'FC5', 'FC1', 'C3', 'CP5', 'CP1', 'FC2', 'FC6', 'C4', 'CP2', 'CP6']
montage1020_full = mne.channels.make_standard_montage('standard_1020')
ch_pos = montage1020_full._get_ch_pos()
ch_pos = {name: ch_pos[name] for name in chan_selection}
kwargs = dict(ch_pos=ch_pos)
for ki, key in enumerate(('lpa', 'nasion', 'rpa')):
d = montage1020_full.dig[ki]
assert d['kind'] == mne.io.constants.FIFF.FIFFV_POINT_CARDINAL
assert d['ident'] == getattr(mne.io.constants.FIFF,
'FIFFV_POINT_' + key.upper())
kwargs[key] = d['r']
montage1020_reduced = mne.channels.make_dig_montage(**kwargs)
montage1020_full.plot(kind='topomap')
montage1020_reduced.plot(kind='topomap')
# """
###############################################################################
# Case 4: sensors with zero z value should be on head border (#5472)
# """
mntg = mne.channels.make_standard_montage('GSN-HydroCel-65_1.0')
info = mne.create_info(mntg.ch_names, sfreq=250., ch_types='eeg', montage=mntg)
ch_names = [f'E{idx}' for idx in range(1, 65)] + ['Cz']
picks = [0, 1, 2, 4, 5, 7, 8, 9, 10, 55, 56, 57, 58, 59, 60, 61]
ch_names = [ch_names[pick] for pick in picks]
picks = mne.pick_channels(info['ch_names'], ch_names)
info_picked = mne.pick_info(info, sel=picks)
activity = [0.26, -0.02, -0.16, 0.2, -0.13, -0.15, 0.1, 0.03, 0.18, 0.12,
0.38, 0.2, 0.39, 0.33, 0.48, 0.46, 0.65, 0.84, 0.74, 0.33, 0.61,
0.46, 1.02, 0.02, -0.02, -0.06, -0.49, 0.08, -0.07, -0.31, -0.09,
0.01, -0.12, 0.13, -0.25, 0.16, 0.01, -0.01, 0.02, 0.04, 0.49,
-0.29, 0.04, -0.24, -0.13, 0., 0.18, 0.19, 0.28, 0.11, 0.26, 0.28,
0.05, -0.06, -0.04, -0.11, -0.11, -0.03, -0.24, 0.01, 0.24, 0.43,
0.44, 0.76, 0.61]
activity = np.array(activity)
fig, ax = plt.subplots(figsize=(3, 6), ncols=1, nrows=2)
topo_kwargs = dict(sensors='kx', outlines='skirt', vmin=-1.5, vmax=1.5)
ax[0].set_title('all channels', fontsize=14)
mne.viz.plot_topomap(activity, info, axes=ax[0], **topo_kwargs)
ax[1].set_title('selected channels', fontsize=14)
mne.viz.plot_topomap(activity[picks], info_picked, axes=ax[1], **topo_kwargs)
# """
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment