Esempio n. 1
0
   def write_script(self, fname=''):
      """write processing script to a file (in the proc_dir)
         - if fname is set, use it, else generate
         - set rvars.file_proc and output_proc
      """

      if not self.script:
         print '** no script to write out'
         return 1
      if fname: name = fname
      else: name = 'script.ttest'

      # store (intended) names for calling tool to execute with
      self.rvars.file_proc = name # store which file we have written to
      self.rvars.output_proc = 'output.%s' % name # file for command output

      if self.cvars.verb > 0: print '++ writing script to %s' % name

      # if requested, make an original copy
      self.LV.retdir = SUBJ.goto_proc_dir(self.cvars.proc_dir)

      if self.cvars.copy_scripts == 'yes': # make an orig copy
         UTIL.write_text_to_file('.orig.%s'%name, self.script, exe=1)
      rv = UTIL.write_text_to_file(name, self.script, exe=1)

      self.LV.retdir = SUBJ.ret_from_proc_dir(self.LV.retdir)
         
      return rv
   def write_uber_command(self):
      if show_func_seq: print '=== R ==='  # rcr - not called

      pdir = self.ttobj.cvars.val('proc_dir')
      if os.path.isdir(pdir):
         sstr = self.make_uber_command()
         UTIL.write_text_to_file('%s/.orig.cmd.utt' % pdir, sstr)
Esempio n. 3
0
    def write_script(self, fname=''):
        """write processing script to a file (in the proc_dir)
         - if fname is set, use it, else generate
         - set rvars.file_proc and output_proc
      """

        if not self.align_script:
            print '** no alignment script to write out'
            return 1
        if fname: name = fname
        else:
            # if self.svars.sid: name = 'script.align.%s' % self.svars.sid
            name = 'script.align'

        # store (intended) names for calling tool to execute with
        self.rvars.file_proc = name  # store which file we have written to
        self.rvars.output_proc = 'output.%s' % name  # file for command output

        if self.uvars.verb > 0: print '++ writing script to %s' % name

        # if requested, make an original copy
        self.LV.retdir = SUBJ.goto_proc_dir(self.cvars.proc_dir)

        if self.uvars.copy_scripts == 'yes':  # make an orig copy
            UTIL.write_text_to_file('.orig.%s' % name,
                                    self.align_script,
                                    exe=1)
        rv = UTIL.write_text_to_file(name, self.align_script, exe=1)

        self.LV.retdir = SUBJ.ret_from_proc_dir(self.LV.retdir)

        return rv
Esempio n. 4
0
    def make_decon_script_old(self):
        """OLD METHOD: create the deconvolution (3dTfitter) script"""

        cmd  = '# ------------------------------------------------------\n'  \
               '# perform neuro deconvolution via 3dTfitter\n\n'

        cmd += "# create response kernel\n"                             \
               "waver -%s -peak 1 -dt %s -inline 1@1 > %s\n\n" %        \
                (self.kernel, self.tr, self.kfile)

        if self.aname.type == '1D': prefix = 'detrend.1D'
        else:                       prefix = 'detrend%s' % self.aname.view

        polort = UTIL.get_default_polort(self.tr, self.reps)
        cmd += '# detrend the input\n'                          \
               '3dDetrend -polort %d -prefix %s %s\n\n' %       \
                (polort,prefix,self.infiles[0])

        if self.maskset: mask = '          -mask %s \\\n' % self.maskset
        else:            mask = ''

        if self.aname.type == '1D':
            cmd += '# transpose the detrended dataset\n'        \
                   '1dtranspose detrend.1D > detrend_tr.1D\n\n'
            dname = 'detrend_tr.1D'
        else:
            dname = 'detrend%s' % self.aname.view

        c2  = '3dTfitter -lsqfit -RHS %s \\\n'           \
              '%s'                                       \
              '          -FALTUNG %s %s \\\n'            \
              '          01 0.0\n\n' % (dname,mask,self.kfile,self.prefix)

        cmd += UTIL.add_line_wrappers(c2)
        return UTIL.write_text_to_file(self.script, cmd, exe=1)
