def run_skullstrip(config, env): fullext = config['outvol_fullext'] overwrite = config['overwrite'] refdir = config['refdir'] cmds = [] if not os.path.exists(refdir): cmds.append('mkdir %(refdir)s' % config) sumadir = config['sumadir'] sid = config['sid'] fs_sid = config['fs_sid'] if not sid: raise ValueError("Subject id is not set, cannot continue") # process the surfvol anatomical. # because it's already skull stripped by freesurfer # simply copy it over; rename brain.nii to surfvol_ss surfvol_srcs = ['%s/%s' % (sumadir, fn) for fn in ['brain.nii', 'T1.nii']] surfvol_trgs = [ '%s/%s' % (refdir, fn) for fn in ['%s_SurfVol_ss%s' % (sid, fullext), '%s_SurfVol%s' % (sid, fullext)] ] for src, trg in zip(surfvol_srcs, surfvol_trgs): if os.path.exists(trg) and not overwrite: print '%s already exists' % trg else: t_p, t_n, t_o, t_e = utils.afni_fileparts(trg) trg_short = '%s%s' % (t_n, t_o) cmds.append( 'cd "%s"; 3dresample -overwrite -orient LPI -inset %s -prefix ./%s' % (refdir, src, trg_short)) cmds.append( _set_vol_space_cmd('%s/%s+orig' % (refdir, t_n), config)) # process experimental volume. expvol_src = config['expvol'] do_ss = config['expvol_ss'] [e_p, e_n, e_o, e_e] = utils.afni_fileparts(expvol_src) expvol_trg_prefix = '%s%s' % (e_n, config['sssuffix'] if do_ss else '') expvol_trg_tmp_prefix = '__tmp_%s' % expvol_trg_prefix expvol_trg = '%s/%s%s' % (refdir, expvol_trg_prefix, fullext) print "Attempt %s -> %s" % (expvol_src, expvol_trg) ext = config['outvol_ext'] if overwrite or not utils.afni_fileexists(expvol_trg): if do_ss: cmds.append( 'cd "%s";3dSkullStrip -overwrite -prefix ./%s%s -input %s' % (refdir, expvol_trg_tmp_prefix, ext, expvol_src)) else: cmds.append('cd "%s";3dbucket -overwrite -prefix ./%s%s %s' % (refdir, expvol_trg_tmp_prefix, ext, expvol_src)) cmds.append( 'cd "%s"; 3dresample -overwrite -orient LPI -prefix %s -inset %s%s' % (refdir, expvol_trg_prefix, expvol_trg_tmp_prefix, ext)) cmds.append('rm %s/%s*' % (refdir, expvol_trg_tmp_prefix)) cmds.append(_set_vol_space_cmd(expvol_trg, config)) else: print "No skull strip because already exists: %s%s" % ( expvol_trg_prefix, ext) utils.run_cmds(cmds, env)
def run_skullstrip(config, env): if config['identity']: return overwrite = config['overwrite'] refdir = config['refdir'] cmds = [] if not os.path.exists(refdir): cmds.append('mkdir %(refdir)s' % config) sumadir = config['sumadir'] sid = config['sid'] fs_sid = config['fs_sid'] if not sid: raise ValueError("Subject id is not set, cannot continue") # process the surfvol anatomical. # because it's already skull stripped by freesurfer # simply copy it over; rename brain.nii to surfvol_ss surfvol_srcs = ['%s/%s' % (sumadir, fn) for fn in ['brain.nii', 'T1.nii']] surfvol_trgs = [ '%s/%s' % (refdir, fn) for fn in ['%s_SurfVol_ss+orig.HEAD' % sid, '%s_SurfVol+orig.HEAD' % sid] ] for src, trg in zip(surfvol_srcs, surfvol_trgs): if os.path.exists(trg) and not overwrite: print '%s already exists' % trg else: t_p, t_n, t_o, t_e = utils.afni_fileparts(trg) trg_short = '%s%s' % (t_n, t_o) cmds.append('cd "%s"; 3dcopy -overwrite %s ./%s' % (refdir, src, trg_short)) # process experimental volume. expvol_src = config['expvol'] do_ss = config['expvol_ss'] [e_p, e_n, e_o, e_e] = utils.afni_fileparts(expvol_src) expvol_trg_prefix = '%s%s' % (e_n, config['sssuffix'] if do_ss else '') if 'nii' in e_e: # ensure e_n+orig is in refdir if overwrite or not utils.afni_fileexists('%s/%s+orig.HEAD' % (refdir, e_n)): print "Converting %s from NIFTI to AFNI format" % e_n cmds.append('cd "%s"; 3dbucket -overwrite -prefix ./%s+orig %s' % (refdir, e_n, expvol_src)) cmds.append( 'if [ -e %s/%s+tlrc.HEAD ]; then 3drefit -view orig -space ORIG %s/%s+tlrc; else echo "File in orig orientation - no refit necessary"; fi' % (refdir, e_n, refdir, e_n)) expvol_src = '%s/%s+orig.HEAD' % (refdir, e_n) expvol_trg = '%s/%s+orig.HEAD' % (refdir, expvol_trg_prefix) print "Attempt %s -> %s" % (expvol_src, expvol_trg) if overwrite or not utils.afni_fileexists(expvol_trg): if do_ss: cmds.append( 'cd "%s";3dSkullStrip -overwrite -prefix ./%s+orig -input %s' % (refdir, expvol_trg_prefix, expvol_src)) else: cmds.append('cd "%s";3dbucket -overwrite -prefix ./%s+orig %s' % (refdir, expvol_trg_prefix, expvol_src)) else: print "No skull strip because already exists: %s+orig" % expvol_trg_prefix utils.run_cmds(cmds, env)
def run_skullstrip(config, env): fullext = config['outvol_fullext'] overwrite = config['overwrite'] refdir = config['refdir'] cmds = [] if not os.path.exists(refdir): cmds.append('mkdir %(refdir)s' % config) sumadir = config['sumadir'] sid = config['sid'] fs_sid = config['fs_sid'] if not sid: raise ValueError("Subject id is not set, cannot continue") # process the surfvol anatomical. # because it's already skull stripped by freesurfer # simply copy it over; rename brain.nii to surfvol_ss surfvol_srcs = ['%s/%s' % (sumadir, fn) for fn in ['brain.nii', 'T1.nii']] surfvol_trgs = ['%s/%s' % (refdir, fn) for fn in ['%s_SurfVol_ss%s' % (sid, fullext), '%s_SurfVol%s' % (sid, fullext)]] for src, trg in zip(surfvol_srcs, surfvol_trgs): if os.path.exists(trg) and not overwrite: print '%s already exists' % trg else: t_p, t_n, t_o, t_e = utils.afni_fileparts(trg) trg_short = '%s%s' % (t_n, t_o) cmds.append('cd "%s"; 3dresample -overwrite -orient LPI -inset %s -prefix ./%s' % (refdir, src, trg_short)) cmds.append(_set_vol_space_cmd('%s/%s+orig' % (refdir, t_n), config)) # process experimental volume. expvol_src = config['expvol'] do_ss = config['expvol_ss'] [e_p, e_n, e_o, e_e] = utils.afni_fileparts(expvol_src) expvol_trg_prefix = '%s%s' % (e_n, config['sssuffix'] if do_ss else '') expvol_trg_tmp_prefix = '__tmp_%s' % expvol_trg_prefix expvol_trg = '%s/%s%s' % (refdir, expvol_trg_prefix, fullext) print "Attempt %s -> %s" % (expvol_src, expvol_trg) ext = config['outvol_ext'] if overwrite or not utils.afni_fileexists(expvol_trg): if do_ss: cmds.append('cd "%s";3dSkullStrip -overwrite -prefix ./%s%s -input %s' % (refdir, expvol_trg_tmp_prefix, ext, expvol_src)) else: cmds.append('cd "%s";3dbucket -overwrite -prefix ./%s%s %s' % (refdir, expvol_trg_tmp_prefix, ext, expvol_src)) cmds.append('cd "%s"; 3dresample -overwrite -orient LPI -prefix %s -inset %s%s' % (refdir, expvol_trg_prefix, expvol_trg_tmp_prefix, ext)) cmds.append('rm %s/%s*' % (refdir, expvol_trg_tmp_prefix)) cmds.append(_set_vol_space_cmd(expvol_trg, config)) else: print "No skull strip because already exists: %s%s" % (expvol_trg_prefix, ext) utils.run_cmds(cmds, env)
def run_skullstrip(config, env): if config['identity']: return overwrite = config['overwrite'] refdir = config['refdir'] cmds = [] if not os.path.exists(refdir): cmds.append('mkdir %(refdir)s' % config) sumadir = config['sumadir'] sid = config['sid'] fs_sid = config['fs_sid'] if not sid: raise ValueError("Subject id is not set, cannot continue") # process the surfvol anatomical. # because it's already skull stripped by freesurfer # simply copy it over; rename brain.nii to surfvol_ss surfvol_srcs = ['%s/%s' % (sumadir, fn) for fn in ['brain.nii', 'T1.nii']] surfvol_trgs = ['%s/%s' % (refdir, fn) for fn in ['%s_SurfVol_ss+orig.HEAD' % sid, '%s_SurfVol+orig.HEAD' % sid]] for src, trg in zip(surfvol_srcs, surfvol_trgs): if os.path.exists(trg) and not overwrite: print '%s already exists' % trg else: t_p, t_n, t_o, t_e = utils.afni_fileparts(trg) trg_short = '%s%s' % (t_n, t_o) cmds.append('cd "%s"; 3dcopy -overwrite %s ./%s' % (refdir, src, trg_short)) # process experimental volume. expvol_src = config['expvol'] do_ss = config['expvol_ss'] [e_p, e_n, e_o, e_e] = utils.afni_fileparts(expvol_src) expvol_trg_prefix = '%s%s' % (e_n, config['sssuffix'] if do_ss else '') if 'nii' in e_e: # ensure e_n+orig is in refdir if overwrite or not utils.afni_fileexists('%s/%s+orig.HEAD' % (refdir, e_n)): print "Converting %s from NIFTI to AFNI format" % e_n cmds.append('cd "%s"; 3dbucket -overwrite -prefix ./%s+orig %s' % (refdir, e_n, expvol_src)) cmds.append('if [ -e %s/%s+tlrc.HEAD ]; then 3drefit -view orig -space ORIG %s/%s+tlrc; else echo "File in orig orientation - no refit necessary"; fi' % (refdir, e_n, refdir, e_n)) expvol_src = '%s/%s+orig.HEAD' % (refdir, e_n) expvol_trg = '%s/%s+orig.HEAD' % (refdir, expvol_trg_prefix) print "Attempt %s -> %s" % (expvol_src, expvol_trg) if overwrite or not utils.afni_fileexists(expvol_trg): if do_ss: cmds.append('cd "%s";3dSkullStrip -overwrite -prefix ./%s+orig -input %s' % (refdir, expvol_trg_prefix, expvol_src)) else: cmds.append('cd "%s";3dbucket -overwrite -prefix ./%s+orig %s' % (refdir, expvol_trg_prefix, expvol_src)) else: print "No skull strip because already exists: %s+orig" % expvol_trg_prefix utils.run_cmds(cmds, env)