def getobs2(snum,stime,observables): if len(Numeric.shape(observables)) == 0: return Numeric.asarray(map(observables,Numeric.arange(0,snum*stime,stime)),dtype='d') else: def mapfun(x): return map(lambda y: y(x),observables) return Numeric.asarray(map(mapfun,Numeric.arange(0,snum*stime,stime)),dtype='d')
def __pairwiseDistances(self, u, v): """ pairwise distance between 2 3-D numpy arrays of atom coordinates. @param u: coordinates @type u: array @param v: coordinates @type v: array @return: Numpy array len(u) x len(v) @rtype:array @author: Wolfgang Rieping. """ ## check input if not type( u ) == arraytype or\ not type( v ) == arraytype: raise ComplexError('unsupported argument type ' + \ str( type(u) ) + ' or ' + str( type(v) ) ) diag1= N.diagonal(N.dot(u,N.transpose(u))) diag2= N.diagonal(N.dot(v,N.transpose(v))) dist= -N.dot(v,N.transpose(u))-N.transpose(N.dot(u,N.transpose(v))) dist= N.transpose(N.asarray(map(lambda column,a:column+a, \ N.transpose(dist), diag1))) return N.transpose(N.sqrt(N.asarray( map(lambda row,a: row+a, dist, diag2))))
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 __init__(self, cntrl, uknots, vknots): self._bezier = None cntrl = numerix.asarray(cntrl, numerix.Float) (dim, nu, nv) = cntrl.shape if dim < 2 or dim > 4: raise NURBSError, 'Illegal control point format' elif dim < 4: self.cntrl = numerix.zeros((4, nu, nv), numerix.Float) self.cntrl[0:dim, :, :] = cntrl self.cntrl[-1, :, :] = numerix.ones((nu, nv), numerix.Float) else: self.cntrl = cntrl # Force the u knot sequence to be a vector in ascending order # and normalise between [0.0,1.0] uknots = numerix.sort(numerix.asarray(uknots, numerix.Float)) nku = uknots.shape[0] uknots = (uknots - uknots[0]) / (uknots[-1] - uknots[0]) if uknots[0] == uknots[-1]: raise NURBSError, 'Illegal uknots sequence' self.uknots = uknots # Force the v knot sequence to be a vector in ascending order # and normalise between [0.0,1.0] vknots = -numerix.sort(-numerix.asarray(vknots, numerix.Float)) nkv = vknots.shape[0] vknots = (vknots - vknots[0]) / (vknots[-1] - vknots[0]) if vknots[0] == vknots[-1]: raise NURBSError, 'Illegal vknots sequence' self.vknots = vknots # Spline Degree self.degree = [nku - nu - 1, nkv - nv - 1] if self.degree[0] < 0 or self.degree[1] < 0: raise NURBSError, 'NURBS order must be a positive integer'
def __init__(self, cntrl, uknots, vknots): self._bezier = None cntrl = numerix.asarray(cntrl, numerix.Float) (dim, nu, nv) = cntrl.shape if dim < 2 or dim > 4: raise NURBSError, 'Illegal control point format' elif dim < 4: self.cntrl = numerix.zeros((4, nu, nv), numerix.Float) self.cntrl[0:dim,:,:] = cntrl self.cntrl[-1,:,:] = numerix.ones((nu,nv), numerix.Float) else: self.cntrl = cntrl # Force the u knot sequence to be a vector in ascending order # and normalise between [0.0,1.0] uknots = numerix.sort(numerix.asarray(uknots, numerix.Float)) nku = uknots.shape[0] uknots = (uknots - uknots[0])/(uknots[-1] - uknots[0]) if uknots[0] == uknots[-1]: raise NURBSError, 'Illegal uknots sequence' self.uknots = uknots # Force the v knot sequence to be a vector in ascending order # and normalise between [0.0,1.0] vknots = -numerix.sort(-numerix.asarray(vknots, numerix.Float)) nkv = vknots.shape[0] vknots = (vknots - vknots[0])/(vknots[-1] - vknots[0]) if vknots[0] == vknots[-1]: raise NURBSError, 'Illegal vknots sequence' self.vknots = vknots # Spline Degree self.degree = [nku-nu-1, nkv-nv-1] if self.degree[0] < 0 or self.degree[1] < 0: raise NURBSError, 'NURBS order must be a positive integer'
def mad(m,axis=0): """Returns Median Absolute Deviation of the given array along the given axis. """ m = Numeric.asarray(m) mx = Numeric.asarray(median(m,axis),Numeric.Float) xt = Numeric.transpose(m, [axis]+range(axis)+range(axis+1,Numeric.rank(m))) # do not use swapaxes: (0,1,2) -swap-> (2,1,0); (0,1,2) -transpose-> (2,0,1) return MLab.median(Numeric.absolute(xt-mx))
def _distSpearmanW_NU(x, y, w): """x,y,w must be Numeric """ x = Numeric.asarray(x) y = Numeric.asarray(y) w = Numeric.asarray(w) assert Numeric.rank(x) == Numeric.rank(y) == Numeric.rank(w) == 1 rankx = Numeric.array(statc.rankdata(x.tolist())) ranky = Numeric.array(statc.rankdata(y.tolist())) return distPearsonW(rankx, ranky, w)
def lowess2(x, y, xest, f=2./3., iter=3): """Returns estimated values of y in data points xest (or None if estimation fails). Lowess smoother: Robust locally weighted regression. The lowess function fits a nonparametric regression curve to a scatterplot. The arrays x and y contain an equal number of elements; each pair (x[i], y[i]) defines a data point in the scatterplot. The function returns the estimated (smooth) values of y. The smoothing span is given by f. A larger value for f will result in a smoother curve. The number of robustifying iterations is given by iter. The function will run faster with a smaller number of iterations.""" x = Numeric.asarray(x, 'd') y = Numeric.asarray(y, 'd') xest = Numeric.asarray(xest, 'd') n = len(x) nest = len(xest) r = min(int(Numeric.ceil(f*n)),n-1) # radius: num. of points to take into LR h = [Numeric.sort(abs(x-x[i]))[r] for i in range(n)] # distance of the r-th point from x[i] w = Numeric.clip(abs(([x]-Numeric.transpose([x]))/h),0.0,1.0) w = 1-w*w*w w = w*w*w hest = [Numeric.sort(abs(x-xest[i]))[r] for i in range(nest)] # r-th min. distance from xest[i] to x west = Numeric.clip(abs(([xest]-Numeric.transpose([x]))/hest),0.0,1.0) # shape: (len(x), len(xest) west = 1-west*west*west west = west*west*west yest = Numeric.zeros(n,'d') yest2 = Numeric.zeros(nest,'d') delta = Numeric.ones(n,'d') try: for iteration in range(iter): # fit xest for i in range(nest): weights = delta * west[:,i] b = Numeric.array([sum(weights*y), sum(weights*y*x)]) A = Numeric.array([[sum(weights), sum(weights*x)], [sum(weights*x), sum(weights*x*x)]]) beta = LinearAlgebra.solve_linear_equations(A,b) yest2[i] = beta[0] + beta[1]*xest[i] # fit x (to calculate residuals and delta) for i in range(n): weights = delta * w[:,i] b = Numeric.array([sum(weights*y), sum(weights*y*x)]) A = Numeric.array([[sum(weights), sum(weights*x)], [sum(weights*x), sum(weights*x*x)]]) beta = LinearAlgebra.solve_linear_equations(A,b) yest[i] = beta[0] + beta[1]*x[i] residuals = y-yest s = MLab.median(abs(residuals)) delta = Numeric.clip(residuals/(6*s),-1,1) delta = 1-delta*delta delta = delta*delta except LinearAlgebra.LinAlgError: print "Warning: NumExtn.lowess2: LinearAlgebra.solve_linear_equations: Singular matrix" yest2 = None return yest2
def __init__(self, crv, pnt=[0., 0., 0.], vector=[1., 0., 0.], theta=2. * math.pi): if not isinstance(crv, Crv.Crv): raise NURBSError, 'Parameter crv not derived from Crv class!' # Translate and rotate the curve into alignment with the z-axis T = translate(-numerix.asarray(pnt, numerix.Float)) # Normalize vector vector = numerix.asarray(vector, numerix.Float) len = numerix.sqrt(numerix.add.reduce(vector * vector)) if len == 0: raise ZeroDivisionError, "Can't normalize a zero-length vector" vector = vector / len if vector[0] == 0.: angx = 0. else: angx = math.atan2(vector[0], vector[2]) RY = roty(-angx) vectmp = numerix.ones((4, ), numerix.Float) vectmp[0:3] = vector vectmp = numerix.dot(RY, vectmp) if vectmp[1] == 0.: angy = 0. else: angy = math.atan2(vector[1], vector[2]) RX = rotx(angy) crv.trans(numerix.dot(RX, numerix.dot(RY, T))) arc = Crv.Arc(1., [0., 0., 0.], 0., theta) narc = arc.cntrl.shape[1] ncrv = crv.cntrl.shape[1] coefs = numerix.zeros((4, narc, ncrv), numerix.Float) angle = numerix.arctan2(crv.cntrl[1, :], crv.cntrl[0, :]) vectmp = crv.cntrl[0:2, :] radius = numerix.sqrt(numerix.add.reduce(vectmp * vectmp)) for i in xrange(0, ncrv): coefs[:, :, i] = numerix.dot( rotz(angle[i]), numerix.dot( translate((0., 0., crv.cntrl[2, i])), numerix.dot(scale((radius[i], radius[i])), arc.cntrl))) coefs[3, :, i] = coefs[3, :, i] * crv.cntrl[3, i] Srf.__init__(self, coefs, arc.uknots, crv.uknots) T = translate(pnt) RX = rotx(-angy) RY = roty(angx) self.trans(numerix.dot(T, numerix.dot(RY, RX)))
def transform_vertices(self,verts): v = Numeric.asarray(verts) homog = make_homogeneous_coord_rows(v) r = numpy.dot(homog,self.matrix) if len(homog.shape) > len(v.shape): r = Numeric.reshape(r,(4,)) return r
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 normalize_homogeneous_rows(v): v = Numeric.asarray(v) homog = make_homogeneous_coord_rows(v) r = (homog/homog[:,3,Numeric.NewAxis])[:,:3] if len(homog.shape) > len(v.shape): r = Numeric.reshape(r,(3,)) return r
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 bounds(self): "Return the bounding box for the surface." w = self.cntrl[3, :, :] cx = numerix.sort(numerix.ravel(self.cntrl[0, :, :] / w)) cy = numerix.sort(numerix.ravel(self.cntrl[1, :, :] / w)) cz = numerix.sort(numerix.ravel(self.cntrl[2, :, :] / w)) return numerix.asarray([cx[0], cy[0], cz[0], cx[-1], cy[-1], cz[-1]], numerix.Float)
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 permutInverse(n): """Returns inverse permutation given integers in range(len(n)), such that permitInverse(permutInverse(range(4)))==range(4). """ n = Numeric.asarray(n) pInv = Numeric.argsort(n) assert Numeric.all(Numeric.equal(n, Numeric.argsort(pInv))), "Inverse not successful; input should be permutation of range(len(input))." return pInv
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 bounds(self): "Return the bounding box for the surface." w = self.cntrl[3,:,:] cx = numerix.sort(numerix.ravel(self.cntrl[0,:,:]/w)) cy = numerix.sort(numerix.ravel(self.cntrl[1,:,:]/w)) cz = numerix.sort(numerix.ravel(self.cntrl[2,:,:]/w)) return numerix.asarray([cx[0], cy[0], cz[0], cx[-1], cy[-1], cz[-1]], numerix.Float)
def indices2condition(indices, length): """Input: indices=[0,3]; output: condition=[1,0,0,1] """ indices = Numeric.asarray(indices) assert len(indices.shape) == 1 assert length >= indices.shape[0] c = Numeric.zeros((length,), Numeric.Int) Numeric.put(c, indices, 1) return c
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 __init__(self, crv, pnt = [0., 0., 0.], vector = [1., 0., 0.], theta = 2.*math.pi): if not isinstance(crv, Crv.Crv): raise NURBSError, 'Parameter crv not derived from Crv class!' # Translate and rotate the curve into alignment with the z-axis T = translate(-numerix.asarray(pnt, numerix.Float)) # Normalize vector vector = numerix.asarray(vector, numerix.Float) len = numerix.sqrt(numerix.add.reduce(vector*vector)) if len == 0: raise ZeroDivisionError, "Can't normalize a zero-length vector" vector = vector/len if vector[0] == 0.: angx = 0. else: angx = math.atan2(vector[0], vector[2]) RY = roty(-angx) vectmp = numerix.ones((4,), numerix.Float) vectmp[0:3] = vector vectmp = numerix.dot(RY, vectmp) if vectmp[1] == 0.: angy = 0. else: angy = math.atan2(vector[1], vector[2]) RX = rotx(angy) crv.trans(numerix.dot(RX, numerix.dot(RY, T))) arc = Crv.Arc(1., [0., 0., 0.], 0., theta) narc = arc.cntrl.shape[1] ncrv = crv.cntrl.shape[1] coefs = numerix.zeros((4, narc, ncrv), numerix.Float) angle = numerix.arctan2(crv.cntrl[1,:], crv.cntrl[0,:]) vectmp = crv.cntrl[0:2,:] radius = numerix.sqrt(numerix.add.reduce(vectmp*vectmp)) for i in xrange(0, ncrv): coefs[:,:,i] = numerix.dot(rotz(angle[i]), numerix.dot(translate((0., 0., crv.cntrl[2,i])), numerix.dot(scale((radius[i], radius[i])), arc.cntrl))) coefs[3,:,i] = coefs[3,:,i] * crv.cntrl[3,i] Srf.__init__(self, coefs, arc.uknots, crv.uknots) T = translate(pnt) RX = rotx(-angy) RY = roty(angx) self.trans(numerix.dot(T, numerix.dot(RY, RX)))
def pairwiseDistances(u, v): """ Pairwise distances between two arrays. @param u: first array @type u: array @param v: second array @type v: array @return: Numeric.array( len(u) x len(v) ) of double @rtype: array """ diag1 = N.diagonal( N.dot( u, N.transpose(u) ) ) diag2 = N.diagonal( N.dot( v, N.transpose(v) ) ) dist = -N.dot( v,N.transpose(u) )\ -N.transpose( N.dot( u, N.transpose(v) ) ) dist = N.transpose( N.asarray( map( lambda column,a:column+a, \ N.transpose(dist), diag1) ) ) return N.transpose( N.sqrt( N.asarray( map( lambda row,a: row+a, dist, diag2 ) ) ))
def pairwiseDistances(u, v): """ Pairwise distances between two arrays. @param u: first array @type u: array @param v: second array @type v: array @return: Numeric.array( len(u) x len(v) ) of double @rtype: array """ diag1 = N.diagonal(N.dot(u, N.transpose(u))) diag2 = N.diagonal(N.dot(v, N.transpose(v))) dist = -N.dot( v,N.transpose(u) )\ -N.transpose( N.dot( u, N.transpose(v) ) ) dist = N.transpose( N.asarray( map( lambda column,a:column+a, \ N.transpose(dist), diag1) ) ) return N.transpose( N.sqrt(N.asarray(map(lambda row, a: row + a, dist, diag2))))
def rankData(n, inverse=False): """Returns ranks of 1D Numeric array in range 1...shape[0]. """ n = Numeric.asarray(n) assert Numeric.rank(n) == 1 r = Numeric.zeros(n.shape[0], Numeric.Float) Numeric.put(r, Numeric.argsort(n), Numeric.arange(n.shape[0])) if inverse: return -1*r+n.shape[0] else: return r+1
def __init__(self, points, k, normalization=NORM_NORM_T0_1, force=False): """ calculate k polynomials of degree 0 to k-1 orthogonal on a set of distinct points map points to interval [-1,1] INPUT: points: array of dictinct points where polynomials are orthogonal k: number of polynomials of degree 0 to k-1 force=True creates basis even if orthogonality is not satisfied due to numerical error USES: x: array of points mapped to [-1,1] T_: matrix of values of polynomials calculated at x, shape (k,len(x)) TT_ = T_ * Numeric.transpose(T_) TTinv_ = inverse(TT_) sc_: scaling factors a, b: coefficients for calculating T (2k-4 different from 0, i.e. 6 for k=5) n: number of points = len(points) normalization = {0|1|2} """ self.k = k # number of basis polynomials of order 0 to k-1 self._force = force self.points = Numeric.asarray(points, Numeric.Float) self.pointsMin = min(points) self.pointsMax = max(points) # scaling x to [-1,1] results in smaller a and b, T is not affected; overflow is NOT a problem! self.xMin = -1 self.xMax = 1 self.x = self._map(self.points, self.pointsMin, self.pointsMax, self.xMin, self.xMax) # calculate basis polynomials self.n = len(points) # the number of approximation points t = Numeric.zeros((k,self.n),Numeric.Float) a = Numeric.zeros((k,1),Numeric.Float) b = Numeric.zeros((k,1),Numeric.Float) t[0,:] = Numeric.ones(self.n,Numeric.Float) if k > 1: t[1,:] = self.x - sum(self.x)/self.n for i in range(1,k-1): a[i+1] = Numeric.innerproduct(self.x, t[i,:] * t[i,:]) / Numeric.innerproduct(t[i,:],t[i,:]) b[i] = Numeric.innerproduct(t[i,:], t[i,:]) / Numeric.innerproduct(t[i-1,:],t[i-1,:]) t[i+1,:] = (self.x - a[i+1]) * t[i,:] - b[i] * t[i-1,:] self.a = a self.b = b # prepare for approximation self._T0 = t # orthonormal _TT0 = Numeric.matrixmultiply(self._T0, Numeric.transpose(self._T0)) self.sc1 = Numeric.sqrt(Numeric.reshape(Numeric.diagonal(_TT0),(self.k,1))) # scaling factors = sqrt sum squared self._T0 self._T1 = self._T0 / self.sc1 # orthonormal and T[0] == 1 self.sc2 = Numeric.sqrt(Numeric.reshape(Numeric.diagonal(_TT0),(self.k,1)) / self.n) # scaling factors = sqrt 1/n * sum squared self._T0 self._T2 = self._T0 / self.sc2 # T[:,-1] == 1 self.sc3 = Numeric.take(self._T0, (-1,), 1) # scaling factors = self._T0[:,-1] self._T3 = self._T0 / self.sc3 # set the variables according to the chosen normalization self.setNormalization(normalization)
def ttest_rsmplA(self, ma3d, callback): """conducts related samples t-test on individual examples wrt factor A (variables, ma3d axis 1); returns Numeric array of p-values in shape (1, numExamples). """ ps = -1*Numeric.ones((ma3d.shape[0],), Numeric.Float) for eIdx in range(ma3d.shape[0]): a = ma3d[eIdx][0] b = ma3d[eIdx][1] cond = Numeric.logical_not(Numeric.logical_or(MA.getmaskarray(a), MA.getmaskarray(b))) a = Numeric.asarray(MA.compress(cond, a)) b = Numeric.asarray(MA.compress(cond, b)) if len(a) >= 2: try: ps[eIdx] = scipy.stats.ttest_rel(a,b)[1] except Exception, inst: print "Warning: %s" % str(inst) print "Example %i:\n%s\n%s\n" % (eIdx, str(a), str(b)) ps[eIdx] = 1.0 else: print "Warning: removing example %i:\n%s\n%s\n" % (eIdx, str(a), str(b)) ps[eIdx] = 1.0 callback()
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 __init__(self, pnts): pnts = numerix.transpose(numerix.asarray(pnts, numerix.Float)) npnts = pnts.shape[1] if npnts < 3: raise NURBSError, 'Point sequence error' cntrl = numerix.zeros((pnts.shape[0], 2 * npnts - 2), numerix.Float) cntrl[:,0] = pnts[:,0] cntrl[:,-1] = pnts[:,-1] cntrl[:,1:-2:2] = pnts[:,1:-1] cntrl[:,2:-1:2] = pnts[:,1:-1] uknots = numerix.zeros(npnts * 2, numerix.Float) uknots[0::2] = numerix.arange(npnts) uknots[1::2] = numerix.arange(npnts) Crv.__init__(self, cntrl, uknots)
def __init__(self, pnts): pnts = numerix.transpose(numerix.asarray(pnts, numerix.Float)) npnts = pnts.shape[1] if npnts < 3: raise NURBSError, 'Point sequence error' cntrl = numerix.zeros((pnts.shape[0], 2 * npnts - 2), numerix.Float) cntrl[:, 0] = pnts[:, 0] cntrl[:, -1] = pnts[:, -1] cntrl[:, 1:-2:2] = pnts[:, 1:-1] cntrl[:, 2:-1:2] = pnts[:, 1:-1] uknots = numerix.zeros(npnts * 2, numerix.Float) uknots[0::2] = numerix.arange(npnts) uknots[1::2] = numerix.arange(npnts) Crv.__init__(self, cntrl, uknots)
def kNNimputeMA(arr2d, K=20, callback=None): """Returns a new 2D MA.array with missing values imputed from K nearest neighbours. Find K rows (axis 0) with the most similar values where similarity measure corresponds to weighted Euclidean distance. Imputed value = weighted average of the corresponding values of K nearest neighbours, where weights equal to tricubic distribution of distances to all rows. Impute missing rows by average over all rows. Version: 30.8.2005 """ arr2d = MA.asarray(arr2d) assert len(arr2d.shape) == 2, "2D array expected" # make a copy for imputation aImp2 = MA.array(arr2d) # leave out columns with 0 known values (columnInd: non-zero columns) columnCond = Numeric.greater(MA.count(arr2d, axis=0), 0) columnIndAll = Numeric.arange(arr2d.shape[1]) columnInd = Numeric.compress(columnCond, columnIndAll) # impute the rows where 0 < #known_values < #non_zero_columns, i.e. exclude the rows with 0 and all (non-zero-column) values countByRows = MA.count(arr2d, axis=1) for rowIdx in Numeric.compress(Numeric.logical_and(Numeric.greater(countByRows, 0), Numeric.less(countByRows, columnInd.shape[0])), Numeric.arange(arr2d.shape[0])): rowResized = MA.resize(arr2d[rowIdx], arr2d.shape) diff = arr2d - rowResized distances = MA.sqrt(MA.add.reduce((diff)**2, 1) / MA.count(diff, axis=1)) # nearest neighbours row indices (without the current row index) indSorted = MA.argsort(distances)[1:] distSorted = distances.take(indSorted) # number of distances different from MA.masked numNonMasked = distSorted.shape[0] - Numeric.add.reduce(Numeric.asarray(MA.getmaskarray(distSorted), Numeric.Int)) # number of distances to account for (K or less) if numNonMasked > 1: weightsSorted = MA.power(1-MA.power(distSorted/distSorted[numNonMasked-1],3),3) # tricubic distribution of all weights else: weightsSorted = Numeric.ones(distSorted.shape[0]) # compute average for each column separately in order to account for K non-masked values colInd4CurrRow = Numeric.compress(Numeric.logical_and(MA.getmaskarray(arr2d[rowIdx]), columnCond), columnIndAll) for colIdx in colInd4CurrRow: # column values sorted by distances columnVals = arr2d[:,colIdx].take(indSorted) # take only those weights where columnVals does not equal MA.masked weightsSortedCompressed = MA.compress(1-MA.getmaskarray(columnVals), weightsSorted) # impute from K (or possibly less) values aImp2[rowIdx,colIdx] = MA.average(columnVals.compressed()[:K], weights=weightsSortedCompressed[:K]) if callback: callback() # impute the unknown rows with average profile avrgRow = MA.average(arr2d, 0) for rowIdx in Numeric.compress(Numeric.equal(countByRows, 0), Numeric.arange(arr2d.shape[0])): aImp2[rowIdx] = avrgRow if callback: callback() return aImp2
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 ttest_ssmpl(self, ma3d, compToVal, callback): """conducts single-sample t-test on individual examples wrt factor A (variables, ma3d axis 1); returns Numeric array of p-values in shape (1, numExamples). """ ps = -1*Numeric.ones((ma3d.shape[0],), Numeric.Float) for eIdx in range(ma3d.shape[0]): data = Numeric.asarray(MA.transpose(ma3d[eIdx]).compressed()) if len(data) >= 2: try: ps[eIdx] = scipy.stats.ttest_1samp(data, compToVal)[1] except: print "Warning: zero variance, check the example %i:" % eIdx, data ps[eIdx] = 1.0 else: ps[eIdx] = 1.0 callback() return ps
def anova2(self, ma3d, groupLens, addInteraction, repMeasuresOnA, callback): """Conducts two-way ANOVA on individual examples; returns a Numeric array of p-values in shape (2, numExamples) or (3, numExamples), depending whether we test for interaction; Note: levels of factors A and B that cause empty cells are removed prior to conducting ANOVA. """ groupLens = Numeric.asarray(groupLens) # arrays to store p-vals if addInteraction: ps = Numeric.ones((3, ma3d.shape[0]), Numeric.Float) else: ps = Numeric.ones((2, ma3d.shape[0]), Numeric.Float) # decide between non-repeated / repeated measures ANOVA for factor time if repMeasuresOnA: fAnova = Anova.AnovaRM12LR else: fAnova = Anova.Anova2wayLR # check for empty cells for all genes at once and remove them tInd2rem = [] ax2Ind = Numeric.concatenate(([0], Numeric.add.accumulate(groupLens))) for aIdx in range(ma3d.shape[1]): for rIdx in range(groupLens.shape[0]): if Numeric.add.reduce(MA.count(ma3d[:,aIdx,ax2Ind[rIdx]:ax2Ind[rIdx+1]],1)) == 0: tInd2rem.append(aIdx) break if len(tInd2rem) > 0: print "Warning: removing time indices %s for all genes" % (str(tInd2rem)) tInd2keep = range(ma3d.shape[1]) for aIdx in tInd2rem: tInd2keep.remove(aIdx) ma3d = ma3d.take(tInd2keep, 1) # for each gene... for eIdx in range(ma3d.shape[0]): # faster check for empty cells for that gene -> remove time indices with empty cells ma2d = ma3d[eIdx] cellCount = MA.zeros((ma2d.shape[0], groupLens.shape[0]), Numeric.Int) for g,(i0,i1) in enumerate(zip(ax2Ind[:-1], ax2Ind[1:])): cellCount[:,g] = MA.count(ma2d[:,i0:i1], 1) ma2dTakeInd = Numeric.logical_not(Numeric.add.reduce(Numeric.equal(cellCount,0),1)) # 1 where to take, 0 where not to take if Numeric.add.reduce(ma2dTakeInd) != ma2dTakeInd.shape[0]: print "Warning: removing time indices %s for gene %i" % (str(Numeric.compress(ma2dTakeInd == 0, Numeric.arange(ma2dTakeInd.shape[0]))), eIdx) ma2d = MA.compress(ma2dTakeInd, ma2d, 0) an = fAnova(ma2d, groupLens, addInteraction, allowReductA=True, allowReductB=True) ps[:,eIdx] = an.ps callback() return ps
def _evalPolyHorner(self, polyCoef, x): """returns (#poly, #x) polynomials evaluated at x where: - result: polynomials in rows, values at x in columns: [f(x0)...f(xn)] - polyCoef: polynomials in rows, coefficients in columns, sorted by increasing power of x: [c0, c1, ...] uses Horner's rule for polynomial evaluation """ x = Numeric.asarray(x, Numeric.Float) one_poly = len(polyCoef.shape) == 1 if one_poly: polyCoef = polyCoef[Numeric.NewAxis,:] # [polyIdx, coefIdx] val = Numeric.zeros((polyCoef.shape[0],len(x)), Numeric.Float) # [polyIdx, xIdx] for idx in range(polyCoef.shape[1]-1,0,-1): val = (val + polyCoef[:,idx:idx+1]) * x val += polyCoef[:,0:1] if one_poly: return val[0,:] else: return val
def ttest_ssmpl(self, ma3d, popMeanVal, callback): """conducts single-sample t-test on individual examples wrt factor A (variables, ma3d axis 1); returns Numeric array of p-values in shape (1, numExamples). """ ps = -1*Numeric.ones((ma3d.shape[0],), Numeric.Float) for eIdx in range(ma3d.shape[0]): data = Numeric.asarray(MA.transpose(ma3d[eIdx]).compressed()) if len(data) >= 2: try: ps[eIdx] = scipy.stats.ttest_1samp(data, popMeanVal)[1] except: print "Warning: zero variance, check the example %i:" % eIdx, data ps[eIdx] = 1.0 else: ## print "Warning: removing example %i:\n%s\n%s\n" % (eIdx, str(data)) print "Warning: removing example %i:" % eIdx, str(data) ps[eIdx] = 1.0 callback() return ps
def _evalPolyHorner(self, polyCoef, x): """returns (#poly, #x) polynomials evaluated at x where: - result: polynomials in rows, values at x in columns: [f(x0)...f(xn)] - polyCoef: polynomials in rows, coefficients in columns, sorted by increasing power of x: [c0, c1, ...] uses Horner's rule for polynomial evaluation """ x = Numeric.asarray(x, Numeric.Float) one_poly = len(polyCoef.shape) == 1 if one_poly: polyCoef = polyCoef[Numeric.NewAxis, :] # [polyIdx, coefIdx] val = Numeric.zeros((polyCoef.shape[0], len(x)), Numeric.Float) # [polyIdx, xIdx] for idx in range(polyCoef.shape[1] - 1, 0, -1): val = (val + polyCoef[:, idx:idx + 1]) * x val += polyCoef[:, 0:1] if one_poly: return val[0, :] else: return val
def evalApproxPoly(self, appxCoef, points=None): """returns (#curve, #points) an approx. polynomials calculated at points given approx. coeff in rows: - appxCoef: curves for approximation in rows, appxCoef in columns [B0, B1, ...] - points relative to self.points TODO: evaluate on a matrix of appxCoef """ if points == None: return Numeric.matrixmultiply(appxCoef, self.T) appxCoef = Numeric.asarray(appxCoef, Numeric.Float) one_curve = len(appxCoef.shape) == 1 if one_curve: appxCoef = appxCoef[Numeric.NewAxis,:] # [curveIdx, coefIdx] mappedPoints = self._map(points, self.pointsMin, self.pointsMax, self.xMin, self.xMax) # eval basis polynomials on mapped points basisEval = self._evalPolyHorner(self.basisCoef, mappedPoints) #[basisIdx == coefIdx, pointIdx] result = Numeric.matrixmultiply(appxCoef, basisEval) if one_curve: return result[0,:] else: return result
def chipdata(self, data): if data != None: self._chipdata = data shp = [0,0,0] shp[0] = len(data[0][1][0]) shp[1] = len(data[0][1][0].domain.attributes) shp[2] = reduce(lambda x,y: x+len(y[1]), data, 0) self._chipdataN = Numeric.zeros(shp, Numeric.Float) idx = 0 for (name, etList) in data: for et in etList: self._chipdataN[:,:,idx] = Numeric.asarray(chipstat.orng2ma(et)) idx += 1 self.infob.setText("Structured Data: %i data files with %i profiles on %i points" % (shp[2], shp[0], shp[1])) else: self._chipdata = None self._chipdataN = None self.infob.setText("No structured data on input") self.setGuiCommonExpChip() if self.commitOnChange: self.senddata(2)
def __init__(self, crv1, crv2): if not isinstance(crv1, Crv.Crv): raise NURBSError, 'Parameter crv1 not derived from Crv class!' if not isinstance(crv2, Crv.Crv): raise NURBSError, 'Parameter crv2 not derived from Crv class!' # ensure both curves have a common degree d = max(crv1.degree, crv2.degree) crv1.degelev(d - crv1.degree) crv2.degelev(d - crv2.degree) # merge the knot vectors, to obtain a common knot vector k1 = crv1.uknots k2 = crv2.uknots ku = [] for item in k1: if not numerix.sometrue(numerix.equal(k2, item)): if item not in ku: ku.append(item) for item in k2: if not numerix.sometrue(numerix.equal(k1, item)): if item not in ku: ku.append(item) ku = numerix.sort(numerix.asarray(ku, numerix.Float)) n = ku.shape[0] ka = numerix.array([], numerix.Float) kb = numerix.array([], numerix.Float) for i in range(0, n): i1 = numerix.compress(numerix.equal(k1, ku[i]), k1).shape[0] i2 = numerix.compress(numerix.equal(k2, ku[i]), k2).shape[0] m = max(i1, i2) ka = numerix.concatenate((ka, ku[i] * numerix.ones( (m - i1, ), numerix.Float))) kb = numerix.concatenate((kb, ku[i] * numerix.ones( (m - i2, ), numerix.Float))) crv1.kntins(ka) crv2.kntins(kb) coefs = numerix.zeros((4, crv1.cntrl.shape[1], 2), numerix.Float) coefs[:, :, 0] = crv1.cntrl coefs[:, :, 1] = crv2.cntrl Srf.__init__(self, coefs, crv1.uknots, [0., 0., 1., 1.])
def evalApproxPoly(self, appxCoef, points=None): """returns (#curve, #points) an approx. polynomials calculated at points given approx. coeff in rows: - appxCoef: curves for approximation in rows, appxCoef in columns [B0, B1, ...] - points relative to self.points TODO: evaluate on a matrix of appxCoef """ if points == None: return Numeric.matrixmultiply(appxCoef, self.T) appxCoef = Numeric.asarray(appxCoef, Numeric.Float) one_curve = len(appxCoef.shape) == 1 if one_curve: appxCoef = appxCoef[Numeric.NewAxis, :] # [curveIdx, coefIdx] mappedPoints = self._map(points, self.pointsMin, self.pointsMax, self.xMin, self.xMax) # eval basis polynomials on mapped points basisEval = self._evalPolyHorner( self.basisCoef, mappedPoints) #[basisIdx == coefIdx, pointIdx] result = Numeric.matrixmultiply(appxCoef, basisEval) if one_curve: return result[0, :] else: return result
def __init__(self, crv1, crv2): if not isinstance(crv1, Crv.Crv): raise NURBSError, 'Parameter crv1 not derived from Crv class!' if not isinstance(crv2, Crv.Crv): raise NURBSError, 'Parameter crv2 not derived from Crv class!' # ensure both curves have a common degree d = max(crv1.degree, crv2.degree) crv1.degelev(d - crv1.degree) crv2.degelev(d - crv2.degree) # merge the knot vectors, to obtain a common knot vector k1 = crv1.uknots k2 = crv2.uknots ku = [] for item in k1: if not numerix.sometrue(numerix.equal(k2, item)): if item not in ku: ku.append(item) for item in k2: if not numerix.sometrue(numerix.equal(k1, item)): if item not in ku: ku.append(item) ku = numerix.sort(numerix.asarray(ku, numerix.Float)) n = ku.shape[0] ka = numerix.array([], numerix.Float) kb = numerix.array([], numerix.Float) for i in range(0, n): i1 = numerix.compress(numerix.equal(k1, ku[i]), k1).shape[0] i2 = numerix.compress(numerix.equal(k2, ku[i]), k2).shape[0] m = max(i1, i2) ka = numerix.concatenate((ka , ku[i] * numerix.ones((m - i1,), numerix.Float))) kb = numerix.concatenate((kb , ku[i] * numerix.ones((m - i2,), numerix.Float))) crv1.kntins(ka) crv2.kntins(kb) coefs = numerix.zeros((4, crv1.cntrl.shape[1], 2), numerix.Float) coefs[:,:,0] = crv1.cntrl coefs[:,:,1] = crv2.cntrl Srf.__init__(self, coefs, crv1.uknots, [0., 0., 1., 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 kNNimputeMA(arr2d, K=20, callback=None): """Returns a new 2D MA.array with missing values imputed from K nearest neighbours. Find K rows (axis 0) with the most similar values where similarity measure corresponds to weighted Euclidean distance. Imputed value = weighted average of the corresponding values of K nearest neighbours, where weights equal to tricubic distribution of distances to all rows. Impute missing rows by average over all rows. Version: 30.8.2005 """ arr2d = MA.asarray(arr2d) assert len(arr2d.shape) == 2, "2D array expected" # make a copy for imputation aImp2 = MA.array(arr2d) # leave out columns with 0 known values (columnInd: non-zero columns) columnCond = Numeric.greater(MA.count(arr2d, axis=0), 0) columnIndAll = Numeric.arange(arr2d.shape[1]) columnInd = Numeric.compress(columnCond, columnIndAll) # impute the rows where 0 < #known_values < #non_zero_columns, i.e. exclude the rows with 0 and all (non-zero-column) values countByRows = MA.count(arr2d, axis=1) for rowIdx in Numeric.compress( Numeric.logical_and(Numeric.greater(countByRows, 0), Numeric.less(countByRows, columnInd.shape[0])), Numeric.arange(arr2d.shape[0])): rowResized = MA.resize(arr2d[rowIdx], arr2d.shape) diff = arr2d - rowResized distances = MA.sqrt( MA.add.reduce((diff)**2, 1) / MA.count(diff, axis=1)) # nearest neighbours row indices (without the current row index) indSorted = MA.argsort(distances)[1:] distSorted = distances.take(indSorted) # number of distances different from MA.masked numNonMasked = distSorted.shape[0] - Numeric.add.reduce( Numeric.asarray(MA.getmaskarray(distSorted), Numeric.Int)) # number of distances to account for (K or less) if numNonMasked > 1: weightsSorted = MA.power( 1 - MA.power(distSorted / distSorted[numNonMasked - 1], 3), 3) # tricubic distribution of all weights else: weightsSorted = Numeric.ones(distSorted.shape[0]) # compute average for each column separately in order to account for K non-masked values colInd4CurrRow = Numeric.compress( Numeric.logical_and(MA.getmaskarray(arr2d[rowIdx]), columnCond), columnIndAll) for colIdx in colInd4CurrRow: # column values sorted by distances columnVals = arr2d[:, colIdx].take(indSorted) # take only those weights where columnVals does not equal MA.masked weightsSortedCompressed = MA.compress( 1 - MA.getmaskarray(columnVals), weightsSorted) # impute from K (or possibly less) values aImp2[rowIdx, colIdx] = MA.average(columnVals.compressed()[:K], weights=weightsSortedCompressed[:K]) if callback: callback() # impute the unknown rows with average profile avrgRow = MA.average(arr2d, 0) for rowIdx in Numeric.compress(Numeric.equal(countByRows, 0), Numeric.arange(arr2d.shape[0])): aImp2[rowIdx] = avrgRow if callback: callback() return aImp2
def condition2indices(condition): """Input: condition=[1,0,0,1]; output: indices=[0,3] """ condition = Numeric.asarray(condition) assert len(condition.shape) == 1 return Numeric.compress(condition, Numeric.arange(condition.shape[0]))
def lowessW(x, y, xest, f=2./3., iter=3, dWeights=None, callback=None): """Returns estimated values of y in data points xest (or None if estimation fails). Lowess smoother: Robust locally weighted regression. The lowess function fits a nonparametric regression curve to a scatterplot. The arrays x and y contain an equal number of elements; each pair (x[i], y[i]) defines a data point in the scatterplot. The function returns the estimated (smooth) values of y. The smoothing span is given by f. A larger value for f will result in a smoother curve. The number of robustifying iterations is given by iter. The function will run faster with a smaller number of iterations. Data points may be assigned weights; if None, all weights equal 1. """ x = Numeric.asarray(x, 'd') y = Numeric.asarray(y, 'd') xest = Numeric.asarray(xest, 'd') n = len(x) if n <> len(y): raise AttributeError, "Error: lowessW(x,y,xest,f,iter,dWeights): len(x)=%i not equal to len(y)=%i" % (len(x), len(y)) nest = len(xest) # weights of data points (optional) if dWeights <> None: dWeights = Numeric.asarray(dWeights, 'd') if len(dWeights) <> n: raise AttributeError, "Error: lowessW(x,y,xest,f,iter,dWeights): len(dWeights)=%i not equal to len(x)=%i" % (len(dWeights), len(x)) ## dWeights = dWeights.reshape((n,1)) else: ## dWeights = Numeric.ones((n,1)) dWeights = Numeric.ones((n,)) r = min(int(Numeric.ceil(f*n)),n-1) # radius: num. of points to take into LR h = [Numeric.sort(abs(x-x[i]))[r] for i in range(n)] # distance of the r-th point from x[i] w = Numeric.clip(abs(([x]-Numeric.transpose([x]))/h),0.0,1.0) w = 1-w*w*w w = w*w*w hest = [Numeric.sort(abs(x-xest[i]))[r] for i in range(nest)] # r-th min. distance from xest[i] to x west = Numeric.clip(abs(([xest]-Numeric.transpose([x]))/hest),0.0,1.0) # shape: (len(x), len(xest)) west = 1-west*west*west west = west*west*west yest = Numeric.zeros(n,'d') yest2 = Numeric.zeros(nest,'d') delta = Numeric.ones(n,'d') try: for iteration in range(int(iter)): # fit xest for i in range(nest): ## print delta.shape, west[:,i].shape, dWeights.shape weights = delta * west[:,i] * dWeights b = Numeric.array([sum(weights*y), sum(weights*y*x)]) A = Numeric.array([[sum(weights), sum(weights*x)], [sum(weights*x), sum(weights*x*x)]]) beta = LinearAlgebra.solve_linear_equations(A,b) yest2[i] = beta[0] + beta[1]*xest[i] # fit x (to calculate residuals and delta) for i in range(n): weights = delta * w[:,i] * dWeights b = Numeric.array([sum(weights*y), sum(weights*y*x)]) A = Numeric.array([[sum(weights), sum(weights*x)], [sum(weights*x), sum(weights*x*x)]]) beta = LinearAlgebra.solve_linear_equations(A,b) yest[i] = beta[0] + beta[1]*x[i] residuals = y-yest s = MLab.median(abs(residuals)) delta = Numeric.clip(residuals/(6*s),-1,1) delta = 1-delta*delta delta = delta*delta if callback: callback() except LinearAlgebra.LinAlgError: print "Warning: NumExtn.lowessW: LinearAlgebra.solve_linear_equations: Singular matrix" yest2 = None return yest2
def __init__(self, u1, u2, v1, v2): if not isinstance(u1, Crv.Crv): raise NURBSError, 'Parameter u1 not derived from Crv class!' if not isinstance(u2, Crv.Crv): raise NURBSError, 'Parameter u2 not derived from Crv class!' if not isinstance(v1, Crv.Crv): raise NURBSError, 'Parameter v1 not derived from Crv class!' if not isinstance(v2, Crv.Crv): raise NURBSError, 'Parameter v2 not derived from Crv class!' r1 = Ruled(u1, u2) r2 = Ruled(v1, v2) r2.swapuv() t = Bilinear(u1.cntrl[:, 0], u1.cntrl[:, -1], u2.cntrl[:, 0], u2.cntrl[:, -1]) # Raise all surfaces to a common degree du = max(r1.degree[0], r2.degree[0], t.degree[0]) dv = max(r1.degree[1], r2.degree[1], t.degree[1]) r1.degelev(du - r1.degree[0], dv - r1.degree[1]) r2.degelev(du - r2.degree[0], dv - r2.degree[1]) t.degelev(du - t.degree[0], dv - t.degree[1]) # Merge the knot vectors, to obtain a common knot vector # uknots: k1 = r1.uknots k2 = r2.uknots k3 = t.uknots k = [] for item in k1: if not numerix.sometrue(numerix.equal(k2, item)): if not numerix.sometrue(numerix.equal(k3, item)): if item not in k: k.append(item) for item in k2: if not numerix.sometrue(numerix.equal(k1, item)): if not numerix.sometrue(numerix.equal(k3, item)): if item not in k: k.append(item) for item in k3: if not numerix.sometrue(numerix.equal(k1, item)): if not numerix.sometrue(numerix.equal(k2, item)): if item not in k: k.append(item) k = numerix.sort(numerix.asarray(k, numerix.Float)) n = k.shape[0] kua = numerix.array([], numerix.Float) kub = numerix.array([], numerix.Float) kuc = numerix.array([], numerix.Float) for i in range(0, n): i1 = numerix.compress(numerix.equal(k1, k[i]), k1).shape[0] i2 = numerix.compress(numerix.equal(k2, k[i]), k2).shape[0] i3 = numerix.compress(numerix.equal(k3, k[i]), k3).shape[0] m = max(i1, i2, i3) kua = numerix.concatenate((kua, k[i] * numerix.ones( (m - i1, ), numerix.Float))) kub = numerix.concatenate((kub, k[i] * numerix.ones( (m - i2, ), numerix.Float))) kuc = numerix.concatenate((kuc, k[i] * numerix.ones( (m - i3, ), numerix.Float))) # vknots: k1 = r1.vknots k2 = r2.vknots k3 = t.vknots k = [] for item in k1: if not numerix.sometrue(numerix.equal(k2, item)): if not numerix.sometrue(numerix.equal(k3, item)): if item not in k: k.append(item) for item in k2: if not numerix.sometrue(numerix.equal(k1, item)): if not numerix.sometrue(numerix.equal(k3, item)): if item not in k: k.append(item) for item in k3: if not numerix.sometrue(numerix.equal(k1, item)): if not numerix.sometrue(numerix.equal(k2, item)): if item not in k: k.append(item) k = numerix.sort(numerix.asarray(k, numerix.Float)) n = k.shape[0] kva = numerix.array([], numerix.Float) kvb = numerix.array([], numerix.Float) kvc = numerix.array([], numerix.Float) for i in range(0, n): i1 = numerix.compress(numerix.equal(k1, k[i]), k1).shape[0] i2 = numerix.compress(numerix.equal(k2, k[i]), k2).shape[0] i3 = numerix.compress(numerix.equal(k3, k[i]), k3).shape[0] m = max(i1, i2, i3) kva = numerix.concatenate((kva, k[i] * numerix.ones( (m - i1, ), numerix.Float))) kvb = numerix.concatenate((kvb, k[i] * numerix.ones( (m - i2, ), numerix.Float))) kvc = numerix.concatenate((kvc, k[i] * numerix.ones( (m - i3, ), numerix.Float))) r1.kntins(kua, kva) r2.kntins(kub, kvb) t.kntins(kuc, kvc) coefs = numerix.zeros((4, t.cntrl.shape[1], t.cntrl.shape[2]), numerix.Float) coefs[ 0, :, :] = r1.cntrl[0, :, :] + r2.cntrl[0, :, :] - t.cntrl[0, :, :] coefs[ 1, :, :] = r1.cntrl[1, :, :] + r2.cntrl[1, :, :] - t.cntrl[1, :, :] coefs[ 2, :, :] = r1.cntrl[2, :, :] + r2.cntrl[2, :, :] - t.cntrl[2, :, :] coefs[ 3, :, :] = r1.cntrl[3, :, :] + r2.cntrl[3, :, :] - t.cntrl[3, :, :] Srf.__init__(self, coefs, r1.uknots, r1.vknots)
def _map(self, x, a,b, m,M): """maps x from the interval [a,b] to the interval [m,M]""" x = Numeric.asarray(x, Numeric.Float) return (M-m)*(x-a)/(b-a) + m