def kntins(self, uknots, vknots=None): """Insert new knots into the surface uknots - knots to be inserted along u direction vknots - knots to be inserted along v direction NOTE: No knot multiplicity will be increased beyond the order of the spline""" if len(vknots): # Force the v knot sequence to be a vector in ascending order vknots = numerix.sort(numerix.asarray(vknots, numerix.Float)) if numerix.any(vknots < 0.) or numerix.any(vknots > 1.): raise NURBSError, 'Illegal vknots sequence' coefs = numerix.resize( self.cntrl, (4 * self.cntrl.shape[1], self.cntrl.shape[2])) coefs, self.vknots = bspkntins(self.degree[1], coefs, self.vknots, vknots) self.cntrl = numerix.resize( coefs, (4, self.cntrl.shape[1], coefs.shape[1])) if len(uknots): # Force the u knot sequence to be a vector in ascending order uknots = numerix.sort(numerix.asarray(uknots, numerix.Float)) if numerix.any(uknots < 0.) or numerix.any(uknots > 1.): raise NURBSError, 'Illegal uknots sequence' coefs = numerix.transpose(self.cntrl, (0, 2, 1)) coefs = numerix.resize( coefs, (4 * self.cntrl.shape[2], self.cntrl.shape[1])) coefs, self.uknots = bspkntins(self.degree[0], coefs, self.uknots, uknots) coefs = numerix.resize(coefs, (4, self.cntrl.shape[2], coefs.shape[1])) self.cntrl = numerix.transpose(coefs, (0, 2, 1))
def extractV(self, u): "Extract curve in v-direction at parameter u." if numerix.any(u < 0.) or numerix.any(u > 1.): raise NURBSError, 'Out of parameter range [0,1]' if u == 0.: cntrl = self.cntrl[:, 0, :] knots = self.vknots[:] elif u == 1.: cntrl = self.cntrl[:, -1, :] knots = self.vknots[:] else: uknots = numerix.repeat( numerix.asarray([u], numerix.Float), [self.degree[1] * (self.cntrl.shape[2] + 1)]) coefs = numerix.transpose(self.cntrl, (0, 2, 1)) coefs = numerix.resize( coefs, (4 * self.cntrl.shape[2], self.cntrl.shape[1])) coefs, knots = bspkntins(self.degree[0], coefs, self.uknots, uknots) coefs = numerix.resize(coefs, (4, self.cntrl.shape[2], coefs.shape[1])) cntrl = numerix.transpose(coefs, (0, 2, 1)) i = 0 j = knots[0] for k in knots[1:]: if k == u: break elif k != j: i += 1 j = k return Crv.Crv(cntrl[:, i, :], self.vknots[:])
def extractU(self, v): "Extract curve in u-direction at parameter v." if numerix.any(v < 0.) or numerix.any(v > 1.): raise NURBSError, 'Out of parameter range [0,1]' if v == 0.: cntrl = self.cntrl[:, :, 0] knots = self.uknots[:] elif v == 1.: cntrl = self.cntrl[:, :, -1] knots = self.uknots[:] else: vknots = numerix.repeat( numerix.asarray([v], numerix.Float), [self.degree[0] * (self.cntrl.shape[1] + 1)]) coefs = numerix.resize( self.cntrl, (4 * self.cntrl.shape[1], self.cntrl.shape[2])) coefs, knots = bspkntins(self.degree[1], coefs, self.vknots, vknots) cntrl = numerix.resize(coefs, (4, self.cntrl.shape[1], coefs.shape[1])) i = 0 j = knots[0] for k in knots[1:]: if k == v: break elif k != j: i += 1 j = k return Crv.Crv(cntrl[:, :, i], self.uknots[:])
def extractV(self, u): "Extract curve in v-direction at parameter u." if numerix.any(u < 0.) or numerix.any(u > 1.): raise NURBSError, 'Out of parameter range [0,1]' if u == 0.: cntrl = self.cntrl[:,0,:] knots = self.vknots[:] elif u == 1.: cntrl = self.cntrl[:,-1,:] knots = self.vknots[:] else: uknots = numerix.repeat(numerix.asarray([u], numerix.Float),[self.degree[1]*(self.cntrl.shape[2] + 1)]) coefs = numerix.transpose(self.cntrl,(0, 2, 1)) coefs = numerix.resize(coefs,(4*self.cntrl.shape[2], self.cntrl.shape[1])) coefs, knots = bspkntins(self.degree[0], coefs, self.uknots, uknots) coefs = numerix.resize(coefs, (4, self.cntrl.shape[2], coefs.shape[1])) cntrl = numerix.transpose(coefs,(0,2,1)) i = 0 j = knots[0] for k in knots[1:]: if k == u: break elif k != j: i += 1 j = k return Crv.Crv(cntrl[:,i,:], self.vknots[:])
def kntins(self, uknots): """Insert new knots into the curve NOTE: No knot multiplicity will be increased beyond the order of the spline""" if len(uknots): uknots = numerix.sort(numerix.asarray(uknots, numerix.Float)) if numerix.any(uknots < 0.) or numerix.any(uknots > 1.): raise NURBSError, 'NURBS curve parameter out of range [0,1]' self.cntrl, self.uknots = bspkntins(self.degree, self.cntrl, self.uknots, uknots)
def cluster( self, n_clusters, weight=1.13, converged=1e-11, aMask=None, force=0 ): """ Calculate new clusters. @param n_clusters: number of clusters @type n_clusters: int @param weight: fuzziness weigth @type weight: float (default: 1.13) @param converged: stop iteration if min dist changes less than converged (default: 1e-11) @type converged: float @param aMask: atom mask applied before clustering @type aMask: [1|0] @param force: re-calculate even if parameters haven't changed (default:0) @type force: 1|0 """ if aMask == None: aMask = N.ones( self.traj.getRef().lenAtoms() ) if self.fc == None or force or self.fcWeight != weight \ or self.n_clusters != n_clusters or N.any( self.aMask != aMask) \ or self.fcConverged != converged: self.n_clusters = n_clusters self.fcWeight = weight self.aMask = aMask self.fc = FuzzyCluster( self.__raveled(), self.n_clusters, self.fcWeight ) self.fcCenters = self.fc.go( self.fcConverged, 1000, nstep=10, verbose=self.verbose )
def __atom2residueMatrix( self, m ): """ Reduce binary matrix of n x k atoms to binary matrix of i x j residues. @param m: atom contact matrix, array n x k with 1(contact) or 0(no contact) @type m: array @return: residue contact matrix, 2-D numpy array(residues_receptor x residues_ligand) @rtype: array """ recInd = N.concatenate((self.rec().resIndex(), [ self.rec().lenAtoms()] )) ligInd = N.concatenate((self.lig_model.resIndex(), [ self.lig_model.lenAtoms() ] )) residueMatrix = N.zeros(( len(recInd)-1, len(ligInd)-1 ), N.Int) for r in range( len(recInd)-1 ): for l in range( len(ligInd)-1 ): res2res = m[ int(recInd[r]):int(recInd[r+1]), int(ligInd[l]):int(ligInd[l+1]) ] if N.any( res2res ): residueMatrix[r, l] = 1 return residueMatrix
def pnt4D(self, ut, vt=None): """Evaluate parametric point[s] and return 4D homogeneous coordinates. If only ut is given then we will evaluate at scattered points. ut(0,:) represents the u direction. ut(1,:) represents the v direction. If both parameters are given then we will evaluate over a [u,v] grid.""" ut = numerix.asarray(ut, numerix.Float) if numerix.any(ut < 0.) or numerix.any(ut > 1.): raise NURBSError, 'NURBS curve parameter out of range [0,1]' if vt: #FIX! # Evaluate over a [u,v] grid vt = numerix.asarray(vt, numerix.Float) if numerix.any(vt < 0.) or numerix.any(vt > 1.): raise NURBSError, 'NURBS curve parameter out of range [0,1]' val = numerix.resize( self.cntrl, (4 * self.cntrl.shape[1], self.cntrl.shape[2])) val = bspeval(self.degree[1], val, self.vknots, vt) val = numerix.resize(val, (4, self.cntrl.shape[1], vt.shape[0])) val = numerix.transpose(val, (0, 2, 1)) val = numerix.resize(self.cntrl, (4 * vt.shape[0], self.cntrl.shape[1])) val = bspeval(self.degree[0], val, self.uknots, ut) val = numerix.resize(val, (4, vt.shape[0], ut.shape[0])) return numerix.transpose(val, (0, 2, 1)) # Evaluate at scattered points nt = ut.shape[1] uval = numerix.resize(self.cntrl, (4 * self.cntrl.shape[1], self.cntrl.shape[2])) uval = bspeval(self.degree[1], uval, self.vknots, ut[1, :]) uval = numerix.resize(uval, (4, self.cntrl.shape[1], nt)) val = numerix.zeros((4, nt), numerix.Float) for v in range(nt): val[:, v] = bspeval( self.degree[0], numerix.resize(uval[:, :, v], (4, self.cntrl.shape[1])), self.uknots, (ut[0, v], ))[:, 0] return val
def pnt4D(self, ut, vt = None): """Evaluate parametric point[s] and return 4D homogeneous coordinates. If only ut is given then we will evaluate at scattered points. ut(0,:) represents the u direction. ut(1,:) represents the v direction. If both parameters are given then we will evaluate over a [u,v] grid.""" ut = numerix.asarray(ut, numerix.Float) if numerix.any(ut < 0.) or numerix.any(ut > 1.): raise NURBSError, 'NURBS curve parameter out of range [0,1]' if vt: #FIX! # Evaluate over a [u,v] grid vt = numerix.asarray(vt, numerix.Float) if numerix.any(vt < 0.) or numerix.any(vt > 1.): raise NURBSError, 'NURBS curve parameter out of range [0,1]' val = numerix.resize(self.cntrl,(4*self.cntrl.shape[1],self.cntrl.shape[2])) val = bspeval(self.degree[1], val, self.vknots, vt) val = numerix.resize(val,(4, self.cntrl.shape[1], vt.shape[0])) val = numerix.transpose(val,(0,2,1)) val = numerix.resize(self.cntrl,(4*vt.shape[0],self.cntrl.shape[1])) val = bspeval(self.degree[0], val, self.uknots, ut) val = numerix.resize(val,(4, vt.shape[0], ut.shape[0])) return numerix.transpose(val,(0,2,1)) # Evaluate at scattered points nt = ut.shape[1] uval = numerix.resize(self.cntrl,(4*self.cntrl.shape[1],self.cntrl.shape[2])) uval = bspeval(self.degree[1],uval,self.vknots,ut[1,:]) uval = numerix.resize(uval,(4, self.cntrl.shape[1], nt)) val = numerix.zeros((4,nt), numerix.Float) for v in range(nt): val[:,v] = bspeval(self.degree[0],numerix.resize(uval[:,:,v],(4,self.cntrl.shape[1])), self.uknots, (ut[0,v],))[:,0] return val
def kntins(self, uknots, vknots = None): """Insert new knots into the surface uknots - knots to be inserted along u direction vknots - knots to be inserted along v direction NOTE: No knot multiplicity will be increased beyond the order of the spline""" if len(vknots): # Force the v knot sequence to be a vector in ascending order vknots = numerix.sort(numerix.asarray(vknots, numerix.Float)) if numerix.any(vknots < 0.) or numerix.any(vknots > 1.): raise NURBSError, 'Illegal vknots sequence' coefs = numerix.resize(self.cntrl,(4*self.cntrl.shape[1], self.cntrl.shape[2])) coefs, self.vknots = bspkntins(self.degree[1], coefs, self.vknots, vknots) self.cntrl = numerix.resize(coefs, (4, self.cntrl.shape[1], coefs.shape[1])) if len(uknots): # Force the u knot sequence to be a vector in ascending order uknots = numerix.sort(numerix.asarray(uknots, numerix.Float)) if numerix.any(uknots < 0.) or numerix.any(uknots > 1.): raise NURBSError, 'Illegal uknots sequence' coefs = numerix.transpose(self.cntrl,(0, 2, 1)) coefs = numerix.resize(coefs,(4*self.cntrl.shape[2], self.cntrl.shape[1])) coefs, self.uknots = bspkntins(self.degree[0], coefs, self.uknots, uknots) coefs = numerix.resize(coefs, (4, self.cntrl.shape[2], coefs.shape[1])) self.cntrl = numerix.transpose(coefs,(0,2,1))
def randomPatches(self, size, n=None, exclude=None, max_overlap=0, exclude_all=None): """ size - int, number of atoms per patch n - int, number of patches (None -> as many as possible, max 100) exclude - [ 1|0 ], don't touch more than |max_overlap| of these atoms (atom mask) max_overlap - int exclude_all - [ 1|0 ], don't touch ANY of these atoms -> [ [ 1|0 ] ], list of atom masks """ if exclude is None: exclude = N.zeros(self.model.lenAtoms(), 'i') if exclude_all is None: exclude_all = N.zeros(self.model.lenAtoms(), 'i') n = n or 500 centers = self.random_translations(n=n, center=self.center) ## start from excluded patch (if given) working outwards origin = centers[0] tabu = exclude_all if not N.any(tabu): tabu = exclude else: origin = self.model.center(mask=tabu) centers = self.orderCenters(centers, origin) r = [] for i in range(n): m = self.patchAround(centers[i], size) if N.sum( m * exclude ) <= max_overlap \ and N.sum( m * exclude_all ) == 0: exclude = exclude + m r += [m] return r
def extractU(self, v): "Extract curve in u-direction at parameter v." if numerix.any(v < 0.) or numerix.any(v > 1.): raise NURBSError, 'Out of parameter range [0,1]' if v == 0.: cntrl = self.cntrl[:,:,0] knots = self.uknots[:] elif v == 1.: cntrl = self.cntrl[:,:,-1] knots = self.uknots[:] else: vknots = numerix.repeat(numerix.asarray([v], numerix.Float),[self.degree[0]*(self.cntrl.shape[1] + 1)]) coefs = numerix.resize(self.cntrl,(4*self.cntrl.shape[1], self.cntrl.shape[2])) coefs, knots = bspkntins(self.degree[1], coefs, self.vknots, vknots) cntrl = numerix.resize(coefs, (4, self.cntrl.shape[1], coefs.shape[1])) i = 0 j = knots[0] for k in knots[1:]: if k == v: break elif k != j: i += 1 j = k return Crv.Crv(cntrl[:,:,i], self.uknots[:])
def cluster(self, n_clusters, weight=1.13, converged=1e-11, aMask=None, force=0): """ Calculate new clusters. @param n_clusters: number of clusters @type n_clusters: int @param weight: fuzziness weigth @type weight: float (default: 1.13) @param converged: stop iteration if min dist changes less than converged (default: 1e-11) @type converged: float @param aMask: atom mask applied before clustering @type aMask: [1|0] @param force: re-calculate even if parameters haven't changed (default:0) @type force: 1|0 """ if aMask == None: aMask = N.ones(self.traj.getRef().lenAtoms()) if self.fc == None or force or self.fcWeight != weight \ or self.n_clusters != n_clusters or N.any( self.aMask != aMask) \ or self.fcConverged != converged: self.n_clusters = n_clusters self.fcWeight = weight self.aMask = aMask self.fc = FuzzyCluster(self.__raveled(), self.n_clusters, self.fcWeight) self.fcCenters = self.fc.go(self.fcConverged, 1000, nstep=10, verbose=self.verbose)
def pnt4D(self, ut): "Evaluate parametric point[s] and return 4D homogeneous coordinates" ut = numerix.asarray(ut, numerix.Float) if numerix.any(ut < 0.) or numerix.any(ut > 1.): raise NURBSError, 'NURBS curve parameter out of range [0,1]' return bspeval(self.degree, self.cntrl, self.uknots, ut)