def script_set_vars(self): """use variables for dataset directories, only """ # init with a section comment hdr = SUBJ.comment_section_string('set process variables') + '\n' cmd = '' # init to nothing self.LV.dirA = '' self.LV.dirB = '' if self.uvars.mask: cmd += 'set mask_dset = %s\n\n' \ % self.strip_suffix(self.uvars.mask, '.HEAD') # set dirA and possibly dirB dirs_set = 0 if not UTIL.is_trivial_dir(self.LV.parent_dirs[0]): cmd += 'set dirA = %s\n' % self.LV.parent_dirs[0] self.LV.dirA = '$dirA' dirs_set = 1 if len(self.uvars.dsets_B) > 0: if not UTIL.is_trivial_dir(self.LV.parent_dirs[1]): if self.LV.parent_dirs[0] != self.LV.parent_dirs[1]: cmd += 'set dirB = %s\n' % self.LV.parent_dirs[1] self.LV.dirB = '$dirB' dirs_set = 1 else: self.LV.dirB = '$dirA' if dirs_set: cmd += '\n' if cmd: return hdr + cmd else: return ''
def script_tabulate_areas(self): """for each zthr, for each clust file, extract max area""" cmd = SUBJ.comment_section_string('extract cluster counts') + '\n' cmd += '# tabulate all results for each uncorrected p-threshold\n' \ 'set maxa_list = () # track all max areas\n' \ 'set failures = () # track cluster failures\n' \ 'foreach pthr ( $pthr_list )\n' \ ' # make note of current file\n' \ ' set zfile = z.max.area.$pthr\n' \ '\n' \ ' # create empty file for this p-value\n' \ ' echo -n "" > $zfile\n' \ '\n' \ ' set file_list = ( clust.out.*.$pthr )\n' \ ' echo "-- processing $#file_list files for p = $pthr"\n' \ '\n' \ ' # process each file, counting through them\n' \ ' foreach findex ( `count -digits 1 1 $#file_list` )\n' \ ' set file = $file_list[$findex]\n' \ '\n' \ ' # print pacifier every 100 files\n' \ ' if ( ! ($findex % 100) ) echo -n .\n' \ ' if ( ! ($findex % 5000) ) echo ""\n' \ '\n' \ ' # grab the area field from first (largest) cluster\n' \ " set maxa = `awk '$1 == 1 {print $3}' $file`\n" \ '\n' \ ' # and append it to the max file (if results exist)\n' \ ' if ( $maxa != "" ) echo $maxa >> $zfile\n' \ '\n' \ ' end # file index\n' \ '\n' \ ' # grab the max area for this p-value, and add to list\n' \ ' # (if the max.area file is not empty for some reason)\n' \ ' set nlines = `cat $zfile | wc -l`\n' \ ' if ( $nlines != 0 ) then\n' \ ' set maxa = `sort -rn $zfile | head -n 1`\n' \ ' else\n' \ ' set failures = ( $failures $pthr )\n' \ ' set maxa = 0\n' \ ' endif\n' \ '\n' \ ' set maxa_list = ( $maxa_list $maxa )\n' \ '\n' \ 'end # pthr\n' \ '\n' \ 'echo ""\n' \ 'echo "p-value thresholds : $pthr_list"\n' \ 'echo "z-score thresholds : $zthr_list"\n' \ 'echo "maximum cluster areas: $maxa_list"\n' \ '\n' \ 'if ( $#failures ) then\n' \ ' echo ""\n' \ ' echo "** no clusters for p = $failures"\n' \ 'endif\n' \ '\n' return cmd
def script_set_vars(self): """use variables for inputs (anat, epi, epi_base) and for options (cost_main, cost_list, align_opts) """ # init with a section comment cmd = SUBJ.comment_section_string('set processing variables') + '\n' # maybe init with top_dir if not self.LV.is_trivial_dir('top_dir'): cmd += '# top data directory\n' \ 'set top_dir = %s\n\n' % self.LV.top_dir # anat and epi might use top_dir if self.LV.is_trivial_dir('top_dir'): astr = self.uvars.anat estr = self.uvars.epi else: astr = '$top_dir/%s' % self.LV.short_names[0][0] estr = '$top_dir/%s' % self.LV.short_names[0][1] # if we are aligning centers, set center_base variable (maybe w/top_dir) if self.uvars.align_centers == 'yes': if self.LV.is_trivial_dir('top_dir'): cstr = self.uvars.center_base else: cdir = UTIL.child_dir_name(self.LV.top_dir, self.uvars.center_base) if cdir == self.uvars.center_base: cstr = cdir else: cstr = '$top_dir/%s' % cdir ccmd = 'set center_base = %s\n' % cstr else: ccmd = '' # now set variables for inputs, possibly including center_base cmd += '# input dataset options (ebase is EPI index)\n' \ 'set in_anat = %s\n' \ 'set in_epi = %s\n' \ 'set in_ebase = %d\n' \ '%s' \ '\n' \ % (astr, estr, self.uvars.epi_base, ccmd) # note whether to use multi_cost cmd += '# main options\n' \ 'set cost_main = %s\n' % self.uvars.cost_list[0] if len(self.uvars.cost_list) > 1: cmd += 'set cost_list = ( %s )\n' % ' '.join( self.uvars.cost_list[1:]) cmd += '\n' # possibly add align_opts list variable cmd += self.make_align_opts_str() return cmd
def script_main(self): """write command with prefix, datasets and extra options """ cmd = SUBJ.comment_section_string('process the data') + '\n' if self.uvars.program == '3dttest++': cmd += self.script_ttest() elif self.uvars.program == '3dMEMA': cmd += self.script_MEMA() else: self.errors.append('** bad program name: %s' % self.uvars.program) return '' return UTIL.add_line_wrappers(cmd)
def script_copy_data(self): """these commands only vary based on results_dir""" cmd = SUBJ.comment_section_string('copy data') + '\n' if self.uvars.is_trivial_dir('results_dir'): rdir = '.' else: rdir = '$results_dir' cmd += '# copy dataset to processing directory\n' \ '3dbucket -prefix %s/anat $in_anat\n' \ '3dbucket -prefix %s/epi $in_epi"[$in_ebase]"\n' \ '\n' % (rdir, rdir) if rdir != '.': cmd += '# enter the processing directory\n' \ 'cd %s\n\n' % rdir return cmd
def script_align_datasets(self): """actually run align_epi_anat.py only current option is -mult_cost, everything else is via variables """ if len(self.uvars.cost_list) > 1: mstr = ' -multi_cost $cost_list' else: mstr = '' cmd = SUBJ.comment_section_string('align data') + '\n' cmd += \ '# test alignment, using variables set above\n' \ 'align_epi_anat.py -anat anat+orig -epi epi+orig ' \ '-epi_base $in_ebase \\\n' \ ' -cost $cost_main%s $align_opts\n' \ '\n' % mstr return cmd
def script_align_centers(self): """if align_centers should be run, deoblique both and align with center_base (probably TT_N27+tlrc) note: these commands should be fixed, since the dataset names are """ if self.uvars.align_centers != 'yes': return '' cmd = SUBJ.comment_section_string('align centers') + '\n' cmd += '# since altering grid, remove any oblique transformation\n' \ '3drefit -deoblique anat+orig epi+orig\n' \ '\n' \ '# align volume centers (we do not trust spatial locations)\n' \ '@Align_Centers -no_cp -base $center_base -dset anat+orig\n' \ '@Align_Centers -no_cp -base $center_base -dset epi+orig\n\n' \ return cmd
def script_results_dir(self): # if no results dir, just put everything here if self.uvars.is_trivial_dir('results_dir'): return '' cmd = SUBJ.comment_section_string('test and create results dir') + '\n' cmd += '# note directory for results\n' \ 'set results_dir = %s\n\n' % self.uvars.results_dir cmd += '# make sure it does not yet exist\n' \ 'if ( -e $results_dir ) then\n' \ ' echo "** results dir \'$results_dir\' already exists"\n' \ ' exit\n' \ 'endif\n\n' cmd += '# create results directory, where the work will be done\n' \ 'mkdir $results_dir\n\n' return cmd
def script_analysis_prep(self): """return a set of commands to: convert pthr_list to zthr_list create a dummy time series of lenth itersize (if > 1) divide (ceil) niter by itersize (if > 1) """ cmd = SUBJ.comment_section_string('prep: make z-scores, etc.') + '\n' cmd += '# make zthr_list (convert p-values to z-scores)\n' \ '# (2-tailed computation mirrors athresh() in SurfClust)\n'\ 'set zthr_list = ()\n' \ 'foreach pthr ( $pthr_list )\n' \ ' # convert from p to z (code for N(0,1) is 5)\n' \ ' set zthr = `ccalc "cdf2stat(1-$pthr,5,0,0,0)"`\n' \ ' set zthr_list = ( $zthr_list $zthr )\n' \ 'end\n\n' cmd += 'echo have p-values: $pthr_list\n' \ 'echo have z-scores: $zthr_list\n\n' cmd += '# make a dummy time file of length $itersize for 3dcalc\n' \ '1deval -num $itersize -expr t > dummy.TRs.$itersize.1D\n\n' cmd += '# divide niter by itersize (take ceiling)\n' \ '@ niter = ( $niter + $itersize - 1 ) / $itersize\n\n' # always create an empty surface, and then an all-1 surface cmd += g_make_empty_surf_str if self.LV.val('smask'): cmd += '# make an all-1 masked surface for 3dmaskave\n' \ '3dcalc -a $empty_surf -b $surf_mask' \ ' -expr "bool(b)" -prefix all_1.gii\n\n' else: cmd += '# make an all-1 surface for 3dmaskave\n' \ '3dcalc -a $empty_surf -expr 1 -prefix all_1.gii\n\n' return cmd
def script_set_vars(self): """use variables for inputs (anat, epi, epi_base) and for options (cost_main, cost_list, align_opts) """ U = self.uvars # for convenience # init with a section comment cmd = SUBJ.comment_section_string('set processing variables') + '\n' # maybe init with top_dir if not self.LV.is_trivial_dir('top_dir'): cmd += '# top data directory\n' \ 'set top_dir = %s\n\n' % self.LV.top_dir # surf_vol and vol_mask might use top_dir if self.LV.is_trivial_dir('top_dir'): # if self.cvars.val('on_surface') != 'yes': self.LV.svol = U.surf_vol # # rcr - fix this, surf_vol should not be needed if only on surface # (for getting the node count, avoid SurfMeasures or any other # program that uses -sv) self.LV.svol = U.surf_vol self.LV.svset = BASE.afni_name(self.LV.svol) self.LV.vmask = U.vol_mask self.LV.vmset = BASE.afni_name(U.vol_mask) self.LV.spec = U.spec_file if self.uvars.surf_mask != '': self.LV.smset = BASE.afni_name(self.uvars.surf_mask) self.LV.smask = self.LV.smset.real_input() else: self.LV.svol = '$top_dir/%s' % self.LV.short_names[0][0] self.LV.svset = BASE.afni_name(self.LV.svol) self.LV.spec = '$top_dir/%s' % self.LV.short_names[0][1] if self.cvars.val('on_surface') == 'yes': if self.uvars.surf_mask != '': self.LV.smask = '$top_dir/%s' % self.LV.short_names[0][2] self.LV.smset = BASE.afni_name(self.LV.smask) else: self.LV.vmask = '$top_dir/%s' % self.LV.short_names[0][2] self.LV.vmset = BASE.afni_name(self.LV.vmask) cmd += '# input datasets and surface specification file\n' \ '# (absolute paths are used since inputs are not copied)\n' \ 'set surf_vol = %s\n' \ 'set spec_file = %s\n' % (self.LV.svol, self.LV.spec) if self.cvars.val('on_surface') != 'yes': cmd += 'set vol_mask = %s\n' % self.LV.vmask if self.LV.val('smask'): cmd += 'set surf_mask = %s\n' % self.LV.smask # as a list, these might come is as strings or floats, be generic plist = ['%s' % p for p in U.pthr_list] cmd += '\n' \ '# iterations and blur/clust parameters\n' \ 'set niter = %d\n' \ 'set itersize = %d\n' \ 'set pthr_list = ( %s )\n\n' \ 'set blur = %g\n' \ 'set rmm = %g\n\n' \ % (U.niter, U.itersize, ' '.join(plist), U.blur, U.rmm) cmd += '# surface mapping parameters\n' \ 'set surfA = %s\n' \ 'set surfB = %s\n' \ 'set map_func = %s\n' \ 'set nsteps = %d\n\n' \ % (U.surfA, U.surfB, U.map_func, self.cvars.nsteps) if self.cvars.keepblocks > 0: cmd += '# note how many blocks to keep output datasets for\n' \ 'set keepblocks = %d\n\n' % self.cvars.keepblocks if self.cvars.time_process: cmd += "# prepare to possibly time programs (/usr/bin/time or '')\n" \ "set time_str = /usr/bin/time \n\n" self.LV.time_str = '$time_str \\\n' else: self.LV.time_str = '' return cmd