pwtools.num.Interpol2D

class pwtools.num.Interpol2D(points=None, values=None, xx=None, yy=None, dd=None, what='rbf_inv_multi', **initkwds)[source]

Bases: object

Common 2D interpolator API.

The API is the same as in scipy.interpolate, e.g.

>>> inter = scipy.interpolate.SomeInterpolatorClass(points, values)
>>> new_values = inter(new_points)

This is for easy testing of multiple interpolators on a surface z = f(x,y), which is given as an unordered set of points.

__init__(points=None, values=None, xx=None, yy=None, dd=None, what='rbf_inv_multi', **initkwds)[source]
Parameters:
  • points ((npoints, 2)) –

  • values ((npoints, 1)) –

  • xx ((npoints, 1)) – Use either points + values or xx + yy + values or dd.

  • yy ((npoints, 1)) – Use either points + values or xx + yy + values or dd.

  • dd (pwtools.mpl.Data2D instance) –

  • what (str, optional) –

    which interpolator to use

    • ’rbf_multi’ : RBFN w/ multiquadric rbf, see Rbf

    • ’rbf_inv_multi’ : RBFN w/ inverse multiquadric rbf

    • ’rbf_gauss’ : RBFN w/ gaussian rbf

    • ’poly’ : PolyFit

    • ’bispl’ : scipy.interpolate.bispl{rep,ev}

    • ’ct’ : scipy.interpolate.CloughTocher2DInterpolator

    • ’linear’ : scipy.interpolate.LinearNDInterpolator

    • ’nearest’ : scipy.interpolate.NearestNDInterpolator

  • **initkwds – keywords passed on to the interpolator’s constructor or fit() method (RBF case)

Notes

Despite the name “Interpol2D”, the RBF methods ‘rbf_*’ as well as ‘poly’ are actually regression methods. You can force interpolation with the RBF methods using the r=0 keyword (see Rbf), which will use scipy.linalg.solve without regularization.

The methods ‘ct’, ‘linear’ and of course ‘nearest’ can be inaccurate (see also test/test_interpol.py). Use only for plotting, not for data evaluation, i.e. accurate minimas etc.

Except for ‘bispl’, all interpolators do actually work in ND as well, as does get_min().

Possible keywords (examples):

  • rbf

    • p=’mean’, r=0 (default) # linalg.solve w/o regularization

    • p=3.5, r=0 # linalg.solve w/o regularization

    • p=’scipy’, r=1e-8 # linalg.solve w/ regularization

  • ct

    • tol = 1e-6 (default)

  • bispl

    • kx = 3, ky = 3 (default)

    • s = 1e-4

    • nxest, nyest

  • poly

    • deg = 5

Examples

>>> from pwtools import num, mpl
>>> x=linspace(-5,5,20)
>>> y=x
>>> X,Y=np.meshgrid(x,y); X=X.T; Y=Y.T
>>> Z=(X+3)**2+(Y+4)**2 + 5
>>> dd=mpl.Data2D(X=X,Y=Y,Z=Z)
>>> fmt="what: {:15} target: [5,30] result: {}"
>>> for method in [('rbf_multi', dict(r=1e-10)),
...                ('rbf_inv_multi', dict(r=1e-10)),
...                ('rbf_gauss', dict(r=1e-10)),
...                ('poly', {'deg': 5}),
...                'ct', 'bispl', 'linear', 'nearest']:
...     if isinstance(method, tuple):
...         what = method[0]
...         kwds = method[1]
...     else:
...         what = method
...         kwds = {}
...     inter=num.Interpol2D(dd=dd, what=what, **kwds)
...     print(fmt.format(what, inter([[-3,-4],[0,0]])))
what: rbf_multi       target: [5,30] result: [  5.00000005  29.99999959]
what: rbf_inv_multi   target: [5,30] result: [  4.99999808  29.99999798]
what: rbf_gauss       target: [5,30] result: [  5.00000051  30.00000352]
what: poly            target: [5,30] result: [  5.  30.]
what: ct              target: [5,30] result: [  4.99762256  30.010856  ]
what: bispl           target: [5,30] result: [  5.  30.]
what: linear          target: [5,30] result: [  5.06925208  30.13850416]
what: nearest         target: [5,30] result: [  5.01385042  33.82271468]
__call__(points, **callkwds)[source]
Parameters:
  • points (2d (M,2) or 1d (N,)) – M points in 2-dim space where to evalutae the interpolator (only one in 1d case)

  • **callkwds (keywords passed to the interpolator's __call__ method) –

Returns:

Y – interpolated values

Return type:

1d array (M,)

Methods

get_min([x0])

Return [x,y] where z(x,y) = min(z) by minimizing z(x,y) w/ scipy.optimize.fmin().