def test_auto_topomap_coords(): """Test mapping of coordinates in 3D space to 2D""" info = Raw(fif_fname).info.copy() picks = pick_types(info, meg=False, eeg=True, eog=False, stim=False) # Remove extra digitization point, so EEG digitization points match up # with the EEG channels del info['dig'][85] # Remove head origin from channel locations, so mapping with digitization # points yields the same result dig_kinds = (FIFF.FIFFV_POINT_CARDINAL, FIFF.FIFFV_POINT_EEG, FIFF.FIFFV_POINT_EXTRA) _, origin_head, _ = fit_sphere_to_headshape(info, dig_kinds) origin_head /= 1000. # to meters for ch in info['chs']: ch['loc'][:3] -= origin_head # Use channel locations l0 = _auto_topomap_coords(info, picks) # Remove electrode position information, use digitization points from now # on. for ch in info['chs']: ch['loc'] = np.zeros(12) l1 = _auto_topomap_coords(info, picks) assert_allclose(l1, l0) # Test plotting mag topomap without channel locations: it should fail mag_picks = pick_types(info, meg='mag') assert_raises(ValueError, _auto_topomap_coords, info, mag_picks) # Test function with too many EEG digitization points: it should fail info['dig'].append({'r': [1, 2, 3], 'kind': FIFF.FIFFV_POINT_EEG}) assert_raises(ValueError, _auto_topomap_coords, info, picks) # Test function with too little EEG digitization points: it should fail info['dig'] = info['dig'][:-2] assert_raises(ValueError, _auto_topomap_coords, info, picks) # Electrode positions must be unique info['dig'].append(info['dig'][-1]) assert_raises(ValueError, _auto_topomap_coords, info, picks) # Test function without EEG digitization points: it should fail info['dig'] = [d for d in info['dig'] if d['kind'] != FIFF.FIFFV_POINT_EEG] assert_raises(RuntimeError, _auto_topomap_coords, info, picks) # Test function without any digitization points, it should fail info['dig'] = None assert_raises(RuntimeError, _auto_topomap_coords, info, picks) info['dig'] = [] assert_raises(RuntimeError, _auto_topomap_coords, info, picks)
def plot_montage(chan_list, ax=None, scores=None, title='', cmap=None): """Plot montage and color code channels.""" from mne.viz.utils import _plot_sensors from mne.channels.layout import _auto_topomap_coords from mne.channels import read_montage from mne.utils import check_version from matplotlib import cm if cmap is None: cmap = cm.viridis # connectivity, ch_names = read_ch_connectivity( # 'biosemi64', picks=montage.selection[:64]) # plt.imshow(connectivity.toarray(), origin='lower') # plt.xlabel('{} Channels'.format(len(ch_names))) # plt.ylabel('{} Channels'.format(len(ch_names))) # plt.title('Between-sensor adjacency') # plt.show() montage = read_montage('biosemi64') info = mne.create_info(montage.ch_names[:64], sfreq=256, ch_types="eeg", montage='biosemi64') pos = _auto_topomap_coords(info, picks=range(64), ignore_overlap=True, to_sphere=True) colors = np.ones((64, 4)) colors[:, 0:3] = 0.9 if not chan_list or chan_list is None: chan_list = montage.ch_names picks = mne.pick_channels(montage.ch_names[:64], chan_list) if scores is None: colors[picks, :] = cmap(100) else: for ch in chan_list: picks2 = mne.pick_channels(montage.ch_names[:64], [ch, ]) colors[picks2, :] = cmap(scores[ch]) f = _plot_sensors(pos, colors, [], montage.ch_names[:64], title=title, show_names=False, ax=ax, show=False, select=False, block=False, to_sphere=True) # Modify dot size scale_factor = 20. try: ax = ax except: # noqa ax = f.axes[0] collection = ax.collections[0] if check_version("matplotlib", "1.4"): collection.set_sizes([scale_factor]) collection.set_linewidths([0]) else: collection._sizes = [scale_factor] collection._linewidths = [0] return f