Esempio n. 1
0
def proc_mats(uopts):
    offset = 0.0        # offset for output times

    if uopts == None: return

    opt = uopts.find_opt('-verb')
    if opt:
        try: verb = int(opt.parlist[0])
        except:
            print "** error: verb must be int, have '%s'" % opt.parlist[0]
            return
    else: verb = 0

    opt    = uopts.find_opt('-files')
    files  = opt.parlist
    opt    = uopts.find_opt('-nruns')
    nruns  = int(opt.parlist[0])

    opt    = uopts.find_opt('-offset')
    if opt:
        try: offset = float(opt.parlist[0])
        except:
            print "** error: offset must be float, have '%s'" % opt.parlist[0]
            return

    opt    = uopts.find_opt('-prefix')
    prefix = opt.parlist[0]

    opt    = uopts.find_opt('-tr')
    try: tr = float(opt.parlist[0])
    except:
        print "** error: TR must be float, have '%s'" % opt.parlist[0]
        return
    
    opt    = uopts.find_opt('-nt')
    try: nt = int(opt.parlist[0])
    except:
        print "** error: -nt must be int, have '%s'" % opt.parlist[0]
        return

    opt = uopts.find_opt('-labels')
    if opt: labels = opt.parlist
    else:   labels = []
    nlab = len(labels)

    # print some info
    if verb: print "-- nt = %d, nruns = %d, TR = %s, %d labels" % \
                   (nt, nruns, str(tr), nlab)

    # new option, -amplitudes (columns of amplitudes to Marry, not just 1s)
    use_amp = 0
    opt = uopts.find_opt('-amplitudes')
    if opt:
        use_amp = 1
        if verb: print '-- using amplitudes to Marry with times...'
    
    newfile_index = 1   # index over output files
    for file in files:
        tmat = TD.read_1D_file(file, verb=verb)
        if not tmat:
            print "read_1D_file failed for file: %s" % file
            return
        mat = afni_util.transpose(tmat)
        del(tmat)

        if len(mat[0]) < nt * nruns:
            print '** error: file %s has only %d entries (%d required)' % \
                  (file, len(mat[0]), nt*nruns)
            return
        elif len(mat[0]) > nt * nruns:
            print '** warning: file %s has %d entries (expected only %d)' % \
                  (file, len(mat[0]), nt*nruns)

        for row in mat:

            # maybe add a label to the file name
            if newfile_index <= nlab: label = ".%s" % labels[newfile_index-1]
            elif nlab == 0:           label = ""
            else:
                print "** %d labels given, but we are on column %d..." % \
                      (nlab, newfile_index)
                label = ".label%d" % (newfile_index)

            newp = "%s.%02d%s" % (prefix,newfile_index,label)
            newfile = afni_util.change_path_basename(file, newp, ".1D")
            if newfile == None: return
            fp = open(newfile, 'w')

            for run in range(nruns):
                rindex = run * nt   # point to start of run

                # if empty run, use placeholders
                if not stim_in_run(row[rindex:rindex+nt], use_amp):
                    if run == 0: fp.write('* *\n')  # first run gets 2
                    else:        fp.write('*\n')
                    continue
                # print '=== have stim in run, row = %s' % row[rindex:rindex+nt]
                time = 0        # in this run
                nstim = 0       # be sure we have more than 1 somewhere
                for lcol in range(nt):
                    if row[rindex+lcol]:
                        nstim += 1
                        # if -amplitudes write as time:amplitude
                        if use_amp:
                            fp.write('%s*%s ' % \
                                     (str(time+offset),str(row[rindex+lcol])))
                        else:
                            fp.write('%s ' % str(time+offset))
                    time += tr
                if run == 0 and nstim == 1:
                    fp.write('*')   # if first time has 1 stim, add '*'
                fp.write('\n')

            fp.close()
            newfile_index += 1
