def __init__(self, x, y, z, tx, ty, w=None, bbox=[None]*4, kx=3, ky=3, eps=None): nx = 2*kx+2+len(tx) ny = 2*ky+2+len(ty) tx1 = zeros((nx,),float) ty1 = zeros((ny,),float) tx1[kx+1:nx-kx-1] = tx ty1[ky+1:ny-ky-1] = ty xb,xe,yb,ye = bbox tx1,ty1,c,fp,ier = dfitpack.surfit_lsq(x,y,z,tx1,ty1,w,\ xb,xe,yb,ye,\ kx,ky,eps,lwrk2=1) if ier>10: tx1,ty1,c,fp,ier = dfitpack.surfit_lsq(x,y,z,tx1,ty1,w,\ xb,xe,yb,ye,\ kx,ky,eps,lwrk2=ier) if ier in [0,-1,-2]: # normal return pass else: if ier<-2: deficiency = (nx-kx-1)*(ny-ky-1)+ier message = _surfit_messages.get(-3) % (deficiency) else: message = _surfit_messages.get(ier,'ier=%s' % (ier)) warnings.warn(message) self.fp = fp self.tck = tx1,ty1,c self.degrees = kx,ky
def __init__(self, x, y, z, tx, ty, w=None, bbox=[None] * 4, kx=3, ky=3, eps=None): """ Input: x,y,z - 1-d sequences of data points (order is not important) tx,ty - strictly ordered 1-d sequences of knots coordinates. Optional input: w - positive 1-d sequence of weights bbox - 4-sequence specifying the boundary of the rectangular approximation domain. By default, bbox=[min(x,tx),max(x,tx), min(y,ty),max(y,ty)] kx,ky=3,3 - degrees of the bivariate spline. eps - a threshold for determining the effective rank of an over-determined linear system of equations. 0 < eps < 1, default is 1e-16. """ nx = 2 * kx + 2 + len(tx) ny = 2 * ky + 2 + len(ty) tx1 = zeros((nx, ), float) ty1 = zeros((ny, ), float) tx1[kx + 1:nx - kx - 1] = tx ty1[ky + 1:ny - ky - 1] = ty xb, xe, yb, ye = bbox tx1,ty1,c,fp,ier = dfitpack.surfit_lsq(x,y,z,tx1,ty1,w,\ xb,xe,yb,ye,\ kx,ky,eps,lwrk2=1) if ier > 10: tx1,ty1,c,fp,ier = dfitpack.surfit_lsq(x,y,z,tx1,ty1,w,\ xb,xe,yb,ye,\ kx,ky,eps,lwrk2=ier) if ier in [0, -1, -2]: # normal return pass else: if ier < -2: deficiency = (nx - kx - 1) * (ny - ky - 1) + ier message = _surfit_messages.get(-3) % (deficiency) else: message = _surfit_messages.get(ier, 'ier=%s' % (ier)) warnings.warn(message) self.fp = fp self.tck = tx1, ty1, c self.degrees = kx, ky
def __init__(self, x, y, z, tx, ty, w=None, bbox = [None]*4, kx=3, ky=3, eps=None): """ Input: x,y,z - 1-d sequences of data points (order is not important) tx,ty - strictly ordered 1-d sequences of knots coordinates. Optional input: w - positive 1-d sequence of weights bbox - 4-sequence specifying the boundary of the rectangular approximation domain. By default, bbox=[min(x,tx),max(x,tx), min(y,ty),max(y,ty)] kx,ky=3,3 - degrees of the bivariate spline. eps - a threshold for determining the effective rank of an over-determined linear system of equations. 0 < eps < 1, default is 1e-16. """ nx = 2*kx+2+len(tx) ny = 2*ky+2+len(ty) tx1 = zeros((nx,),float) ty1 = zeros((ny,),float) tx1[kx+1:nx-kx-1] = tx ty1[ky+1:ny-ky-1] = ty xb,xe,yb,ye = bbox tx1,ty1,c,fp,ier = dfitpack.surfit_lsq(x,y,z,tx1,ty1,w,\ xb,xe,yb,ye,\ kx,ky,eps,lwrk2=1) if ier>10: tx1,ty1,c,fp,ier = dfitpack.surfit_lsq(x,y,z,tx1,ty1,w,\ xb,xe,yb,ye,\ kx,ky,eps,lwrk2=ier) if ier in [0,-1,-2]: # normal return pass else: if ier<-2: deficiency = (nx-kx-1)*(ny-ky-1)+ier message = _surfit_messages.get(-3) % (deficiency) else: message = _surfit_messages.get(ier,'ier=%s' % (ier)) warnings.warn(message) self.fp = fp self.tck = tx1,ty1,c self.degrees = kx,ky
def bisplrep(x,y,z,w=None,xb=None,xe=None,yb=None,ye=None,kx=3,ky=3,task=0, s=None,eps=1e-16,tx=None,ty=None,full_output=0, nxest=None,nyest=None,quiet=1): """Find a bivariate B-spline representation of a surface. Description: Given a set of data points (x[i], y[i], z[i]) representing a surface z=f(x,y), compute a B-spline representation of the surface. Inputs: x, y, z -- Rank-1 arrays of data points. w -- Rank-1 array of weights. By default w=ones(len(x)). xb, xe -- End points of approximation interval in x. yb, ye -- End points of approximation interval in y. By default xb, xe, yb, ye = x.min(), x.max(), y.min(), y.max() kx, ky -- The degrees of the spline (1 <= kx, ky <= 5). Third order (kx=ky=3) is recommended. task -- If task=0, find knots in x and y and coefficients for a given smoothing factor, s. If task=1, find knots and coefficients for another value of the smoothing factor, s. bisplrep must have been previously called with task=0 or task=1. If task=-1, find coefficients for a given set of knots tx, ty. s -- A non-negative smoothing factor. If weights correspond to the inverse of the standard-deviation of the errors in z, then a good s-value should be found in the range (m-sqrt(2*m),m+sqrt(2*m)) where m=len(x) eps -- A threshold for determining the effective rank of an over-determined linear system of equations (0 < eps < 1) --- not likely to need changing. tx, ty -- Rank-1 arrays of the knots of the spline for task=-1 full_output -- Non-zero to return optional outputs. nxest, nyest -- Over-estimates of the total number of knots. If None then nxest = max(kx+sqrt(m/2),2*kx+3), nyest = max(ky+sqrt(m/2),2*ky+3) quiet -- Non-zero to suppress printing of messages. Outputs: (tck, {fp, ier, msg}) tck -- A list [tx, ty, c, kx, ky] containing the knots (tx, ty) and coefficients (c) of the bivariate B-spline representation of the surface along with the degree of the spline. fp -- The weighted sum of squared residuals of the spline approximation. ier -- An integer flag about splrep success. Success is indicated if ier<=0. If ier in [1,2,3] an error occurred but was not raised. Otherwise an error is raised. msg -- A message corresponding to the integer flag, ier. Remarks: SEE bisplev to evaluate the value of the B-spline given its tck representation. See also: splprep, splrep, splint, sproot, splev - evaluation, roots, integral UnivariateSpline, BivariateSpline - an alternative wrapping of the FITPACK functions """ x,y,z=map(myasarray,[x,y,z]) x,y,z=map(ravel,[x,y,z]) # ensure 1-d arrays. m=len(x) if not (m==len(y)==len(z)): raise TypeError, 'len(x)==len(y)==len(z) must hold.' if w is None: w=ones(m,float) else: w=myasarray(w) if not len(w) == m: raise TypeError,' len(w)=%d is not equal to m=%d'%(len(w),m) if xb is None: xb=x.min() if xe is None: xe=x.max() if yb is None: yb=y.min() if ye is None: ye=y.max() if not (-1<=task<=1): raise TypeError, 'task must be either -1,0, or 1' if s is None: s=m-sqrt(2*m) if task==-1: if tx is None: raise TypeError, 'Knots_x must be given for task=-1' _surfit_cache['tx']=myasarray(tx) if ty is None: raise TypeError, 'Knots_y must be given for task=-1' _surfit_cache['ty']=myasarray(ty) if nx<2*kx+2: raise TypeError,'There must be at least 2*kx+2 knots_x for task=-1' if ny<2*ky+2: raise TypeError,'There must be at least 2*ky+2 knots_y for task=-1' if not ((1<=kx<=5) and (1<=ky<=5)): raise TypeError, 'Given degree of the spline (kx,ky=%d,%d) is not \ supported. (1<=k<=5)'%(kx,ky) if m<(kx+1)*(ky+1): raise TypeError, 'm>=(kx+1)(ky+1) must hold' if nxest is None: nxest=kx+sqrt(m/2) if nyest is None: nyest=ky+sqrt(m/2) nxest,nyest=max(nxest,2*kx+3),max(nyest,2*ky+3) if task>=0 and s==0: nxest=int(kx+sqrt(3*m)) nyest=int(ky+sqrt(3*m)) if task == 0: nx,tx,ny,ty,c,fp,wrk1,ier = dfitpack.surfit_smth0(x,y,z,w,xb,xe,yb,ye, kx,ky,s=s,eps=eps, nxest=nxest,nyest=nyest) elif task==1: try: tx,ty=_surfit_cache['tx'],_surfit_cache['ty'] nx,ny= _surfit_cache['nx'],_surfit_cache['ny'] wrk1 = _surfit_cache['wrk1'] except KeyError: raise ValueError, 'task=1 can only be called after task=0' nx,tx,ny,ty,c,fp,wrk1,ier=dfitpack.surfit_smth1(x,y,z,nx,tx,ny,ty,wrk1, w,xb,xe,yb,ye,kx,ky,s=s,eps=eps, nxest=nxest,nyest=nyest) elif task==-1: tx = myasarray(tx) ty = myasarray(ty) tx,ty,c,fp,ier = dfitpack.surfit_lsq(x,y,z,tx,ty,w, xb,xe,yb,ye, kx,ky,eps, nxest=nxest,nyest=nyest) if ier>10: tx,ty,c,fp,ier = dfitpack.surfit_lsq(x,y,z,tx,ty,w,\ xb,xe,yb,ye,\ kx,ky,eps,\ nxest=nxest,nyest=nyest) if task>=0: _surfit_cache['tx']=tx _surfit_cache['ty']=ty _surfit_cache['nx']=nx _surfit_cache['ny']=ny _surfit_cache['wrk1']=wrk1 tck=[tx[:nx],ty[:ny],c[:(nx-kx-1)*(ny-ky-1)],kx,ky] if ier<=0 and not quiet: print _iermess2[ier][0] print "\tkx,ky=%d,%d nx,ny=%d,%d m=%d fp=%f s=%f"%(kx,ky,len(tx), len(ty),m,fp,s) ierm=min(11,max(-3,ier)) if ierm>0 and not full_output: if ier in [1,2,3,4,5]: print "Warning: "+_iermess2[ierm][0] print "\tkx,ky=%d,%d nx,ny=%d,%d m=%d fp=%f s=%f"%(kx,ky,len(tx), len(ty),m,fp,s) else: try: raise _iermess2[ierm][1],_iermess2[ierm][0] except KeyError: raise _iermess2['unknown'][1],_iermess2['unknown'][0] if full_output: try: return tck,fp,ier,_iermess2[ierm][0] except KeyError: return tck,fp,ier,_iermess2['unknown'][0] else: return tck