def corrcoef(*args): """ corrcoef(X) where X is a matrix returns a matrix of correlation coefficients for each row of X. corrcoef(x,y) where x and y are vectors returns the matrix or correlation coefficients for x and y. Numeric arrays can be real or complex The correlation matrix is defined from the covariance matrix C as r(i,j) = C[i,j] / (C[i,i]*C[j,j]) """ if len(args) == 2: X = transpose(array([args[0]] + [args[1]])) elif len(args == 1): X = args[0] else: raise RuntimeError, 'Only expecting 1 or 2 arguments' C = cov(X) d = resize(diagonal(C), (2, 1)) r = divide(C, sqrt(matrixmultiply(d, transpose(d))))[0, 1] try: return r.real except AttributeError: return r
def kfupdate(self, dt, rs): self.ab = array((rs.ax, -rs.ay, -rs.az)) ph = self.phi th = self.theta P = self.pmat A = array(((-rs.q*cos(ph)*tan(th)+rs.r*sin(ph)*tan(th), (-rs.q*sin(ph)+rs.r*cos(ph))/(cos(th)*cos(th))), (rs.q*sin(ph)-rs.r*cos(ph) , 0))) dph = rs.p - rs.q*sin(ph)*tan(th) - rs.r*cos(ph)*tan(th) dth = - rs.q*cos(ph) - rs.r*sin(ph) dP = dot(A, P) + dot(P, transpose(A)) + self.Q ph = ph + dph * dt th = th + dth * dt P = P + dP * dt Cx = array((0 , cos(th))) Cy = array((-cos(th)*cos(ph), sin(th)*sin(ph))) Cz = array((cos(th)*sin(ph) , sin(th)*cos(ph))) C = array((Cx, Cy, Cz)) L = dot(dot(P, transpose(C)), inverse(self.R + dot(dot(C, P), transpose(C)))) h = array((sin(th), -cos(th)*sin(ph), -cos(th)*cos(ph))) P = dot(identity(2) - dot(L, C), P) ph = ph + dot(L[0], self.ab - h) th = th + dot(L[1], self.ab - h) ph = ((ph+pi) % (2*pi)) - pi; th = ((th+pi) % (2*pi)) - pi; self.pmat = P self.phi = ph self.theta = th psidot = rs.q * sin(ph) / cos(th) + rs.r * cos(ph) / cos(th); self.psi += psidot * dt; self.quat = eul2quat(ph,th,self.psi) # self.dcmb2e = quat2dcm(quatinv(self.quat)) # self.ae = dot(self.dcmb2e, self.ab) self.ae = quatrotate(quatinv(self.quat), self.ab) self.ae[2] = -self.ae[2]-1 self.ae[1] = -self.ae[1] self.ve += self.ae * dt * 9.81 self.xe += self.ve * dt
def render(self): s = self.svgDrawing if not s: return x, y, z = self.body.getPosition() modelview = glGetDoublev(GL_MODELVIEW_MATRIX) projection = glGetDoublev(GL_PROJECTION_MATRIX) viewport = glGetIntegerv(GL_VIEWPORT) # calculate the z coordinate m = transpose(reshape(modelview, (4, 4))) wz = -matrixmultiply(m, reshape((x, y, z, 1), (4, 1)))[2][0] # don't draw anything if we're behind the viewer if wz < 0.1: return # calculate the screen-space x and y coordinates x, y, z = gluProject(x, y, z, modelview, projection, viewport) scale = self.scale / wz s.transform.reset() s.transform.translate(x, y) s.transform.scale(scale, -scale) s.transform.rotate(self.rotation) s.draw()
def ReadFromGrid3DProbeArray(self, grid3D, probearray): """Reads the vtkStructuredGrids from grid3D and probearray""" # Reading from grid3D for iso surfaces self.GetGrid3DReader().ReadFromGrid3D(grid3D) # Reading scalars for probearray # Preparing probearray: # **NOTE** VTK requires the additional axis probearray_vtk = transpose(probearray)[..., NewAxis] self.scalardata = vtkFloatArrayFromNumPyArray( copy.copy(probearray_vtk)) #self.scalardata=vtkScalarsFromArray(transpose(probearray)) self.GetvtkStructuredGridProbe().GetPointData().SetScalars( self.scalardata.GetvtkFloatArray()) # Inserting the points self.GetvtkStructuredGridProbe().SetPoints( self.GetGrid3DReader().GetvtkStructuredGrid().GetPoints()) # and setting the dimension N1, N2, N3 = grid3D.GetSpatialShape() self.GetvtkStructuredGridProbe().SetDimensions(N1, N2, N3) # Update the UpdateExtent of the probe # This is for some (unknown) reason not done during the # update of the probe self.GetvtkStructuredGridProbe().UpdateInformation() self.GetvtkStructuredGridProbe().SetUpdateExtentToWholeExtent()
def learn_step(self, input, output): if rank(input) == 1: input = reshape(input, (self.num_inputs, 1)) if rank(output) == 1: output = reshape(output, (self.num_outputs, 1)) result = self(input) err = output - result self.MSE = norm(err.flat**2) / self.num_outputs self.debug("MSE =", self.MSE) alpha = self.alpha / sum(input**2) self.w += alpha * transpose(dot(input, transpose(err))) self.debug("update ratio =", norm(self(input) - result) / norm(err))
def learn_step(self,input,output): if rank(input) == 1: input = reshape(input,(self.num_inputs,1)) if rank(output) == 1: output = reshape(output,(self.num_outputs,1)) result = self(input) err = output - result self.MSE = norm(err.flat**2)/self.num_outputs self.debug("MSE =",self.MSE) alpha = self.alpha/sum(input**2) self.w += alpha*transpose(dot(input,transpose(err))) self.debug( "update ratio =", norm(self(input)-result)/norm(err))
def InnerProduct(self,vector,other): """Innerproduct for vectors""" if vector.GetSpace()==other.GetSpace(): from Numeric import matrixmultiply,transpose # (v,o)=(v_coor^T*basis^T*basis*o_coor) metric=matrixmultiply(self.GetBasis(),transpose(self.GetBasis())) return dot(vector.GetCoordinates(),matrixmultiply(metric,other.GetCoordinates())) else: raise ValueError, 'Innerproduct of two vectors in different vector space not implemented'
def GetReciprocalBravaisLattice(self): from LinearAlgebra import inverse from Numeric import transpose,pi import copy reciprocal=copy.copy(self) # Using : rec=2pi*(unitcell^T)^-1 recbasis=2*pi*inverse(transpose(self.GetBasis())) reciprocal.SetBasis(recbasis) return reciprocal
def GetReciprocalBravaisLattice(self): from LinearAlgebra import inverse from Numeric import transpose, pi import copy reciprocal = copy.copy(self) # Using : rec=2pi*(unitcell^T)^-1 recbasis = 2 * pi * inverse(transpose(self.GetBasis())) reciprocal.SetBasis(recbasis) return reciprocal
def prettyPrint(self, include_header=False, transpose_data=False): """Returns a string method of the data and character order. include_header: include charcter order or not transpose_data: data as is (rows are positions) or transposed (rows are characters) to line it up with an alignment """ h = self.CharOrder d = self.Data result = [] if include_header and not transpose_data: r = [h]+d.tolist() elif include_header and transpose_data: r =[[x] + y for x,y in zip(h,transpose(d).tolist())] elif transpose_data: r = transpose(d) else: r = d return '\n'.join(['\t'.join(map(str,i)) for i in r])
def get_trial_avg(trial_data,step=1): from Numeric import concatenate,array,transpose from plastk.utils import stats trial_data = get_trial_data(trial_data,step) indices = trial_data[0][:,0] data = array([x[:,1] for x in trial_data]) mean,var,stderr = stats(data) return transpose(array((indices, mean,stderr)))
def InnerProduct(self, vector, other): """Innerproduct for vectors""" if vector.GetSpace() == other.GetSpace(): from Numeric import matrixmultiply, transpose # (v,o)=(v_coor^T*basis^T*basis*o_coor) metric = matrixmultiply(self.GetBasis(), transpose(self.GetBasis())) return dot(vector.GetCoordinates(), matrixmultiply(metric, other.GetCoordinates())) else: raise ValueError, 'Innerproduct of two vectors in different vector space not implemented'
def detrend_linear(x): """Remove the best fit line from x""" # I'm going to regress x on xx=range(len(x)) and return # x - (b*xx+a) # xx = arange(len(x), typecode=x.typecode()) xx = arange(len(x), type(x)) X = transpose(array([xx] + [x])) C = cov(X) b = C[0, 1] / C[0, 0] a = mean(x) - b * mean(xx) return x - (b * xx + a)
def Repeat(self,periods): """Returns a repeated grid This method can be used used to repeat the grid an integer number of times according to the unit cell. Note that the unit cell in the returned grid will also be expanded according to 'periods' . """ from ArrayTools import RepeatArray from Numeric import transpose,asarray a=copy.copy(self) # Finding the new grid valueorder=len(self.GetShape())-len(self.GetSpatialShape()) # Only the spatial coordinates are repeated a.SetArray(RepeatArray(a.GetArray(),valueorder*[1]+periods)) # Finding the new space newspace=copy.copy(self.GetSpace()) newspace.SetBasis(transpose(transpose(self.GetSpace().GetBasis())*asarray(periods))) a.SetSpace(newspace) return a
def __init__(self, parent, filename, title=""): self.parent = parent self.filename = filename self.title = title or filename self.fP = filename + "PCoC.png" self.fS = filename + "SimRel.png" self.fF = filename + "FFT.png" if not (os.path.exists(self.fP) or os.path.exists(self.fS) or os.path.exists(self.fF)): self.soundinfo = SoundInfo(filename) self.vPCoC = self.thingy(self.fP, transpose(self.soundinfo.PC_cepstr.arr)) self.vSimrel = self.thingy(self.fS, self.soundinfo.simrel.arr) self.vFFT = self.thingy(self.fF, transpose(self.soundinfo.fft)) else: self.vPCoC = self.thingy(self.fP) self.vSimrel = self.thingy(self.fS) self.vFFT = self.thingy(self.fF)
def matrix_putting_axis_at_z(axis): #bruce 060608 """ Return an orthonormal matrix which can be used (via dot(points, matrix) for a Numeric array of 3d points) to transform points so that axis transforms to the z axis. (Not sure if it has determinant 1 or -1. If you're sure, change it to always be determinant 1.) """ # this code was modified from ops_select.py's findAtomUnderMouse, as it prepares to call Chunk.findAtomUnderMouse z = norm(axis) x = arbitrary_perpendicular(axis) y = cross(z,x) matrix = transpose(V(x,y,z)) return matrix
def ReadFromGrid3D(self, grid3D): """Reads the vtkStructuredGrid from grid3D""" # Inserting the points # the grid is copied to make it contiguous and transposed # to make the flattened order: x0,y0,z0,x1,y1,z1,... self.pointdata = vtkPointsFromArray( copy.copy(transpose(grid3D.GetCartesianCoordinates()))) self.GetvtkStructuredGrid().SetPoints(self.pointdata.GetvtkPoints()) # Inserting the scalars #self.scalardata=vtkScalarsFromArray(transpose(grid3D.GetGridValues())) # Preparing input scalars: # **NOTE** VTK requires the final axis scalars = transpose(grid3D.GetGridValues())[..., NewAxis] self.scalardata = vtkFloatArrayFromNumPyArray(copy.copy(scalars)) self.GetvtkStructuredGrid().GetPointData().SetScalars( self.scalardata.GetvtkFloatArray()) # Setting the dimensions of the grid N1, N2, N3 = grid3D.GetSpatialShape() self.GetvtkStructuredGrid().SetDimensions(N1, N2, N3)
def matrix_putting_axis_at_z(axis): #bruce 060608 """ Return an orthonormal matrix which can be used (via dot(points, matrix) for a Numeric array of 3d points) to transform points so that axis transforms to the z axis. (Not sure if it has determinant 1 or -1. If you're sure, change it to always be determinant 1.) """ # this code was modified from ops_select.py's findAtomUnderMouse, as it prepares to call Chunk.findAtomUnderMouse z = norm(axis) x = arbitrary_perpendicular(axis) y = cross(z, x) matrix = transpose(V(x, y, z)) return matrix
def ReadFromArray(self, array): """Reads the vtkPolyData from 3x3 array""" # Calculating the position of the corners edges = [[0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 0], [0, 0, 1], [1, 0, 1], [0, 1, 1], [1, 1, 1]] corners = map(lambda edge, cell=array, mul=matrixmultiply, transpose= transpose: mul(transpose(cell), edge), edges) # Reading the vtkPoints to the vtkStructuredGrid self.pointdata = vtkPointsFromArray(corners) self.GetvtkStructuredGrid().SetDimensions(2, 2, 2) self.GetvtkStructuredGrid().SetPoints(self.pointdata.GetvtkPoints())
def test_columnDegeneracy(self): """columnDegeneracy: shoudl work as expected""" p1 = self.consensus p1.Data = transpose(p1.Data) p2 = self.not_same_value p2.Data = transpose(p2.Data) self.assertEqual(p1.columnDegeneracy(),[1,1,1,2,1]) self.assertEqual(p1.columnDegeneracy(cutoff=.5),[1,1,1,2,1]) self.assertEqual(p1.columnDegeneracy(cutoff=.75),[1,2,1,3,2]) #when a row seems to add up to the cutoff value, it's not #always found because of floating point error. E.g. second row #in this example self.assertEqual(p1.columnDegeneracy(cutoff=1),[2,4,1,4,2]) #when the cutoff can't be found, the number of rows in the #profile is returned (for each column) self.assertEqual(p1.columnDegeneracy(cutoff=1.5),[4,4,4,4,4]) self.assertEqual(p2.columnDegeneracy(cutoff=.95),[4,2,4,1]) self.assertEqual(p2.columnDegeneracy(cutoff=1.4),[4,3,4,1]) self.assertEqual(self.empty.columnDegeneracy(),[])
def savitzky_golay(window_size=None,order=2): if window_size is None: window_size = order + 2 if window_size % 2 != 1 or window_size < 1: raise TypeError("window size must be a positive odd number") if window_size < order + 2: raise TypeError("window size is too small for the polynomial") # A second order polynomial has 3 coefficients order_range = range(order+1) half_window = (window_size-1)//2 B = array( [ [k**i for i in order_range] for k in range(-half_window, half_window+1)] ) # -1 # [ T ] T # [ B * B ] * B M = matrixmultiply( inverse( matrixmultiply(transpose(B), B)), transpose(B) ) return M
def test_row_uncertainty(self): """row_uncertainty: should handle pos/neg/zero/empty arrays as expected """ #normal valid array b = transpose(array([[.25,.2,.45,.25,1],[.25,.2,.45,0,0],\ [.25,.3,.05,.75,0],[.25,.3,.05,0,0]])) self.assertFloatEqual(row_uncertainty(b),[2,1.97,1.47,0.81,0],1e-3) #one-dimensional array self.assertRaises(ValueError, row_uncertainty,\ array([.25,.25,.25,.25])) #zeros self.assertEqual(row_uncertainty(array([[0,0]])),array([0])) #empty 2D array self.assertEqual(row_uncertainty(array([[]])),array([0])) self.assertEqual(row_uncertainty(array([[],[]])),array([0,0])) #negative number self.assertNotEqual(row_uncertainty(array([[-2]])),\ row_uncertainty(array([[-2]])))
def transpose_and_output(self, outfname, list_of_top_mas): """ 05-09-05 --ls_NA_fillin() """ sys.stderr.write("Outputing the data...") ls_2d = [] for ma in list_of_top_mas: ls_2d.append(ma.raw_data()) matrix = array(ls_2d) matrix = transpose(matrix) writer = csv.writer(open(outfname, 'w'), delimiter='\t') writer.writerow(matrix.shape) writer.writerow(["column", "column"]+range(len(matrix[0]))) for i in range(matrix.shape[0]): ls_with_NA_filled = self.ls_NA_fillin(matrix[i]) writer.writerow([i, i]+ls_with_NA_filled) sys.stderr.write("Done.\n")
def run(self): "Superimpose the coordinate sets." if self.coords is None or self.reference_coords is None: raise Exception, "No coordinates set." coords=self.coords reference_coords=self.reference_coords # center on centroid av1=sum(coords)/self.n av2=sum(reference_coords)/self.n coords=coords-av1 reference_coords=reference_coords-av2 # correlation matrix a=matrixmultiply(transpose(coords), reference_coords) u, d, vt=singular_value_decomposition(a) self.rot=transpose(matrixmultiply(transpose(vt), transpose(u))) # check if we have found a reflection if determinant(self.rot)<0: vt[2]=-vt[2] self.rot=transpose(matrixmultiply(transpose(vt), transpose(u))) self.tran=av2-matrixmultiply(av1, self.rot)
def refmat(p,q): """ Return a (left multiplying) matrix that mirrors p onto q. Example: >>> mirror=refmat(p,q) >>> qq=p.left_multiply(mirror) >>> print q, qq # q and qq should be the same @type p,q: L{Vector} @return: The mirror operation, a 3x3 Numeric array. """ p.normalize() q.normalize() if (p-q).norm()<1e-5: return eye(3) pq=p-q pq.normalize() b=pq.get_array() b.shape=(3, 1) i=eye(3) ref=i-2*matrixmultiply(b, transpose(b)) return ref
def readSegy(filename): # printverbose("readSegy : Trying to read "+filename,0) data = open(filename).read() filesize=len(data) SH=getSegyHeader(filename) bps=getBytePerSample(SH) ntraces = (filesize-3600)/(SH['ns']*bps+240) # ntraces = 100 printverbose("readSegy : Length of data : " + str(filesize),2) SH["ntraces"]=ntraces; ndummy_samples=240/bps printverbose("readSegy : ndummy_samples="+str(ndummy_samples),6) printverbose("readSegy : ntraces=" + str(ntraces) + " nsamples="+str(SH['ns']),2) # GET TRACE index=3600; nd=(filesize-3600)/bps # READ ALL SEGY TRACE HEADRES SegyTraceHeaders = getAllSegyTraceHeaders(SH,data) printverbose("readSegy : reading segy data",2) # READ ALL DATA EXCEPT FOR SEGY HEADER #Data = zeros((SH['ns'],ntraces)) revision=SH["SegyFormatRevisionNumber"] if (revision==256): revision=1 dsf=SH["DataSampleFormat"] DataDescr=SH_def["DataSampleFormat"]["descr"][revision][dsf] printverbose("readSegy : SEG-Y revision = "+str(revision),1) printverbose("readSegy : DataSampleFormat="+str(dsf)+"("+DataDescr+")",1) if (SH["DataSampleFormat"]==1): printverbose("readSegy : Assuming DSF=1, IBM FLOATS",2) Data1 = getValue(data,index,'ibm',endian,nd) elif (SH["DataSampleFormat"]==2): printverbose("readSegy : Assuming DSF=" + str(SH["DataSampleFormat"]) + ", 32bit INT",2) Data1 = getValue(data,index,'l',endian,nd) elif (SH["DataSampleFormat"]==3): printverbose("readSegy : Assuming DSF=" + str(SH["DataSampleFormat"]) + ", 16bit INT",2) Data1 = getValue(data,index,'h',endian,nd) elif (SH["DataSampleFormat"]==5): printverbose("readSegy : Assuming DSF=" + str(SH["DataSampleFormat"]) + ", IEEE",2) Data1 = getValue(data,index,'float',endian,nd) elif (SH["DataSampleFormat"]==8): printverbose("readSegy : Assuming DSF=" + str(SH["DataSampleFormat"]) + ", 8bit CHAR",2) Data1 = getValue(data,index,'B',endian,nd) else: printverbose("readSegy : DSF=" + str(SH["DataSampleFormat"]) + ", NOT SUPORTED",2) Data = Data1[0] printverbose("readSegy : - reshaping",2) Data=reshape(Data,(ntraces,SH['ns']+ndummy_samples)) printverbose("readSegy : - stripping header dummy data",2) Data=Data[:,ndummy_samples:(SH['ns']+ndummy_samples)] printverbose("readSegy : - transposing",2) Data=transpose(Data) # SOMEONE NEEDS TO IMPLEMENT A NICER WAY DO DEAL WITH DSF=8 if (SH["DataSampleFormat"]==8): for i in arange(ntraces): for j in arange(SH['ns']): if Data[i][j]>128: Data[i][j]=Data[i][j]-256 printverbose("readSegy : read data",2) return Data,SH,SegyTraceHeaders
def nipals_arr(X, PCs, threshold, E_matrices): """ @param X: 2-dimensional matrix of number data. @type X: Numeric array @param PCs: Number of Principal Components. @type PCs: int @param threshold: Convergence check value. For checking on convergence to zero (e.g. 0.000001). @type threshold: float @param E_matrices: If E-matrices should be retrieved or not. E-matrices (for each PC) or explained_var (explained variance for each PC). @type E_matrices: bool @return: (Scores, Loadings, E) """ (rows, cols) = shape(X) maxPCs = min(rows, cols) # max number of PCs is min(objects, variables) if maxPCs < PCs: PCs = maxPCs # change to maxPCs if PCs > maxPCs Scores = zeros((rows, PCs), Float) # all Scores (T) Loadings = zeros((PCs, cols), Float) # all Loadings (P) E = X.copy() #E[0] (should already be mean centered) if E_matrices: Error_matrices = zeros((PCs, rows, cols), Float) # all Error matrices (E) else: explained_var = zeros((PCs), Float) tot_explained_var = 0 # total object residual variance for PC[0] (calculating from E[0]) e_tot0 = 0 # for E[0] the total object residual variance is 100% for k in range(rows): e_k = E[k, :]**2 e_tot0 += sum(e_k) t = get_column(E) # extract a column p = zeros((cols), Float) # do iterations (0, PCs) for i in range(PCs): convergence = False ready_for_compare = False E_t = transpose(E) while not convergence: _temp = eigenvalue_vec(t) p = mat_prod( E_t, t) / _temp # ..................................... step 1 _temp = eigenvalue_vec(p)**(-0.5) p = p * _temp # .................................................... step 2 _temp = eigenvalue_vec(p) t = mat_prod( E, p) / _temp # ....................................... step 3 eigenval_new = eigenvalue_vec(t) if not ready_for_compare: ready_for_compare = True else: # ready for convergence check if (eigenval_new - eigenval_old) < threshold * eigenval_new: # ... step 4 convergence = True eigenval_old = eigenval_new remove_tp_prod( E, t, p) # .............................................. step 5 # add Scores and Loadings for PC[i] to the collection of all PCs Scores[:, i] = t Loadings[i, :] = p if E_matrices: # complete error matrix # can calculate object residual variance (row-wise) or variable resiudal variance (column-wise) # total residual variance can also be calculated Error_matrices[i] = E.copy() else: # total object residual variance for E[i] e_tot = 0 for k in range(rows): e_k = E[k, :]**2 e_tot += sum(e_k) tot_obj_residual_var = (e_tot / e_tot0) explained_var[i] = 1 - tot_obj_residual_var - tot_explained_var tot_explained_var += explained_var[i] if E_matrices: return Scores, Loadings, Error_matrices else: return Scores, Loadings, explained_var
print v4 print calc_angle(v1, v2, v3) dih=calc_dihedral(v1, v2, v3, v4) # Test dihedral sign assert(dih>0) print "DIHEDRAL ", dih ref=refmat(v1, v3) rot=rotmat(v1, v3) print v3 print v1.left_multiply(ref) print v1.left_multiply(rot) print v1.right_multiply(transpose(rot)) # - print v1-v2 print v1-1 print v1+(1,2,3) # + print v1+v2 print v1+3 print v1-(1,2,3) # * print v1*v2 # / print v1/2 print v1/(1,2,3) # **
def ACL(tree): """Returns a normalized dictionary of sequence weights {seq_id: weight} tree: a PhyloNode object The ACL method is named after Altschul, Carroll and Lipman, who published a paper on sequence weighting in 1989. The ACL method is based on an idea of Felsenstein (1973). Imagine electrical current flows from the root of the tree down the edges and out the leaves. If the edge lengths are proportional to their electrical resistances, current flowing out each leaf equals the leaf weight. The first step in the calculation of the weight vector is calculating a variance-covariance matrix. The variance of a leaf is proportional to the distance from the root to that leaf. The covariance of two leaves is proportional to the distance from the root to the last common ancestor of the two leaves. The second step in the calculation results in a vector of weights. Suppose there are n leaves on the tree. Let i be the vector of size n, all of whose elements are 1.0. The weight vector is calculated as: w = (inverse(M)*i)/(transpose(i)*inverse(M)*i) See Altschul 1989 """ #clip branch lengths to avoid error due to negative or zero branch lengths _clip_branch_lengths(tree) #get a list of sequence IDs (in the order that the tree will be traversed) seqs = [] for n in tree.TerminalDescendants: seqs.append(n.Data) #initialize the variance-covariance matrix m = zeros([len(seqs),len(seqs)],Float64) #calculate (co)variances #variance of a node is defined as the distance from the root to the leaf #covariance of two nodes is defined as the distance from the root to the #last common ancestor of the two leaves. for x in tree.TerminalDescendants: for y in tree.TerminalDescendants: idx_x = seqs.index(x.Data) idx_y = seqs.index(y.Data) if idx_x == idx_y: m[idx_x,idx_y] = x.distance(tree) else: lca = x.lastCommonAncestor(y) dist_lca_root = lca.distance(tree) m[idx_x,idx_y] = dist_lca_root m[idx_y,idx_x] = dist_lca_root #get the inverse of the variance-covariance matrix inv = inverse(m) #build vector i (vector or ones, length = # of leaves in the tree) i = ones(len(seqs),Float64) numerator = matrixmultiply(inv, i) denominator = matrixmultiply(matrixmultiply(transpose(i),inv),i) weight_vector = numerator/denominator #return a Weights object (is dict {seq_id: weight}) return Weights(dict(zip(seqs,weight_vector)))
rowMatrix=[0]*len(HelixPoints) while r<q: point2=HelixPoints[r] lineH2=linesH[point2] xH2=float(lineH2[30:38].strip())/apix yH2=float(lineH2[38:46].strip())/apix zH2=float(lineH2[46:54].strip())/apix H1H2Length=math.sqrt((xH1-xH2)**2+(yH1-yH2)**2+(zH1-zH2)**2) rowMatrix[r]=H1H2Length r=r+1 # print rowMatrix distMatrix[q]=rowMatrix q=q+1 outPdbHelix.close() TdistMatrix=transpose(distMatrix) bigMatrix=distMatrix+TdistMatrix s=0 while s<len(HelixPoints): t=0 # print "Within %f of %d: "%(dist, atomArray[s]) while t<len(bigMatrix[s]): if bigMatrix[s][t]<=dist: PointMatrix[s][t]=atomArray[t] # print atomArray[t],bigMatrix[s][t] else: PointMatrix[s][t]=0 t=t+1 # print PointMatrix[s] s=s+1
return polyhedron # == helper definitions for selection_polyhedron [moved here from drawer.py by bruce 060119] # mat mult by rowvector list and max/min reduce to find extrema D = math.sqrt(2.0)/2.0 T = math.sqrt(3.0)/3.0 # 0 1 2 3 4 5 6 7 8 9 10 11 12 # 13 14 15 16 17 18 19 20 21 22 23 24 25 polyXmat = A([[1, 0, 0, D, D, D, D, 0, 0, T, T, T, T], [0, 1, 0, D, -D, 0, 0, D, D, T, T, -T, -T], [0, 0, 1, 0, 0, D, -D, D, -D, T, -T, T, -T]]) del D, T polyMat = cat(transpose(polyXmat),transpose(polyXmat)) polyTab = [( 9, (0,7,3), [3,0,5,2,7,1,3]), (10, (0,1,4), [3,1,8,15,6,0,3]),#(10, (0,4,1), [3,0,6,15,8,1,3]), (11, (8,11,7), [4,14,21,2,5,0,4]), (12, (8,4,9), [4,0,6,15,20,14,4]), (22, (5,10,9), [18,13,16,14,20,15,18]), (23, (10,6,11), [16,13,19,2,21,14,16]), (24, (1,2,5), [8,1,17,13,18,15,8]), (25, (3,6,2), [7,2,19,13,17,1,7])] #( 9, (0,7,3), [3,0,5,2,7,1,3]), #(10, (0,1,4), [3,1,8,15,6,0,3]), #(11, (8,11,7), [4,14,21,2,5,0,4]), #(12, (8,4,9), [4,0,6,15,20,14,4]), #(22, (5,10,9), [18,13,16,14,20,15,18]), #(23, (10,6,11), [16,13,19,2,21,14,16]),
def readSegy(filename) : """ Data,SegyHeader,SegyTraceHeaders=getSegyHeader(filename) """ printverbose("readSegy : Trying to read "+filename,0) data = open(filename).read() filesize=len(data) SH=getSegyHeader(filename) bps=getBytePerSample(SH) ntraces = (filesize-3600)/(SH['ns']*bps+240) # ntraces = 100 printverbose("readSegy : Length of data : " + str(filesize),2) SH["ntraces"]=ntraces; ndummy_samples=240/bps printverbose("readSegy : ndummy_samples="+str(ndummy_samples),6) printverbose("readSegy : ntraces=" + str(ntraces) + " nsamples="+str(SH['ns']),2) # GET TRACE index=3600; nd=(filesize-3600)/bps # READ ALL SEGY TRACE HEADRES SegyTraceHeaders = getAllSegyTraceHeaders(SH,data) printverbose("readSegy : reading segy data",2) # READ ALL DATA EXCEPT FOR SEGY HEADER #Data = zeros((SH['ns'],ntraces)) revision=SH["SegyFormatRevisionNumber"] if (revision==100): revision=1 dsf=SH["DataSampleFormat"] DataDescr=SH_def["DataSampleFormat"]["descr"][revision][dsf] printverbose("readSegy : SEG-Y revision = "+str(revision),1) printverbose("readSegy : DataSampleFormat="+str(dsf)+"("+DataDescr+")",1) if (SH["DataSampleFormat"]==1): printverbose("readSegy : Assuming DSF=1, IBM FLOATS",2) Data1 = getValue(data,index,'ibm',endian,nd) elif (SH["DataSampleFormat"]==2): printverbose("readSegy : Assuming DSF=" + str(SH["DataSampleFormat"]) + ", 32bit INT",2) Data1 = getValue(data,index,'l',endian,nd) elif (SH["DataSampleFormat"]==3): printverbose("readSegy : Assuming DSF=" + str(SH["DataSampleFormat"]) + ", 16bit INT",2) Data1 = getValue(data,index,'h',endian,nd) elif (SH["DataSampleFormat"]==5): printverbose("readSegy : Assuming DSF=" + str(SH["DataSampleFormat"]) + ", IEEE",2) Data1 = getValue(data,index,'float',endian,nd) elif (SH["DataSampleFormat"]==8): printverbose("readSegy : Assuming DSF=" + str(SH["DataSampleFormat"]) + ", 8bit CHAR",2) Data1 = getValue(data,index,'B',endian,nd) else: printverbose("readSegy : DSF=" + str(SH["DataSampleFormat"]) + ", NOT SUPORTED",2) Data = Data1[0] printverbose("readSegy : - reshaping",2) Data=reshape(Data,(ntraces,SH['ns']+ndummy_samples)) printverbose("readSegy : - stripping header dummy data",2) Data=Data[:,ndummy_samples:(SH['ns']+ndummy_samples)] printverbose("readSegy : - transposing",2) Data=transpose(Data) # SOMEONE NEEDS TO IMPLEMENT A NICER WAY DO DEAL WITH DSF=8 if (SH["DataSampleFormat"]==8): for i in arange(ntraces): for j in arange(SH['ns']): if Data[i][j]>128: Data[i][j]=Data[i][j]-256 printverbose("readSegy : read data",2)
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 CoordinatesFromCartesianCoordinates(self, coordinates): from LinearAlgebra import solve_linear_equations return solve_linear_equations(transpose(self.GetBasis()), asarray(coordinates))
# == helper definitions for selection_polyhedron [moved here from drawer.py by bruce 060119] # mat mult by rowvector list and max/min reduce to find extrema D = math.sqrt(2.0) / 2.0 T = math.sqrt(3.0) / 3.0 # 0 1 2 3 4 5 6 7 8 9 10 11 12 # 13 14 15 16 17 18 19 20 21 22 23 24 25 polyXmat = A([[1, 0, 0, D, D, D, D, 0, 0, T, T, T, T], [0, 1, 0, D, -D, 0, 0, D, D, T, T, -T, -T], [0, 0, 1, 0, 0, D, -D, D, -D, T, -T, T, -T]]) del D, T polyMat = cat(transpose(polyXmat), transpose(polyXmat)) polyTab = [ (9, (0, 7, 3), [3, 0, 5, 2, 7, 1, 3]), (10, (0, 1, 4), [3, 1, 8, 15, 6, 0, 3]), #(10, (0,4,1), [3,0,6,15,8,1,3]), (11, (8, 11, 7), [4, 14, 21, 2, 5, 0, 4]), (12, (8, 4, 9), [4, 0, 6, 15, 20, 14, 4]), (22, (5, 10, 9), [18, 13, 16, 14, 20, 15, 18]), (23, (10, 6, 11), [16, 13, 19, 2, 21, 14, 16]), (24, (1, 2, 5), [8, 1, 17, 13, 18, 15, 8]), (25, (3, 6, 2), [7, 2, 19, 13, 17, 1, 7]) ] #( 9, (0,7,3), [3,0,5,2,7,1,3]), #(10, (0,1,4), [3,1,8,15,6,0,3]), #(11, (8,11,7), [4,14,21,2,5,0,4]), #(12, (8,4,9), [4,0,6,15,20,14,4]),
def nipals_arr(X, PCs, threshold, E_matrices): """ @param X: 2-dimensional matrix of number data. @type X: Numeric array @param PCs: Number of Principal Components. @type PCs: int @param threshold: Convergence check value. For checking on convergence to zero difference (e.g. 0.000001). @type threshold: float @param E_matrices: If E-matrices should be retrieved or not. E-matrices (for each PC) or explained_var (explained variance for each PC). @type E_matrices: bool @return: (Scores, Loadings, E) """ (rows, cols) = shape(X) maxPCs = min(rows, cols) # max number of PCs is min(objects, variables) if maxPCs < PCs: PCs = maxPCs # change to maxPCs if PCs > maxPCs Scores = zeros((rows, PCs), Float) # all Scores (T) Loadings = zeros((PCs, cols), Float) # all Loadings (P) E = X.copy() #E[0] (should already be mean centered) if E_matrices: Error_matrices = zeros((PCs, rows, cols), Float) # all Error matrices (E) else: explained_var = zeros((PCs), Float) tot_explained_var = 0 # total object residual variance for PC[0] (calculating from E[0]) e_tot0 = 0 # for E[0] the total object residual variance is 100% for k in range(rows): e_k = E[k, :]**2 e_tot0 += sum(e_k) t = get_column(E) # extract a column p = zeros((cols), Float) # do iterations (0, PCs) for i in range(PCs): convergence = False ready_for_compare = False E_t = transpose(E) while not convergence: _temp = vec_inner(t) p = mat_prod(E_t, t) / _temp # ..................................... step 1 _temp = vec_inner(p)**(-0.5) p = p * _temp # .................................................... step 2 _temp = vec_inner(p) t = mat_prod(E, p) / _temp # ....................................... step 3 eigenval_new = vec_inner(t) if not ready_for_compare: ready_for_compare = True else: # ready for convergence check if (eigenval_new - eigenval_old) < threshold*eigenval_new: # ... step 4 convergence = True eigenval_old = eigenval_new; remove_tp_prod(E, t, p) # .............................................. step 5 # add Scores and Loadings for PC[i] to the collection of all PCs Scores[:, i] = t; Loadings[i, :] = p if E_matrices: # complete error matrix # can calculate object residual variance (row-wise) or variable resiudal variance (column-wise) # total residual variance can also be calculated Error_matrices[i] = E.copy() else: # total object residual variance for E[i] e_tot = 0 for k in range(rows): e_k = E[k, :]**2 e_tot += sum(e_k) tot_obj_residual_var = (e_tot / e_tot0) explained_var[i] = 1 - tot_obj_residual_var - tot_explained_var tot_explained_var += explained_var[i] if E_matrices: return Scores, Loadings, Error_matrices else: return Scores, Loadings, explained_var
def CoordinatesFromCartesianCoordinates(self,coordinates): from LinearAlgebra import solve_linear_equations return solve_linear_equations(transpose(self.GetBasis()),asarray(coordinates))