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 make_ttpp_set_list(self, bsub, indent=0): """return a multi-line string of the form: SID1 "dset1[bsub]" SID2 "dset2[bsub]" ... indent is the initial indentation """ # note the max subject ID length ml = 0 for subj in self.subjects: if len(subj.sid) > ml: ml = len(subj.sid) if not UTIL.is_trivial_dir(self.common_dir) and self.common_dname: sdir = self.common_dname else: sdir = '' sstr = '' for subj in self.subjects: if sdir: # see if the dataset is in a directory underneath cdir = UTIL.child_dir_name(self.common_dir, subj.ddir) if UTIL.is_trivial_dir(cdir): cstr = '' else: cstr = '%s/' % cdir dset = '$%s/%s%s' % (sdir, cstr, subj.dfile) else: dset = subj.dset sstr += '%*s%s "%s[%s]" \\\n' % (indent, '', subj.sid, dset, bsub) return sstr
def make_generic_set_list(self, bsub, indent=0): """return a multi-line string of the form: "dset1[bsub]" "dset2[bsub]" ... indent is per-line indentation """ if not UTIL.is_trivial_dir(self.common_dir) and self.common_dname: sdir = self.common_dname else: sdir = '' sstr = '' for subj in self.subjects: if sdir: # see if the dataset is in a directory underneath cdir = UTIL.child_dir_name(self.common_dir, subj.ddir) if UTIL.is_trivial_dir(cdir): cstr = '' else: cstr = '%s/' % cdir dset = '$%s/%s%s' % (sdir, cstr, subj.dfile) else: dset = subj.dset if bsub == None: sstr += '%*s%s \\\n' % (indent, '', dset) else: # use bsub sstr += '%*s"%s[%s]" \\\n' % (indent, '', dset, bsub) return sstr
def goto_proc_dir(dname): """ go to processing directory, returning return_dir - if proc_dir does not exist, create it - cd - return ret_dir """ if UTIL.is_trivial_dir(dname): return '' # nowhere to go retdir = os.getcwd() # so need return directory # if the directory does not yet exist, create it if not os.path.isdir(dname): try: os.makedirs(dname) except: print('** failed makedirs(%s)' % dname) return '' # now try to go there try: os.chdir(dname) except: print('** failed to go to process dir, %s' % dname) return '' return retdir # only returned on success
def make_anova2_set_list(self, bsub, indent=0): """return a multi-line string of the form: -alevels #bsub -blevels #subj -dset ALEVEL BLEVEL "dset#A[bsub#B]" ... indent is the initial indentation """ sdir = self.common_dname sstr = '%*s-alevels %d \\\n' \ '%*s-blevels %d \\\n' \ % (indent,'', len(bsub), indent, '', len(self.subjects)) for isubj, subj in enumerate(self.subjects): if sdir: # see if the dataset is in a directory underneath cdir = UTIL.child_dir_name(self.common_dir, subj.ddir) if UTIL.is_trivial_dir(cdir): cstr = '' else: cstr = '%s/' % cdir dset = '$%s/%s%s' % (sdir, cstr, subj.dfile) else: dset = subj.dset for ibeta, beta in enumerate(bsub): sstr += '%*s-dset %2d %2d "%s[%s]" \\\n' \ % (indent, '', ibeta+1, isubj+1, dset, beta) return sstr
def set_common_data_dir(self, cname='data_dir'): """return the directory common to all subject ddir names""" cdir = UTIL.common_dir([s.dset for s in self.subjects]) if UTIL.is_trivial_dir(cdir) or (len(cdir) < len(cname)): self.common_dir = '' self.common_dname = '' else: self.common_dir = cdir self.common_dname = cname if self.verb > 1: print('++ setting common dir, %s = %s' % (cname, cdir))
def make_anova3_t4_set_list(self, bsub, subjlists, factors, indent=0): """return a multi-line string of the form: -alevels #alevels -blevels #blevels -clevels #subj -dset ALEVEL BLEVEL SUBJ "dset#A[bsub#B]" ... - factors should be of length 2 - indent is the initial indentation - as in type 5, have A change slower than B, but subj be slowest (so factor order per subject matches command line) """ errs = 0 if len(subjlists) != 1: print('** MAt4SL: bad subject list count = %d' % len(subjlists)) return None nA = factors[0] nB = factors[1] slist = subjlists[0] if nA * nB != len(bsub): print('** MAt4SL: bad factor count: %d, %d, %d' % (nA, nB, len(bsub))) return None sstr = '' sstr += '%*s-alevels %d \\\n' % (indent, '', nA) sstr += '%*s-blevels %d \\\n' % (indent, '', nB) sstr += '%*s-clevels %d \\\n' % (indent, '', len(slist.subjects)) sdir = slist.common_dname for isubj, subj in enumerate(slist.subjects): if sdir: # see if the dataset is in a directory underneath cdir = UTIL.child_dir_name(slist.common_dir, subj.ddir) if UTIL.is_trivial_dir(cdir): cstr = '' else: cstr = '%s/' % cdir dset = '$%s/%s%s' % (sdir, cstr, subj.dfile) else: dset = subj.dset for iA in range(nA): for iB in range(nB): sstr += '%*s-dset %2d %2d %2d "%s[%s]" \\\n' \ % (indent, '', iA+1, iB+1, isubj+1, dset, bsub[iA*nB+iB]) if errs: return None return sstr
def set_data_dirs(self, subjlist2=None): """Given 1 or 2 file lists, set directories and return initial script to apply them with variables. return status and script text if there are 0 or 1 common dirs use them if there are 2 and they are different, adjust var names """ s2 = subjlist2 # so much typing... self.set_common_data_dir() uses2dir = 0 diffdirs = 0 if s2 != None: s2.set_common_data_dir() uses2dir = not UTIL.is_trivial_dir(s2.common_dir) if not UTIL.is_trivial_dir(s2.common_dir) and \ not UTIL.is_trivial_dir(self.common_dir) and \ self.common_dir != s2.common_dir: # differentiate the directory variable names self.common_dname = 'data1' s2.common_dname = 'data2' diffdirs = 1 cmd = '' if not UTIL.is_trivial_dir(self.common_dir) or uses2dir: cmd += '# apply any data directories with variables\n' if not UTIL.is_trivial_dir(self.common_dir): cmd += 'set %s = %s\n' % (self.common_dname, self.common_dir) if diffdirs: cmd += 'set %s = %s\n' % (s2.common_dname, s2.common_dir) if cmd: cmd += '\n' return 0, cmd
def make_anova3_t5_set_list(self, bsub, subjlists, factors=0, indent=0): """return a multi-line string of the form: -alevels #subjlists -blevels #bsub -clevels #subj -dset GROUP BLEVEL SUBJ "dset#A[bsub#B]" ... factors is ignored, and exists only to match type4 function indent is the initial indentation """ sstr = '' sstr += '%*s-alevels %d \\\n' % (indent, '', len(subjlists)) sstr += '%*s-blevels %d \\\n' % (indent, '', len(bsub)) sstr += '%*s-clevels %d \\\n' % (indent, '', len( subjlists[0].subjects)) slen0 = len(subjlists[0].subjects) errs = 0 for ilist, slist in enumerate(subjlists): if len(slist.subjects) != slen0: print('** subject list %d length differs from SL 1 (%d != %d)\n'\ % (ilist+1, len(slist.subjects), slen0)) errs += 1 sdir = slist.common_dname for isubj, subj in enumerate(slist.subjects): if sdir: # see if the dataset is in a directory underneath cdir = UTIL.child_dir_name(slist.common_dir, subj.ddir) if UTIL.is_trivial_dir(cdir): cstr = '' else: cstr = '%s/' % cdir dset = '$%s/%s%s' % (sdir, cstr, subj.dfile) else: dset = subj.dset for ibeta, beta in enumerate(bsub): sstr += '%*s-dset %2d %2d %2d "%s[%s]" \\\n' \ % (indent, '', ilist+1, ibeta+1, isubj+1, dset, beta) if errs: return None return sstr
def make_ttestpp_command(self, set_labs=None, bsubs=None, subjlist2=None, prefix=None, comp_dir=None, options=None, verb=1): """create a basic 3dttest++ command Note: this is almost identical to make_mema_command, except for use of tsubs. ** labs, bsubs should be lists of strings, even if they are integral sub-bricks if set_labs=None, use defaults depending on # of subject lists else, set_labs must have 1 or 2 elements, for 1 or 2 sets of subjects bsubs can be of length 1 even with 2 set_labs, in that case: - length 1: must have subjlist2 set, and apply to it - length 2: no subjlist2, use with subjlist 1 attach options after subject lists set_labs - set labels (None, 1 or 2 labels) bsubs - beta sub-bricks (length 1 or 2, matching labels) subjlist2 - second subject list for 2-sample test (want if the datasets differ across sets) prefix - prefix for 3dtest++ output comp_dir - comparison direction, either -AminusB or -BminusA (if 2 sets) options - other options added to the 3dtest++ command verb - verbose level return None on failure, command on success """ if prefix == '' or prefix == None: prefix = 'ttest++_result' if verb > 1: print('-- make_ttest++_command: have prefix %s' % prefix) s2 = subjlist2 # sooooo much typing... if set_labs == None: if s2 == None: set_labs = ['setA'] else: set_labs = ['setA', 'setB'] if verb > 2: print('-- tt++_cmd: adding default set labels') if bsubs == None: bsubs, tsubs = ['0'], ['1'] indent = 3 # minimum indent: spaces to following -set option # want any '-AminusB' option at top of command if len(set_labs) > 1: copt = '%*s%s \\\n' % (indent, '', comp_dir) else: copt = '' # maybe we will use directory variables self.set_common_data_dir() if not UTIL.is_trivial_dir(self.common_dir): if s2 != None: s2.set_common_data_dir() if UTIL.is_trivial_dir(s2.common_dir): # then do not use either self.common_dir = '' self.common_dname = '' # else, use both (if same, default variable is okay) elif self.common_dir != s2.common_dir: # different, so update the names to be different self.common_dname = 'data1' s2.common_dname = 'data2' cmd = '' if not UTIL.is_trivial_dir(self.common_dir): cmd += '# apply any data directories with variables\n' \ 'set %s = %s\n' % (self.common_dname, self.common_dir) if s2 != None: if not UTIL.is_trivial_dir(s2.common_dir) \ and s2.common_dir != self.common_dir: cmd += 'set %s = %s\n' % (s2.common_dname, s2.common_dir) cmd += '\n' # command and first set of subject files cmd += '3dttest++ \\\n' \ '%*s-prefix %s \\\n' \ '%s' \ '%*s-setA %s \\\n%s' \ % (indent, ' ', prefix, copt, indent, ' ', set_labs[0], self.make_ttpp_set_list(bsubs[0], indent+3)) # maybe add second set of subject files if len(set_labs) > 1: if verb > 2: print('-- tt++_cmd: have labels for second subject set') # separate tests for bad test types if comp_dir == None: print('** make_tt++_cmd: missing test type, should be in %s' \ % g_ttpp_tests) return # failure if comp_dir not in g_ttpp_tests: print('** make_tt++_cmd: comp_dir (%s) must be in the list %s' \ % (comp_dir, g_ttpp_tests)) return # failure # note subject list and sub-brick labels if s2 != None: S = s2 if verb > 2: print('-- second subject list was passed') else: S = self if verb > 2: print('-- no second subject list, using same list') if len(bsubs) > 1: b = bsubs[1] else: if S != s2: print('** make_tt++_cmd: same subject list in comparison') b = bsubs[0] cmd += '%*s-setB %s \\\n%s' % \ (indent, ' ', set_labs[1], S.make_ttpp_set_list(b, indent+3)) if len(options) > 0: cmd += '%*s%s' % (indent, '', ' '.join(options)) # strip trailing backslash (must dupe memory) if cmd[-2:] == '\\\n': cmd = cmd[0:-2] cmd += '\n\n' return cmd
def make_anova3_command(self, bsubs=None, prefix=None, subjlists=None, options=None, factors=[], verb=1): """create a basic 3dANOVA3 -type 5 command ** other types may be added later... ** bsubs should be lists of strings, even if integral sub-bricks (they are applied as sub-brick selectors) attach options after subject lists bsubs - beta sub-bricks (1 list of sub-brick selectors) prefix - prefix for command output subjlists - len > 1 for type 5 options - other options added to the command atype - 3dANOVA3 -type (should be 4 or 5) factors - if type 4, #factors of each type (f0*f1 = len(b)) verb - verbose level Note: for type 5: factor A is group, B is condition, C is subject return None on failure, command on success """ if prefix == '' or prefix == None: prefix = 'anova3_result' if verb > 1: print('-- make_anova2_command: have prefix %s' % prefix) if bsubs == None: print('** missing sub-brick selection list') return None if len(bsubs) < 2: print('** anova3_command: need at least 2 sub-bricks (have %d)' \ % len(bsubs)) return None ncond = len(factors) ngroups = len(subjlists) atype = 0 if ngroups > 1: atype = 5 elif ncond == 2: atype = 4 if atype == 4: if ngroups != 1: print('** anova3_cmd: -type 4 requires only 1 dset group') return None if ncond != 2: print('** anova3_cmd: -type 4 requires 2 factor lengths') print(' (product should be length -subs_betas)') return None if factors[0] * factors[1] != len(bsubs): print('** anova3_cmd: -type 4 factor mismatch') print(' (%d x %d design requires %d betas, have %d' \ % (factors[0], factors[1], factors[0]*factors[1], len(bsubs))) return None elif atype == 5: if ngroups < 2: print('** anova3_cmd: -type 5 requires >= 2 subject lists') return None if ncond > 1: print('** anova3_cmd: -type 5 should not have sets of factors') return None else: print( '** anova3_cmd: cannot detect -type 4 or -type 5, seek -help!') return None indent = 4 # indent after main command # maybe we will use directory variables cmd = '#!/bin/tcsh\n\n' found = 0 slen0 = len(subjlists[0].subjects) subjlists[0].set_common_data_dir() cd0 = subjlists[0].common_dir for ilist, slist in enumerate(subjlists): slist.set_common_data_dir() if not UTIL.is_trivial_dir(slist.common_dir): if not found: # first time found cmd += '# apply any data directories with variables\n' found = 1 if ilist > 0 and slist.common_dir == cd0: slist.common_dname = 'data1' else: slist.common_dname = 'data%d' % (ilist + 1) cmd += 'set %s = %s\n' % (slist.common_dname, slist.common_dir) if found: cmd += '\n' if atype == 4: cmd += '# note: factor A is cond 1, B is cond 2, C is subject\n\n' sfunc = self.make_anova3_t4_set_list else: cmd += '# note: factor A is group, B is condition, C is subject\n\n' sfunc = self.make_anova3_t5_set_list # command and first set of subject files cstr = sfunc(bsubs, subjlists, factors, indent) if cstr == None: return None cmd += '3dANOVA3 -type %d \\\n' '%s' % (atype, cstr) if len(options) > 0: cmd += '%*s%s \\\n' % (indent, '', ' '.join(options)) else: # add some basic options opt = '-amean 1 amean1 -bmean 1 bmean1' cmd += '%*s-amean 1 amean1 \\\n' \ '%*s-bmean 1 bmean1 \\\n' % (indent, '', indent, '') print('++ no contrast options given, adding simple: %s' % opt) if prefix.find('/') >= 0: pp = prefix else: pp = './%s' % prefix cmd += '%*s-bucket %s\n' % (indent, '', pp) cmd += '\n' return cmd
def make_anova2_command(self, bsubs=None, prefix=None, options=None, verb=1): """create a basic 3dANOVA2 -type 3 command ** bsubs should be lists of strings, even if integral sub-bricks (they are applied as sub-brick selectors) attach options after subject lists bsubs - beta sub-bricks (1 list of sub-brick selectors) prefix - prefix for command output options - other options added to the command verb - verbose level return None on failure, command on success """ if prefix == '' or prefix == None: prefix = 'anova2_result' if verb > 1: print('-- make_anova2_command: have prefix %s' % prefix) if bsubs == None: print('** missing sub-brick selection list') return None if len(bsubs) < 2: print('** anova2_command: need at least 2 sub-bricks (have %d)' \ % len(bsubs)) return None indent = 9 # minimum indent: spaces to following -set option cmd = '#!/bin/tcsh\n\n' # maybe we will use directory variables self.set_common_data_dir() if not UTIL.is_trivial_dir(self.common_dir): self.common_dname = 'data' cmd += '# apply any data directories with variables\n' \ 'set %s = %s\n' % (self.common_dname, self.common_dir) cmd += '\n' cmd += '# note: factor A is condition, B is subject\n\n' # command and first set of subject files cmd += '3dANOVA2 -type 3 \\\n' \ '%s' % self.make_anova2_set_list(bsubs, indent) if len(options) > 0: cmd += '%*s%s \\\n' % (indent, '', ' '.join(options)) else: # add some basic option opt = '-amean 1 amean1' cmd += '%*s%s \\\n' % (indent, '', opt) print('++ no contrast options given, adding simple: %s' % opt) if prefix.find('/') >= 0: pp = prefix else: pp = './%s' % prefix cmd += '%*s-bucket %s\n' % (indent, '', pp) cmd += '\n' return cmd
def proc_dir_file_exists(dname, fname): if UTIL.is_trivial_dir(dname): pathname = fname else: pathname = '%s/%s' % (dname, fname) return os.path.isfile(pathname)
def make_mema_command(self, set_labs=None, bsubs=None, tsubs=None, subjlist2=None, prefix=None, ttype=None, options=None, verb=1): """create a basic 3dMEMA command ** labs, bsubs, tsubs should be lists of strings, even if they are integral sub-bricks if set_labs=None, use defaults depending on # of subject lists else, set_labs must have 1 or 2 elements, for 1 or 2 sets of subjects bsubs and tsubs can be of length 1 even with 2 set_labs, in that case: - length 1: must have subjlist2 set, and apply to it - length 2: no subjlist2, use with subjlist 1 attach options after subject lists set_labs - set labels (None, 1 or 2 labels) bsubs - beta sub-bricks (length 1 or 2, matching labels) tsubs - t-stat sub-bricks (as with bsubs) subjlist2 - second subject list for 2-sample test (want if the datasets differ across sets) prefix - prefix for 3dMEMA output ttype - used for a 2-sample test, to distinguish between paired and unpaired (results is using either -conditions (paired) or -group (un-)) ** ttype == paired is no longer valid options - other options added to the 3dMEMA command verb - verbose level return None on failure, command on success """ if prefix == '' or prefix == None: prefix = 'mema_result' if verb > 1: print('++ make_mema_cmd: have prefix %s' % prefix) s2 = subjlist2 # sooooo much typing... if set_labs == None: if s2 == None: set_labs = ['setA'] else: set_labs = ['setA', 'setB'] if verb > 2: print('++ mema_cmd: adding default set labels') if bsubs == None: bsubs, tsubs = ['0'], ['1'] # maybe we will use directory variables self.set_common_data_dir() if not UTIL.is_trivial_dir(self.common_dir): if s2 != None: s2.set_common_data_dir() if UTIL.is_trivial_dir(s2.common_dir): # then do not use either self.common_dir = '' self.common_dname = '' # else, use both (if same, default variable is okay) elif self.common_dir != s2.common_dir: # different, so update the names to be different self.common_dname = 'data1' s2.common_dname = 'data2' cmd = '' if not UTIL.is_trivial_dir(self.common_dir): cmd += '# apply any data directories with variables\n' \ 'set %s = %s\n' % (self.common_dname, self.common_dir) if s2 != None: if not UTIL.is_trivial_dir(s2.common_dir) \ and s2.common_dir != self.common_dir: cmd += 'set %s = %s\n' % (s2.common_dname, s2.common_dir) cmd += '\n' # command and first set of subject files cmd += '3dMEMA -prefix %s \\\n' \ ' -set %s \\\n%s' % \ (prefix,set_labs[0],self.make_mema_set_list(bsubs[0],tsubs[0],10)) # maybe add second set of subject files if len(set_labs) > 1: if verb > 2: print('-- mema_cmd: have labels for second subject set') # note subject list and sub-brick labels if s2 != None: S = s2 if verb > 2: print('-- second subject list was passed') else: S = self if verb > 2: print('-- no second subject list, using same list') if len(bsubs) > 1: b, t = bsubs[1], tsubs[1] else: if S != s2: print('** make_mema_cmd: same subject list in comparison') b, t = bsubs[0], tsubs[0] cmd += '%7s-set %s \\\n%s' % \ (' ', set_labs[1], S.make_mema_set_list(b, t, 10)) # either 2-sample or paired if ttype not in g_mema_tests: print("** invalid 3dMEMA test %s, not in %s" % (ttype, g_mema_tests)) return None if ttype == 'paired': print('** 3dMEMA -type paired: no longer valid\n' \ ' (input contrast and t-stat from original regression)') return None else: opt = '-groups' cmd += '%7s%s %s %s \\\n' % ('', opt, set_labs[0], set_labs[1]) if len(options) > 0: cmd += '%7s%s' % ('', ' '.join(options)) # strip trailing backslash (must dupe memory) if cmd[-2:] == '\\\n': cmd = cmd[0:-2] cmd += '\n\n' return cmd