ml4gw.transforms.spline_interpolation

Adaptation of code from https://github.com/dottormale/Qtransform_torch/

Classes

SplineInterpolate1D(x_in[, kx, sx, x_out])

Perform 1D spline interpolation based on De Boor's method.

SplineInterpolate2D(x_in, y_in[, kx, ky, ...])

Perform 2D spline interpolation based on De Boor's method.

SplineInterpolateBase(*args, **kwargs)

Base class for spline interpolation.

class ml4gw.transforms.spline_interpolation.SplineInterpolate1D(x_in, kx=3, sx=0.0, x_out=None)

Bases: SplineInterpolateBase

Perform 1D spline interpolation based on De Boor's method. It is allowed to have two spatial dimensions, but the second dimension cannot be interpolated along. To interpolate along both dimensions, use SplineInterpolate2D.

Supports batched, multi-channel inputs, so acceptable data shapes are (width), (height, width), (batch, width), (batch, height, width), (batch, channel, width), and (batch, channel, height, width).

During initialization of this Module, both the desired input and output coordinate Tensors can be specified to allow pre-computation of the B-spline basis matrices, though the only mandatory argument is the coordinates of the data along the width dimension.

Unlike scipy's implementation of spline interpolation, the data to be interpolated is not passed until actually calling the object. This is useful for cases where the input and output coordinates are known in advance, but the data is not, so that the interpolator can be set up ahead of time.

Parameters:
  • x_in (Tensor) -- Coordinates of the width dimension of the data

  • kx (int) -- Degree of spline interpolation along the width dimension. Default is cubic.

  • sx (float) -- Regularization factor to avoid singularities during matrix inversion for interpolation along the width dimension. Not to be confused with the s parameter in scipy's spline methods, which controls the number of knots.

  • x_out (Optional[Tensor]) -- Coordinates for the data to be interpolated to along the width dimension. If not specified during initialization, this must be specified during the object call.

evaluate_spline(C)

Evaluate a bivariate spline on a grid of x and y points.

Parameters:

C (Tensor) -- Coefficient tensor of shape (batch_size, mx, my).

Returns:

Interpolated values at the grid points.

Return type:

Z_interp

forward(Z, x_out=None)

Compute the interpolated data

Parameters:
  • Z (Tensor) -- Tensor of data to be interpolated. Must be between 2 and 4 dimensions. The shape of the tensor must agree with the input coordinates given on initialization.

  • x_out (Optional[Tensor]) -- Coordinates to interpolate the data to along the width dimension. Overrides any value that was set during initialization.

Return type:

Tensor

Returns:

A 4D tensor with shape (batch, channel, height, width). Depending on the input data shape, many of these dimensions may have length 1.

spline_fit_natural(Z)
class ml4gw.transforms.spline_interpolation.SplineInterpolate2D(x_in, y_in, kx=3, ky=3, sx=0.0, sy=0.0, x_out=None, y_out=None)

Bases: SplineInterpolateBase

Perform 2D spline interpolation based on De Boor's method. Supports batched, multi-channel inputs, so acceptable data shapes are (height, width), (batch, height, width), and (batch, channel, height, width).

During initialization of this Module, both the desired input and output coordinate Tensors can be specified to allow pre-computation of the B-spline basis matrices, though the only mandatory arguments are the input coordinates.

Unlike scipy's implementation of spline interpolation, the data to be interpolated is not passed until actually calling the object. This is useful for cases where the input and output coordinates are known in advance, but the data is not, so that the interpolator can be set up ahead of time.

