def degelev(self, utimes, vtimes=None): """Degree elevate the surface. utimes - degree elevate utimes along u direction. vtimes - degree elevate vtimes along v direction.""" if vtimes: if vtimes < 0: raise NURBSError, 'Degree must be positive' coefs = numerix.resize( self.cntrl, (4 * self.cntrl.shape[1], self.cntrl.shape[2])) coefs, vknots, nh = bspdegelev(self.degree[1], coefs, self.vknots, vtimes) coefs = coefs[:, :nh + 1] self.vknots = vknots[:nh + self.degree[1] + vtimes + 2] self.degree[1] += vtimes self.cntrl = numerix.resize( coefs, (4, self.cntrl.shape[1], coefs.shape[1])) if utimes: if utimes < 0: raise NURBSError, 'Degree must be positive' coefs = numerix.transpose(self.cntrl, (0, 2, 1)) coefs = numerix.resize( coefs, (4 * self.cntrl.shape[2], self.cntrl.shape[1])) coefs, uknots, nh = bspdegelev(self.degree[0], coefs, self.uknots, utimes) coefs = coefs[:, :nh + 1] self.uknots = uknots[:nh + self.degree[0] + utimes + 2] self.degree[0] += utimes 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 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 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 pnt3D(self, ut, vt = None): """Evaluate parametric point[s] and return 3D cartesian coordinate[s] 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.""" val = self.pnt4D(ut, vt) if len(val.shape) < 3: return val[0:3,:]/numerix.resize(val[-1,:], (3, val.shape[1])) else: #FIX! return val[0:3,:,:]/numerix.resize(val[-1,:,:], (3, val.shape[1], val.shape[2]))
def bezier(self, update = None): "Decompose surface to bezier patches and return overlaping control points." if update or not self._bezier: cntrl = numerix.resize(self.cntrl,(4*self.cntrl.shape[1], self.cntrl.shape[2])) cntrl = bspbezdecom(self.degree[1], cntrl, self.vknots) cntrl = numerix.resize(cntrl, (4, self.cntrl.shape[1], cntrl.shape[1])) temp1 = cntrl.shape[1] temp2 = cntrl.shape[2] cntrl = numerix.transpose(cntrl,(0, 2, 1)) cntrl = numerix.resize(cntrl,(4*temp2, temp1)) cntrl = bspbezdecom(self.degree[0], cntrl, self.uknots) cntrl = numerix.resize(cntrl, (4, temp2, cntrl.shape[1])) self._bezier = numerix.transpose(cntrl,(0, 2, 1)) return self._bezier
def __unmaskedMatrix( self, contacts, rec_mask, lig_mask ): """ Map contacts between selected rec and lig atoms back to all atoms matrix. @param contacts: contact matrix, array sum_rec_mask x sum_lig_mask @type contacts: array @param rec_mask: atom mask @type rec_mask: [1|0] @param lig_mask: atom mask @type lig_mask: [1|0] @return: atom contact matrix, array N_atoms_rec x N_atoms_lig @rtype: array """ l_rec = len( self.rec_model ) l_lig = len( self.lig_model ) ## map contacts back to all atoms matrix r = N.zeros( l_rec * l_lig ) rMask = N.ravel( N.outerproduct( rec_mask, lig_mask ) ) ## (Optimization: nonzero is time consuming step) N.put( r, N.nonzero( rMask ), N.ravel( contacts ) ) return N.resize( r, (l_rec, l_lig))
def reset_utils(self): self.utility = resize(0.0,(self.num_squares_x,self.num_squares_y)); for e in self.pits: self.utility[e] = -1.0 e = self.goal self.utility[e] = 1.0
def __init__(self, classifier, labeled_tokens): """ Entry conf[i][j] is the number of times a document with label i was given label j. """ assert _chktype(1, classifier, ClassifierI) assert _chktype(2, labeled_tokens, [Token], (Token,)) try: import numpy.oldnumeric as Numeric except: raise ImportError("ConfusionMatrix requires Numeric") # Extract the labels. ldict = {} for ltok in labeled_tokens: ldict[ltok.type().label()] = 1 labels = ldict.keys() # Construct a label->index dictionary indices = {} for i in range(len(labels)): indices[labels[i]] = i confusion = Numeric.zeros((len(labels), len(labels))) for ltok in labeled_tokens: utok = Token(ltok.type().text(), ltok.loc()) ctok = classifier.classify(utok) confusion[indices[ltok.type().label()], indices[ctok.type().label()]] += 1 self._labels = labels self._confusion = confusion self._max_conf = max(Numeric.resize(confusion, (len(labels) ** 2,)))
def __init__(self, classifier, labeled_tokens): """ Entry conf[i][j] is the number of times a document with label i was given label j. """ assert _chktype(1, classifier, ClassifierI) assert _chktype(2, labeled_tokens, [Token], (Token, )) try: import numpy.oldnumeric as Numeric except: raise ImportError('ConfusionMatrix requires Numeric') # Extract the labels. ldict = {} for ltok in labeled_tokens: ldict[ltok.type().label()] = 1 labels = ldict.keys() # Construct a label->index dictionary indices = {} for i in range(len(labels)): indices[labels[i]] = i confusion = Numeric.zeros((len(labels), len(labels))) for ltok in labeled_tokens: utok = Token(ltok.type().text(), ltok.loc()) ctok = classifier.classify(utok) confusion[indices[ltok.type().label()], indices[ctok.type().label()]] += 1 self._labels = labels self._confusion = confusion self._max_conf = max(Numeric.resize(confusion, (len(labels)**2, )))
def reset_utils(self): self.utility = resize(0.0, (self.num_squares_x, self.num_squares_y)) for e in self.pits: self.utility[e] = -1.0 e = self.goal self.utility[e] = 1.0
def bounds(self): "Return the boundingbox for the curve" ww = numerix.resize(self.cntrl[-1, :], (3, self.cntrl.shape[1])) cntrl = numerix.sort(self.cntrl[0:3, :] / ww) return numerix.asarray([ cntrl[0, 0], cntrl[1, 0], cntrl[2, 0], cntrl[0, -1], cntrl[1, -1], cntrl[2, -1] ], numerix.Float)
def __call__(image): from gamera.plugins import _string_io pixel_type = image.data.pixel_type shape = (image.nrows, image.ncols) typecode = _typecodes[pixel_type] if pixel_type == RGB: shape += (3,) array = n.fromstring(_string_io._to_raw_string(image), typecode) return n.resize(array, shape)
def __init__(self, receptor_file): from PyBabel.babelElements import babel_elements self.babel_elements = babel_elements self.criteria = "Interactions" Filter.__init__(self, self.criteria) self.receptor_file = receptor_file rptr = open(receptor_file) rec_lines = rptr.readlines() rptr.close() rec_coords = [] rec_vdw_rad = [] for ll in rec_lines: if ll.find("HETATM") > -1 or ll.find("ATOM") > -1: rec_coords.append( [float(ll[30:38]), float(ll[38:46]), float(ll[46:54])]) be_key = strip(ll[76:]) if be_key == 'A': be_key = 'C' elif be_key == 'OA': be_key = 'O' elif be_key == 'NA': be_key = 'N' elif be_key == 'SA': be_key = 'S' elif be_key == 'HD': be_key = 'H' rec_vdw_rad.append(babel_elements[be_key]['vdw_rad']) # lenC and bigC are about the receptor... self.lenC = len(rec_vdw_rad) lenC = self.lenC #len(rec_vdw_rad) #1844 #len(rec_coords) #1844 checkRadii = Numeric.array(rec_vdw_rad, 'f') self.bigRC = Numeric.resize(checkRadii, (lenC, lenC)) #receptor info self.bigC = Numeric.resize(rec_coords, (lenC, lenC, 3))
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 setupCutoff(self, bAts, sAts, cutoff): #len(bAts)>len(sAts) lenC = len(bAts) lenK = len(sAts) #set up sum of radii cutoff matrix: checkRadii = Numeric.array(bAts.vdwRadius, 'f') bigRC = Numeric.resize(checkRadii, (lenC, lenC)) bigR = bigRC[:lenK] keyRadii = Numeric.array(sAts.vdwRadius, 'f') keyRadii.shape = (lenK, 1) smallR = keyRadii cutoff = bigR + smallR return cutoff
def interpolateSmoothArray(self, first, last, nPts): """Insert self.chord-1 points into self.smooth such that each residue is represented by exactly self.nchords points""" # create array to hold new points beg = Numeric.ones((self.nrib, nPts, 4)).astype('f') # set all points to the coordinates of first point in each rib beg = beg * Numeric.reshape(self.smooth[:, first], (self.nrib, 1, 4)) # compute vectors from first to second point for each rib # and divide by number of intervals vec = (self.smooth[:, last] - self.smooth[:, first]) / nPts # resize vector vec1 = Numeric.resize(vec[0], (nPts, 4)) vec2 = Numeric.resize(vec[1], (nPts, 4)) vec = Numeric.concatenate((vec1, vec2)) vec.shape = (self.nrib, nPts, 4) scale = Numeric.arange(nPts) scale.shape = (1, nPts, 1) # add it 0,1,2,3 ... nchord-1 times to the new points return beg + vec * scale
def interpolateSmoothArray(self, first, last, nPts): """Insert self.chord-1 points into self.smooth such that each residue is represented by exactly self.nchords points""" # create array to hold new points beg = Numeric.ones( (self.nrib, nPts, 4) ).astype('f') # set all points to the coordinates of first point in each rib beg = beg * Numeric.reshape( self.smooth[:,first], (self.nrib,1,4) ) # compute vectors from first to second point for each rib # and divide by number of intervals vec = (self.smooth[:,last] - self.smooth[:,first]) / nPts # resize vector vec1 = Numeric.resize( vec[0], (nPts, 4) ) vec2 = Numeric.resize( vec[1], (nPts, 4) ) vec = Numeric.concatenate( (vec1, vec2) ) vec.shape = (self.nrib, nPts, 4) scale = Numeric.arange(nPts) scale.shape = (1,nPts,1) # add it 0,1,2,3 ... nchord-1 times to the new points return beg + vec*scale
def __init__(self, receptor_file): from PyBabel.babelElements import babel_elements self.babel_elements = babel_elements self.criteria = "Interactions" Filter.__init__(self, self.criteria) self.receptor_file = receptor_file rptr = open(receptor_file) rec_lines = rptr.readlines() rptr.close() rec_coords =[] rec_vdw_rad =[] for ll in rec_lines: if ll.find("HETATM")>-1 or ll.find("ATOM")>-1: rec_coords.append([float(ll[30:38]), float(ll[38:46]),float(ll[46:54])]) be_key = strip(ll[76:]) if be_key=='A': be_key = 'C' elif be_key=='OA': be_key = 'O' elif be_key=='NA': be_key = 'N' elif be_key=='SA': be_key = 'S' elif be_key=='HD': be_key = 'H' rec_vdw_rad.append(babel_elements[be_key]['vdw_rad']) # lenC and bigC are about the receptor... self.lenC = len(rec_vdw_rad) lenC = self.lenC #len(rec_vdw_rad) #1844 #len(rec_coords) #1844 checkRadii = Numeric.array(rec_vdw_rad, 'f') self.bigRC = Numeric.resize(checkRadii, (lenC, lenC)) #receptor info self.bigC = Numeric.resize(rec_coords, (lenC,lenC,3))
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 degelev(self, utimes, vtimes = None): """Degree elevate the surface. utimes - degree elevate utimes along u direction. vtimes - degree elevate vtimes along v direction.""" if vtimes: if vtimes < 0: raise NURBSError, 'Degree must be positive' coefs = numerix.resize(self.cntrl,(4*self.cntrl.shape[1], self.cntrl.shape[2])) coefs, vknots, nh = bspdegelev(self.degree[1], coefs, self.vknots, vtimes) coefs = coefs[:,:nh + 1] self.vknots = vknots[:nh + self.degree[1] + vtimes + 2] self.degree[1] += vtimes self.cntrl = numerix.resize(coefs, (4, self.cntrl.shape[1], coefs.shape[1])) if utimes: if utimes < 0: raise NURBSError, 'Degree must be positive' coefs = numerix.transpose(self.cntrl,(0, 2, 1)) coefs = numerix.resize(coefs,(4*self.cntrl.shape[2], self.cntrl.shape[1])) coefs, uknots, nh = bspdegelev(self.degree[0], coefs, self.uknots, utimes) coefs = coefs[:,:nh + 1] self.uknots = uknots[:nh + self.degree[0] + utimes + 2] self.degree[0] += utimes coefs = numerix.resize(coefs, (4, self.cntrl.shape[2], coefs.shape[1])) self.cntrl = numerix.transpose(coefs,(0,2,1))
def getAppxCoef2d_significant(self, arr2d, maxNumCoef, alpha): """Returns[:,j] approx. coeffcients with p-value < alpha; subsequent coef. are equal to 0. Reference: Ott, pp.606. The significance of the coef. estimated by comparing the variance drop of the model2 with the variance of the model1, where: model 1: complete model with maxNumCoef coef. different from 0 model 2: reduced model with coef. in range (0,k) different from 0 null hypothesis (H0): coef. k,k+1,...,maxNumCoef are equal to 0 if H0 rejected (pval below some alpha (e.g. 0.05) -> there exist an important coef. among coef. k,k+1,...,maxNumCoef repeat the test for k=0,1,...maxNumCoef-1 """ assert len(arr2d.shape) == 2, "2d array expected" assert 0 < maxNumCoef <= self.k coefMax = self.getAppxCoef(arr2d, maxNumCoef) curveMax = self.getAppxCurve(coefMax) SSE1 = Numeric.add.reduce((arr2d - curveMax)**2, 1) MSE1 = SSE1 / (arr2d.shape[1] - maxNumCoef) #print "SSE1[0], MSE1[0]",SSE1[0], MSE1[0] pvals = Numeric.zeros((arr2d.shape[0], maxNumCoef), Numeric.Float) for k in range( maxNumCoef): # test cofInd: [maxNum-1, maxNum-2, ..., minNum] #print "Keeping %i coeff" % (k) shpk = list(coefMax.shape) shpk[1] -= k coefk = Numeric.concatenate( (coefMax[:, :k], Numeric.zeros((shpk), Numeric.Float)), 1) curvek = self.getAppxCurve(coefk) SSE2 = Numeric.add.reduce((arr2d - curvek)**2, 1) MSdrop = (SSE2 - SSE1) / (maxNumCoef - k) F = MSdrop / MSE1 #2007-10-11: F -> F.filled(???) pvals[:, k] = scipy.stats.fprob( (maxNumCoef - k), arr2d.shape[1] - maxNumCoef, F.filled(ApproxOrthPolyBasis._F_fillValue)) pvals = Numeric.where( pvals > alpha, Numeric.resize(Numeric.arange(pvals.shape[1]), pvals.shape), pvals.shape[1]) # MAX where significant, idx where nonsignificant firstNonSignIdx = MLab.min(pvals, 1) # idx of the first non-significant coef. coefSign = Numeric.zeros(coefMax.shape, Numeric.Float) for idx in range(coefSign.shape[1]): coefSign[:, idx] = Numeric.where(idx < firstNonSignIdx, coefMax[:, idx], 0) return coefSign
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 setupCutoff(self, bAts, sAts, cutoff): #len(bAts)>len(sAts) lenC = len(bAts) lenK = len(sAts) #set up sum of radii cutoff matrix: checkRadii = Numeric.array(bAts.vdwRadius, 'f') bigRC = Numeric.resize(checkRadii, (lenC, lenC)) bigR = bigRC[:lenK] keyRadii = Numeric.array(sAts.vdwRadius, 'f') keyRadii.shape = (lenK, 1) smallR = keyRadii cutoff = bigR + smallR #create Numeric.ones of appropriate shape * self.const #add it to cutoff constFactor = Numeric.ones(len(bigR.ravel()))*self.constant #print "len(constFactor)=", len(constFactor) constFactor.shape = cutoff.shape #print "constFactor.shape=" , constFactor.shape #print "1:cutoff[0][0]=", cutoff[0][0] cutoff = cutoff+constFactor #print "2:cutoff[0][0]=", cutoff[0][0] return cutoff
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 getAppxCoef2d_significant(self, arr2d, maxNumCoef, alpha): """Returns[:,j] approx. coeffcients with p-value < alpha; subsequent coef. are equal to 0. Reference: Ott, pp.606. The significance of the coef. estimated by comparing the variance drop of the model2 with the variance of the model1, where: model 1: complete model with maxNumCoef coef. different from 0 model 2: reduced model with coef. in range (0,k) different from 0 null hypothesis (H0): coef. k,k+1,...,maxNumCoef are equal to 0 if H0 rejected (pval below some alpha (e.g. 0.05) -> there exist an important coef. among coef. k,k+1,...,maxNumCoef repeat the test for k=0,1,...maxNumCoef-1 """ assert len(arr2d.shape) == 2, "2d array expected" assert 0 < maxNumCoef <= self.k coefMax = self.getAppxCoef(arr2d, maxNumCoef) curveMax = self.getAppxCurve(coefMax) SSE1 = Numeric.add.reduce((arr2d-curveMax)**2,1) MSE1 = SSE1 / (arr2d.shape[1]-maxNumCoef) #print "SSE1[0], MSE1[0]",SSE1[0], MSE1[0] pvals = Numeric.zeros((arr2d.shape[0], maxNumCoef), Numeric.Float) for k in range(maxNumCoef): # test cofInd: [maxNum-1, maxNum-2, ..., minNum] #print "Keeping %i coeff" % (k) shpk = list(coefMax.shape) shpk[1] -= k coefk = Numeric.concatenate((coefMax[:,:k], Numeric.zeros((shpk), Numeric.Float)),1) curvek = self.getAppxCurve(coefk) SSE2 = Numeric.add.reduce((arr2d-curvek)**2,1) MSdrop =(SSE2-SSE1) / (maxNumCoef-k) F = MSdrop / MSE1 #2007-10-11: F -> F.filled(???) pvals[:,k] = scipy.stats.fprob((maxNumCoef-k), arr2d.shape[1]-maxNumCoef, F.filled(ApproxOrthPolyBasis._F_fillValue)) pvals = Numeric.where(pvals > alpha, Numeric.resize(Numeric.arange(pvals.shape[1]),pvals.shape), pvals.shape[1]) # MAX where significant, idx where nonsignificant firstNonSignIdx = MLab.min(pvals, 1) # idx of the first non-significant coef. coefSign = Numeric.zeros(coefMax.shape, Numeric.Float) for idx in range(coefSign.shape[1]): coefSign[:,idx] = Numeric.where(idx<firstNonSignIdx, coefMax[:,idx], 0) return coefSign
def reset_reward(self): self.reward = resize(0.0,(self.num_squares_x,self.num_squares_y)) self.reward[self.goal] = 100 for e in self.pits: self.reward[e] = -50
def pnt3D(self, ut): "Evaluate parametric point[s] and return 3D cartesian coordinate[s]" val = self.pnt4D(ut) return val[0:3, :] / numerix.resize(val[-1, :], (3, val.shape[1]))
def parse_result( self ): """ Extract some information about the profile as well as the match state emmission scores. Keys of the returned dictionary:: 'AA', 'name', 'NrSeq', 'emmScore', 'accession', 'maxAllScale', 'seqNr', 'profLength', 'ent', 'absSum' @return: dictionary with warious information about the profile @rtype: dict """ ## check that the outfut file is there and seems valid if not os.path.exists( self.f_out ): raise HmmerError,\ 'Hmmerfetch result file %s does not exist.'%self.f_out if T.fileLength( self.f_out ) < 10: raise HmmerError,\ 'Hmmerfetch result file %s seems incomplete.'%self.f_out profileDic = {} ## read result hmm = open( self.f_out, 'r') out = hmm.read() hmm.close() ## collect some data about the hmm profile profileDic['name'] = self.hmmName profileDic['profLength'] = \ int( string.split(re.findall('LENG\s+[0-9]+', out)[0])[1] ) profileDic['accession'] = \ string.split(re.findall('ACC\s+PF[0-9]+', out)[0])[1] profileDic['NrSeq'] = \ int( string.split(re.findall('NSEQ\s+[0-9]+', out)[0])[1] ) profileDic['AA'] = \ string.split(re.findall('HMM[ ]+' + '[A-Y][ ]+'*20, out)[0] )[1:] ## collect null emmission scores pattern = 'NULE[ ]+' + '[-0-9]+[ ]+'*20 nullEmm = [ float(j) for j in string.split(re.findall(pattern, out)[0])[1:] ] ## get emmision scores prob=[] for i in range(1, profileDic['profLength']+1): pattern = "[ ]+%i"%i + "[ ]+[-0-9]+"*20 e = [ float(j) for j in string.split(re.findall(pattern, out)[0]) ] prob += [ e ] profileDic['seqNr'] = N.transpose( N.take( prob, (0,),1 ) ) profileDic['emmScore'] = N.array(prob)[:,1:] ## calculate emission probablitities emmProb, nullProb = self.hmmEmm2Prob( nullEmm, profileDic['emmScore']) ent = [ N.resize( self.entropy(e, nullProb), (1,20) )[0] for e in emmProb ] profileDic['ent'] = N.array(ent) ###### TEST ##### proba = N.array(prob)[:,1:] ## # test set all to max score ## p = proba ## p1 = [] ## for i in range( len(p) ): ## p1 += [ N.resize( p[i][N.argmax( N.array( p[i] ) )] , N.shape( p[i] ) ) ] ## profileDic['maxAll'] = p1 # test set all to N.sum( abs( probabilities ) ) p = proba p2 = [] for i in range( len(p) ) : p2 += [ N.resize( N.sum( N.absolute( p[i] )), N.shape( p[i] ) ) ] profileDic['absSum'] = p2 # set all to normalized max score p = proba p4 = [] for i in range( len(p) ) : p_scale = (p[i] - N.average(p[i]) )/ math.SD(p[i]) p4 += [ N.resize( p_scale[N.argmax( N.array(p_scale) )] , N.shape( p[i] ) ) ] profileDic['maxAllScale'] = p4 return profileDic
def __init__(self, root, width, height): Tkinter.Toplevel.__init__(self, root) self.inaccessible = [(-1,-1),\ ( 4, 2),( 5, 2),( 6, 2),( 4, 3),( 5, 3),( 6, 3),\ ( 9, 2),(10, 2),(11, 2),( 9, 3),(10, 3),(11, 3),( 9, 4),(10, 4),(11, 4),\ (13, 1),(14, 1),(13, 2),(14, 2),(13, 3),(14, 3),(13, 4),(14, 4),\ (3,6),(4,6),(5,6),(3,7),(4,7),(5,7),(3,8),(4,8),(5,8),(3,9),(4,9),(5,9),\ (7,6),(8,6), (7,7),(8,7), (7,8),(8,8), \ (6,9),(7,9),(8,9), (6,10),(7,10),(8,10),(9,10),(10,10),(6,11),(7,11),\ (8,11),(9,11),(10,11),(6,12),(7,12),(8,12),(9,12),(6,13),(7,13),(8,13),\ (11,6),(12,6),(13,6),(11,7),(12,7),(13,7),(11,8),(12,8),(13,8),\ (0,11),(1,11),(2,11),(0,12),(1,12),(2,12),(0,13),(1,13),(2,13),(0,14),(1,14),(2,14)] self.path_color = "#5ee563" self.visited_color = "#c5c5c5" self.current_pos_color = "#00AF32" self.inaccessible_color = "black" self.gridline_color = "black" self.background_color = "white" self.path = [] self.visited = [] self.pits = [] self.goal = [] self.goal_id = -1 self.pit_ids = [] # how many states ? self.num_squares_x = 15 self.num_squares_y = 15 self.squares = resize(0, (self.num_squares_x, self.num_squares_y)) # various object members self.done = 0 self.quit = 0 self.root = root self.width = width self.height = height self.complete = 0 # various tk objects self.title("SymbolicSimulator: RLWorld") self.canvas = Tkinter.Canvas(self, width=self.width, height=self.height, bg="black") self.canvas.pack() self.winfo_toplevel().protocol('WM_DELETE_WINDOW', self.destroy) # set height and width of images self.square_height = self.width / self.num_squares_x self.square_width = self.height / self.num_squares_y # goal image goldFilename = pyrobotdir() + "/images/rlgoal.gif" goldImage = Image.open(goldFilename) goldImage = goldImage.resize( [self.square_height - 2, self.square_width - 2]) self.goldImageTk = ImageTk.PhotoImage(goldImage) # pit image pitFilename = pyrobotdir() + "/images/rlpit.gif" pitImage = Image.open(pitFilename) pitImage = pitImage.resize( [self.square_height - 2, self.square_width - 2]) self.pitImageTk = ImageTk.PhotoImage(pitImage, height=self.square_height, width=self.square_width) for i in range(0, self.num_squares_x): for j in range(0, self.num_squares_y): self.squares[i][j] = self.canvas.create_rectangle( i * self.square_width, j * self.square_height, (i + 1) * self.square_width - 1, (j + 1) * self.square_height - 1, fill=self.background_color, tag="square-%d-%d" % (i, j)) # initialize the world self.initWorld() self.resetStates() # used by simulator self.properties = ["location", "obstacles", "goal", "home", \ "final", "visited", "complete", \ "pits", "path"] self.movements = ["up", "right", "down", "left"] self.ports = [60000] # start things off self.redraw() self.drawInaccessible()
def sine_array(hz, peak, sampleRate=44100,length=1.0): #Compute N samples of a sine wave with given frequency and peak amplitude (defaults to one second). return Numeric.resize(sine_array_onecycle(hz, peak), (int(sampleRate*length),))
def bounds(self): "Return the boundingbox for the curve" ww = numerix.resize(self.cntrl[-1,:], (3, self.cntrl.shape[1])) cntrl = numerix.sort(self.cntrl[0:3,:]/ww) return numerix.asarray([cntrl[0,0], cntrl[1,0], cntrl[2,0], cntrl[0,-1], cntrl[1,-1], cntrl[2,-1]], numerix.Float)
def pnt3D(self, ut): "Evaluate parametric point[s] and return 3D cartesian coordinate[s]" val = self.pnt4D(ut) return val[0:3,:]/numerix.resize(val[-1,:], (3, val.shape[1]))
def select(self, keyAts, checkAts, cutoff=3.0, percentCutoff=1.0, keyMat=None, checkMat=None): """ keyAts, checkAts, cutoff, percentCutoff keyAts: first set of atoms checkAts: a second set of atoms which is checked vs. keyAts cutoff: either a single float by default 3.0 or a matrix with shape: (max(len(keyAts),len(checkAts)), min(len(keyAts),len(checkAts))) percentCutoff: by default 1.0 (cutoff is multiplied by this value) keyMat: transformation of keyAts checkMat: transformation of checkAts returns 'pairDict' whose keys are atoms used as reference points and whose values are atoms within cutoff distance of corresponding key. If 'return_dist' flag is set, 'distDict' is returned also, whose keys are the same atoms which are used as reference points and whose values are lists of distances to atoms within cutoff distance of corresponding key """ lenK = len(keyAts) lenC = len(checkAts) #data arrays are used to find atoms with given indices quickly atar = Numeric.array(checkAts.data) keyAtar = Numeric.array(keyAts.data) #basic arrays of coords used to build others c = Numeric.array(checkAts.coords, 'f') if checkMat: c = self.mul(c, checkMat) k = Numeric.array(keyAts.coords, 'f') if keyMat: k = self.mul(k, keyMat) # first build matrix of distances between all pairs of ats # rows correspond to ats in larger set, columns to those in smaller # first build square matrix if lenC >= lenK: bigC = Numeric.resize(c, (lenC, lenC, 3)) k.shape = (lenK,1,3) bigM = bigC[:lenK] smallM = k cutoff = self.setupCutoff(checkAts, keyAts, cutoff) #print "0a:cutoff[0][0]=", cutoff[0][0] cutoff.shape = (lenK, -1) else: bigK = Numeric.resize(k, (lenK, lenK, 3)) c.shape = (lenC,1,3) bigM = bigK[:lenC] smallM = c cutoff = self.setupCutoff(keyAts, checkAts, cutoff) #print "0b:cutoff[0][0]=", cutoff[0][0] cutoff.shape = (lenC, -1) # distance matrix d = bigM - smallM # distance squared matrix dSQ = d * d # next step sums deltaX**2, deltaY**2, deltaZ**2 dSQMAT = Numeric.sum(dSQ,2) #percentCutoff lets user relax sum of radii #the smaller the percentCutoff the smaller the key #dSQ has to be less than cutoff = cutoff * percentCutoff cutoffSQMAT = cutoff * cutoff #cutoffSQMAT = cutoffSQMAT * percentCutoff # ansMat has 1 where sq dist. is smaller than cutoff ansMat = Numeric.logical_and(self.func(dSQMAT, cutoffSQMAT) , \ Numeric.not_equal(dSQMAT, 0.)) if lenK > lenC: # in this case need to rearrange matrix # which got shuffled in if-else above ansMat = Numeric.swapaxes(ansMat, 0, 1) dSQMAT = Numeric.swapaxes(dSQMAT, 0, 1) # finally, build result dictionaries which have atom keys: # pairDict has values which are lists of close atoms # distDict has values which are lists of distances pairDict = {} distDict = {} # get a list of rows which have non-zero entries # to loop over in next section rowIndices = Numeric.nonzero(Numeric.sum(ansMat,1)) # rows correspond to ats in keyAts # columns correspond to ats in checkAts for i in rowIndices: # atindex is a list [7 8 9] indexing into checkAts atindex = Numeric.nonzero(ansMat[i]) # keyAtar[i] is ith atom in keyAts keyAt = keyAtar[i] pairDict[keyAt] = Numeric.take(atar, atindex) if self.return_dist: distDict[keyAt] = [] for ind in atindex: distDict[keyAt].append(math.sqrt(dSQMAT[i][ind])) #getting distDict back is optional if self.return_dist: return pairDict, distDict else: return pairDict
def reset_frequencies(self): self.frequencies = resize(0,(self.num_squares_x,self.num_squares_y))
def reset_frequencies(self): self.frequencies = resize(0, (self.num_squares_x, self.num_squares_y))
def sine_array(hz, peak, sampleRate=44100, length=1.0): #Compute N samples of a sine wave with given frequency and peak amplitude (defaults to one second). return Numeric.resize(sine_array_onecycle(hz, peak), (int(sampleRate * length), ))
def parse_result(self): """ Extract some information about the profile as well as the match state emmission scores. Keys of the returned dictionary:: 'AA', 'name', 'NrSeq', 'emmScore', 'accession', 'maxAllScale', 'seqNr', 'profLength', 'ent', 'absSum' @return: dictionary with warious information about the profile @rtype: dict """ ## check that the outfut file is there and seems valid if not os.path.exists(self.f_out): raise HmmerError,\ 'Hmmerfetch result file %s does not exist.'%self.f_out if T.fileLength(self.f_out) < 10: raise HmmerError,\ 'Hmmerfetch result file %s seems incomplete.'%self.f_out profileDic = {} ## read result hmm = open(self.f_out, 'r') out = hmm.read() hmm.close() ## collect some data about the hmm profile profileDic['name'] = self.hmmName profileDic['profLength'] = \ int( string.split(re.findall('LENG\s+[0-9]+', out)[0])[1] ) profileDic['accession'] = \ string.split(re.findall('ACC\s+PF[0-9]+', out)[0])[1] profileDic['NrSeq'] = \ int( string.split(re.findall('NSEQ\s+[0-9]+', out)[0])[1] ) profileDic['AA'] = \ string.split(re.findall('HMM[ ]+' + '[A-Y][ ]+'*20, out)[0] )[1:] ## collect null emmission scores pattern = 'NULE[ ]+' + '[-0-9]+[ ]+' * 20 nullEmm = [ float(j) for j in string.split(re.findall(pattern, out)[0])[1:] ] ## get emmision scores prob = [] for i in range(1, profileDic['profLength'] + 1): pattern = "[ ]+%i" % i + "[ ]+[-0-9]+" * 20 e = [float(j) for j in string.split(re.findall(pattern, out)[0])] prob += [e] profileDic['seqNr'] = N.transpose(N.take(prob, (0, ), 1)) profileDic['emmScore'] = N.array(prob)[:, 1:] ## calculate emission probablitities emmProb, nullProb = self.hmmEmm2Prob(nullEmm, profileDic['emmScore']) ent = [ N.resize(self.entropy(e, nullProb), (1, 20))[0] for e in emmProb ] profileDic['ent'] = N.array(ent) ###### TEST ##### proba = N.array(prob)[:, 1:] ## # test set all to max score ## p = proba ## p1 = [] ## for i in range( len(p) ): ## p1 += [ N.resize( p[i][N.argmax( N.array( p[i] ) )] , N.shape( p[i] ) ) ] ## profileDic['maxAll'] = p1 # test set all to N.sum( abs( probabilities ) ) p = proba p2 = [] for i in range(len(p)): p2 += [N.resize(N.sum(N.absolute(p[i])), N.shape(p[i]))] profileDic['absSum'] = p2 # set all to normalized max score p = proba p4 = [] for i in range(len(p)): p_scale = (p[i] - N.average(p[i])) / math.SD(p[i]) p4 += [ N.resize(p_scale[N.argmax(N.array(p_scale))], N.shape(p[i])) ] profileDic['maxAllScale'] = p4 return profileDic
def reset_reward(self): self.reward = resize(0.0, (self.num_squares_x, self.num_squares_y)) self.reward[self.goal] = 100 for e in self.pits: self.reward[e] = -50