Esempio n. 1
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. 2
0
    def make_align_opts_str(self):
        """any align options, one per line"""

        # keep comment separate to get indent length
        cmnt = '# all other align_epi_anat.py options\n' \

        cstr = 'set align_opts = ( '
        clen = len(cstr)
        istr = ' ' * clen

        # put one option on first line
        cstr += '%s \\\n' % '-tshift off'

        # ---------- here is the main option application ---------

        # then add each option offset by initial indentation
        cstr += '%s%s \\\n' % (istr, '-volreg off')

        if self.uvars.giant_move == 'yes':
            cstr += '%s%s \\\n' % (istr, '-giant_move')

        if self.uvars.add_edge == 'yes':
            if len(self.uvars.cost_list) > 1:
                self.errors.append(
                    '** -AddEdge does not currently work with -multi_cost')
            else:
                cstr += '%s%s \\\n' % (istr, '-AddEdge')

        if len(self.uvars.aea_opts) > 0:
            cstr += '%s%s \\\n' % (istr, ' '.join(self.uvars.aea_opts))

        # does the anatomy have a skull?
        if self.uvars.anat_has_skull != 'yes':
            cstr += '%s%s \\\n' % (istr, '-anat_has_skull no')

        # -epi_strip method
        if self.uvars.epi_strip_meth != '3dSkullStrip':
            cstr += '%s%s \\\n' % (istr,
                                   '-epi_strip %s' % self.uvars.epi_strip_meth)

        # want -save_all, -prep_off?

        # last indent is left by 2 to align ()
        cstr += '%*s)\n\n' % (clen - 2, '')

        # finally, align the line wrappers
        cstr = UTIL.add_line_wrappers(cstr)

        return cmnt + cstr
Esempio n. 3
0
   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)
Esempio n. 4
0
   def make_uber_command(self):
      """generate a script that would invoke the uber_ttest.py interface
         with control and user vars set (and so fields filled)

         Put any key elements in quotes:
            basis functions
            gltsym
      """
      if show_func_seq: print('=== xO ===')


      # first apply subject variables
      self.update_uvars_from_gui()

      cmd = 'uber_ttest.py'

      # apply each uvar with -uvar option
      prefix = ' \\\n    -uvar '     # append before next command
      for atr in self.uvars.attributes():
         if atr == 'name': continue     # skip
         if self.uvars.vals_are_equal(atr, LTT.g_udef_strs): continue
         # show this one
         val = self.uvars.val(atr)
         
         # special cases first: stim_basis, gltsym
         #if atr == 'gltsym':            # special case
         #   val = ["'%s'" % v for v in val]
         #elif atr == 'stim_basis':      # special case
         #   val = ["'%s'" % v for v in val]
         #   cmd += (prefix + '%s %s' % (atr, ' '.join(val)))

         # some options might need quotes


         if self.uvars.has_simple_type(atr):
            val = UTIL.quotize_list([val], quote_chars='#%<>')[0]
            cmd += (prefix + '%s %s' % (atr, val))
         elif type(val) == list:
            val = UTIL.quotize_list(val, quote_chars='#%<>')
            cmd += (prefix + '%s %s' % (atr, ' '.join(val)))
         else:
            print('** make_uber_command: bad attr %s' % atr)

      return UTIL.add_line_wrappers(cmd + '\n')
Esempio n. 5
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
        # rcr - in progress?  unused: adata, nt, transp
        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 = int(12 / trup + 0.5)
            elif kernel == 'BLOCK': ntk = int(15 / trup + 0.5)
            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. 6
