def go(self, xn): if (self.x == None): self.x = ones(len(self.b)) * xn * 1.0 self.y = ones(len(self.a)) * xn * 1.0 * sum(self.b) / (1+sum(self.a)) self.x = concatenate([[xn], self.x[:-1]]) yn = dot(self.b, self.x) - dot(self.a, self.y) self.y = concatenate([[yn], self.y[:-1]]) return yn
def delete_n_rows(self, i, n=1): """ Delete `n` rows, starting at the row with the index `i`. Returns the old data. """ n = min(self.nrows-i, n) # To make sure that the operation does not fail and leaves invalid # data, we first create the new column datas in new_data, and then, # if it worked for all columns, we will assign the new data. new_data = list() rv = list() j = 0 for col in self.columns: rv.append( col.data[i:i+n] ) new_data.append(concatenate( [col.data[0:i], col.data[i+n:]] )) j += 1 # assign new data j = 0 for col in self.columns: col.data = new_data[j] j += 1 self.update_rows() return rv
def insert_rows(self, i, rows): """ Insert the given `rows` (list of one-dimensional arrays) at row `i`. The given arrays must all have the same length and there must be exactly as many arrays as there are columns. Of course the arrays must match the Column type or it must be possible to convert them to the Column type using astype. """ # [ [all new rows of col1], [all new rows of col2], ...] if len(rows) != self.ncols: raise ValueError("When adding new rows, you must provide the values in a transposed form: TODO: EXAMPLE.") # To make sure that the operation does not fail and leaves invalid # data, we first create the new column datas in new_data, and then, # if it worked for all columns, we will assign the new data. new_data = list() j = 0 first_length = None for col in self.columns: if first_length is None: first_length = len(rows[j]) else: if len(rows[j]) != first_length: raise ValueError("Each column must have the same number of rows inserted. Row %d has not the same length as Row 0" % j ) new_data.append( concatenate([col.data[0:i], rows[j], col.data[i:]]) ) j += 1 # assign new data j = 0 for col in self.columns: col.data = new_data[j] j += 1 self.update_rows()
def iahistogram(f): """ o Purpose Image histogram. o Synopsis h = iahistogram(f) o Input f: o Output h: o Description o Examples f = iaread('woodlog.pgm') iashow(f) h = iahistogram(f) g,d = iaplot(h) g('set data style boxes') g.plot(d) showfig(h) """ from Numeric import asarray,searchsorted,sort,ravel,concatenate,product f = asarray(f) n = searchsorted(sort(ravel(f)), range(max(ravel(f))+1)) n = concatenate([n, [product(f.shape)]]) h = n[1:]-n[:-1] return h
def atom_array_of_part(part): "Return an Array of all atoms in the Part. Try to be linear time." res = [] for m in part.molecules: res.append( array(m.atlist) ) # concatenate might fail for chunks with exactly 1 atom -- fix later ####@@@@ return concatenate(res) ###k
def fingerprint(self, which_blocks='quartets', include_mean=True,\ normalize=True): """Returns fingerprint data for fingerprint plots. which_blocks: whether to include only the usual 4-codon quartets (the default), the split blocks only, or all blocks. include_mean: whether to include the mean (True). normalize: whether to normalize so that the quartets sum to 1 (True) """ if which_blocks == 'split': blocks = self.SplitBlocks elif which_blocks == 'quartets': blocks = self.SingleAABlocks elif which_blocks == 'all': blocks = self.Blocks else: raise "Got invalid option %s for which_blocks:\n" % which_blocks+\ " (valid options: 'split', 'quartets', 'all')." result = [] for b in blocks: #iterates over doublet string U, C, A, G = [self[b+i] for i in 'UCAG'] all = U+C+A+G if G+C: g_ratio = G/(G+C) else: g_ratio = 0.5 if A+U: a_ratio = A/(A+U) else: a_ratio=0.5 result.append([g_ratio, a_ratio, all]) result = array(result) if normalize: #make the shown bubbles sum to 1 sum_ = sum(result[:,-1]) if sum_: result[:,-1] /= sum_ if include_mean: #calculate mean from all codons third = self.positionalBases().Third U, C, A, G = [third[i] for i in 'UCAG'] if G+C: g_ratio = G/(G+C) else: g_ratio = 0.5 if A+U: a_ratio = A/(A+U) else: a_ratio=0.5 result = concatenate((result, array([[g_ratio,a_ratio,1]]))) return result
def Translate(array, translation): """Method for translating an array used by Simulations.Dacapo.Grid""" from Numeric import concatenate, take import copy newarray = array size = array.shape for dim in range(len(translation)): axis = dim - len(translation) newarray = concatenate( (take(newarray, range(translation[dim], size[axis]), axis), take(newarray, range(translation[dim]), axis)), axis) # the array is copied to make it contiguous return copy.copy(newarray)
def UpdateVTKData(self): """Rereads the vtkPolyData""" from Numeric import concatenate,array,dot import copy # The null vector is omitted, can not be plotted # concatenating vectors and positions to a single list vecpos=concatenate((self.GetPositions(),self.GetVectors()),1) filtervecpos=array(filter(lambda vecpos,dot=dot:dot(vecpos[3:6],vecpos[3:6])!=0,vecpos)) if len(filtervecpos)==0: raise ValueError, "All vectors are null-vectors" else: # Unpacking the filtered list, copied to be contiguous filterpos=copy.copy(filtervecpos[:,0:3]) filtervec=copy.copy(filtervecpos[:,3:6]) # The vectors scaled by scale self.GetVTKData().ReadFromPositionsVectors(filterpos,self.GetScale()*filtervec)
def UpdateVTKData(self): """Rereads the vtkPolyData""" from Numeric import concatenate, array, dot import copy # The null vector is omitted, can not be plotted # concatenating vectors and positions to a single list vecpos = concatenate((self.GetPositions(), self.GetVectors()), 1) filtervecpos = array( filter(lambda vecpos, dot=dot: dot(vecpos[3:6], vecpos[3:6]) != 0, vecpos)) if len(filtervecpos) == 0: raise ValueError, "All vectors are null-vectors" else: # Unpacking the filtered list, copied to be contiguous filterpos = copy.copy(filtervecpos[:, 0:3]) filtervec = copy.copy(filtervecpos[:, 3:6]) # The vectors scaled by scale self.GetVTKData().ReadFromPositionsVectors( filterpos, self.GetScale() * filtervec)
def RepeatArray(array, periods): """Method for repeating an array according to periods This method is used for repeating an array according to the specified periods. 'periods' should be a sequence having the same length as the number of indices in 'array'. **An example:** To repeat an array having three axis by (2,1,3) use: 'RepeatArray(array,(2,1,3))' """ from Numeric import concatenate import copy repeatarray = copy.copy(array) for i in range(len(periods)): repeatarray = concatenate(periods[i] * [repeatarray], axis=i) # the array is copied to make it contiguous return copy.copy(repeatarray)
def iagaussian(s,mu,sigma): """ o Purpose Generate a 2D Gaussian image. o Synopsis g = iagaussian(s,mu,sigma) o Input s: [rows columns] mu: Mean vector. 2D point (x;y). Point of maximum value. sigma: covariance matrix (square). [ Sx^2 Sxy; Syx Sy^2] o Output g: o Description A 2D Gaussian image is an image with a Gaussian distribution. It can be used to generate test patterns or Gaussian filters both for spatial and frequency domain. The integral of the gaussian function is 1.0. o Examples import Numeric f = iagaussian([8,4], [3,1], [[1,0],[0,1]]) print Numeric.array2string(f, precision=4, suppress_small=1) g = ianormalize(f, [0,255]).astype(Numeric.UnsignedInt8) print g f = iagaussian(100, 50, 10*10) g = ianormalize(f, [0,1]) g,d = iaplot(g) showfig(g) f = iagaussian([50,50], [25,10], [[10*10,0],[0,20*20]]) g = ianormalize(f, [0,255]).astype(Numeric.UnsignedInt8) iashow(g) """ from Numeric import asarray,product,arange,NewAxis,transpose,matrixmultiply,reshape,concatenate,resize,sum,zeros,Float,ravel,pi,sqrt,exp from LinearAlgebra import inverse,determinant if type(sigma).__name__ in ['int', 'float', 'complex']: sigma = [sigma] s, mu, sigma = asarray(s), asarray(mu), asarray(sigma) if (product(s) == max(s)): x = arange(product(s)) d = x - mu if len(d.shape) == 1: tmp1 = d[:,NewAxis] tmp3 = d else: tmp1 = transpose(d) tmp3 = tmp1 if len(sigma) == 1: tmp2 = 1./sigma else: tmp2 = inverse(sigma) k = matrixmultiply(tmp1, tmp2) * tmp3 else: aux = arange(product(s)) x, y = iaind2sub(s, aux) xx = reshape(concatenate((x,y)), (2, product(x.shape))) d = transpose(xx) - resize(reshape(mu,(len(mu),1)), (s[0]*s[1],len(mu))) if len(sigma) == 1: tmp = 1./sigma else: tmp = inverse(sigma) k = matrixmultiply(d, tmp) * d k = sum(transpose(k)) g = zeros(s, Float) aux = ravel(g) if len(sigma) == 1: tmp = sigma else: tmp = determinant(sigma) aux[:] = 1./(2*pi*sqrt(tmp)) * exp(-1./2 * k) return g
def row_gather(self,name): """experimental""" from Numeric import concatenate return concatenate(self[name])
def get_episode_data(ep_data,step=1): from Numeric import array,concatenate,arange data = ep_data[0:len(ep_data):step] indices = [[float(i)] for i in range(0,len(ep_data),step)] return concatenate((array(indices)*1.0,data),axis=1)