def process_logfile(self): """main function to process logfile """ # replace any known label groups self.expand_labels() # read logfile inlines = UTIL.read_text_file(self.logfile, lines=1, verb=self.verb) llist = self.make_label_list(inlines) if self.show_vals & SHOW_RANK: if self.verb: pref = 'rank : ' else: pref = '' lshow = [ll[2] for ll in llist if ll[2] != None] print '%s%s' % (pref, ','.join(lshow)) if self.show_vals & SHOW_ORIG: if self.verb: pref = 'orig : ' else: pref = '' if self.show_vals & SHOW_ALL_ORIG: lshow = [ll[1] for ll in llist] else: lshow = [ll[1] for ll in llist if ll[2] != None] print '%s%s' % (pref, ','.join(lshow)) return 0
def parse_3col_tsv(fname, verb=1): """Read one 3 column tsv (tab separated value) file, and return: - status (0 if okay) - header list (length 3?) - list of onset, duration, label values A 3 column tsv file should have an optional header line, followed by rows of VAL VAL LABEL, separated by tabs. """ lines = UTIL.read_text_file(fname, lines=1) if len(lines) < 1: print("** failed parse_3col_tsv for '%s'" % fname) return 1, [], [] # pare lines down to useful ones newlines = [] for lind, line in enumerate(lines): vv = line.split('\t') if len(vv) == 3: newlines.append(vv) elif len(vv) > 0: print('** skipping bad line %d of 3col tsv file %s' % (lind, fname)) if verb > 2: print(' vals[%d] = %s' % (len(vv), vv)) elif verb > 2: print('** skipping empty line %d of 3col tsv file %s' % (lind, fname)) lines = newlines if len(lines) < 1: print("** parse_3col_tsv for '%s' is empty" % fname) return 1, [], [] # set header list, if a header exists header = [] l0 = lines[0] try: onset = float(l0[0]) dur = float(l0[1]) lab = l0[2].replace(' ', '_') # and convert spaces to underscores except: header = lines.pop(0) # now lines should be all: onset, duration, label slist = [] for line in lines: try: onset = float(line[0]) dur = float(line[1]) lab = line[2].replace(' ', '_') # convert spaces to underscores except: print('** bad line 3col tsv file %s: %s' % (fname, ' '.join(line))) return 1, [], [] slist.append([onset, dur, lab]) return 0, header, slist
def check_dotfiles(self, show=0, pack=0): global g_dotfiles home = os.environ['HOME'] # get list of existing files dfound = [] for dfile in g_dotfiles: if os.path.isfile('%s/%s' % (home, dfile)): dfound.append(dfile) print('found under $HOME : %s' % dfile) if show: for dfile in dfound: print(UTIL.section_divider(dfile, hchar='=')) print('%s\n' % UTIL.read_text_file('%s/%s' % (home, dfile), lines=0)) if pack: import shutil package = self.dot_file_pack pgz = '%s.tgz' % package # maybe user included the extension ext = package.find('.tgz') if ext >= 0: pgz = package package = package[0:ext] if os.path.exists(package) or os.path.exists('%s.tgz' % package): print("** error: package dir '%s' or file '%s' already exists"\ % (package, pgz)) return 1 try: os.mkdir(package) except: print("** failed to make dot file package dir '%s'" % package) return 1 for dfile in dfound: shutil.copy2('%s/%s' % (home, dfile), package) os.system("tar cfz %s %s" % (pgz, package)) shutil.rmtree(package) if os.path.exists(pgz): print('++ dot file package is in %s' % pgz) else: print('** failed to make dot file packge %s' % pgz) return 0
def check_dotfiles(self, show=0, pack=0): global g_dotfiles home = os.environ['HOME'] # get list of existing files dfound = [] for dfile in g_dotfiles: if os.path.isfile('%s/%s' % (home, dfile)): dfound.append(dfile) print 'found under $HOME : %s' % dfile if show: for dfile in dfound: print UTIL.section_divider(dfile, hchar='=') print '%s\n' % UTIL.read_text_file('%s/%s' % (home, dfile), lines=0) if pack: import shutil package = self.dot_file_pack pgz = '%s.tgz' % package # maybe user included the extension ext = package.find('.tgz') if ext >= 0: pgz = package package = package[0:ext] if os.path.exists(package) or os.path.exists('%s.tgz'%package): print "** error: package dir '%s' or file '%s' already exists"\ % (package, pgz) return 1 try: os.mkdir(package) except: print "** failed to make dot file package dir '%s'" % package return 1 for dfile in dfound: shutil.copy2('%s/%s' % (home, dfile), package) os.system("tar cfz %s %s" % (pgz, package)) shutil.rmtree(package) if os.path.exists(pgz): print '++ dot file package is in %s' % pgz else: print '** failed to make dot file packge %s' % pgz
fff.write(text_title_desc) grp = "" for x in lll: # make new table if grp if x[0] != grp: grp = x[0] fff.write(table_head % (grp)) fff.write(" * - %s\n" % (x[3])) fff.write(" - :ref:`%s <ahelp_%s>`\n" % (x[2], x[2])) fff.write(" - %s\n" % (x[4])) fff.close() # ================================================================= if __name__ == "__main__": # --------------------- get input ------------------------ print "++ Command line:\n ", ' '.join(sys.argv) (ifile, ofile) = get_arg(sys.argv[1:]) all_lines = au.read_text_file(ifile) lll = parse_field_lines(all_lines) write_out_edu_rst(ofile, lll) print("++ Done writing educational classification rst!")
return GG # ================================================================= if __name__ == "__main__": # --------------------- get input ------------------------ print("++ Command line:\n ", ' '.join(sys.argv)) (ifile, inumb, ofile) = get_arg(sys.argv[1:]) # --------------------- proc it ------------------------ bad_list_init = au.read_text_file(ifile) bad_list_proc = Convert_StrList_to_NumArr(bad_list_init) good_list = Invert_BadInd_List(bad_list_proc, inumb) good_encoded = au.encode_1D_ints(good_list) # ------------------- write out ------------------------- print("++ OK, the list of good indices in AFNI selector format is:") print("\n %s\n" % good_encoded) f = open(ofile, 'w') f.write(good_encoded) f.close() print(" ... which has been written to file: %s" % ofile)
def parse_Ncol_tsv(fname, verb=1): """Read one N column tsv (tab separated value) file, and return: - ncol: -1 on error, else >= 0 - header list (length ncol) - list of onset, duration, label values, amplitudes? An N column tsv file should have an optional header line, followed by rows of VAL VAL LABEL, separated by tabs. """ lines = UTIL.read_text_file(fname, lines=1) if len(lines) < 1: print("** failed parse_3col_tsv for '%s'" % fname) return -1, [], [] # pare lines down to useful ones newlines = [] norig = 0 for lind, line in enumerate(lines): vv = line.split('\t') vlen = len(vv) if len == 0: if verb > 2: print('** skipping empty line %d of 3col tsv file %s' \ % (lind, fname)) continue # possibly initialize norig if norig == 0: norig = vlen # require consistency if vlen != norig: print('** line %d ncol=%d, mismatch with orig ncol %d' % \ (lind, vlen, norig)) print('** skipping bad line: %s' % line) continue newlines.append(vv) lines = newlines if len(lines) < 1: print("** parse_Ncol_tsv for '%s' is empty" % fname) return -1, [], [] if norig < 3: print("** parse_Ncol_tsv: bad ncols = %d" % norig) return -1, [], [] # set header list, if a header exists header = [] l0 = lines[0] try: onset = float(l0[0]) dur = float(l0[1]) lab = l0[2].replace(' ', '_') # and convert spaces to underscores except: header = lines.pop(0) # decide whether there are amplitudes ainds = [] if len(lines) > 0 and norig > 3: line = lines[0] # find all columns that look like floats for ind in range(3, len(line)): try: amp = float(line[ind]) ainds.append(ind) except: pass # now lines should be all: onset, duration, label and possibly amplitudes slist = [] for line in lines: try: onset = float(line[0]) dur = float(line[1]) lab = line[2].replace(' ', '_') # convert spaces to underscores if len(ainds) > 0: amps = [float(line[aind]) for aind in ainds] except: print('** bad line Ncol tsv file %s: %s' % (fname, ' '.join(line))) return -1, [], [] if len(ainds) > 0: slist.append([onset, dur, lab, amps]) else: slist.append([onset, dur, lab]) if len(ainds) > 0: nuse = 4 else: nuse = 3 return nuse, header, slist
def parse_Ncol_tsv(fname, verb=1, hlabels=['onset', 'duration', 'trial_type']): """Read one N column tsv (tab separated value) file, and return: - ncol: -1 on error, else >= 0 - header list (length ncol) - list of onset, duration, label values, amplitudes? If all hlabels exist in header labels, then extract those columns. If all hlabels are integers, extract those 0-based columns. The format for hlabels should indicate : onset, duration, type and amplitudes, if any: onset time, duration, trial type, amp1, amp2, ... ampA An N column tsv file should have an optional header line, followed by rows of VAL VAL LABEL, separated by tabs. """ lines = UTIL.read_text_file(fname, lines=1) if len(lines) < 1: print("** failed parse_3col_tsv for '%s'" % fname) return -1, [], [] # pare lines down to useful ones newlines = [] norig = 0 for lind, line in enumerate(lines): vv = line.split('\t') vlen = len(vv) if len == 0: if verb > 2: print('** skipping empty line %d of 3col tsv file %s' \ % (lind, fname)) continue # possibly initialize norig if norig == 0: norig = vlen # require consistency if vlen != norig: print('** line %d ncol=%d, mismatch with orig ncol %d' % \ (lind, vlen, norig)) print('** skipping bad line: %s' % line) continue newlines.append(vv) lines = newlines if len(lines) < 1: print("** parse_Ncol_tsv for '%s' is empty" % fname) return -1, [], [] if norig < 3: print("** parse_Ncol_tsv: bad ncols = %d in %s" % (norig, fname)) return -1, [], [] # decide on column extration indices, based on hlabels and lines[0:2] col_inds = tsv_hlabels_to_col_list(hlabels, lines, verb=verb) if len(col_inds) < 3: print("** failed to make tsv column index list in %s" % fname) return -1, [], [] # set header list, if a header exists header = [] l0 = lines[0] try: onset = float(l0[col_inds[0]]) dur = float(l0[col_inds[1]]) lab = l0[col_inds[2]].replace(' ', '_') # convert spaces to underscores except: l0 = lines.pop(0) header = [l0[col_inds[i]] for i in range(len(col_inds))] # decide whether there are amplitudes ainds = col_inds[3:] #if len(lines) > 0 and norig > 3: # line = lines[0] # # find all columns that look like floats # for ind in range(3, len(line)): # try: # amp = float(line[ind]) # ainds.append(ind) # except: pass # now lines should be all: onset, duration, label and possibly amplitudes slist = [] oind = col_inds[0] dind = col_inds[1] lind = col_inds[2] for line in lines: try: onset = float(line[oind]) dur = float(line[dind]) lab = line[lind].replace(' ', '_') # convert spaces to underscores if len(ainds) > 0: amps = [float(line[aind]) for aind in ainds] except: print('** bad line Ncol tsv file %s: %s' % (fname, ' '.join(line))) return -1, [], [] if len(ainds) > 0: slist.append([onset, dur, lab, amps]) else: slist.append([onset, dur, lab]) nuse = len(col_inds) return nuse, header, slist