Thin Plate Spline (TPS)
Python (NumPy & SciPy) implementation of the generalized Polyharmonic Spline interpolation (also known as Thin Plate Spline in 2D). It learns a smooth elastic mapping between two Euclidean spaces with support for:
- Arbitrary input and output dimensions
- Arbitrary spline order
k - Optional regularization
Useful for interpolation, deformation fields, and smooth non-linear regression.
For a faster implementation in PyTorch, see torch-tps.
🚀 Install
Pip
$ pip install thin-plate-spline
From source
git clone git@github.com:raphaelreme/tps.git # OR https://github.com/raphaelreme/tps.git
cd tps
pip install .
Getting started
import numpy as np
from tps import ThinPlateSpline
# Control points
X_train = np.random.normal(0, 1, (800, 3)) # 800 points in R^3
Y_train = np.random.normal(0, 1, (800, 2)) # Values for each point (800 values in R^2)
# New source points to interpolate
X_test = np.random.normal(0, 1, (300 0, 3))
# Initialize spline model (Regularization is controlled with alpha parameter)
tps = ThinPlateSpline(alpha=0.5)
# Fit spline from control points
tps.fit(X_train, Y_train)
# Interpolate new points
Y_test = tps.transform(X_test)
Examples
See the example/ folder for scripts showing:
- Interpolation in 1D, 2D, 3D
- Arbitrary input and output dimensions
- Image warping with elastic deformation
Image Warping
Example of increasing/decreasing/randomly deforming a dog's face using sparse control points.
Code: example/image_warping.py
🧠 Theory Summary
The model solves the regularized interpolation problem:
$$ min_f \sum_{i=1}^n (y_i - f(x_i))^2 + \int |\nabla^{\text{order}} f|_2^2 dx $$
With solution:
$$ f(x) = P(x) + \sum_{i=1}^n w_i G(|x - x_i|_2) $$
Where:
- $G(r)$: radial basis function (RBF) (depends on
orderand the input dimensiond) - $P(x)$: a polynomial of degree
order - 1
Default kernel (TPS):
- $G(r) = r^2 \log(r)$
General kernel:
- $G(r) = r^{(2 \text{order} - d)} \text{ if d is odd}$
- $G(r) = r^{(2\text{order} - d)} \log(r) \text{ otherwise}$
🔧 API
ThinPlateSpline(alpha=0.0, order=2, enforce_tps_kernel=False)
Creates a general polyharmonic spline interpolator (Default to TPS in 2D and natural cubic splines in 1D).
- alpha (float): Regularization strength (default 0.0)
- order (int): Spline order (default is 2 for TPS)
- enforce_tps_kernel (bool): Force TPS kernel r^2 log r, even when mathematically suboptimal
.fit(X, Y)
Fits the model to control point pairs.
- X:
(n, d)input coordinates - Y:
(n, v)target coordinates
Returns: self
.transform(X)
Applies the learned mapping to new input points.
- X:
(n', d)points
Returns: (n', v) interpolated values
License
MIT License
Metadata
Release files for thin-plate-spline 1.2.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| thin_plate_spline-1.2.2.tar.gz | 7.8 kB | Without provenance | Details for thin_plate_spline-1.2.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| thin_plate_spline-1.2.2-py3-none-any.whl | Python 3 | none | any | Details for thin_plate_spline-1.2.2-py3-none-any.whl |
Total release size: 17.4 kB
Release files / thin_plate_spline-1.2.2.tar.gz
| Download URL | thin_plate_spline-1.2.2.tar.gz |
|---|---|
| Size | 7.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fd5631778a0bf3a58eec7673c601936b743fd9c82344239b8779b213be1a9d0b
|
|
BLAKE2b-256 checksum How to use checksums |
e988458d27807c54a6a9eea8c248573267314e4d2385cb9f1bf4c77e05dfa7bd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|
Release files / thin_plate_spline-1.2.2-py3-none-any.whl
| Download URL | thin_plate_spline-1.2.2-py3-none-any.whl |
|---|---|
| Size | 9.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9cee92540c89ebebed860e2bb093afc8692eb5bb1c019b6fb57d74b2fde7418f
|
|
BLAKE2b-256 checksum How to use checksums |
c9abb840ace5889cac5f527960de068e36e05411aa26924a2da440ba33b3acf1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|
Security for thin-plate-spline 1.2.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.