def test_plot_montage(): """Test plotting montages.""" m = make_standard_montage('easycap-M1') m.plot() plt.close('all') m.plot(kind='3d') plt.close('all') m.plot(kind='3d', show_names=True) plt.close('all') m.plot(kind='topomap') plt.close('all') m.plot(kind='topomap', show_names=True) plt.close('all') N_HSP, N_HPI = 2, 1 montage = make_dig_montage(nasion=[1, 1, 1], lpa=[2, 2, 2], rpa=[3, 3, 3], hsp=np.full((N_HSP, 3), 4), hpi=np.full((N_HPI, 3), 4), coord_frame='head') assert '0 channels' in repr(montage) with pytest.raises(RuntimeError, match='No valid channel positions'): montage.plot() d = read_dig_fif(fname=fif_fname) assert '61 channels' in repr(d)
def test_inst_source(): """Test DigSource.""" from mne.gui._file_traits import DigSource tempdir = _TempDir() inst = DigSource() assert inst.inst_fname == '-' inst.file = inst_path assert inst.inst_dir == op.dirname(inst_path) lpa = array([[-7.13766068e-02, 0.00000000e+00, 5.12227416e-09]]) nasion = array([[3.72529030e-09, 1.02605611e-01, 4.19095159e-09]]) rpa = array([[7.52676800e-02, 0.00000000e+00, 5.58793545e-09]]) assert_allclose(inst.lpa, lpa) assert_allclose(inst.nasion, nasion) assert_allclose(inst.rpa, rpa) montage = read_dig_fif(inst_path) # test reading DigMontage montage_path = op.join(tempdir, 'temp_montage.fif') montage.save(montage_path) inst.file = montage_path assert_allclose(inst.lpa, lpa) assert_allclose(inst.nasion, nasion) assert_allclose(inst.rpa, rpa)
def test_fif_dig_montage(): """Test FIF dig montage support.""" dig_montage = read_dig_fif(fif_dig_montage_fname) # test round-trip IO temp_dir = _TempDir() fname_temp = op.join(temp_dir, 'test.fif') _check_roundtrip(dig_montage, fname_temp) # Make a BrainVision file like the one the user would have had raw_bv = read_raw_brainvision(bv_fname, preload=True) raw_bv_2 = raw_bv.copy() mapping = dict() for ii, ch_name in enumerate(raw_bv.ch_names): mapping[ch_name] = 'EEG%03d' % (ii + 1,) raw_bv.rename_channels(mapping) for ii, ch_name in enumerate(raw_bv_2.ch_names): mapping[ch_name] = 'EEG%03d' % (ii + 33,) raw_bv_2.rename_channels(mapping) raw_bv.add_channels([raw_bv_2]) for ch in raw_bv.info['chs']: ch['kind'] = FIFF.FIFFV_EEG_CH # Set the montage raw_bv.set_montage(dig_montage) # Check the result evoked = read_evokeds(evoked_fname)[0] # check info[chs] matches assert_equal(len(raw_bv.ch_names), len(evoked.ch_names) - 1) for ch_py, ch_c in zip(raw_bv.info['chs'], evoked.info['chs'][:-1]): assert_equal(ch_py['ch_name'], ch_c['ch_name'].replace('EEG ', 'EEG')) # C actually says it's unknown, but it's not (?): # assert_equal(ch_py['coord_frame'], ch_c['coord_frame']) assert_equal(ch_py['coord_frame'], FIFF.FIFFV_COORD_HEAD) c_loc = ch_c['loc'].copy() c_loc[c_loc == 0] = np.nan assert_allclose(ch_py['loc'], c_loc, atol=1e-7) # check info[dig] assert_dig_allclose(raw_bv.info, evoked.info) # Roundtrip of non-FIF start montage = make_dig_montage(hsp=read_polhemus_fastscan(hsp), hpi=read_mrk(hpi)) elp_points = read_polhemus_fastscan(elp) ch_pos = {"EEG%03d" % (k + 1): pos for k, pos in enumerate(elp_points[8:])} montage += make_dig_montage(nasion=elp_points[0], lpa=elp_points[1], rpa=elp_points[2], ch_pos=ch_pos) pytest.raises(RuntimeError, montage.save, fname_temp) # must be head coord montage = transform_to_head(montage) _check_roundtrip(montage, fname_temp)
def test_fif_dig_montage(): """Test FIF dig montage support.""" with pytest.deprecated_call(): dig_montage = read_dig_montage(fif=fif_dig_montage_fname) # test round-trip IO temp_dir = _TempDir() fname_temp = op.join(temp_dir, 'test.fif') _check_roundtrip(dig_montage, fname_temp) # Make a BrainVision file like the one the user would have had raw_bv = read_raw_brainvision(bv_fname, preload=True) raw_bv_2 = raw_bv.copy() mapping = dict() for ii, ch_name in enumerate(raw_bv.ch_names): mapping[ch_name] = 'EEG%03d' % (ii + 1, ) raw_bv.rename_channels(mapping) for ii, ch_name in enumerate(raw_bv_2.ch_names): mapping[ch_name] = 'EEG%03d' % (ii + 33, ) raw_bv_2.rename_channels(mapping) raw_bv.add_channels([raw_bv_2]) for ii in range(2): # Set the montage raw_bv.set_montage(dig_montage) # Check the result evoked = read_evokeds(evoked_fname)[0] assert_equal(len(raw_bv.ch_names), len(evoked.ch_names) - 1) for ch_py, ch_c in zip(raw_bv.info['chs'], evoked.info['chs'][:-1]): assert_equal(ch_py['ch_name'], ch_c['ch_name'].replace('EEG ', 'EEG')) # C actually says it's unknown, but it's not (?): # assert_equal(ch_py['coord_frame'], ch_c['coord_frame']) assert_equal(ch_py['coord_frame'], FIFF.FIFFV_COORD_HEAD) c_loc = ch_c['loc'].copy() c_loc[c_loc == 0] = np.nan assert_allclose(ch_py['loc'], c_loc, atol=1e-7) assert_dig_allclose(raw_bv.info, evoked.info) # Roundtrip of non-FIF start names = ['nasion', 'lpa', 'rpa', '1', '2', '3', '4', '5'] montage = read_dig_montage(hsp, hpi, elp, names, transform=False) pytest.raises(RuntimeError, montage.save, fname_temp) # must be head coord montage = read_dig_montage(hsp, hpi, elp, names) _check_roundtrip(montage, fname_temp) # Test old way matches new way with pytest.deprecated_call(): dig_montage = read_dig_montage(fif=fif_dig_montage_fname) dig_montage_fif = read_dig_fif(fif_dig_montage_fname) assert dig_montage.dig == dig_montage_fif.dig assert object_diff(dig_montage.ch_names, dig_montage_fif.ch_names) == ''
def _check_roundtrip(montage, fname): """Check roundtrip writing.""" with pytest.deprecated_call(): assert_equal(montage.coord_frame, 'head') montage.save(fname) montage_read = read_dig_fif(fname=fname) assert_equal(str(montage), str(montage_read)) with pytest.deprecated_call(): for kind in ('elp', 'hsp', 'nasion', 'lpa', 'rpa'): if getattr(montage, kind, None) is not None: assert_allclose(getattr(montage, kind), getattr(montage_read, kind), err_msg=kind) with pytest.deprecated_call(): assert_equal(montage_read.coord_frame, 'head')
def _check_roundtrip(montage, fname): """Check roundtrip writing.""" montage.save(fname) montage_read = read_dig_fif(fname=fname) assert_equal(repr(montage), repr(montage_read)) assert_equal(_check_get_coord_frame(montage_read.dig), 'head') # XXX: it tests the same as assert_dig_all_close but without info # This is a possible refactor dig_py = sorted(montage_read.dig, key=_dig_sort_key) dig_bin = sorted(montage.dig, key=_dig_sort_key) assert len(dig_py) == len(dig_bin) for ii, (d_py, d_bin) in enumerate(zip(dig_py, dig_bin)): for key in ('ident', 'kind', 'coord_frame'): assert d_py[key] == d_bin[key] assert_allclose(d_py['r'], d_bin['r'], rtol=1e-5, atol=1e-5, err_msg='Failure on %s:\n%s\n%s' % (ii, d_py['r'], d_bin['r']))
def test_plot_montage(): """Test plotting montages.""" m = read_montage('easycap-M1') m.plot() plt.close('all') m.plot(kind='3d') plt.close('all') m.plot(kind='3d', show_names=True) plt.close('all') m.plot(kind='topomap') plt.close('all') m.plot(kind='topomap', show_names=True) plt.close('all') d = read_dig_montage(hsp, hpi, elp, point_names) assert '0 channels' in repr(d) with pytest.raises(RuntimeError, match='No valid channel positions'): d.plot() d = read_dig_fif(fname=fif_fname) assert '61 channels' in repr(d)
def test_digitization_source(tmpdir): """Test DigSource.""" from mne.gui._file_traits import DigSource tempdir = str(tmpdir) inst = DigSource() assert inst.inst_fname == '-' inst.file = inst_path assert inst.inst_dir == op.dirname(inst_path) # FIFF lpa = array([[-7.13766068e-02, 0.00000000e+00, 5.12227416e-09]]) nasion = array([[3.72529030e-09, 1.02605611e-01, 4.19095159e-09]]) rpa = array([[7.52676800e-02, 0.00000000e+00, 5.58793545e-09]]) assert_allclose(inst.lpa, lpa) assert_allclose(inst.nasion, nasion) assert_allclose(inst.rpa, rpa) # DigMontage montage = read_dig_fif(inst_path) montage_path = op.join(tempdir, 'temp_montage.fif') montage.save(montage_path) inst.file = montage_path assert_allclose(inst.lpa, lpa) assert_allclose(inst.nasion, nasion) assert_allclose(inst.rpa, rpa) # EGI MFF inst.file = op.join(data_path, 'EGI', 'test_egi.mff') assert len(inst.points) == 0 assert len(inst.eeg_points) == 130 assert_allclose(inst.lpa * 1000, [[-67.1, 0, 0]], atol=0.1) assert_allclose(inst.nasion * 1000, [[0.0, 103.6, 0]], atol=0.1) assert_allclose(inst.rpa * 1000, [[67.1, 0, 0]], atol=0.1) # CTF inst.file = op.join(data_path, 'CTF', 'testdata_ctf.ds') assert len(inst.points) == 0 assert len(inst.eeg_points) == 8 assert_allclose(inst.lpa * 1000, [[-74.3, 0.0, 0.0]], atol=0.1) assert_allclose(inst.nasion * 1000, [[0.0, 117.7, 0.0]], atol=0.1) assert_allclose(inst.rpa * 1000, [[84.9, -0.0, 0.0]], atol=0.1)