def set_censor_arr(self): self.censor_width = self.all_x[1] - self.all_x[0] # add to this in different forms, where True values mean # censoring is flagged to have occured cen_arr = [False] * self.npts # combine all strings of censor lists: # ... from user-entered strings if self.censor_in_trs: maxind = self.npts - 1 for ttt in self.censor_in_trs: ll = au.decode_1D_ints(ttt, imax=maxind) for ii in ll: cen_arr[ii] = True # ... from user-entered files if self.censor_in_files: for fff in self.censor_in_files: x = LAD.Afni1D(fff) if x.nt != self.npts : sys.exit("** ERROR: len of censor file {} ({}) does not " "match Npts={}".format(fff, x.nt, self.npts)) for i in range(self.npts): val = x.mat[0][i] if not(val): cen_arr[i] = True # And finally store the list of xvals to censor self.ncensor = cen_arr.count(True) if self.ncensor : for i in range(self.npts): if cen_arr[i]: self.censor_arr.append(self.all_x[i])
def set_cols_from_string(self, cstr): """decode the string into a 1D column selection and apply it to col_list return an error message string, or None on success""" if not self.matX: return "please load an X matrix, first" ncols = self.matX.ncols clist = UTIL.decode_1D_ints(cstr, imax=ncols-1) if not clist: return "invalid column list\n\n--> please use AFNI sub-brick notation" elif not self.AM.list2_is_in_list1(range(ncols), clist): return "column list outside xmat cols 0..%d" % (ncols-1) self.col_list = clist return None # be explicit
def set_cols_from_string(self, cstr): """decode the string into a 1D column selection and apply it to col_list return an error message string, or None on success""" if not self.matX: return "please load an X matrix, first" ncols = self.matX.ncols clist = UTIL.decode_1D_ints(cstr, imax=ncols - 1) if not clist: return "invalid column list\n\n--> please use AFNI sub-brick notation" elif not self.AM.list2_is_in_list1(range(ncols), clist): return "column list outside xmat cols 0..%d" % (ncols - 1) self.col_list = clist return None # be explicit
def init_from_1D(self, fname): """initialize AfniXmat from a 1D file""" mat, clines = TD.read_data_file(fname) if not mat: return if not TD.data_is_rect(mat): print '** matrix is not rectangular in %s' % fname return self.mat = N.array(mat) self.nrows = len(mat) self.ncols = len(mat[0]) self.ready = 1 for line in clines: label, data = c1D_line2labelNdata(line) if not label: continue verb_level = 3 # cutoff for verbose level try: # to process some of the data if label == 'ni_type': ncols, type = data.split('*') ncols = int(ncols) if self.verb > verb_level: print "-- label %s: cols = %d, type = %s" % \ (label, ncols, type) if ncols != self.ncols: print "** matrix cols %d != %s cols %d" % \ (self.ncols, label, ncols) elif label == 'ni_dimen': nrows = int(data) if self.verb > verb_level: print "-- label %s: rows = %d" % (label, nrows) if nrows != self.nrows: print "** matrix rows %d != %s rows %d" % \ (self.nrows, label, nrows) elif label == 'ColumnLabels': self.labels = [str.strip() for str in data.split(';')] if self.verb > verb_level: print "-- label %s: labels = %s" % (label, self.labels) if self.ncols != len(self.labels): print "** %d ColumnLabels but %d columns" % \ (len(self.labels), self.ncols) self.labels = None elif label == 'ColumnGroups': self.groups = UTIL.decode_1D_ints(data) if self.groups: if len(self.groups) != self.ncols: print "** ColumnGroups len %d != ncols %d" % \ (len(self.groups), self.ncols) if self.verb > verb_level: print "-- label %s: groups %s" % (label,self.groups) elif label == 'RowTR': self.tr = float(data) if self.verb > verb_level: print "-- label %s: TR %s" % (label,self.tr) elif label == 'GoodList': self.goodlist = UTIL.decode_1D_ints(data) if self.goodlist: if len(self.goodlist) != self.nrows: print "** GoodList missing %d rows" % \ self.nrows-len(self.goodlist) if self.verb > verb_level: print "-- label %s: goodlist %s" % (label,self.goodlist) elif label == 'NRowFull': self.nrowfull = int(data) if self.verb > verb_level: print "-- label %s: nrowfull %s" % (label,self.nrowfull) elif self.verb > 2: print "** unknown comment label '%s'" % label except: print "** failed to process comment label '%s'" % label
def init_from_1D(self, fname): """initialize AfniXmat from a 1D file""" mat, clines = TD.read_data_file(fname) if not mat: return if not TD.data_is_rect(mat): print '** matrix is not rectangular in %s' % fname return self.mat = N.array(mat) self.nrows = len(mat) self.ncols = len(mat[0]) self.ready = 1 for line in clines: label, data = c1D_line2labelNdata(line) if not label: continue verb_level = 3 # cutoff for verbose level try: # to process some of the data if label == 'ni_type': ncols, type = data.split('*') ncols = int(ncols) if self.verb > verb_level: print "-- label %s: cols = %d, type = %s" % \ (label, ncols, type) if ncols != self.ncols: print "** matrix cols %d != %s cols %d" % \ (self.ncols, label, ncols) elif label == 'ni_dimen': nrows = int(data) if self.verb > verb_level: print "-- label %s: rows = %d" % (label, nrows) if nrows != self.nrows: print "** matrix rows %d != %s rows %d" % \ (self.nrows, label, nrows) elif label == 'ColumnLabels': self.labels = [str.strip() for str in data.split(';')] if self.verb > verb_level: print "-- label %s: labels = %s" % (label, self.labels) if self.ncols != len(self.labels): print "** %d ColumnLabels but %d columns" % \ (len(self.labels), self.ncols) self.labels = None elif label == 'ColumnGroups': self.groups = UTIL.decode_1D_ints(data) if self.groups: if len(self.groups) != self.ncols: print "** ColumnGroups len %d != ncols %d" % \ (len(self.groups), self.ncols) if self.verb > verb_level: print "-- label %s: groups %s" % (label, self.groups) elif label == 'RowTR': self.tr = float(data) if self.verb > verb_level: print "-- label %s: TR %s" % (label, self.tr) elif label == 'GoodList': self.goodlist = UTIL.decode_1D_ints(data) if self.goodlist: if len(self.goodlist) != self.nrows: print "** GoodList missing %d rows" % \ self.nrows-len(self.goodlist) if self.verb > verb_level: print "-- label %s: goodlist %s" % (label, self.goodlist) elif label == 'NRowFull': self.nrowfull = int(data) if self.verb > verb_level: print "-- label %s: nrowfull %s" % (label, self.nrowfull) elif self.verb > 2: print "** unknown comment label '%s'" % label except: print "** failed to process comment label '%s'" % label
return ofile, maxind, lstr # ================================================================= if __name__ == "__main__": # --------------------- 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()
# ================================================================= if __name__=="__main__": # --------------------- get input ------------------------ print("++ Command line:\n ", ' '.join(sys.argv)) (ofile, maxind, lstr) = get_arg(sys.argv[1:]) # [PT: May 22, 2018] want INTERSECTION, not union... Nlstr = len(lstr) if Nlstr < 1: sys.exit("Couldn't find any number strings??") # put all selectors together sss = set(au.decode_1D_ints(lstr[0], imax=maxind)) for i in range(1,Nlstr): ll = lstr[i] sss = set.intersection(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)