def test_fs_xfm(): """Test reading and writing of Freesurfer transforms.""" for subject in ('fsaverage', 'sample'): fname = op.join(data_path, 'subjects', subject, 'mri', 'transforms', 'talairach.xfm') xfm, kind = _read_fs_xfm(fname) if subject == 'fsaverage': assert_allclose(xfm, np.eye(4), atol=1e-5) # fsaverage is in MNI assert kind == 'MNI Transform File' tempdir = _TempDir() fname_out = op.join(tempdir, 'out.xfm') _write_fs_xfm(fname_out, xfm, kind) xfm_read, kind_read = _read_fs_xfm(fname_out) assert kind_read == kind assert_allclose(xfm, xfm_read, rtol=1e-5, atol=1e-5) # Some wacky one xfm[:3] = np.random.RandomState(0).randn(3, 4) _write_fs_xfm(fname_out, xfm, 'foo') xfm_read, kind_read = _read_fs_xfm(fname_out) assert kind_read == 'foo' assert_allclose(xfm, xfm_read, rtol=1e-5, atol=1e-5) # degenerate conditions with open(fname_out, 'w') as fid: fid.write('foo') with pytest.raises(ValueError, match='Failed to find'): _read_fs_xfm(fname_out) _write_fs_xfm(fname_out, xfm[:2], 'foo') with pytest.raises(ValueError, match='Could not find'): _read_fs_xfm(fname_out)