Esempio n. 2
0
def plot_pdf_from_file(fname, nbin, scale=1, L=0):

    import lib_textdata as TD
    data = TD.read_1D_file(fname)[0]
    data.sort()
    show_val_bins(data, nbin, scale=scale, L=L)
Esempio n. 3
0
def proc_mats(uopts):
    offset = 0.0  # offset for output times

    if uopts == None: return

    opt = uopts.find_opt('-verb')
    if opt:
        try:
            verb = int(opt.parlist[0])
        except:
            print "** error: verb must be int, have '%s'" % opt.parlist[0]
            return
    else:
        verb = 0

    opt = uopts.find_opt('-files')
    files = opt.parlist

    # set run_trs nruns from either -run_trs or -nruns and -nt
    opt = uopts.find_opt('-run_trs')
    if opt:
        val, err = uopts.get_type_list(int, '', opt=opt)
        if err: return 1
        run_trs = val
        nruns = len(run_trs)
        if uopts.find_opt('-nruns') or uopts.find_opt('-nt'):
            print '** please use either -run_trs or -nt/-nruns'
            return 1
    else:
        if not uopts.find_opt('-nruns') and not uopts.find_opt('-nt'):
            print '** please use either -run_trs or -nt/-nruns (missing one)'
            return 1
        opt = uopts.find_opt('-nruns')
        nruns = int(opt.parlist[0])

        opt = uopts.find_opt('-nt')
        try:
            nt = int(opt.parlist[0])
        except:
            print "** error: -nt must be int, have '%s'" % opt.parlist[0]
            return
        run_trs = [nt] * nruns

    # not the total number of TRs, regardless
    ntotal = afni_util.loc_sum(run_trs)

    opt = uopts.find_opt('-offset')
    if opt:
        try:
            offset = float(opt.parlist[0])
        except:
            print "** error: offset must be float, have '%s'" % opt.parlist[0]
            return

    opt = uopts.find_opt('-prefix')
    prefix = opt.parlist[0]

    opt = uopts.find_opt('-tr')
    try:
        tr = float(opt.parlist[0])
    except:
        print "** error: TR must be float, have '%s'" % opt.parlist[0]
        return

    opt = uopts.find_opt('-labels')
    if opt: labels = opt.parlist
    else: labels = []
    nlab = len(labels)

    # print some info
    if verb:        print "-- run_trs = %s, TR = %s, %d labels" \
              % (run_trs, str(tr), nlab)

    # new option, -amplitudes (columns of amplitudes to Marry, not just 1s)
    use_amp = 0
    opt = uopts.find_opt('-amplitudes')
    if opt:
        use_amp = 1
        if verb: print '-- using amplitudes to Marry with times...'

    newfile_index = 1  # index over output files
    for file in files:
        tmat = TD.read_1D_file(file, verb=verb)
        if not tmat:
            print "read_1D_file failed for file: %s" % file
            return
        mat = afni_util.transpose(tmat)
        del (tmat)

        if len(mat[0]) < ntotal:
            print '** error: file %s has only %d entries (%d required)' % \
                  (file, len(mat[0]), ntotal)
            return
        elif len(mat[0]) > ntotal:
            print '** warning: file %s has %d entries (expected only %d)' % \
                  (file, len(mat[0]), ntotal)

        for row in mat:

            # maybe add a label to the file name
            if newfile_index <= nlab: label = ".%s" % labels[newfile_index - 1]
            elif nlab == 0: label = ""
            else:
                print "** %d labels given, but we are on column %d..." % \
                      (nlab, newfile_index)
                label = ".label%d" % (newfile_index)

            newp = "%s.%02d%s" % (prefix, newfile_index, label)
            newfile = afni_util.change_path_basename(file, newp, ".1D")
            if newfile == None: return
            fp = open(newfile, 'w')

            runend = -1
            for run in range(nruns):
                runstart = runend + 1
                runend = runstart + run_trs[run] - 1
                # old: rindex = run * nt   # point to start of run
                rindex = runstart

                # if empty run, use placeholders
                if not stim_in_run(row[runstart:runend + 1], use_amp):
                    if run == 0: fp.write('* *\n')  # first run gets 2
                    else: fp.write('*\n')
                    continue
                # print '=== have stim in run, row = %s' % row[rindex:rindex+nt]
                time = 0  # in this run
                nstim = 0  # be sure we have more than 1 somewhere
                for lcol in range(run_trs[run]):
                    if row[rindex + lcol]:
                        nstim += 1
                        # if -amplitudes write as time:amplitude
                        if use_amp:
                            fp.write('%s*%s ' % \
                                     (str(time+offset),str(row[rindex+lcol])))
                        else:
                            fp.write('%s ' % str(time + offset))
                    time += tr
                if run == 0 and nstim == 1:
                    fp.write('*')  # if first time has 1 stim, add '*'
                fp.write('\n')

            fp.close()
            newfile_index += 1