Esempio n. 5
0
    def make_decon_script_old(self):
        """OLD METHOD: create the deconvolution (3dTfitter) script"""

        cmd  = '# ------------------------------------------------------\n'  \
               '# perform neuro deconvolution via 3dTfitter\n\n'

        cmd += "# create response kernel\n"                             \
               "waver -%s -peak 1 -dt %s -inline 1@1 > %s\n\n" %        \
                (self.kernel, self.tr, self.kfile)

        if self.aname.type == '1D': prefix = 'detrend.1D'
        else: prefix = 'detrend%s' % self.aname.view

        polort = UTIL.get_default_polort(self.tr, self.reps)
        cmd += '# detrend the input\n'                          \
               '3dDetrend -polort %d -prefix %s %s\n\n' %       \
                (polort,prefix,self.infiles[0])

        if self.maskset: mask = '          -mask %s \\\n' % self.maskset
        else: mask = ''

        if self.aname.type == '1D':
            cmd += '# transpose the detrended dataset\n'        \
                   '1dtranspose detrend.1D > detrend_tr.1D\n\n'
            dname = 'detrend_tr.1D'
        else:
            dname = 'detrend%s' % self.aname.view

        c2  = '3dTfitter -lsqfit -RHS %s \\\n'           \
              '%s'                                       \
              '          -FALTUNG %s %s \\\n'            \
              '          01 0.0\n\n' % (dname,mask,self.kfile,self.prefix)

        cmd += UTIL.add_line_wrappers(c2)
        return UTIL.write_text_to_file(self.script, cmd, exe=1)
Esempio n. 6
0
   def cb_save_text(self, event):
      win = event.GetEventObject()
      win = win.GetTopLevelParent()
      if self.XM.verb > 3:
         print "-- cb: save text from window '%s' ..." % win.GetTitle()
      try:
         ctrl = win.ctrl
         text = ctrl.GetValue()
      except:
         print '** no ctrl/text for save_text window'
         return

      fname = wx.SaveFileSelector('text', '')
      if fname == '': return

      if self.XM.verb > 1:
         print "-- saving text from window '%s' to file '%s'" %   \
               (win.GetTitle(), fname)
      UTIL.write_text_to_file(fname, text)
Esempio n. 7
0
    def execute(self):

        if not self.ready_for_action():
            return 1

        if self.verb > 1:
            print "-- make %s command with %d set(s) of dsets of length(s): %s" % (
                self.command,
                len(self.dsets),
                ", ".join([str(len(dlist)) for dlist in self.dsets]),
            )

        # might deal with subject IDs and attributes later
        for ind, dlist in enumerate(self.dsets):
            slist = SUBJ.SubjectList(dset_l=dlist, verb=self.verb)
            if slist.status:
                return 1
            if slist.set_ids_from_dsets(prefix=self.subj_prefix, suffix=self.subj_suffix, dpre=self.dent_pre):
                print "** cannot set subject IDs from datasets"
                return 1
            self.slist.append(slist)
            if self.verb > 2:
                slist.show("slist %d" % ind)

        cmd = None
        if self.command == "3dMEMA":
            cmd = self.get_mema_command()
        elif self.command == "3dttest++":
            cmd = self.get_ttpp_command()
        elif self.command == "3dANOVA2":
            cmd = self.get_anova2_command()
        elif self.command == "3dANOVA3":
            cmd = self.get_anova3_command()
        elif self.command:
            cmd = self.get_generic_command()
        else:
            print "** command not implemented: %s" % self.command

        # bail on failure, else wrap command
        if cmd == None:
            print "** failed making %s command" % self.command
            return 1
        cmd = UTIL.add_line_wrappers(cmd)

        # either write to file or print
        if self.write_script:
            if UTIL.write_text_to_file(self.write_script, cmd):
                print "** failed to write command to file '%s'" % self.write_script
                return 1
            if self.verb > 0:
                print "++ command written to file %s" % self.write_script
        else:
            print cmd
