bag.math.interpolate

This module defines various interpolation classes.

Module Contents

Classes

LinearInterpolator

A linear interpolator on a regular grid for arbitrary dimensions.

Functions

interpolate_grid(→ bag.math.dfun.DiffFunction)

Interpolates multidimensional data on a regular grid.

bag.math.interpolate.interpolate_grid(scale_list: List[Tuple[float, float]], values: numpy.ndarray, method: str = 'spline', extrapolate: bool = False, delta: float = 0.0001, num_extrapolate: int = 3) bag.math.dfun.DiffFunction[source]

Interpolates multidimensional data on a regular grid.

returns an Interpolator for the given dataset.

Parameters:
  • scale_list (List[Tuple[float, float]]) – a list of (offset, spacing).

  • values (np.ndarray) – The output data in N dimensions. The length in each dimension must be at least 2.

  • method (str) – The interpolation method. Either ‘linear’, or ‘spline’. Defaults to ‘spline’.

  • extrapolate (bool) – True to extrapolate data output of given bounds. Defaults to False.

  • delta (float) – the finite difference step size. Finite difference is only used for linear interpolation and spline interpolation on 3D data or greater. Defaults to 1e-4 of the grid spacing.

  • num_extrapolate (int) – If spline interpolation is selected on 3D data or greater, we linearly extrapolate the given data by this many points to fix behavior near input boundaries.

Returns:

fun – the interpolator function.

Return type:

DiffFunction

class bag.math.interpolate.LinearInterpolator(points: Sequence[numpy.ndarray], values: numpy.ndarray, delta_list: List[float], extrapolate: bool = False)[source]

Bases: bag.math.dfun.DiffFunction

A linear interpolator on a regular grid for arbitrary dimensions.

This class is backed by scipy.interpolate.RegularGridInterpolator. Derivatives are calculated using finite difference.

Parameters:
  • points (Sequence[np.ndarray]) – list of points of each dimension.

  • values (np.ndarray) – The output data in N dimensions.

  • delta_list (List[float]) – list of finite difference step size for each axis.

  • extrapolate (bool) – True to extrapolate data output of given bounds. Defaults to False.

get_input_points(idx: int) numpy.ndarray[source]

Returns the input points for the given dimension.

__call__(xi: numpy.ndarray) numpy.ndarray[source]

Interpolate at the given coordinate.

Parameters:

xi (numpy.ndarray) – The coordinates to evaluate, with shape (…, ndim)

Returns:

val – The interpolated values at the given coordinates.

Return type:

numpy.ndarray

integrate(xstart: float, xstop: float, axis: int = -1, logx: bool = False, logy: bool = False, raw: bool = False) Union[LinearInterpolator, numpy.ndarray][source]

Integrate away the given axis.

if logx/logy is True, that means this LinearInterpolator is actually used to do linear interpolation on the logarithm of the actual data. This method will returns the integral of the actual data.

Parameters:
  • xstart (float) – the X start value.

  • xstop (float) – the X stop value.

  • axis (int) – the axis of integration. If unspecified, this will be the last axis.

  • logx (bool) – True if the values on the given axis are actually the logarithm of the real values.

  • logy (bool) – True if the Y values are actually the logarithm of the real values.

  • raw (bool) – True to return the raw data points instead of a LinearInterpolator object.

Returns:

result – float if this interpolator has only 1 dimension, otherwise a new LinearInterpolator is returned.

Return type:

Union[LinearInterpolator, np.ndarray]