Parameters:
  • x_in (Tensor) -- Coordinates of the width dimension of the data

  • y_in (Tensor) -- Coordinates of the height dimension of the data.

  • kx (int) -- Degree of spline interpolation along the width dimension. Default is cubic.

  • ky (int) -- Degree of spline interpolation along the height dimension. Default is cubic.

  • sx (float) -- Regularization factor to avoid singularities during matrix inversion for interpolation along the width dimension. Not to be confused with the s parameter in scipy's spline methods, which controls the number of knots.

  • sy (float) -- Regularization factor to avoid singularities during matrix inversion for interpolation along the height dimension.

  • x_out (Optional[Tensor]) -- Coordinates for the data to be interpolated to along the width dimension. If not specified during initialization, this must be specified during the object call.

  • y_out (Optional[Tensor]) -- Coordinates for the data to be interpolated to along the height dimension. If not specified during initialization, this must be specified during the object call.

bivariate_spline_fit_natural(Z)
evaluate_bivariate_spline(C)

Evaluate a bivariate spline on a grid of x and y points.

Parameters:

C (Tensor) -- Coefficient tensor of shape (batch_size, mx, my).

Returns:

Interpolated values at the grid points.

Return type:

Z_interp

forward(Z, x_out=None, y_out=None)

Compute the interpolated data

Parameters:
  • Z (Tensor) -- Tensor of data to be interpolated. Must be between 2 and 4 dimensions. The shape of the tensor must agree with the input coordinates given on initialization.

  • x_out (Optional[Tensor]) -- Coordinates to interpolate the data to along the width dimension. Overrides any value that was set during initialization.

  • y_out (Optional[Tensor]) -- Coordinates to interpolate the data to along the height dimension. Overrides any value that was set during initialization.

Return type:

Tensor

Returns:

A 4D tensor with shape (batch, channel, height, width). Depending on the input data shape, many of these dimensions may have length 1.

class ml4gw.transforms.spline_interpolation.SplineInterpolateBase(*args, **kwargs)

Bases: Module

Base class for spline interpolation.

bspline_basis_natural(x, k, t)

Compute bspline basis function using de Boor's recursive formula (See https://en.wikipedia.org/wiki/De_Boor%27s_algorithm for reference) :type x: Tensor :param x: Tensor of data point positions. :type k: int :param k: Degree of the spline. :type t: Tensor :param t: Tensor of knot positions.

Return type:

Tensor

Returns:

Tensor containing the kth-order B-spline basis functions

Parameters:
  • x (Tensor)

  • k (int)

  • t (Tensor)

compute_L_R(x, t, d, m)

Compute the L and R values for B-spline basis functions. L and R are respectively the first and second coefficient multiplying B_{i,p-1}(x) and B_{i+1,p-1}(x) in De Boor's recursive formula for Bspline basis funciton computation See https://en.wikipedia.org/wiki/De_Boor%27s_algorithm for details

Parameters:
  • x (Tensor) -- Tensor of data point positions.

  • t (Tensor) -- Tensor of knot positions.

  • d (int) -- Current degree of the basis function.

  • m (int) -- Number of intervals (n - k - 1, where n is the number of knots and k is the degree).

Returns:

Tensor containing left values for the B-spline basis functions. R: Tensor containing right values for the B-spline basis functions.

Return type:

L

generate_fitpack_knots(x, k)

Generates a knot sequence for B-spline interpolation in the same way as the FITPACK algorithm used by SciPy.

Parameters:
  • x (Tensor) -- Tensor of data point positions.

  • k (int) -- Degree of the spline.

Return type:

Tensor

Returns:

Tensor of knot positions.

zeroth_order(x, k, t, n, m)

Compute the zeroth-order B-spline basis functions according to de Boors recursive formula. See https://en.wikipedia.org/wiki/De_Boor%27s_algorithm for reference

Parameters:
  • x (Tensor) -- Tensor of data point positions.

  • k (int) -- Degree of the spline.

  • t (Tensor) -- Tensor of knot positions.

  • n (int) -- Number of data points.

  • m (int) -- Number of intervals (n - k - 1, where n is the number of knots and k is the degree).

Returns:

Tensor containing the zeroth-order B-spline basis functions.

Return type:

b