TensorSpline#

class splineops.interpolate.tensorspline.TensorSpline(data: ndarray[Any, dtype[_ScalarType_co]], coordinates: ndarray[Any, dtype[_ScalarType_co]] | Sequence[ndarray[Any, dtype[_ScalarType_co]]], bases: SplineBasis | str | Sequence[SplineBasis | str], modes: ExtensionMode | str | Sequence[ExtensionMode | str])#

Bases: object

A class to handle a tensor spline for multi-dimensional interpolation and approximation.

This class allows you to store N-dimensional data and perform interpolation using a variety of spline bases and extension modes. It is flexible and can handle different extension modes and spline bases.

Parameters:
  • data (array_like) – The input N-dimensional array to be interpolated.

  • coordinates (array_like) – The coordinates corresponding to the input data.

  • bases (str or sequence of str) –

    The spline bases used for interpolation. It can be a single basis applied across all axes or a sequence of bases for each axis.

    The following spline bases are available:

    • ”bspline0”, “bspline0-sym”: Zero-degree or piecewise constant B-splines and symmetric zero-degree or piecewise constant B-splines.

    • ”bspline1” to “bspline9”: First to ninth-degree B-splines.

    • ”omoms0”, “omoms0-sym”: Zero-degree O-MOMS splines and symmetric zero-degree O-MOMS splines.

    • ”omoms1” to “omoms5”: First to fifth-degree O-MOMS splines.

    • ”omoms2-sym”, “omoms4-sym”: Symmetric second and fourth-degree O-MOMS splines.

    • ”nearest”, “nearest-sym”: Nearest neighbor interpolation.

    • ”linear”: Linear interpolation.

    • ”keys”: Keys spline interpolation.

  • modes (str or sequence of str) –

    Signal extension modes used to handle boundaries. It can be a single mode applied across all axes or a sequence of modes for each axis.

    The following extension modes are available for handling boundaries:

    • ”zero” (0 0 0 0 | a b c d | 0 0 0 0) The input is extended by filling all values beyond the boundary with zeroes.

    • ”mirror” (d c b | a b c d | c b a) The input is extended by reflecting around the center of the data points adjacent to the border.

Example

  1. 1D Interpolation:

Here’s an example to illustrate 1-dimensional interpolation using the TensorSpline class.

>>> import numpy as np
>>> from splineops.interpolate.tensorspline import TensorSpline
>>> data = np.array([1.0, 2.0, 3.0, 4.0])
>>> coordinates = np.linspace(0, data.size - 1, data.size)
>>> bases = "linear"  # Linear interpolation
>>> modes = "mirror"  # Mirror extension mode
>>> tensor_spline = TensorSpline(data=data, coordinates=coordinates, bases=bases, modes=modes)

To interpolate the data at a new point:

>>> eval_coords = np.array([1.5])
>>> data_eval = tensor_spline(coordinates=eval_coords, grid=False)
>>> print(data_eval)
[2.5]

In this example, the interpolated value at x = 1.5 is 2.5, which is the midpoint between data[1] (2.0) and data[2] (3.0).

  1. 2D Interpolation:

Here’s a simple example to illustrate 2-dimensional interpolation using the TensorSpline class.

>>> a = np.arange(12.).reshape((4, 3))
>>> a
array([[ 0.,  1.,  2.],
       [ 3.,  4.,  5.],
       [ 6.,  7.,  8.],
       [ 9., 10., 11.]])
>>> xx = np.linspace(0, a.shape[0] - 1, a.shape[0])
>>> yy = np.linspace(0, a.shape[1] - 1, a.shape[1])
>>> coordinates = xx, yy
>>> bases = ["bspline1", "bspline1"]  # Linear interpolation along both axes
>>> modes = ["mirror", "mirror"]      # Mirror extension mode handling along both axes
>>> tensor_spline = TensorSpline(data=a, coordinates=coordinates, bases=bases, modes=modes)

To interpolate the array a at coordinates (0.5, 0.5) and (2, 1):

>>> eval_coords = np.array([[0.5, 2], [0.5, 1]])
>>> data_eval_pts = tensor_spline(coordinates=eval_coords, grid=False)
>>> print(data_eval_pts)
[2. 7.]

In this example, the interpolated value at (0.5, 0.5) is 2.0, and the value at (2, 1) is 7.0.

__init__(data: ndarray[Any, dtype[_ScalarType_co]], coordinates: ndarray[Any, dtype[_ScalarType_co]] | Sequence[ndarray[Any, dtype[_ScalarType_co]]], bases: SplineBasis | str | Sequence[SplineBasis | str], modes: ExtensionMode | str | Sequence[ExtensionMode | str]) None#

Initialize the TensorSpline object with the given data, coordinates, bases, and modes.

Parameters:
  • data (array_like) – The input N-dimensional array to be interpolated.

  • coordinates (array_like) – The coordinates corresponding to the input data.

  • bases (str or sequence of str) – The spline bases used for interpolation. It can be a single basis applied across all axes or a sequence of bases for each axis.

  • modes (str or sequence of str) – Signal extension modes used to handle boundaries. It can be a single mode applied across all axes or a sequence of modes for each axis.

Example

>>> a = np.arange(12.).reshape((4, 3))
>>> xx = np.linspace(0, a.shape[0] - 1, a.shape[0])
>>> yy = np.linspace(0, a.shape[1] - 1, a.shape[1])
>>> coordinates = xx, yy
>>> tensor_spline = TensorSpline(data=a, coordinates=coordinates, bases="bspline1", modes="mirror")
eval(coordinates: ndarray[Any, dtype[_ScalarType_co]] | Sequence[ndarray[Any, dtype[_ScalarType_co]]], grid: bool = True) ndarray[Any, dtype[_ScalarType_co]]#

Evaluate the tensor spline at the given coordinates.

Parameters:
  • coordinates (array_like) – The coordinates at which to evaluate the tensor spline. If grid is True, must be a sequence of 1-D arrays representing the grid points along each axis. If grid is False, must be a sequence of N-D arrays of the same shape.

  • grid (bool, optional) – If True (default), assumes the input coordinates define a grid and evaluates the tensor spline over this grid. If False, treats the input coordinates as a list of points at which to evaluate the tensor spline.

Returns:

data – The interpolated values at the specified coordinates.

Return type:

ndarray

Example

>>> a = np.arange(12.).reshape((4, 3))
>>> xx = np.linspace(0, a.shape[0] - 1, a.shape[0])
>>> yy = np.linspace(0, a.shape[1] - 1, a.shape[1])
>>> coordinates = xx, yy
>>> tensor_spline = TensorSpline(data=a, coordinates=coordinates, bases="bspline1", modes="mirror")
>>> eval_coords = np.array([[0.5, 2], [0.5, 1]])
>>> data_eval_pts = tensor_spline.eval(coordinates=eval_coords, grid=False)
>>> print(data_eval_pts)
[2. 7.]