def update_textlist_from_xmat(self): """given the X matrix and columns, fill screen data list""" if not self.XM.matX: return matx = self.XM.matX ccols = self.XM.col_list if self.XM.verb > 1: print '++ updating text for columns: %s' % ccols ind = self.textlist_index(gSTR_XMAT_FILE) if ind >= 0: self.tw_stlist[ind].SetLabel(os.path.basename(matx.fname)) # show columns self.set_textlist_label(self.textlist_index(gSTR_COLS_CHOSEN), str=UTIL.encode_1D_ints(ccols)) self.set_textlist_label(self.textlist_index(gSTR_COLS_ALL), str=UTIL.encode_1D_ints(range(matx.ncols))) self.set_textlist_label(self.textlist_index(gSTR_COLS_MAIN), str=UTIL.encode_1D_ints(matx.cols_by_group_list([],allroi=1))) self.set_textlist_label(self.textlist_index(gSTR_COLS_BASE), str=UTIL.encode_1D_ints(matx.cols_by_group_list([-1]))) self.set_textlist_label(self.textlist_index(gSTR_COLS_MOTION), str=UTIL.encode_1D_ints(matx.cols_by_group_list([0]))) # show select condition numbers cond = matx.cond_num_by_cols(ccols) self.set_textlist_label(self.textlist_index(gSTR_COND_CHOSEN), val=cond) cond = matx.cond_num_by_cols(range(matx.ncols)) self.set_textlist_label(self.textlist_index(gSTR_COND_ALL), val=cond) cond = matx.cond_num_by_cols(matx.cols_by_group_list([],allroi=1)) self.set_textlist_label(self.textlist_index(gSTR_COND_MAIN), val=cond) cond = matx.cond_num_by_cols(matx.cols_by_group_list([-1],allroi=1)) self.set_textlist_label(self.textlist_index(gSTR_COND_M_BASE), val=cond) cond = matx.cond_num_by_cols(matx.cols_by_group_list([0],allroi=1)) self.set_textlist_label(self.textlist_index(gSTR_COND_M_MOT), val=cond) cond = matx.cond_num_by_cols(matx.cols_by_group_list([-1,0])) self.set_textlist_label(self.textlist_index(gSTR_COND_BASE_MOT), val=cond)
def make_col_type_string(self): """return a string showing which columns are main, baseline or motion (return error code (0=success) and cormat string)""" mat = self.matX if not mat.ready: return 1, '** no X-matrix to get col types from' mstr = 'Columns by regressor type:\n\n' mstr += ' main regressor columns : %s\n' % \ UTIL.encode_1D_ints(range(self.matX.ncols)) mstr += ' chosen regressor columns : %s\n' % \ UTIL.encode_1D_ints(self.col_list) mstr += ' baseline regressor columns : %s\n' % \ UTIL.encode_1D_ints(self.matX.cols_by_group_list([-1])) mstr += ' motion regressor columns : %s\n' % \ UTIL.encode_1D_ints(self.matX.cols_by_group_list([0])) return 0, mstr
# ================================================================= 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) print("++ Done.") sys.exit(0)
# --------------------- get input ------------------------ print "++ Command line:\n ", ' '.join(sys.argv) (ofile, maxind, lstr) = get_arg(sys.argv[1:]) # put all selectors together sss = set() for ll in lstr: sss = set.union(sss, set(au.decode_1D_ints(ll, imax=maxind))) # listify and sort list_final = list(sss) list_final.sort() good_encoded = au.encode_1D_ints(list_final) # ------------------- 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 print "++ Done." sys.exit(0)
def plot_mat_by_cols(self, amat, cols=[]): """plot the given columns from a single AfniMatrix""" if not isinstance(amat, AM.AfniXmat): print('** plot_MBC: instance is not AfniXmat') return if not cols: if amat.verb > 1: print('-- plotting default of %d cols' % amat.ncols) cols = [i for i in range(amat.ncols)] ncols = len(cols) if ncols < 1: return # create reduced matrix and column labels mat = amat.mat[:, cols] yformat = FormatStrFormatter('%5.1f') if amat.labels: labels = N.array(amat.labels) labels = labels[cols] labels = labels.tolist() # create axis formatter string striplabs = 1 for label in labels: # strip trailing '#' if indices are all 0 ind = regress_index(label) if ind > 0: striplabs = 0 maxlen = 0 for l in range(len(labels)): label = labels[l] if striplabs: labels[l] = labels[l][0:labels[l].rfind('#')] if len(labels[l]) > maxlen: maxlen = len(labels[l]) for i in range(ncols): if i == 0: title = '%s [%s]' % (os.path.basename( amat.fname), UTIL.encode_1D_ints(cols)) else: title = '' if self.as_one: # then only create one axis if i == 0: ax = self.figure.add_subplot(1, 1, 1, title=title) else: ax = self.figure.add_subplot(ncols, 1, i + 1, title=title) data = mat[:, i] ax.plot(data) # apply y tickmarks ymin, ymax = ax.get_ylim() width = ymax - ymin if ymin * ymax < 0.0: ymean = 0.0 else: ymean = round((ymin + ymax) / 2.0, 2) ax.grid(True) ax.yaxis.set_major_formatter(yformat) amax = round(data.max(), 1) if self.as_one: pass # use default yticks elif amax == 1.0 and ymin == 0.0: ax.set_yticks(N.array([ymin, amax])) elif ncols > 10: ax.set_yticks(N.array([ymin, ymax])) else: ax.set_yticks(N.array([ymin, ymean, ymax])) # now that yticks are set, prevent tight limits if not self.as_one: if ymin == data.min(): ymin -= 0.15 * width ax.set_ylim((ymin, ymax)) if ymax == data.max(): ymax += 0.15 * width ax.set_ylim((ymin, ymax)) if amat.run_len > 10 and amat.nruns > 1: ax.set_xticks( N.array( [r * amat.run_len for r in range(amat.nruns + 1)])) if i < ncols - 1: ax.set_xticklabels([]) else: ax.set_xlabel('TRs') if self.as_one: ax.set_ylabel('') elif amat.labels: ax.set_ylabel('%-*s ' % (maxlen, labels[i]), rotation='horizontal') rv, plist = self.axis_posn(ax) # rcr - fix #if rv == 0: ax.set_position((0.2, plist[1], plist[2], plist[3])) if rv == 0: ax.set_position((0.15, plist[1], 0.7, 0.7 / ncols)) self.Fit() self.canvas.draw()
def plot_mat_by_cols(self, amat, cols=[]): """plot the given columns from a single AfniMatrix""" if not isinstance(amat, AM.AfniXmat): print "** plot_MBC: instance is not AfniXmat" return if not cols: if amat.verb > 1: print "-- plotting default of %d cols" % amat.ncols cols = [i for i in range(amat.ncols)] ncols = len(cols) if ncols < 1: return # create reduced matrix and column labels mat = amat.mat[:, cols] yformat = FormatStrFormatter("%5.1f") if amat.labels: labels = N.array(amat.labels) labels = labels[cols] labels = labels.tolist() # create axis formatter string striplabs = 1 for label in labels: # strip trailing '#' if indices are all 0 ind = regress_index(label) if ind > 0: striplabs = 0 maxlen = 0 for l in range(len(labels)): label = labels[l] if striplabs: labels[l] = labels[l][0 : labels[l].rfind("#")] if len(labels[l]) > maxlen: maxlen = len(labels[l]) for i in range(ncols): if i == 0: title = "%s [%s]" % (os.path.basename(amat.fname), UTIL.encode_1D_ints(cols)) else: title = "" if self.as_one: # then only create one axis if i == 0: ax = self.figure.add_subplot(1, 1, 1, title=title) else: ax = self.figure.add_subplot(ncols, 1, i + 1, title=title) data = mat[:, i] ax.plot(data) # apply y tickmarks ymin, ymax = ax.get_ylim() width = ymax - ymin if ymin * ymax < 0.0: ymean = 0.0 else: ymean = round((ymin + ymax) / 2.0, 2) ax.grid(True) ax.yaxis.set_major_formatter(yformat) amax = round(data.max(), 1) if self.as_one: pass # use default yticks elif amax == 1.0 and ymin == 0.0: ax.set_yticks(N.array([ymin, amax])) elif ncols > 10: ax.set_yticks(N.array([ymin, ymax])) else: ax.set_yticks(N.array([ymin, ymean, ymax])) # now that yticks are set, prevent tight limits if not self.as_one: if ymin == data.min(): ymin -= 0.15 * width ax.set_ylim((ymin, ymax)) if ymax == data.max(): ymax += 0.15 * width ax.set_ylim((ymin, ymax)) if amat.run_len > 10 and amat.nruns > 1: ax.set_xticks(N.array([r * amat.run_len for r in range(amat.nruns + 1)])) if i < ncols - 1: ax.set_xticklabels([]) else: ax.set_xlabel("TRs") if self.as_one: ax.set_ylabel("") elif amat.labels: ax.set_ylabel("%-*s " % (maxlen, labels[i]), rotation="horizontal") rv, plist = self.axis_posn(ax) # rcr - fix # if rv == 0: ax.set_position((0.2, plist[1], plist[2], plist[3])) if rv == 0: ax.set_position((0.15, plist[1], 0.7, 0.7 / ncols)) self.Fit() self.canvas.draw()
#!/usr/bin/env python2.7 # put an encoded string of ints into a text file import afni_util as au import sys as sys import numpy as np if len(sys.argv) - 4: print "\n\nBAD! Need 3 arguments!\n\n" sys.exit(1) ofile = sys.argv[1] N = int(sys.argv[2]) f = float(sys.argv[3]) x = np.arange(N) L = int(f * N) np.random.shuffle(x) y = list(x[:L]) y.sort() enums = au.encode_1D_ints(y) print enums f = open(ofile, 'w') f.write(enums + '\n') f.close()