pwtools.num.Spline

class pwtools.num.Spline(x, y, eps=1e-10, checkeps=True, **splrep_kwargs)[source]

Bases: Fit1D

Fit1D-based spline interpolator.

Like scipy.interpolate.UnivariateSpline, this is a wrapper around scipy.interpolate.splrep/splev with some nice features like y->x lookup and interpolation accuracy check etc. It basically simplifies setting up a spline interpolation and holds x-y data plus the spline knots (self.tck) together in one place. You can work with the methods here, but you can also use the normal tck (self.tck) in scipy.interpolate.splev() etc.

Examples

>>> from scipy.interpolate import splev
>>> from pwtools import num
>>> x = linspace(0,10,100)
>>> y = sin(x)
>>> sp = num.Spline(x,y)
>>> plot(x,y)
>>> plot(x, sp(x))
>>> plot(x, splev(x, sp.tck)) # the same
>>> plot(x, sp(x, der=1), label='1st derivative')
>>> xx = sp.invsplev(0.5, xab=[0, pi/2])
>>> print("at %f degrees, sin(x) = 0.5" %(xx/pi*180))
>>>
>>> y = x**2 - 5
>>> sp = num.Spline(x,y)
>>> print("the root is at x=%f" %sp.invsplev(0.0))
>>> legend()
__init__(x, y, eps=1e-10, checkeps=True, **splrep_kwargs)[source]
Parameters:
  • x (numpy 1d arrays)

  • y (numpy 1d arrays)

  • eps (float) – Accuracy threshold. Spline must interpolate points with an error less then eps. Useless if you use splrep(…,s=..) with “s” (the smoothing factor) much bigger than 0. See ckeckeps.

  • checkeps (bool) – Whether to use eps to ckeck interpolation accuracy.

  • **splrep_kwargs (keywords args to splrep(), default: k=3, s=0)

__call__(x, *args, **kwargs)[source]

Call self as a function.

Methods

get_max([x0, xab])

Convenience method.

get_min([x0, xab])

Return x where y(x) = min(y) by calculating the root of the fit's 1st derivative (by calling self(x, der=1)).

get_root([x0, xab])

Return x where y(x) = 0 by calculating the root of the fit function.

invsplev(y0[, x0, xab])

Lookup x for a given y, i.e. "inverse spline evaluation", hence the name.

is_mono()

Return True if the curve described by the fit function f(x) is monotonic.

splev(x, *args, **kwds)