midgard.math.interpolation

Methods for interpolating in numpy arrays

Description:

Different interpolation methods are decorated with @register_interpolator and will then become available for use as kind in interpolate and moving_window.

Example:

>>> import numpy as np
>>> np.set_printoptions(precision=3, suppress=True)
>>> x = np.linspace(-1, 1, 11)
>>> y = x**3 - x
>>> y
array([ 0.   ,  0.288,  0.384,  0.336,  0.192,  0.   , -0.192, -0.336,
       -0.384, -0.288,  0.   ])

>>> x_new = np.linspace(-0.8, 0.8, 11)
>>> interpolate(x, y, x_new, kind='cubic')
array([ 0.288,  0.378,  0.369,  0.287,  0.156, -0.   , -0.156, -0.287,
       -0.369, -0.378, -0.288])

Developer info:

To add your own interpolators, you can simply decorate your interpolator functions with @register_interpolator. Your interpolator function should have the signature

(x: np.ndarray, y: np.ndarray) -> Callable

For instance, the following would implement a terrible interpolation function that sets all values to zero:

from midgard.math.interpolation import register_interpolator

@register_interpolator
def zero(x: np.ndarray, y: np.ndarray) -> Callable:

    def _zero(x_new: np.ndarray) -> np.ndarray:
        return np.zeros(y.shape)

    return _zero

This function would then be available as an interpolator. For instance, one could do

>>> interpolate(x, y, x_new, kind='zero')  # doctest: +SKIP
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])

barycentric_interpolator()

barycentric_interpolator(x:numpy.ndarray, y:numpy.ndarray, **ipargs:Any) -> Callable

The interpolating polynomial through the given points

Uses the scipy.interpolate.BarycentricInterpolator function behind the scenes.

Args:

Returns:

Barycentric interpolation function

cubic()

cubic(x:numpy.ndarray, y:numpy.ndarray, **ipargs:Any) -> Callable

Cubic spline interpolation through the given points

Uses the scipy.interpolate.interp1d function with kind='cubic' behind the scenes.

Args:

Returns:

Cubic spline interpolation function

get_interpolator()

get_interpolator(name:str) -> Callable

Return an interpolation function

Interpolation functions are registered by the @register_interpolator-decorator. The name-parameter corresponds to the function name of the interpolator.

Args:

Returns:

Interpolation function with the given name.

interpolate()

interpolate(x:numpy.ndarray, y:numpy.ndarray, x_new:numpy.ndarray, *, kind:str, **ipargs:Any) -> numpy.ndarray

Interpolate values from one x-array to another

See interpolators() for a list of valid interpolators.

Args:

Returns:

Array of interpolated y-values.

interpolate_with_derivative()

interpolate_with_derivative(x:numpy.ndarray, y:numpy.ndarray, x_new:numpy.ndarray, *, kind:str, dx:float=0.5, **ipargs:Any) -> numpy.ndarray

Interpolate values from one x-array to another as well as find derivatives

See interpolators() for a list of valid interpolators.

Args:

Returns:

Tuple with array of interpolated y-values and array of derivatives.

interpolated_univariate_spline()

interpolated_univariate_spline(x:numpy.ndarray, y:numpy.ndarray, **ipargs:Any) -> Callable

One-dimensional interpolating spline for the given points

Uses the scipy.interpolate.InterpolatedUnivariateSpline function behind the scenes.

The original only deals with one-dimensional y arrays, so multiple calls are made for higher dimensional y arrays. The dimensions are handled independently of each other.

Args:

Returns:

Interpolating spline function

interpolators()

interpolators() -> List[str]

Return a list of available interpolators

Returns:

Names of available interpolators.

lagrange()

lagrange(x:numpy.ndarray, y:numpy.ndarray, *, window:int=10, bounds_error:bool=True, assume_sorted:bool=False) -> Callable

Computes the lagrange polynomial passing through a certain set of points

See https://en.wikipedia.org/wiki/Lagrange_polynomial

Uses window of the original points to calculate the Lagrange polynomials. The window of points is chosen by finding the closest original point and essentially picking the window // 2 indices on either side.

Args:

Returns:

Lagrange interpolation function.

linear()

linear(x:numpy.ndarray, y:numpy.ndarray, **ipargs:Any) -> Callable

Linear interpolation through the given points

Uses the scipy.interpolate.interp1d function with kind='linear' behind the scenes.

Args:

Returns:

Linear interpolation function

register_interpolator()

register_interpolator(func:Callable) -> Callable

Register an interpolation function

This function should be used as a @register_interpolator-decorator

Args:

Returns:

Same function.