Esempio n. 4
0
def proc_mats(uopts):
    offset = 0.0  # offset for output times

    if uopts == None: return

    opt = uopts.find_opt('-verb')
    if opt:
        try:
            verb = int(opt.parlist[0])
        except:
            print("** error: verb must be int, have '%s'" % opt.parlist[0])
            return
    else:
        verb = 0

    opt = uopts.find_opt('-files')
    files = opt.parlist

    consec_ok = (uopts.find_opt('-no_consec') == None)

    # set run_trs nruns from either -run_trs or -nruns and -nt
    opt = uopts.find_opt('-run_trs')
    if opt:
        val, err = uopts.get_type_list(int, '', opt=opt)
        if err: return 1
        run_trs = val
        nruns = len(run_trs)
        if uopts.find_opt('-nruns') or uopts.find_opt('-nt'):
            print('** please use either -run_trs or -nt/-nruns')
            return 1
    else:
        if not uopts.find_opt('-nruns') or not uopts.find_opt('-nt'):
            print('** please use either -run_trs or both -nt and -nruns')
            return 1
        opt = uopts.find_opt('-nruns')
        nruns = int(opt.parlist[0])

        opt = uopts.find_opt('-nt')
        try:
            nt = int(opt.parlist[0])
        except:
            print("** error: -nt must be int, have '%s'" % opt.parlist[0])
            return
        run_trs = [nt] * nruns

    # note the total number of TRs, regardless
    ntotal = afni_util.loc_sum(run_trs)

    opt = uopts.find_opt('-offset')
    if opt:
        try:
            offset = float(opt.parlist[0])
        except:
            print("** error: offset must be float, have '%s'" % opt.parlist[0])
            return

    opt = uopts.find_opt('-prefix')
    prefix = opt.parlist[0]

    opt = uopts.find_opt('-tr')
    try:
        tr = float(opt.parlist[0])
    except:
        print("** error: TR must be float, have '%s'" % opt.parlist[0])
        return

    opt = uopts.find_opt('-labels')
    if opt: labels = opt.parlist
    else: labels = []
    nlab = len(labels)

    # print some info
    if verb:        print("-- run_trs = %s, TR = %s, %d labels" \
              % (run_trs, str(tr), nlab))

    # new option, -amplitudes (columns of amplitudes to Marry, not just 1s)
    use_amp = 0
    opt = uopts.find_opt('-amplitudes')
    if opt:
        use_amp = 1
        if verb: print('-- using amplitudes to Marry with times...')

    newfile_index = 1  # index over output files
    for fname in files:
        tmat = TD.read_1D_file(fname, verb=verb)
        if not tmat:
            print("read_1D_file failed for file: %s" % fname)
            return
        mat = afni_util.transpose(tmat)
        del (tmat)

        if len(mat[0]) < ntotal:
            print('** error: file %s has only %d entries (%d required)' % \
                  (fname, len(mat[0]), ntotal))
            return
        elif len(mat[0]) > ntotal:
            print('** warning: file %s has %d entries (expected only %d)' % \
                  (fname, len(mat[0]), ntotal))

        for row in mat:

            # maybe add a label to the file name
            if newfile_index <= nlab: label = ".%s" % labels[newfile_index - 1]
            elif nlab == 0: label = ""
            else:
                print("** %d labels given, but we are on column %d..." % \
                      (nlab, newfile_index))
                label = ".label%d" % (newfile_index)

            newp = "%s.%02d%s" % (prefix, newfile_index, label)
            newfile = afni_util.change_path_basename(fname, newp + ".1D")
            if newfile == None: return
            if prefix == '-' or prefix == 'stdout': fp = sys.stdout
            elif prefix == 'stderr': fp = sys.stderr
            else: fp = open(newfile, 'w')

            blocked_consec = 0  # note whether we block consecutive events
            runend = -1
            for run in range(nruns):
                runstart = runend + 1
                runend = runstart + run_trs[run] - 1
                # old: rindex = run * nt   # point to start of run
                rindex = runstart

                # if empty run, use placeholders
                if not stim_in_run(row[runstart:runend + 1], use_amp):
                    if run == 0: fp.write('* *\n')  # first run gets 2
                    else: fp.write('*\n')
                    continue
                # print '=== have stim in run, row = %s' % row[rindex:rindex+nt]
                time = 0  # in this run
                nstim = 0  # be sure we have more than 1 somewhere
                first = 1  # first occurance if no consec
                for lcol in range(run_trs[run]):
                    if row[rindex + lcol]:
                        # note whether anything has been blocked
                        if not first and not consec_ok:
                            if verb > 2:
                                print('-- blocked consec: file %s, run %d, ind %d'\
                                      % (newp, run, lcol))
                            blocked_consec = 1

                        # if consec is bad, only write on first occurance
                        if first or consec_ok:
                            nstim += 1
                            tstr = '%s' % str(time + offset)

                            # if -amplitudes write as time*amplitude
                            if use_amp: astr = '*%s' % str(row[rindex + lcol])
                            else: astr = ''

                            fp.write('%s%s ' % (tstr, astr))

                        first = 0  # not first occurance
                    else:
                        first = 1

                    time += tr
                if run == 0 and nstim == 1:
                    fp.write('*')  # if first time has 1 stim, add '*'
                fp.write('\n')

            if fp != sys.stdout and fp != sys.stderr: fp.close()
            newfile_index += 1

            if verb and blocked_consec:
                print('-- blocked consecutive events file %s, output %s' \
                      % (fname, newp))
