def test_make_dig_points(): """Test application of Polhemus HSP to info.""" extra_points = _read_dig_points(hsp_fname) info = create_info(ch_names=['Test Ch'], sfreq=1000., ch_types=None) assert info['dig'] is None info['dig'] = _make_dig_points(extra_points=extra_points) assert (info['dig']) assert_allclose(info['dig'][0]['r'], [-.10693, .09980, .06881]) elp_points = _read_dig_points(elp_fname) nasion, lpa, rpa = elp_points[:3] info = create_info(ch_names=['Test Ch'], sfreq=1000., ch_types=None) assert info['dig'] is None info['dig'] = _make_dig_points(nasion, lpa, rpa, elp_points[3:], None) assert (info['dig']) idx = [d['ident'] for d in info['dig']].index(FIFF.FIFFV_POINT_NASION) assert_array_equal(info['dig'][idx]['r'], np.array([.0013930, .0131613, -.0046967])) pytest.raises(ValueError, _make_dig_points, nasion[:2]) pytest.raises(ValueError, _make_dig_points, None, lpa[:2]) pytest.raises(ValueError, _make_dig_points, None, None, rpa[:2]) pytest.raises(ValueError, _make_dig_points, None, None, None, elp_points[:, :2]) pytest.raises(ValueError, _make_dig_points, None, None, None, None, elp_points[:, :2])
def _read_dig_montage( hsp=None, hpi=None, elp=None, point_names=None, unit='auto', fif=None, egi=None, bvct=None, transform=True, dev_head_t=False, ): """Unfolds the `read_dig_montage` old behavior of the call below. montage = read_dig_montage(hsp, hpi, elp, names, transform=True, dev_head_t=False) """ assert isinstance(hsp, str), 'original call hsp was string' assert op.splitext(hpi)[-1] == '.sqd', 'original call hpi was .sqd' assert isinstance(elp, str), 'original call elp was string' hsp = _read_dig_points(hsp, unit=unit) hpi = read_mrk(hpi) elp = _read_dig_points(elp, unit=unit) data = Bunch(nasion=None, lpa=None, rpa=None, hsp=hsp, hpi=hpi, elp=elp, coord_frame='unknown', point_names=point_names, dig_ch_pos=None) data = _fix_data_fiducials(data) data = _transform_to_head_call(data) with pytest.deprecated_call(): montage = DigMontage(**data) return montage
def test_set_dig_montage(): """Test applying DigMontage to inst.""" # Extensive testing of applying `dig` to info is done in test_meas_info # with `test_make_dig_points`. names = ['nasion', 'lpa', 'rpa', '1', '2', '3', '4', '5'] hsp_points = _read_dig_points(hsp) elp_points = _read_dig_points(elp) nasion, lpa, rpa = elp_points[:3] nm_trans = get_ras_to_neuromag_trans(nasion, lpa, rpa) elp_points = apply_trans(nm_trans, elp_points) nasion, lpa, rpa = elp_points[:3] hsp_points = apply_trans(nm_trans, hsp_points) montage = read_dig_montage(hsp, hpi, elp, names, transform=True, dev_head_t=True) temp_dir = _TempDir() fname_temp = op.join(temp_dir, 'test.fif') montage.save(fname_temp) with pytest.deprecated_call(): montage_read = read_dig_montage(fif=fname_temp) for use_mon in (montage, montage_read): info = create_info(['Test Ch'], 1e3, ['eeg']) with pytest.warns(None): # warns on one run about not all positions _set_montage(info, use_mon) hs = np.array([ p['r'] for i, p in enumerate(info['dig']) if p['kind'] == FIFF.FIFFV_POINT_EXTRA ]) nasion_dig = np.array([ p['r'] for p in info['dig'] if all([ p['ident'] == FIFF.FIFFV_POINT_NASION, p['kind'] == FIFF.FIFFV_POINT_CARDINAL ]) ]) lpa_dig = np.array([ p['r'] for p in info['dig'] if all([ p['ident'] == FIFF.FIFFV_POINT_LPA, p['kind'] == FIFF.FIFFV_POINT_CARDINAL ]) ]) rpa_dig = np.array([ p['r'] for p in info['dig'] if all([ p['ident'] == FIFF.FIFFV_POINT_RPA, p['kind'] == FIFF.FIFFV_POINT_CARDINAL ]) ]) hpi_dig = np.array( [p['r'] for p in info['dig'] if p['kind'] == FIFF.FIFFV_POINT_HPI]) assert_allclose(hs, hsp_points, atol=1e-7) assert_allclose(nasion_dig.ravel(), nasion, atol=1e-7) assert_allclose(lpa_dig.ravel(), lpa, atol=1e-7) assert_allclose(rpa_dig.ravel(), rpa, atol=1e-7) assert_allclose(hpi_dig, elp_points[3:], atol=1e-7)
def test_io_dig_points(): """Test Writing for dig files.""" tempdir = _TempDir() points = _read_dig_points(hsp_fname) dest = op.join(tempdir, 'test.txt') dest_bad = op.join(tempdir, 'test.mne') pytest.raises(ValueError, _write_dig_points, dest, points[:, :2]) pytest.raises(ValueError, _write_dig_points, dest_bad, points) _write_dig_points(dest, points) points1 = _read_dig_points(dest, unit='m') err = "Dig points diverged after writing and reading." assert_array_equal(points, points1, err) points2 = np.array([[-106.93, 99.80], [99.80, 68.81]]) np.savetxt(dest, points2, delimiter='\t', newline='\n') pytest.raises(ValueError, _read_dig_points, dest)
def test_io_dig_points(tmpdir): """Test Writing for dig files.""" points = _read_dig_points(hsp_fname) dest = str(tmpdir.join('test.txt')) dest_bad = str(tmpdir.join('test.mne')) with pytest.raises(ValueError, match='must be of shape'): _write_dig_points(dest, points[:, :2]) with pytest.raises(ValueError, match='extension'): _write_dig_points(dest_bad, points) _write_dig_points(dest, points) points1 = _read_dig_points(dest, unit='m') err = "Dig points diverged after writing and reading." assert_array_equal(points, points1, err) points2 = np.array([[-106.93, 99.80], [99.80, 68.81]]) np.savetxt(dest, points2, delimiter='\t', newline='\n') with pytest.raises(ValueError, match='must be of shape'): _read_dig_points(dest)
def test_read_dig_montage(): """Test read_dig_montage.""" names = ['nasion', 'lpa', 'rpa', '1', '2', '3', '4', '5'] montage = read_dig_montage(hsp, hpi, elp, names, transform=False) elp_points = _read_dig_points(elp) hsp_points = _read_dig_points(hsp) hpi_points = read_mrk(hpi) with pytest.deprecated_call(): assert_equal(montage.point_names, names) assert_array_equal(montage.elp, elp_points) assert_array_equal(montage.hsp, hsp_points) assert (montage.dev_head_t is None) montage = read_dig_montage(hsp, hpi, elp, names, transform=True, dev_head_t=True) # check coordinate transformation # nasion with pytest.deprecated_call(): assert_almost_equal(montage.nasion[0], 0) assert_almost_equal(montage.nasion[2], 0) # lpa and rpa with pytest.deprecated_call(): assert_allclose(montage.lpa[1:], 0, atol=1e-16) assert_allclose(montage.rpa[1:], 0, atol=1e-16) # device head transform EXPECTED_DEV_HEAD_T = np.array( [[-3.72201691e-02, -9.98212167e-01, -4.67667497e-02, -7.31583414e-04], [8.98064989e-01, -5.39382685e-02, 4.36543170e-01, 1.60134431e-02], [-4.38285221e-01, -2.57513699e-02, 8.98466990e-01, 6.13035748e-02], [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.00000000e+00]]) assert_allclose(montage.dev_head_t, EXPECTED_DEV_HEAD_T, atol=1e-7) # Digitizer as array with pytest.deprecated_call(): m2 = read_dig_montage(hsp_points, hpi_points, elp_points, names, unit='m') assert_array_equal(m2.hsp, montage.hsp) m3 = read_dig_montage(hsp_points * 1000, hpi_points, elp_points * 1000, names) with pytest.deprecated_call(): assert_allclose(m3.hsp, montage.hsp) # test unit parameter and .mat support tempdir = _TempDir() mat_hsp = op.join(tempdir, 'test.mat') savemat(mat_hsp, dict(Points=(1000 * hsp_points).T), oned_as='row') montage_cm = read_dig_montage(mat_hsp, hpi, elp, names, unit='cm') with pytest.deprecated_call(): assert_allclose(montage_cm.hsp, montage.hsp * 10.) assert_allclose(montage_cm.elp, montage.elp * 10.) pytest.raises(ValueError, read_dig_montage, hsp, hpi, elp, names, unit='km') # extra columns extra_hsp = op.join(tempdir, 'test.txt') with open(hsp, 'rb') as fin: with open(extra_hsp, 'wb') as fout: for line in fin: if line.startswith(b'%'): fout.write(line) else: # extra column fout.write(line.rstrip() + b' 0.0 0.0 0.0\n') with pytest.warns(RuntimeWarning, match='Found .* columns instead of 3'): montage_extra = read_dig_montage(extra_hsp, hpi, elp, names) with pytest.deprecated_call(): assert_allclose(montage_extra.hsp, montage.hsp) assert_allclose(montage_extra.elp, montage.elp)