Esempio n. 8
0
    def execute(self):

        if not self.ready_for_action(): return 1

        if self.verb > 1:
            print('-- make %s command with %d set(s) of dsets of length(s): %s' \
                  % (self.command, len(self.dsets),
                     ', '.join([str(len(dlist)) for dlist in self.dsets]) ))

        # might deal with subject IDs and attributes later
        for ind, dlist in enumerate(self.dsets):
            slist = SUBJ.SubjectList(dset_l=dlist, verb=self.verb)
            if slist.status: return 1
            if slist.set_ids_from_dsets(prefix=self.subj_prefix,
                                        suffix=self.subj_suffix,
                                        dpre=self.dent_pre):
                print('** cannot set subject IDs from datasets')
                return 1
            self.slist.append(slist)
            if self.verb > 2: slist.show("slist %d" % ind)

        cmd = None
        if self.command == '3dMEMA':
            cmd = self.get_mema_command()
        elif self.command == '3dttest++':
            cmd = self.get_ttpp_command()
        elif self.command == '3dANOVA2':
            cmd = self.get_anova2_command()
        elif self.command == '3dANOVA3':
            cmd = self.get_anova3_command()
        elif self.command:
            cmd = self.get_generic_command()
        else:
            print('** command not implemented: %s' % self.command)

        # bail on failure, else wrap command
        if cmd == None:
            print('** failed making %s command' % self.command)
            return 1
        cmd = UTIL.add_line_wrappers(cmd)

        # either write to file or print
        if self.write_script:
            if UTIL.write_text_to_file(self.write_script, cmd):
                print("** failed to write command to file '%s'" %
                      self.write_script)
                return 1
            if self.verb > 0:
                print('++ command written to file %s' % self.write_script)
        else:
            print(cmd)
Esempio n. 9
0
3dBrickStat -mask $mask -percentile $bot $step 100 -non-zero \\
            $pre.abs+tlrc > $pre.abs.1D

# have percentile p-values, remove unwanted percentile rankings
1dcat $pre.abs.1D'[1..$(2)]' > $pre.at.1D

# transpose and convert t to p
1deval -a $pre.at.1D\\' -expr "fitt_t2p(a,$dof)" > $pre.p.1D

# convert p to q    (** note: the minimum is not applied, we do not want it)
1deval -a $pre.p.1D -expr "a*$nv/($nv-t)" > $pre.q.1D

# put both together in output file
1dcat $pre.p.1D $pre.q.1D > $output


# ----------------------------------------
# nuke temporary files (comment out to keep)
rm -f $pre.*

"""

ss += static_script

# and write output
U.write_text_to_file(outfile, ss)
try:
    os.chmod(outfile, 0o755)
except OSError as e:
    print(e)
Esempio n. 10
0
# do the work: abs(t) => (positive) percentile t-stats => p-values

3dcalc -a $dset"[$sub]" -expr 'abs(a)' -overwrite -prefix $pre.abs
3dBrickStat -mask $mask -percentile $bot $step 100 -non-zero \\
            $pre.abs+tlrc > $pre.abs.1D

# have percentile p-values, remove unwanted percentile rankings
1dcat $pre.abs.1D'[1..$(2)]' > $pre.at.1D

# transpose and convert t to p
1deval -a $pre.at.1D\\' -expr "fitt_t2p(a,$dof)" > $pre.p.1D

# convert p to q    (** note: the minimum is not applied, we do not want it)
1deval -a $pre.p.1D -expr "a*$nv/($nv-t)" > $pre.q.1D

# put both together in output file
1dcat $pre.p.1D $pre.q.1D > $output


# ----------------------------------------
# nuke temporary files (comment out to keep)
rm -f $pre.*

"""

