Skip to main content

N-dimensional interpolation/extrapolation methods

Project description

InterpN

Repo | Python Docs | Rust Docs

N-dimensional interpolation/extrapolation methods, no-std and no-alloc compatible, prioritizing correctness, performance, and compatiblity with memory-constrained environments.

Available as a rust crate and python library.

These methods perform zero allocation when evaluated (except, optionally, for the output). Because of this, they have minimal per-call overhead, and are particularly effective when examining small numbers of observation points. See the performance page for detailed benchmarks.

Features

Feature →
↓ Interpolant Method
Regular
Grid
Rectilinear
Grid
Json
Serialization
Linear
Cubic

The methods provided here, while more limited in scope than scipy's,

  • are significantly faster under most conditions
  • use almost no RAM (and perform no heap allocations at all)
  • produce significantly improved floating-point error (by several orders of magnitude)
  • are json-serializable using Pydantic
  • can also be used easily in web and embedded applications via the Rust library
  • are permissively licensed

ND throughput 1000 obs

See here for more info about quality-of-fit, throughput, and memory usage.

Installation

pip install interpn

Profile-Guided Optimization

To build the extension with profile-guided optimization using pre-built profiles, do sh ./scripts/pgo_install.sh. You can also generate your own PGO profiles like sh ./scripts/pgo_profile.sh. after installing these extra compiler dependencies:

rustup component add llvm-tools-preview

Rust Examples

Regular Grid

use interpn::{multilinear, multicubic};

// Define a grid
let x = [1.0_f64, 2.0, 3.0, 4.0];
let y = [0.0_f64, 1.0, 2.0, 3.0];

// Grid input for rectilinear method
let grids = &[&x[..], &y[..]];

// Grid input for regular grid method
let dims = [x.len(), y.len()];
let starts = [x[0], y[0]];
let steps = [x[1] - x[0], y[1] - y[0]];

// Values at grid points
let z = [2.0; 16];

// Observation points to interpolate/extrapolate
let xobs = [0.0_f64, 5.0];
let yobs = [-1.0, 3.0];
let obs = [&xobs[..], &yobs[..]];

// Storage for output
let mut out = [0.0; 2];

// Do interpolation
multilinear::regular::interpn(&dims, &starts, &steps, &z, &obs, &mut out);
multicubic::regular::interpn(&dims, &starts, &steps, &z, false, &obs, &mut out);

Rectilinear Grid

use interpn::{multilinear, multicubic};

// Define a grid
let x = [1.0_f64, 2.0, 3.0, 4.0];
let y = [0.0_f64, 1.0, 2.0, 3.0];

// Grid input for rectilinear method
let grids = &[&x[..], &y[..]];

// Values at grid points
let z = [2.0; 16];

// Points to interpolate/extrapolate
let xobs = [0.0_f64, 5.0];
let yobs = [-1.0, 3.0];
let obs = [&xobs[..], &yobs[..]];

// Storage for output
let mut out = [0.0; 2];

// Do interpolation
multilinear::rectilinear::interpn(grids, &z, &obs, &mut out).unwrap();
multicubic::rectilinear::interpn(grids, &z, false, &obs, &mut out).unwrap();

Python Examples

Available Methods

import interpn
import numpy as np

# Build grid
x = np.linspace(0.0, 10.0, 5)
y = np.linspace(20.0, 30.0, 4)
grids = [x, y]

xgrid, ygrid = np.meshgrid(x, y, indexing="ij")
zgrid = (xgrid + 2.0 * ygrid)  # Values at grid points

# Grid inputs for true regular grid
dims = [x.size, y.size]
starts = np.array([x[0], y[0]])
steps = np.array([x[1] - x[0], y[1] - y[0]])

# Initialize different interpolators
# Call like `linear_regular.eval([xs, ys])`
linear_regular = interpn.MultilinearRegular.new(dims, starts, steps, zgrid)
cubic_regular = interpn.MulticubicRegular.new(dims, starts, steps, zgrid)
linear_rectilinear = interpn.MultilinearRectilinear.new(grids, zgrid)
cubic_rectilinear = interpn.MulticubicRectilinear.new(grids, zgrid)

Multilinear Interpolation

import interpn
import numpy as np

# Build grid
x = np.linspace(0.0, 10.0, 5)
y = np.linspace(20.0, 30.0, 4)

xgrid, ygrid = np.meshgrid(x, y, indexing="ij")
zgrid = (xgrid + 2.0 * ygrid)  # Values at grid points

# Grid inputs for true regular grid
dims = [x.size, y.size]
starts = np.array([x[0], y[0]])
steps = np.array([x[1] - x[0], y[1] - y[0]])