0
    def make_script(self):
        """create the review EPI (plugout_drive) script"""

        c2 = "#!/bin/tcsh\n\n"

        c2 += "# ------------------------------------------------------\n"  \
              "# review EPI data via 'afni' and 'plugout_drive'\n\n"        \
              "# note that when running this script, prompts to change\n"   \
              "# datasets will appear in the terminal window\n\n"

        c2 += "# ------------------------------------------------------\n"  \
              "# set the list of datasets\n"                                \
              "set dsets = ( %s )\n\n" %                                    \
                 ' '.join([dset.prefix for dset in self.adsets])

        c2 += '# ------------------------------------------------------\n'  \
              '# verify that the input data exists\n'                       \
              'if ( ! -f $dsets[1]+orig.HEAD ) then\n'                      \
              '    echo "** missing data to review (e.g. $dsets[1])"\n'     \
              '    exit\n'                                                  \
              'endif\n\n'

        c2 += '# ------------------------------------------------------\n'  \
              '# start afni is listening mode, and take a brief nap\n\n'    \
              'afni -yesplugouts &\n\n'                                     \
              'sleep 5\n\n'

        cmd = UTIL.add_line_wrappers(c2)

        c2  = '# ------------------------------------------------------\n'  \
              '# tell afni to load the first dataset and open windows\n\n'  \
              'plugout_drive \\\n'                                          \
              '    -com "SWITCH_UNDERLAY %s" \\\n'                          \
               % self.adsets[0].prefix

        # open windows in the list
        if self.verb > 1:
            print('++ opening windows: %s' % ', '.join(self.windows))

        for ind in range(len(self.windows)):
            c2 += '    -com "OPEN_WINDOW %simage  \\\n'                  \
                  '                      geom=%dx%d+%d+%d" \\\n' %          \
                  (self.windows[ind], self.im_size[0], self.im_size[1],
                   self.im_xoff+ind*self.im_size[0], self.im_yoff)

        c2 += '    -com "OPEN_WINDOW sagittalgraph  \\\n'                   \
              '                      geom=%dx%d+%d+%d" \\\n' %              \
              (self.gr_size[0], self.gr_size[1], self.gr_xoff, self.gr_yoff)

        # terminate the initial plugout_drive command
        c2 += '    -quit\n\n'

        c2 = UTIL.add_line_wrappers(c2)  # do it early, for verb

        if self.verb > 2: print('initial drive command:\n%s' % c2)

        cmd += c2

        cmd += 'sleep 2    # give afni time to open the windows\n\n\n'

        c2  = '# ------------------------------------------------------\n'  \
              '# process each dataset using video mode\n\n'                 \
              'foreach dset ( $dsets )\n'                                   \
              '    plugout_drive \\\n'                                      \
              '        -com "SWITCH_UNDERLAY $dset" \\\n'                   \
              '        -com "OPEN_WINDOW sagittalgraph  \\\n'               \
              '                          keypress=a\\\n'                    \
              '                          keypress=v"\\\n'                   \
              '        -quit\n\n'                                           \
              '    sleep 2    # wait for plugout_drive output\n\n'          \
              '    echo ""\n'                                               \
              '    echo "++ now viewing $dset, hit enter to continue"\n'    \
              '    set ret = $<    # wait for user to hit enter\n'          \
              'end\n\n\n'

        cmd += UTIL.add_line_wrappers(c2)

        c2  = '# ------------------------------------------------------\n'  \
              '# stop video mode when the user is done\n\n'                 \
              'plugout_drive -com "OPEN_WINDOW sagittalgraph keypress=s"'   \
                           ' -quit\n\n\n'                                   \
              'sleep 2    # wait for plugout_drive output\n\n'              \
              'echo ""\n'                                                   \
              'echo "data review complete"\n\n\n'

        cmd += UTIL.add_line_wrappers(c2)

        c2  = '# ----------------------------------------------------------'\
              "------\n# auto-generated by gen_epi_review.py, %s\n" % g_version
        c2 += "#\n# %s %s\n" % (os.path.basename(sys.argv[0]), ' '.join(
            UTIL.quotize_list(sys.argv[1:], '')))

        cmd += UTIL.add_line_wrappers(c2)

        fp = open(self.script, "w")
        if not fp:
            print('** failed to open %s for writing decon script' %
                  self.script)
            return 1

        fp.write(cmd)
        fp.close()
        try:
            os.chmod(self.script, 0o755)
        except OSError as e:
            print(e)

        return