Example #1
0
   def parse_infile_names(self):
      """try to get subject and possibly group names from infiles

         fill self.snames and self.gnames, if possible

         1. get SID
            - if files look like out.ss_review.SID.txt, that is a good start
            - else, look for varying part of filename
         2. get GID
            - replace SID in infile names and for varying group name
      """

      rv, slist = UTIL.list_minus_pref_suf(self.infiles,'out.ss_review.','.txt')
      if rv < 0: return
      if rv > 0:
         if self.verb > 1: print '++ trying to get SID from glob form'
         slist = UTIL.list_minus_glob_form(self.infiles, strip='dir')
      else:
         if self.verb > 1: print "++ have SIDs from 'out.ss_reiview' form"

      if len(slist) == 0:
         if self.verb > 1: print "-- empty SID list"
         return

      # make sure names are unique and not empty
      if not UTIL.vals_are_unique(slist):
         if self.verb > 1: print '-- SIDs not detected: not unique'
         return
      minlen = min([len(ss) for ss in slist])
      if minlen < 1:
         if self.verb > 1: print '-- SIDs not detected: some would be empty'
         return

      # we have a subject list
      self.snames = slist

      # now go for GID, start by replacing SIDs in infiles
      newfiles = [fname.replace(slist[ind], 'SUBJ') for ind, fname in
                        enumerate(self.infiles)]

      if UTIL.vals_are_constant(newfiles):
         print '-- no groups detected from filenames'
         return

      # okay, try to make a group list
      glist = UTIL.list_minus_glob_form(newfiles)

      # cannot have dirs in result
      for gid in glist:
         if gid.find('/') >= 0:
            if self.verb>1: print '-- no GIDs, dirs vary in multiple places'
            return

      minlen = min([len(ss) for ss in glist])
      if minlen < 1:
         if self.verb > 1: print '-- GIDs not detected: some would be empty'
         return

      if self.verb > 1: print "++ have GIDs from infiles"
      self.gnames = glist
Example #2
0
   def parse_infile_names(self):
      """try to get subject and possibly group names from infiles

         fill self.snames and self.gnames, if possible

         1. get SID
            - if files look like out.ss_review.SID.txt, that is a good start
            - else, look for varying part of filename
         2. get GID
            - replace SID in infile names and for varying group name
      """

      rv, slist = UTIL.list_minus_pref_suf(self.infiles,'out.ss_review.','.txt')
      if rv < 0: return
      if rv > 0:
         if self.verb > 1: print '++ trying to get SID from glob form'
         slist = UTIL.list_minus_glob_form(self.infiles, strip='dir')
      else:
         if self.verb > 1: print "++ have SIDs from 'out.ss_reiview' form"

      if len(slist) == 0:
         if self.verb > 1: print "-- empty SID list"
         return

      # make sure names are unique and not empty
      if not UTIL.vals_are_unique(slist):
         if self.verb > 1: print '-- SIDs not detected: not unique'
         return
      minlen = min([len(ss) for ss in slist])
      if minlen < 1:
         if self.verb > 1: print '-- SIDs not detected: some would be empty'
         return

      # we have a subject list
      self.snames = slist

      # now go for GID, start by replacing SIDs in infiles
      newfiles = [fname.replace(slist[ind], 'SUBJ') for ind, fname in
                        enumerate(self.infiles)]

      if UTIL.vals_are_constant(newfiles):
         print '-- no groups detected from filenames'
         return

      # okay, try to make a group list
      glist = UTIL.list_minus_glob_form(newfiles)

      # cannot have dirs in result
      for gid in glist:
         if gid.find('/') >= 0:
            if self.verb>1: print '-- no GIDs, dirs vary in multiple places'
            return

      minlen = min([len(ss) for ss in glist])
      if minlen < 1:
         if self.verb > 1: print '-- GIDs not detected: some would be empty'
         return

      if self.verb > 1: print "++ have GIDs from infiles"
      self.gnames = glist
Example #3
0
    def set_ids_from_dsets(self, prefix='', suffix='', hpad=0, tpad=0, dpre=0):
        """use the varying part of the dataset names for subject IDs

         If hpad > 0 or tpad > 0, expand into the head or tail of the dsets.
         If prefix or suffix is passed, apply them.

         return 0 on success, 1 on error
      """

        if hpad < 0 or tpad < 0:
            print('** set_ids_from_dsets: will not apply negative padding')
            return 1

        # try filenames without paths, first
        dlist = [s.dset.split('/')[-1] for s in self.subjects]
        if UTIL.vals_are_constant(dlist):
            print('** constant dataset names (%s)' % dlist[0])
            print('   trying directories...')
            dlist = [s.dset for s in self.subjects]

        slist = UTIL.list_minus_glob_form(dlist,
                                          hpad,
                                          tpad,
                                          keep_dent_pre=dpre)

        # in the case of diretories, check for success
        # (maybe we can try to skip past them, that might be okay)
        for index in range(len(slist)):
            if '/' in slist[index]:
                posn = slist[index].rfind('/')
                slist[index] = slist[index][posn + 1:]
                if len(slist[index]) < 1:
                    print(
                        '** failed to extract subject IDs from directory list')
                    print('   (directories do not vary at single level)')
                    return 1

        if len(slist) != len(self.subjects):
            print('** failed to set SIDs from dset names\n'        \
                  '   dsets = %s\n'                                \
                  '   slist = %s' % (dlist, slist))
            return 1

        if not UTIL.vals_are_unique(slist):
            print('** cannot set IDs from dsets, labels not unique: %s' %
                  slist)
            print('-- labels come from dsets: %s' % dlist)
            return 1

        for ind, subj in enumerate(self.subjects):
            subj.sid = '%s%s%s' % (prefix, slist[ind], suffix)

        return 0
Example #4
0
   def set_ids_from_dsets(self, prefix='', suffix='', hpad=0, tpad=0, dpre=0):
      """use the varying part of the dataset names for subject IDs

         If hpad > 0 or tpad > 0, expand into the head or tail of the dsets.
         If prefix or suffix is passed, apply them.

         return 0 on success, 1 on error
      """

      if hpad < 0 or tpad < 0:
         print '** set_ids_from_dsets: will not apply negative padding'
         return 1

      # try filenames without paths, first
      dlist = [s.dset.split('/')[-1] for s in self.subjects]
      if UTIL.vals_are_constant(dlist):
         print '** constant dataset names (%s)' % dlist[0]
         print '   trying directories...'
         dlist = [s.dset for s in self.subjects]

      slist = UTIL.list_minus_glob_form(dlist, hpad, tpad, keep_dent_pre=dpre)

      # in the case of diretories, check for success
      # (maybe we can try to skip past them, that might be okay)
      for index in range(len(slist)):
         if '/' in slist[index]:
            posn = slist[index].rfind('/')
            slist[index] = slist[index][posn+1:]
            if len(slist[index]) < 1:
               print '** failed to extract subject IDs from directory list'
               print '   (directories do not vary at single level)'
               return 1

      if len(slist) != len(self.subjects):
         print '** failed to set SIDs from dset names\n'        \
               '   dsets = %s\n'                                \
               '   slist = %s' % (dlist, slist)
         return 1

      if not UTIL.vals_are_unique(slist):
         print '** cannot set IDs from dsets, labels not unique: %s' % slist
         print '-- labels come from dsets: %s' % dlist
         return 1

      for ind, subj in enumerate(self.subjects):
         subj.sid = '%s%s%s' % (prefix, slist[ind], suffix)

      return 0
Example #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
        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)
Example #6
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)