Skip to main content

Cubic spline interpolation on multidimensional grids in PyTorch

Project description

torch-cubic-spline-grids

License PyPI Python Version CI codecov

Cubic spline interpolation on multidimensional grids in PyTorch.

The primary goal of this package is to provide learnable, continuous parametrisations of 1-4D spaces.


Overview

torch_cubic_spline_grids provides a set of PyTorch components called grids.

Grids are defined by

  • their dimensionality (1d, 2d, 3d, 4d...)
  • the number of points covering each dimension (resolution)
  • the number of values stored on each grid point (n_channels)
  • how we interpolate between values on grid points

All grids in this package consist of uniformly spaced points covering the full extent of each dimension.

First steps

Let's make a simple 2D grid with one value on each grid point.

import torch
from torch_cubic_spline_grids import CubicBSplineGrid2d

grid = CubicBSplineGrid2d(resolution=(5, 3), n_channels=1)
  • grid.ndim is 2
  • grid.resolution is (5, 3) (or (h, w))
  • grid.n_channels is 1
  • grid.data.shape is (1, 5, 3) (or (c, h, w))

In words, the grid extends over two dimensions (h, w) with 5 points in h and 3 points in w. There is one value stored at each point on the 2D grid. The grid data is stored in a tensor of shape (c, *grid_resolution).

We can obtain the value (interpolant) at any continuous point on the grid. The grid coordinate system extends from [0, 1] along each grid dimension. The interpolant is obtained by sequential application of cubic spline interpolation along each dimension of the grid.

coords = torch.rand(size=(10, 2))  # values in [0, 1]
interpolants = grid(coords)
  • interpolants.shape is (10, 1)

Optimisation

Values at each grid point can be optimised by minimising a loss function associated with grid interpolants. In this way the continuous space of the grid can be made to more accurately model a 1-4D space.

The image above shows the values of 6 control points on a 1D grid being optimised such that interpolating between them with cubic B-spline interpolation approximates a single oscillation of a sine wave.

Notebooks are available for this 1D example and a similar 2D example.

Types of grids

torch_cubic_spline_grids provides grids which can be interpolated with cubic B-spline interpolation or cubic Catmull-Rom spline interpolation.

spline continuity interpolating?
cubic B-spline C2 No
Catmull-Rom spline C1 Yes

If your need the resulting curve to intersect the data on the grid you should use the cubic Catmull-Rom spline grids

  • CubicCatmullRomGrid1d
  • CubicCatmullRomGrid2d
  • CubicCatmullRomGrid3d
  • CubicCatmullRomGrid4d

If you require continuous second derivatives then the cubic B-spline grids are more suitable.

  • CubicBSplineGrid1d
  • CubicBSplineGrid2d
  • CubicBSplineGrid3d
  • CubicBSplineGrid4d

Regularisation

The number of points in each dimension should be chosen such that interpolating on the grid can approximate the underlying phenomenon being modelled without overfitting. A low resolution grid provides a regularising effect by smoothing the model.

Installation

torch_cubic_spline_grids is available on PyPI

pip install torch-cubic-spline-grids

Related work

This is a PyTorch implementation of the way Warp models continuous deformation fields and locally variable optical parameters in cryo-EM images. The approach is described in Dimitry Tegunov's paper:

Many methods in Warp are based on a continuous parametrization of 1- to 3-dimensional spaces. This parameterization is achieved by spline interpolation between points on a coarse, uniform grid, which is computationally efficient. A grid extends over the entirety of each dimension that needs to be modeled. The grid resolution is defined by the number of control points in each dimension and is scaled according to physical constraints (for example, the number of frames or pixels) and available signal. The latter provides regularization to prevent overfitting of sparse data with too many parameters. When a parameter described by the grid is retrieved for a point in space (and time), for example for a particle (frame), B-spline interpolation is performed at that point on the grid. To fit a grid’s parameters, in general, a cost function associated with the interpolants at specific positions on the grid is optimized.


For a fantastic introduction to splines I recommend Freya Holmer's YouTube video.

The Continuity of Splines - YouTube

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torch_cubic_spline_grids-0.5.0.tar.gz (147.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

torch_cubic_spline_grids-0.5.0-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

Details for the file torch_cubic_spline_grids-0.5.0.tar.gz.

File metadata

  • Download URL: torch_cubic_spline_grids-0.5.0.tar.gz
  • Upload date:
  • Size: 147.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for torch_cubic_spline_grids-0.5.0.tar.gz
Algorithm Hash digest
SHA256 ed6206b3e58523b7ec2e16fd0a3f92fc8cd350b223e20e4197c7994bdf1e19e0
MD5 84e3e5e361c5ba2cd317af73863bd580
BLAKE2b-256 b4f37ed395e615571c267d33bdaaf47f9298196c06a57fe958200262c95643a8

See more details on using hashes here.

Provenance

The following attestation bundles were made for torch_cubic_spline_grids-0.5.0.tar.gz:

Publisher: deploy.yml on teamtomo/teamtomo

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file torch_cubic_spline_grids-0.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for torch_cubic_spline_grids-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7d5b749c9af376c9015674c7ff8ea65d68d1bdce4bcf4dd8dbed6eb00e2b9f8e
MD5 076c792623b7d439200d3e74db6b034e
BLAKE2b-256 e98e1fea801b03177fb68dfec8afa4927fd51529dcef532ef17a73ace9f8344d

See more details on using hashes here.

Provenance

The following attestation bundles were made for torch_cubic_spline_grids-0.5.0-py3-none-any.whl:

Publisher: deploy.yml on teamtomo/teamtomo

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page