ss += static_script

# and write output
U.write_text_to_file(outfile, ss)
os.chmod(outfile, 0755)
 def write_uber_command(self):
    pdir = self.atest.cvars.val('proc_dir')
    if os.path.isdir(pdir):
       sstr = self.make_uber_command()
       UTIL.write_text_to_file('%s/.orig.cmd.uat' % pdir, sstr)
Esempio n. 12
0
    def make_decon_script(self):
        """create the deconvolution (3dTfitter) script"""

        nups    = '%02d' % self.tr_nup
        trup    = self.tr / self.tr_nup
        penalty = '012'
        kernel  = self.kernel
        kfile   = self.kfile

        # todo: include run lengths

        # maybe call the 3D version
        if self.aname.type != '1D': return self.make_decon_script_3d()

        cmd  = '# ------------------------------------------------------\n'  \
               '# perform neuro deconvolution via 3dTfitter\n\n'

        cmd += '# make and copy files into output directory\n'          \
               'set outdir = %s\n'                                      \
               'if ( ! -d $outdir ) mkdir $outdir\n\n' % self.outdir

        # get a list of (hopefully shortened) file labels
        llist = UTIL.list_minus_glob_form(self.infiles)
        llen = len(self.infiles)
        fix = 0
        if len(llist) != llen: fix = 1
        else:
           if llist[0] == self.infiles[0]: fix = 1
        if fix: llist = ['%02d.ts'%(ind+1) for ind in range(llen)]
        else  : llist = ['%02d.%s'%(ind+1,llist[ind]) for ind in range(llen)]

        # when copying file in, use 1dtranspose if they are horizontal
        adata = LD.AfniData(self.infiles[0], verb=self.verb)
        if not adata: return 1
        if adata.nrows == 1:
           nt = adata.ncols/self.tr_nup
           trstr = '(no transpose on read)'
           trchr = ''
        else:
           nt = adata.nrows/self.tr_nup
           transp = 1
           trstr = '(transpose on read)'
           trchr = '\\\''

        cmd += 'set files = ( %s )\n'           \
               'set labels = ( %s )\n\n'        \
               'cp -pv $files $outdir\n'        \
               'cd $outdir\n\n'                 \
               % (' '.join(self.infiles), ' '.join(llist))

        if self.kfile_in: kfile = self.kfile_in
        else:
           # generate a response kernel using 3dDeconvolve
           if   kernel == 'GAM':   ntk = 12/trup
           elif kernel == 'BLOCK': ntk = 15/trup
           else:
              print '** only GAM/BLOCK basis functions are allowed now'
              return 1

           if kernel == 'BLOCK': kernel = 'BLOCK(0.1,1)'
           tmpc = '# create response kernel\n'                      \
                  '3dDeconvolve -nodata %d %g -polort -1 \\\n'      \
                  '   -num_stimts 1 -stim_times 1 "1D:0" "%s" \\\n' \
                  '   -x1D %s -x1D_stop\n\n' % (ntk, trup, kernel, kfile)
           cmd += UTIL.add_line_wrappers(tmpc)

        pind = 0
        cmd += '# process each input file\n'                      \
               'foreach findex ( `count -digits 2 1 $#files` )\n' \
               '   # no zero-padding in shell index\n'            \
               '   set ival   = `ccalc -i $findex`\n'             \
               '   set infile = $files[$ival]:t\n'                \
               '   set label  = $labels[$ival]\n\n'               \

        polort = UTIL.get_default_polort(self.tr, self.reps)
        olab = 'p%02d.det.$label.1D' % pind
        cmd += '   # detrend the input %s\n'                      \
               '   3dDetrend -polort %d -prefix %s $infile%s\n\n' \
               % (trstr, polort, olab, trchr)

        pind += 1
        ilab = olab
        olab = 'p%02d.det.tr.$label.1D' % pind
        cmd += '   # transpose the detrended dataset\n'        \
               '   1dtranspose %s > %s\n\n' % (ilab, olab)

        pind += 1
        ilab = olab
        olab = 'p%02d.up%s.$label.1D' % (pind, nups)
        cmd += '   # upsample by factor of %d\n'                \
               '   1dUpsample %d %s > %s\n\n'                   \
               % (self.tr_nup, self.tr_nup, ilab, olab)

        sfile_up = olab # save original detrended upsample label

        pind += 1
        ilab = olab
        olab = 'p%02d.neuro.up%s.$label.1D' % (pind, nups)
        tmpc = '   3dTfitter -RHS %s \\\n'              \
              '             -FALTUNG %s %s \\\n'        \
              '             012 -2 -l2lasso -6\n\n' % (ilab,kfile,olab)
        cmd += UTIL.add_line_wrappers(tmpc)

        pind += 1
        ilab = olab
        olab = 'p%02d.neuro.up%s.tr.$label.1D' % (pind, nups)
        cmd += '   # transpose the neuro signal\n'      \
               '   1dtranspose %s > %s\n\n' % (ilab, olab)

        pind += 1
        ilab = olab
        olab = 'p%02d.reconv.up%s.tr.$label.1D' % (pind, nups)
        cmd += '   # reconvolve the neuro signal to compare with orig\n'\
               '   set nt = `cat %s | wc -l`\n'                         \
               '   waver -FILE %g %s -input %s \\\n'                \
               '         -numout $nt > %s\n\n'                          \
               % (ilab, trup, kfile, ilab, olab)

        rfile_up = olab # save reconvolved label

        cmd += 'end\n\n\n'

        cmd += 'echo "compare upsample input to reconvolved result via:"\n' \
               'echo "set label = %s"\n'                                    \
               'echo "1dplot -one %s %s"\n\n'                                \
               % (llist[0], sfile_up, rfile_up)

        return UTIL.write_text_to_file(self.script, cmd, exe=1)
