def test_surf_fs_asc(self, temp_fn): s = surf.generate_sphere(5) * 100 surf_fs_asc.write(temp_fn, s, overwrite=True) t = surf_fs_asc.read(temp_fn) assert_array_almost_equal(s.vertices, t.vertices) assert_array_almost_equal(s.vertices, t.vertices) theta = np.asarray([0, 0., 180.]) r = s.rotate(theta, unit='deg') l2r = surf.get_sphere_left_right_mapping(s, r) l2r_expected = [0, 1, 2, 6, 5, 4, 3, 11, 10, 9, 8, 7, 15, 14, 13, 12, 16, 19, 18, 17, 21, 20, 23, 22, 26, 25, 24] assert_array_equal(l2r, np.asarray(l2r_expected)) sides_facing = 'apism' for side_facing in sides_facing: l, r = surf.reposition_hemisphere_pairs(s + 10., t + (-10.), side_facing) m = surf.merge(l, r) # not sure at the moment why medial rotation # messes up - but leave for now eps = 666 if side_facing == 'm' else .001 assert_true((abs(m.center_of_mass) < eps).all())
def test_surf_fs_asc(self, temp_fn): s = surf.generate_sphere(5) * 100 surf_fs_asc.write(temp_fn, s, overwrite=True) t = surf_fs_asc.read(temp_fn) assert_array_almost_equal(s.vertices, t.vertices) assert_array_almost_equal(s.vertices, t.vertices) theta = np.asarray([0, 0., 180.]) r = s.rotate(theta, unit='deg') l2r = surf.get_sphere_left_right_mapping(s, r) l2r_expected = [ 0, 1, 2, 6, 5, 4, 3, 11, 10, 9, 8, 7, 15, 14, 13, 12, 16, 19, 18, 17, 21, 20, 23, 22, 26, 25, 24 ] assert_array_equal(l2r, np.asarray(l2r_expected)) sides_facing = 'apism' for side_facing in sides_facing: l, r = surf.reposition_hemisphere_pairs(s + 10., t + (-10.), side_facing) m = surf.merge(l, r) # not sure at the moment why medial rotation # messes up - but leave for now eps = 666 if side_facing == 'm' else .001 assert_true((abs(m.center_of_mass) < eps).all())
def run_makespec_bothhemis(config, env): refdir = config['refdir'] overwrite = config['overwrite'] icolds, hemis = _get_hemis_icolds(config) if hemis != ['l', 'r']: raise ValueError("Cannot run without left and right hemisphere") for icold in icolds: specs = [] for hemi in hemis: #surfprefix = '%s%sh' % (config['mi_icopat'] % icold, hemi) specfn = afni_suma_spec.canonical_filename(icold, hemi, config['alsuffix']) specpathfn = os.path.join(refdir, specfn) specs.append(afni_suma_spec.read(specpathfn)) specs = afni_suma_spec.hemi_pairs_add_views(specs, 'inflated', refdir, overwrite=overwrite) specs = afni_suma_spec.hemi_pairs_add_views(specs, 'sphere.reg', refdir, overwrite=overwrite) spec_both = afni_suma_spec.combine_left_right(specs) # generate spec files for both hemispheres hemiboth = 'b' specfn = afni_suma_spec.canonical_filename(icold, hemiboth, config['alsuffix']) specpathfn = os.path.join(refdir, specfn) spec_both.write(specpathfn, overwrite=overwrite) # merge left and right into one surface # and generate the spec files as well hemimerged = 'm' specfn = afni_suma_spec.canonical_filename(icold, hemimerged, config['alsuffix']) specpathfn = os.path.join(refdir, specfn) if config['overwrite'] or not os.path.exists(specpathfn): spec_merged, surfs_to_join = afni_suma_spec.merge_left_right( spec_both) spec_merged.write(specpathfn, overwrite=overwrite) full_path = lambda x: os.path.join(refdir, x) for fn_out, fns_in in surfs_to_join.iteritems(): surfs_in = [surf_fs_asc.read(full_path(fn)) for fn in fns_in] surf_merged = surf.merge(*surfs_in) if config['overwrite'] or not os.path.exists( full_path(fn_out)): surf_fs_asc.write(surf_merged, full_path(fn_out), overwrite=overwrite) print "Merged surfaces written to %s" % fn_out
def run_makespec_bothhemis(config, env): refdir = config['refdir'] overwrite = config['overwrite'] icolds, hemis = _get_hemis_icolds(config) if hemis != ['l', 'r']: raise ValueError("Cannot run without left and right hemisphere") for icold in icolds: specs = [] for hemi in hemis: #surfprefix = '%s%sh' % (config['mi_icopat'] % icold, hemi) specfn = afni_suma_spec.canonical_filename(icold, hemi, config['alsuffix']) specpathfn = os.path.join(refdir, specfn) specs.append(afni_suma_spec.read(specpathfn)) specs = afni_suma_spec.hemi_pairs_add_views(specs, 'inflated', refdir, overwrite=overwrite) specs = afni_suma_spec.hemi_pairs_add_views(specs, 'sphere.reg', refdir, overwrite=overwrite) spec_both = afni_suma_spec.combine_left_right(specs) # generate spec files for both hemispheres hemiboth = 'b' specfn = afni_suma_spec.canonical_filename(icold, hemiboth, config['alsuffix']) specpathfn = os.path.join(refdir, specfn) spec_both.write(specpathfn, overwrite=overwrite) # merge left and right into one surface # and generate the spec files as well hemimerged = 'm' specfn = afni_suma_spec.canonical_filename(icold, hemimerged, config['alsuffix']) specpathfn = os.path.join(refdir, specfn) if config['overwrite'] or not os.path.exists(specpathfn): spec_merged, surfs_to_join = afni_suma_spec.merge_left_right(spec_both) spec_merged.write(specpathfn, overwrite=overwrite) full_path = lambda x:os.path.join(refdir, x) for fn_out, fns_in in surfs_to_join.iteritems(): surfs_in = [surf_fs_asc.read(full_path(fn)) for fn in fns_in] surf_merged = surf.merge(*surfs_in) if config['overwrite'] or not os.path.exists(full_path(fn_out)): surf_fs_asc.write(surf_merged, full_path(fn_out), overwrite=overwrite) print "Merged surfaces written to %s" % fn_out
def read(fn): '''General read function for surfaces For now only supports ascii (as used in AFNI's SUMA), Caret and freesurfer formats ''' if fn.endswith('.asc'): from mvpa2.support.nibabel import surf_fs_asc return surf_fs_asc.read(fn) elif fn.endswith('.coord'): from mvpa2.support.nibabel import surf_caret return surf_caret.read(fn) else: import nibabel.freesurfer.io as fsio coords, faces = fsio.read_geometry(fn) return Surface(coords, faces)
def average_fs_asc_surfs(fn1, fn2, fnout): '''averages two surfaces''' surf1 = surf_fs_asc.read(fn1) surf2 = surf_fs_asc.read(fn2) surfavg = surf1 * .5 + surf2 * .5 surf_fs_asc.write(surfavg, fnout, overwrite=True)