# Observation points pointed back at the grid
obs = [xgrid.flatten(), ygrid.flatten()]

# Initialize
interpolator = interpn.MultilinearRegular.new(dims, starts, steps, zgrid.flatten())

# Interpolate
out = interpolator.eval(obs)

# Check result
assert np.allclose(out, zgrid.flatten(), rtol=1e-13)

# Serialize and deserialize
roundtrip_interpolator = interpn.MultilinearRegular.model_validate_json(
    interpolator.model_dump_json()
)
out2 = roundtrip_interpolator.eval(obs)

# Check result from roundtrip serialized/deserialized interpolator
assert np.all(out == out2)

License

Licensed under either of

at your option.

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

interpn-0.6.4.tar.gz (703.4 kB view details)

Uploaded Source

Built Distributions

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

interpn-0.6.4-cp39-abi3-win_amd64.whl (337.1 kB view details)

Uploaded CPython 3.9+Windows x86-64

interpn-0.6.4-cp39-abi3-win32.whl (336.5 kB view details)

Uploaded CPython 3.9+Windows x86

interpn-0.6.4-cp39-abi3-musllinux_1_2_x86_64.whl (621.7 kB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ x86-64

interpn-0.6.4-cp39-abi3-musllinux_1_2_i686.whl (637.7 kB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ i686

interpn-0.6.4-cp39-abi3-musllinux_1_2_armv7l.whl (716.0 kB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ ARMv7l

interpn-0.6.4-cp39-abi3-musllinux_1_2_aarch64.whl (593.0 kB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ ARM64

interpn-0.6.4-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (449.1 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ x86-64

interpn-0.6.4-cp39-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (522.5 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ s390x

interpn-0.6.4-cp39-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (443.7 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ppc64le

interpn-0.6.4-cp39-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (452.3 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARMv7l

interpn-0.6.4-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (411.4 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

interpn-0.6.4-cp39-abi3-macosx_11_0_arm64.whl (381.0 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

interpn-0.6.4-cp39-abi3-macosx_10_12_x86_64.whl (420.9 kB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

Details for the file interpn-0.6.4.tar.gz.

File metadata

  • Download URL: interpn-0.6.4.tar.gz
  • Upload date:
  • Size: 703.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.9.6

File hashes

Hashes for interpn-0.6.4.tar.gz
Algorithm Hash digest
SHA256 669e274ad607ee91e4935178cdea24ddb307488dc8845b5ab6b9b24542819d28
MD5 047d6bf0017d16a5374c77622465d77b
BLAKE2b-256 4647165a18f25ec8d1c27e20ca0d2f3f5c740afa48c76bf54d926bfdfc67fbb7

See more details on using hashes here.

File details

Details for the file interpn-0.6.4-cp39-abi3-win_amd64.whl.

File metadata

  • Download URL: interpn-0.6.4-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 337.1 kB
  • Tags: CPython 3.9+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.9.6

File hashes

Hashes for interpn-0.6.4-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 ee4ad3312441993468d313f7e38ae140b7701ba5220ff5035e8b1067a253c162
MD5 baab022909bb9058f804372bf87eb17c
BLAKE2b-256 8fa8fad462814b003ea1487aa7a438e75474039c9b725087a7cd3bdddc5df749

See more details on using hashes here.

File details

Details for the file interpn-0.6.4-cp39-abi3-win32.whl.

File metadata

  • Download URL: interpn-0.6.4-cp39-abi3-win32.whl
  • Upload date:
  • Size: 336.5 kB
  • Tags: CPython 3.9+, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.9.6

File hashes

Hashes for interpn-0.6.4-cp39-abi3-win32.whl
Algorithm Hash digest
SHA256 7f88647cabe0b1eb749c45177c31fd0f57d5003637e035c3d2d8c30dec522a4d
MD5 98eb4b568b9b8ceb8d682bb813fe1d3c
BLAKE2b-256 59efa76e9f4c0e071e77f31719030996889ab034e664c17175bc0ec74dfcec73

See more details on using hashes here.

File details

Details for the file interpn-0.6.4-cp39-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for interpn-0.6.4-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 91986b29873dc12b5d23467e18a9f12e0fc2b68af5c4f9973693077ec622fc1f
MD5 d28aa5c8e548a8c186c2d44896e71910
BLAKE2b-256 cbfed86875c4d76967be41a4bc93af005a5ff326ef2874ad761e69a103cbbd88

See more details on using hashes here.

File details

Details for the file interpn-0.6.4-cp39-abi3-musllinux_1_2_i686.whl.

File metadata

File hashes

Hashes for interpn-0.6.4-cp39-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 d637609cae2e747d159a0842634f294c379d99932fe9017d2732183df4866d97
MD5 4fac418dc0bc4967b078982fa95a3ec2
BLAKE2b-256 0503653d9f38a02b153169e9b60359652f351ab43191c2f7ece9b1628e435d7e

See more details on using hashes here.

File details

Details for the file interpn-0.6.4-cp39-abi3-musllinux_1_2_armv7l.whl.

File metadata

File hashes

Hashes for interpn-0.6.4-cp39-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 3df06e9391d04d53efa1e31389edf544b2b5119bf95e434f3a37e6dc9fda2530
MD5 0f74564d3d4caf0c4011d291d056c921
BLAKE2b-256 c41dabb198b3ec9b3d9dee781fce27f7d766a3ba19932b5a5d5fb9ba4c2251e3

See more details on using hashes here.

File details

Details for the file interpn-0.6.4-cp39-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for interpn-0.6.4-cp39-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 d77939fb1f0b65a579079594d09d017f6f6b505b0649f688dbde3d780f331287
MD5 5fbb68f8eec7a2d141c11d3b15b795c0
BLAKE2b-256 41041580aa4a3248be94cac039c69532ae79b51c438e482b8d08cd27a5aded4c

See more details on using hashes here.

File details

Details for the file interpn-0.6.4-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for interpn-0.6.4-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e7698bf0b4f4a5fe4dcf26a7e09bf150931404f1c2ecfdd5851845f87910a361
MD5 f1f69a4bef84fae857710c681a5e52fb
BLAKE2b-256 0458f4bd56f3cf7165d632f5aaf2495fe7e65f4789f325e4e8aeb8021573e674

See more details on using hashes here.

File details

Details for the file interpn-0.6.4-cp39-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl.

File metadata

File hashes

Hashes for interpn-0.6.4-cp39-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 921b415013ada328f5dcf4552388437bc356a8f8d2e51fece5b9971b1a8a1c44
MD5 659c836b801c17dd054aa3dd6e0f4dc9
BLAKE2b-256 c0489028fdc9ee1786630a15c56bd19d1847b7c8ec1ccbd3c1dbbb248a2faedf

See more details on using hashes here.

File details

Details for the file interpn-0.6.4-cp39-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl.

File metadata

File hashes

Hashes for interpn-0.6.4-cp39-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 cb4e9f3491ebb3868e05f9b60140e8930ca8128a8589307cf5de9648756d16ce
MD5 ea0aff8413581cbc9e08239496a4a31b
BLAKE2b-256 583c63d7e88f8f50f3554fd0f6822b385721ccd46342d500634e82c41ded8715

See more details on using hashes here.

File details

Details for the file interpn-0.6.4-cp39-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.

File metadata

File hashes

Hashes for interpn-0.6.4-cp39-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 8d0b6883a8c927b1cec6693d7849e6fdda9dec83cf689bc75540f44b8b1b9307
MD5 f151133ee55fe4deec4cc2241066ace6
BLAKE2b-256 4aa3957f7f1d6d6e29ce2615a92ce61932cc18eef8ef4d07ad4c4d42542b34ab

See more details on using hashes here.

File details

Details for the file interpn-0.6.4-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for interpn-0.6.4-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 be35281425fcc04a42e07a5db026809ae95c31d5d6ce102ba9a6f8e2bc7775a6
MD5 f13b5031a94b73e8b36aceaa248b1e16
BLAKE2b-256 176cf84c3ca2934e139e9affca76aa42e6c386bb5116b52fc847ded838572fc2

See more details on using hashes here.

File details

Details for the file interpn-0.6.4-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for interpn-0.6.4-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9ec89df1d24c542d8adb0aaecc3b8e6464c7bcffd675377da05b78d4fe2fe690
MD5 392ed146f7d96c1575aca0d1fa56e3ad
BLAKE2b-256 3b13a00c20ac260ae4e55bb13d9568428ebd755c4956db5d32aca10d25789987

See more details on using hashes here.

File details

Details for the file interpn-0.6.4-cp39-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for interpn-0.6.4-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 1fd751a16f30f30a95e82a89a3dea32c3da2a9014d724300055fb3e5bd799e44
MD5 ebacb6ed3bc8f297b5b8d5dba6e1f173
BLAKE2b-256 c3beefd183e4987dd463555c6fdfa49c441f841595e2c64f64ee59e7cc67d2a4

See more details on using hashes here.

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