Esempio n. 13
0
    def make_decon_script(self):
        """create the deconvolution (3dTfitter) script"""

        nups = '%02d' % self.tr_nup
        trup = self.tr / self.tr_nup
        penalty = '012'
        kernel = self.kernel
        kfile = self.kfile

        # todo: include run lengths

        # maybe call the 3D version
        if self.aname.type != '1D': return self.make_decon_script_3d()

        cmd  = '# ------------------------------------------------------\n'  \
               '# perform neuro deconvolution via 3dTfitter\n\n'

        cmd += '# make and copy files into output directory\n'          \
               'set outdir = %s\n'                                      \
               'if ( ! -d $outdir ) mkdir $outdir\n\n' % self.outdir

        # get a list of (hopefully shortened) file labels
        llist = UTIL.list_minus_glob_form(self.infiles)
        llen = len(self.infiles)
        fix = 0
        if len(llist) != llen: fix = 1
        else:
            if llist[0] == self.infiles[0]: fix = 1
        if fix: llist = ['%02d.ts' % (ind + 1) for ind in range(llen)]
        else:
            llist = ['%02d.%s' % (ind + 1, llist[ind]) for ind in range(llen)]

        # when copying file in, use 1dtranspose if they are horizontal
        adata = LD.AfniData(self.infiles[0], verb=self.verb)
        if not adata: return 1
        if adata.nrows == 1:
            nt = adata.ncols / self.tr_nup
            trstr = '(no transpose on read)'
            trchr = ''
        else:
            nt = adata.nrows / self.tr_nup
            transp = 1
            trstr = '(transpose on read)'
            trchr = '\\\''

        cmd += 'set files = ( %s )\n'           \
               'set labels = ( %s )\n\n'        \
               'cp -pv $files $outdir\n'        \
               'cd $outdir\n\n'                 \
               % (' '.join(self.infiles), ' '.join(llist))

        if self.kfile_in: kfile = self.kfile_in
        else:
            # generate a response kernel using 3dDeconvolve
            if kernel == 'GAM': ntk = 12 / trup
            elif kernel == 'BLOCK': ntk = 15 / trup
            else:
                print '** only GAM/BLOCK basis functions are allowed now'
                return 1

            if kernel == 'BLOCK': kernel = 'BLOCK(0.1,1)'
            tmpc = '# create response kernel\n'                      \
                   '3dDeconvolve -nodata %d %g -polort -1 \\\n'      \
                   '   -num_stimts 1 -stim_times 1 "1D:0" "%s" \\\n' \
                   '   -x1D %s -x1D_stop\n\n' % (ntk, trup, kernel, kfile)
            cmd += UTIL.add_line_wrappers(tmpc)

        pind = 0
        cmd += '# process each input file\n'                      \
               'foreach findex ( `count -digits 2 1 $#files` )\n' \
               '   # no zero-padding in shell index\n'            \
               '   set ival   = `ccalc -i $findex`\n'             \
               '   set infile = $files[$ival]:t\n'                \
               '   set label  = $labels[$ival]\n\n'               \

        polort = UTIL.get_default_polort(self.tr, self.reps)
        olab = 'p%02d.det.$label.1D' % pind
        cmd += '   # detrend the input %s\n'                      \
               '   3dDetrend -polort %d -prefix %s $infile%s\n\n' \
               % (trstr, polort, olab, trchr)

        pind += 1
        ilab = olab
        olab = 'p%02d.det.tr.$label.1D' % pind
        cmd += '   # transpose the detrended dataset\n'        \
               '   1dtranspose %s > %s\n\n' % (ilab, olab)

        pind += 1
        ilab = olab
        olab = 'p%02d.up%s.$label.1D' % (pind, nups)
        cmd += '   # upsample by factor of %d\n'                \
               '   1dUpsample %d %s > %s\n\n'                   \
               % (self.tr_nup, self.tr_nup, ilab, olab)

        sfile_up = olab  # save original detrended upsample label

        pind += 1
        ilab = olab
        olab = 'p%02d.neuro.up%s.$label.1D' % (pind, nups)
        tmpc = '   3dTfitter -RHS %s \\\n'              \
              '             -FALTUNG %s %s \\\n'        \
              '             012 -2 -l2lasso -6\n\n' % (ilab,kfile,olab)
        cmd += UTIL.add_line_wrappers(tmpc)

        pind += 1
        ilab = olab
        olab = 'p%02d.neuro.up%s.tr.$label.1D' % (pind, nups)
        cmd += '   # transpose the neuro signal\n'      \
               '   1dtranspose %s > %s\n\n' % (ilab, olab)

        pind += 1
        ilab = olab
        olab = 'p%02d.reconv.up%s.tr.$label.1D' % (pind, nups)
        cmd += '   # reconvolve the neuro signal to compare with orig\n'\
               '   set nt = `cat %s | wc -l`\n'                         \
               '   waver -FILE %g %s -input %s \\\n'                \
               '         -numout $nt > %s\n\n'                          \
               % (ilab, trup, kfile, ilab, olab)

        rfile_up = olab  # save reconvolved label

        cmd += 'end\n\n\n'

        cmd += 'echo "compare upsample input to reconvolved result via:"\n' \
               'echo "set label = %s"\n'                                    \
               'echo "1dplot -one %s %s"\n\n'                                \
               % (llist[0], sfile_up, rfile_up)

        return UTIL.write_text_to_file(self.script, cmd, exe=1)
Esempio n. 14
0
 def write_uber_command(self):
     pdir = self.atest.cvars.val('proc_dir')
     if os.path.isdir(pdir):
         sstr = self.make_uber_command()
         UTIL.write_text_to_file('%s/.orig.cmd.uat' % pdir, sstr)