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 run_makespec(config, env): '''Generates the SUMA specifcation files for all hemispheres and ld values''' refdir = config['refdir'] icolds, hemis = _get_hemis_icolds(config) for icold in icolds: for hemi in hemis: # make spec file surfprefix = '%s%sh' % (config['mi_icopat'] % icold, hemi) specfn = afni_suma_spec.canonical_filename(icold, hemi, config['alsuffix']) specpathfn = os.path.join(refdir, specfn) if config['overwrite'] or not os.path.exists(specpathfn): suma_makespec(refdir, surfprefix, config['surfformat'], specpathfn, removepostfix=config['alsuffix']) else: print "Skipping spec for %s" % specpathfn # make simple script to run afni and suma runsumafn = '%s/%sh_ico%d_runsuma.sh' % (refdir, hemi, icold) surfvol = '%(sid)s_SurfVol%(al2expsuffix)s+orig' % config if config['overwrite'] or not os.path.exists(runsumafn): suma_makerunsuma(runsumafn, specfn, surfvol)
def run_makespec(config, env): """Generates the SUMA specifcation files for all hemispheres and ld values""" refdir = config["refdir"] icolds, hemis = _get_hemis_icolds(config) for icold in icolds: for hemi in hemis: # make spec file surfprefix = "%s%sh" % (config["mi_icopat"] % icold, hemi) specfn = afni_suma_spec.canonical_filename(icold, hemi, config["alsuffix"]) specpathfn = pathjoin(refdir, specfn) if config["overwrite"] or not os.path.exists(specpathfn): suma_makespec(refdir, surfprefix, config["surfformat"], specpathfn, removepostfix=config["alsuffix"]) else: print "Skipping spec for %s" % specpathfn # make simple script to run afni and suma runsumafn = "%s/%sh_ico%d_runsuma.sh" % (refdir, hemi, icold) surfvol = "%(sid)s_SurfVol%(al2expsuffix)s+orig" % config if config["overwrite"] or not os.path.exists(runsumafn): suma_makerunsuma(runsumafn, specfn, surfvol)
def run_makespec(config, env): '''Generates the SUMA specifcation files for all hemispheres and ld values''' refdir = config['refdir'] icolds, hemis = _get_hemis_icolds(config) for icold in icolds: for hemi in hemis: # make spec file surfprefix = '%s%sh' % (config['mi_icopat'] % icold, hemi) specfn = afni_suma_spec.canonical_filename(icold, hemi, config['alsuffix']) specpathfn = os.path.join(refdir, specfn) if config['overwrite'] or not os.path.exists(specpathfn): suma_makespec(refdir, surfprefix, specpathfn, removepostfix=config['alsuffix']) else: print "Skipping spec for %s" % specpathfn # make simple script to run afni and suma runsumafn = '%s/%sh_ico%d_runsuma.sh' % (refdir, hemi, icold) surfvol = '%(sid)s_SurfVol%(al2expsuffix)s+orig' % config if config['overwrite'] or not os.path.exists(runsumafn): suma_makerunsuma(runsumafn, specfn, surfvol)
def run_makesurfmasks(config, env): refdir = config['refdir'] overwrite = config['overwrite'] if config['sid'] is None: raise ValueError('Need sid') sumfn = '%s_qa_surf_mask' % config['sid'] # output file fullext = config['outvol_fullext'] volor = config['outvol_ext'] sumfn_path = '%s/%s%s' % (refdir, sumfn, fullext) qafn_path = '%s/%s.png' % (refdir, sumfn) checkfn_paths = (sumfn_path, qafn_path) if all(map(os.path.exists, checkfn_paths)) and not overwrite: print "Already exist: %s" % (", ".join(checkfn_paths)) return icolds, hemis = _get_hemis_icolds(config) volexts = ['%s%s' % (volor, e) for e in '.HEAD', '.BRIK*'] sssuffix = config['sssuffix'] if config['expvol_ss'] else '' expvol_fn = '%s%s%s' % (utils.afni_fileparts( config['expvol'])[1], sssuffix, volor) #if overwrite or not os.path.exists('%s/%s' % (refdir, sv_al_nii_fn)): # cmd = 'cd %s; 3dcopy -overwrite %s %s' % (refdir, sv_al_orig_fn, sv_al_nii_fn) # utils.run_cmds(cmd, env) if hemis != ['l', 'r']: raise ValueError("Cannot run without left and right hemisphere") icold = max(icolds) oneDfn = '__t.1D' oneDtfn = '__tt.1D' # transposed cmds = [ 'cd %s' % refdir, '1deval -1D: -num %d -expr 1 > %s' % (icold**2 * 10 + 1, oneDfn), '1dtranspose %s > %s' % (oneDfn, oneDtfn) ] utils.run_cmds(';'.join(cmds), env) tmpfns = [oneDfn, oneDtfn] s2v_cmd = ('3dSurf2Vol -map_func mask2 -data_expr "a*%%d" -spec %%s %%s -sv %s' ' -grid_parent %s. -prefix %%s -sdata %s -overwrite') % \ (expvol_fn, expvol_fn, oneDtfn) infix2val = { '-surf_A pial': 1, '-surf_A smoothwm': 2, '-surf_A smoothwm -surf_B pial -f_steps 20': 4 } volfns = [] for hemi in hemis: specfn = afni_suma_spec.canonical_filename(icold, hemi, config['alsuffix']) for infix, val in infix2val.iteritems(): fnprefix = '__m%d_%sh' % (val, hemi) cmd = s2v_cmd % (val, specfn, infix, fnprefix) utils.run_cmds('cd %s;%s' % (refdir, cmd)) tmpfns.extend(['%s%s' % (fnprefix, e) for e in volexts]) volfns.append(fnprefix + volor) cmds = ['cd %s' % refdir] catfn = '__cat' cmds.extend([ '3dTcat -overwrite -prefix %s %s' % (catfn, ' '.join(volfns)), '3dTstat -overwrite -sum -prefix %s %s%s' % (sumfn, catfn, volor) ]) tmpfns.extend(['%s%s' % (catfn, e) for e in volexts]) cmds.extend('rm %s' % fn for fn in tmpfns) cmds.append('echo "Surface mask in %s"' % sumfn) utils.run_cmds(';'.join(cmds), env) # make plot if overwrite or not os.path.exists(qafn_path): expvol_path = '%s/%s' % (refdir, expvol_fn) _make_slice_plot(expvol_path, sumfn_path, qafn_path)
def run_makespec_bothhemis(config, env): refdir = config['refdir'] overwrite = config['overwrite'] icolds, hemis = _get_hemis_icolds(config) ext = format2extension(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) s = afni_suma_spec.read(specpathfn) specs.append(afni_suma_spec.read(specpathfn)) add_states = ['inflated', 'full.patch.flat', 'sphere.reg'] add_states_required = [True, False, True] # flat surface is optional for add_state, is_req in zip(add_states, add_states_required): has_state = all([ len(spec.find_surface_from_state(add_state)) == 1 for spec in specs ]) if not has_state: if is_req: error('cannot find state %s' % add_state) else: # skip this state print "Optional state %s not found - skipping" % add_state continue specs = afni_suma_spec.hemi_pairs_add_views(specs, add_state, ext, 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.read(full_path(fn)) for fn in fns_in] if all(['full.patch.flat' in fn for fn in fns_in]): # left hemi of flat; rotate 180 degrees, reposition again surfs_in[0] = surfs_in[0] * [-1, -1, 1] surfs_in = surf.reposition_hemisphere_pairs( surfs_in[0], surfs_in[1], 'm') surf_merged = surf.merge(*surfs_in) if config['overwrite'] or not os.path.exists( full_path(fn_out)): surf.write(full_path(fn_out), surf_merged) print "Merged surfaces written to %s" % fn_out
def run_makesurfmasks(config, env): refdir = config['refdir'] overwrite = config['overwrite'] if config['sid'] is None: raise ValueError('Need sid') sumfn = '%s_qa_surf_mask' % config['sid'] # output file fullext = config['outvol_fullext'] volor = config['outvol_ext'] sumfn_path = '%s/%s%s' % (refdir, sumfn, fullext) qafn_path = '%s/%s.png' % (refdir, sumfn) checkfn_paths = (sumfn_path, qafn_path) if all(map(os.path.exists, checkfn_paths)) and not overwrite: print "Already exist: %s" % (", ".join(checkfn_paths)) return icolds, hemis = _get_hemis_icolds(config) volexts = ['%s%s' % (volor, e) for e in '.HEAD', '.BRIK*'] sssuffix = config['sssuffix'] if config['expvol_ss'] else '' expvol_fn = '%s%s%s' % (utils.afni_fileparts(config['expvol'])[1], sssuffix, volor) #if overwrite or not os.path.exists('%s/%s' % (refdir, sv_al_nii_fn)): # cmd = 'cd %s; 3dcopy -overwrite %s %s' % (refdir, sv_al_orig_fn, sv_al_nii_fn) # utils.run_cmds(cmd, env) if hemis != ['l', 'r']: raise ValueError("Cannot run without left and right hemisphere") icold = max(icolds) oneDfn = '__t.1D' oneDtfn = '__tt.1D' # transposed cmds = ['cd %s' % refdir, '1deval -1D: -num %d -expr 1 > %s' % (icold ** 2 * 10 + 1, oneDfn), '1dtranspose %s > %s' % (oneDfn, oneDtfn)] utils.run_cmds(';'.join(cmds), env) tmpfns = [oneDfn, oneDtfn] s2v_cmd = ('3dSurf2Vol -map_func mask2 -data_expr "a*%%d" -spec %%s %%s -sv %s' ' -grid_parent %s. -prefix %%s -sdata %s -overwrite') % \ (expvol_fn, expvol_fn, oneDtfn) infix2val = {'-surf_A pial':1, '-surf_A smoothwm':2, '-surf_A smoothwm -surf_B pial -f_steps 20': 4} volfns = [] for hemi in hemis: specfn = afni_suma_spec.canonical_filename(icold, hemi, config['alsuffix']) for infix, val in infix2val.iteritems(): fnprefix = '__m%d_%sh' % (val, hemi) cmd = s2v_cmd % (val, specfn, infix, fnprefix) utils.run_cmds('cd %s;%s' % (refdir, cmd)) tmpfns.extend(['%s%s' % (fnprefix, e) for e in volexts]) volfns.append(fnprefix + volor) cmds = ['cd %s' % refdir] catfn = '__cat' cmds.extend(['3dTcat -overwrite -prefix %s %s' % (catfn, ' '.join(volfns)), '3dTstat -overwrite -sum -prefix %s %s%s' % (sumfn, catfn, volor)]) tmpfns.extend(['%s%s' % (catfn, e) for e in volexts]) cmds.extend('rm %s' % fn for fn in tmpfns) cmds.append('echo "Surface mask in %s"' % sumfn) utils.run_cmds(';'.join(cmds), env) # make plot if overwrite or not os.path.exists(qafn_path): expvol_path = '%s/%s' % (refdir, expvol_fn) _make_slice_plot(expvol_path, sumfn_path, qafn_path)
def run_makespec_bothhemis(config, env): refdir = config['refdir'] overwrite = config['overwrite'] icolds, hemis = _get_hemis_icolds(config) ext = format2extension(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 = pathjoin(refdir, specfn) s = afni_suma_spec.read(specpathfn) specs.append(afni_suma_spec.read(specpathfn)) add_states = ['inflated', 'full.patch.flat', 'sphere.reg'] add_states_required = [True, False, True] # flat surface is optional for add_state, is_req in zip(add_states, add_states_required): has_state = all([len(spec.find_surface_from_state(add_state)) == 1 for spec in specs]) if not has_state: if is_req: error('cannot find state %s' % add_state) else: # skip this state print "Optional state %s not found - skipping" % add_state continue specs = afni_suma_spec.hemi_pairs_add_views(specs, add_state, ext, 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 = pathjoin(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 = pathjoin(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:pathjoin(refdir, x) for fn_out, fns_in in surfs_to_join.iteritems(): surfs_in = [surf.read(full_path(fn)) for fn in fns_in] if all(['full.patch.flat' in fn for fn in fns_in]): # left hemi of flat; rotate 180 degrees, reposition again surfs_in[0] = surfs_in[0] * [-1, -1, 1] surfs_in = surf.reposition_hemisphere_pairs(surfs_in[0], surfs_in[1], 'm') surf_merged = surf.merge(*surfs_in) if config['overwrite'] or not os.path.exists(full_path(fn_out)): surf.write(full_path(fn_out), surf_merged) print "Merged surfaces written to %s" % fn_out
def run_makesurfmasks(config, env): refdir = config["refdir"] overwrite = config["overwrite"] if config["sid"] is None: raise ValueError("Need sid") sumfn = "%s_qa_surf_mask" % config["sid"] # output file fullext = config["outvol_fullext"] volor = config["outvol_ext"] sumfn_path = "%s/%s%s" % (refdir, sumfn, fullext) qafn_path = "%s/%s.png" % (refdir, sumfn) checkfn_paths = (sumfn_path, qafn_path) if all(map(os.path.exists, checkfn_paths)) and not overwrite: print "Already exist: %s" % (", ".join(checkfn_paths)) return icolds, hemis = _get_hemis_icolds(config) volexts = ["%s%s" % (volor, e) for e in ".HEAD", ".BRIK*"] sssuffix = config["sssuffix"] if config["expvol_ss"] else "" expvol_fn = "%s%s%s" % (utils.afni_fileparts(config["expvol"])[1], sssuffix, volor) # if overwrite or not os.path.exists('%s/%s' % (refdir, sv_al_nii_fn)): # cmd = 'cd %s; 3dcopy -overwrite %s %s' % (refdir, sv_al_orig_fn, sv_al_nii_fn) # utils.run_cmds(cmd, env) if hemis != ["l", "r"]: raise ValueError("Cannot run without left and right hemisphere") icold = max(icolds) oneDfn = "__t.1D" oneDtfn = "__tt.1D" # transposed cmds = [ "cd %s" % refdir, "1deval -1D: -num %d -expr 1 > %s" % (icold ** 2 * 10 + 1, oneDfn), "1dtranspose %s > %s" % (oneDfn, oneDtfn), ] utils.run_cmds(";".join(cmds), env) tmpfns = [oneDfn, oneDtfn] s2v_cmd = ( '3dSurf2Vol -map_func mask2 -data_expr "a*%%d" -spec %%s %%s -sv %s' " -grid_parent %s. -prefix %%s -sdata %s -overwrite" ) % (expvol_fn, expvol_fn, oneDtfn) infix2val = {"-surf_A pial": 1, "-surf_A smoothwm": 2, "-surf_A smoothwm -surf_B pial -f_steps 20": 4} volfns = [] for hemi in hemis: specfn = afni_suma_spec.canonical_filename(icold, hemi, config["alsuffix"]) for infix, val in infix2val.iteritems(): fnprefix = "__m%d_%sh" % (val, hemi) cmd = s2v_cmd % (val, specfn, infix, fnprefix) utils.run_cmds("cd %s;%s" % (refdir, cmd)) tmpfns.extend(["%s%s" % (fnprefix, e) for e in volexts]) volfns.append(fnprefix + volor) cmds = ["cd %s" % refdir] catfn = "__cat" cmds.extend( [ "3dTcat -overwrite -prefix %s %s" % (catfn, " ".join(volfns)), "3dTstat -overwrite -sum -prefix %s %s%s" % (sumfn, catfn, volor), ] ) tmpfns.extend(["%s%s" % (catfn, e) for e in volexts]) cmds.extend("rm %s" % fn for fn in tmpfns) cmds.append('echo "Surface mask in %s"' % sumfn) utils.run_cmds(";".join(cmds), env) # make plot if overwrite or not os.path.exists(qafn_path): expvol_path = "%s/%s" % (refdir, expvol_fn) _make_slice_plot(expvol_path, sumfn_path, qafn_path)