示例#1
0
def interpolate_g(xi, yi, zi, xx, yy, knots=10, error=False, mask=None):
    """Create a grid of zi values interpolating the values from xi,yi,zi
    
    **ARGUMENTS**
    
        ========= ==================================================================
        xi,yi,zi  1D Lists  or arrays containing the values to use as base for the 
                  interpolation
        xx,yy     1D vectors or lists containing the output coordinates
        samples   tuple containing the shape of the output array.
        knots     number of knots to be used in each direction
        error     if set to true, half of the points (x, y, z) are used to create 
                  the interpolation, and half are used to evaluate the interpolation
                  error
        ========= ==================================================================
    
    
    """
    xi = array(xi)
    yi = array(yi)
    zi = array(zi)

    #print xi
    #print yi
    #print zi
    assert xi.ndim == 1, "xi must ba a 1D array or list"
    assert yi.ndim == 1, "yi must ba a 1D array or list"
    assert zi.ndim == 1, "zi must ba a 1D array or list"

    assert xx.ndim == 1, "xx must ba a 1D array or list"
    assert yy.ndim == 1, "yy must ba a 1D array or list"

    assert len(xi) == len(yi) and len(xi) == len(
        zi), "xi, yi, zi must have the same number of items"

    if error == True:
        # Create a list of indexes to be able to select the points that are going
        # to be used as spline generators, and as control points
        idx = where(arange(len(xi)) % 2 == 0, False, True)

# Use only half of the samples to create the Spline,
    if error == True:
        isp = argwhere(idx == True)
        ich = argwhere(idx == False)

        xsp = xi[isp]
        ysp = yi[isp]
        zsp = zi[isp]

        xch = xi[ich]
        ych = yi[ich]
        zch = zi[ich]
    else:
        xsp = xi
        ysp = yi
        zsp = zi

    #Distribute homogeneously the knots
    xk = linspace(xsp.min(), xsp.max(), knots)
    yk = linspace(ysp.min(), ysp.max(), knots)

    # LSQBivariateSpline using some knots gives smaller error than
    # SmoothBivariateSpline
    di = interpolate.LSQBivariateSpline(xsp, ysp, zsp, xk[1:-1], yk[1:-1])
    #print xsp,ysp,zsp
    #di=interpolate.SmoothBivariateSpline(xsp, ysp, zsp)

    # Evaluate error
    if error == True:
        zch1 = di.ev(xch, ych)
        er = (zch.flatten() - zch1).std()

    if mask == None:
        #d=griddata(xi,  yi,  zi,  xx, yy) #
        d = di(xx, yy).transpose()
    else:
        d = ma_array(di(xx, yy).transpose(), mask=mask)

    if error == True: return d, er
    else: return d
示例#2
0
文件: misc.py 项目: andykee/pyoptools
def interpolate_g(xi,yi,zi,xx,yy,knots=10, error=False,mask=None):
    """Create a grid of zi values interpolating the values from xi,yi,zi
    
    xi,yi,zi  1D Lists  or arrays containing the values to use as base for the interpolation
    xx,yy     1D vectors or lists containing the output coordinates
    samples  tuple containing the shape of the output array.
    knots     number of knots to be used in each direction
    error     if set to true, half of the points (x, y, z) are used to create 
              the interpolation, and half are used to evaluate the interpolation error
    
    
    """
    xi=array(xi)
    yi=array(yi)
    zi=array(zi)
    
    #print xi
    #print yi
    #print zi
    assert xi.ndim==1 ,"xi must ba a 1D array or list" 
    assert yi.ndim==1 ,"yi must ba a 1D array or list" 
    assert zi.ndim==1 ,"zi must ba a 1D array or list" 
    
    assert xx.ndim==1 ,"xx must ba a 1D array or list" 
    assert yy.ndim==1 ,"yy must ba a 1D array or list" 
    
    assert len(xi)==len(yi) and len(xi)==len(zi), "xi, yi, zi must have the same number of items"
    
    
    
    if error==True:
        # Create a list of indexes to be able to select the points that are going 
        # to be used as spline generators, and as control points
        idx=where(arange(len(xi)) %2 ==0, False, True)

   # Use only half of the samples to create the Spline, 
    if error == True:
        isp=argwhere(idx==True)
        ich=argwhere(idx==False)
        
        xsp=xi[isp]
        ysp=yi[isp]
        zsp=zi[isp]
        
        xch=xi[ich]
        ych=yi[ich]
        zch=zi[ich]
    else:
        xsp=xi
        ysp=yi
        zsp=zi
    
    #Distribute homogeneously the knots
    xk=linspace(xsp.min(), xsp.max(),knots)
    yk=linspace(ysp.min(), ysp.max(),knots)
    
    # LSQBivariateSpline using some knots gives smaller error than 
    # SmoothBivariateSpline
    di=interpolate.LSQBivariateSpline(xsp, ysp, zsp, xk[1:-1],  yk[1:-1])
    #print xsp,ysp,zsp
    #di=interpolate.SmoothBivariateSpline(xsp, ysp, zsp)
        
    # Evaluate error
    if error==True:
        zch1=di.ev(xch, ych)
        er=(zch.flatten()-zch1).std()

    if mask==None:
        #d=griddata(xi,  yi,  zi,  xx, yy) #
        d=di(xx,yy).transpose()
    else:
        d=ma_array(di(xx,yy).transpose(), mask=mask)
    
    if error==True: return d, er
    else: return d