def proc_mats(uopts):
    offset = 0.0        # offset for output times

    if uopts == None: return

    opt = uopts.find_opt('-verb')
    if opt:
        try: verb = int(opt.parlist[0])
        except:
            print "** error: verb must be int, have '%s'" % opt.parlist[0]
            return
    else: verb = 0

    opt    = uopts.find_opt('-files')
    files  = opt.parlist

    consec_ok = (uopts.find_opt('-no_consec') == None)

    # set run_trs nruns from either -run_trs or -nruns and -nt
    opt    = uopts.find_opt('-run_trs')
    if opt:
       val, err = uopts.get_type_list(int, '', opt=opt)
       if err: return 1
       run_trs = val
       nruns = len(run_trs)
       if uopts.find_opt('-nruns') or uopts.find_opt('-nt'):
          print '** please use either -run_trs or -nt/-nruns'
          return 1
    else:
       if not uopts.find_opt('-nruns') or not uopts.find_opt('-nt'):
          print '** please use either -run_trs or both -nt and -nruns'
          return 1
       opt    = uopts.find_opt('-nruns')
       nruns = int(opt.parlist[0])

       opt    = uopts.find_opt('-nt')
       try: nt = int(opt.parlist[0])
       except:
           print "** error: -nt must be int, have '%s'" % opt.parlist[0]
           return
       run_trs = [nt]*nruns

    # note the total number of TRs, regardless
    ntotal = afni_util.loc_sum(run_trs)

    opt    = uopts.find_opt('-offset')
    if opt:
        try: offset = float(opt.parlist[0])
        except:
            print "** error: offset must be float, have '%s'" % opt.parlist[0]
            return

    opt    = uopts.find_opt('-prefix')
    prefix = opt.parlist[0]

    opt    = uopts.find_opt('-tr')
    try: tr = float(opt.parlist[0])
    except:
        print "** error: TR must be float, have '%s'" % opt.parlist[0]
        return

    opt = uopts.find_opt('-labels')
    if opt: labels = opt.parlist
    else:   labels = []
    nlab = len(labels)

    # print some info
    if verb: print "-- run_trs = %s, TR = %s, %d labels" \
                   % (run_trs, str(tr), nlab)

    # new option, -amplitudes (columns of amplitudes to Marry, not just 1s)
    use_amp = 0
    opt = uopts.find_opt('-amplitudes')
    if opt:
        use_amp = 1
        if verb: print '-- using amplitudes to Marry with times...'
    
    newfile_index = 1   # index over output files
    for fname in files:
        tmat = TD.read_1D_file(fname, verb=verb)
        if not tmat:
            print "read_1D_file failed for file: %s" % fname
            return
        mat = afni_util.transpose(tmat)
        del(tmat)

        if len(mat[0]) < ntotal:
            print '** error: file %s has only %d entries (%d required)' % \
                  (fname, len(mat[0]), ntotal)
            return
        elif len(mat[0]) > ntotal:
            print '** warning: file %s has %d entries (expected only %d)' % \
                  (fname, len(mat[0]), ntotal)

        for row in mat:

            # maybe add a label to the file name
            if newfile_index <= nlab: label = ".%s" % labels[newfile_index-1]
            elif nlab == 0:           label = ""
            else:
                print "** %d labels given, but we are on column %d..." % \
                      (nlab, newfile_index)
                label = ".label%d" % (newfile_index)

            newp = "%s.%02d%s" % (prefix,newfile_index,label)
            newfile = afni_util.change_path_basename(fname, newp, ".1D")
            if newfile == None: return
            if   prefix == '-' or prefix == 'stdout': fp = sys.stdout
            elif prefix == 'stderr':                  fp = sys.stderr
            else:                                     fp = open(newfile, 'w')

            blocked_consec = 0  # note whether we block consecutive events
            runend = -1
            for run in range(nruns):
                runstart = runend + 1
                runend = runstart + run_trs[run] - 1
                # old: rindex = run * nt   # point to start of run
                rindex = runstart

                # if empty run, use placeholders
                if not stim_in_run(row[runstart:runend+1], use_amp):
                    if run == 0: fp.write('* *\n')  # first run gets 2
                    else:        fp.write('*\n')
                    continue
                # print '=== have stim in run, row = %s' % row[rindex:rindex+nt]
                time = 0        # in this run
                nstim = 0       # be sure we have more than 1 somewhere
                first = 1       # first occurance if no consec
                for lcol in range(run_trs[run]):
                    if row[rindex+lcol]:
                       # note whether anything has been blocked
                       if not first and not consec_ok:
                          if verb > 2:
                             print '-- blocked consec: file %s, run %d, ind %d'\
                                   % (newp, run, lcol)
                          blocked_consec = 1

                       # if consec is bad, only write on first occurance
                       if first or consec_ok:
                         nstim += 1
                         tstr = '%s' % str(time+offset)

                         # if -amplitudes write as time*amplitude
                         if use_amp: astr = '*%s' % str(row[rindex+lcol])
                         else:       astr = ''

                         fp.write('%s%s ' % (tstr, astr))

                       first = 0 # not first occurance
                    else: first = 1

                    time += tr
                if run == 0 and nstim == 1:
                    fp.write('*')   # if first time has 1 stim, add '*'
                fp.write('\n')

            if fp != sys.stdout and fp != sys.stderr: fp.close()
            newfile_index += 1

            if verb and blocked_consec:
               print '-- blocked consecutive events file %s, output %s' \
                     % (fname, newp)