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
Nearest-Neighbor
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/distr_pgo_install.sh. You can also generate your own PGO profiles like sh ./scripts/distr_pgo_profile.sh. after installing this extra compiler dependency:

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.8.0.tar.gz (1.5 MB view details)

Uploaded Source

Built Distributions

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

interpn-0.8.0-cp310-abi3-win_amd64.whl (285.4 kB view details)

Uploaded CPython 3.10+Windows x86-64

interpn-0.8.0-cp310-abi3-win32.whl (299.7 kB view details)

Uploaded CPython 3.10+Windows x86

interpn-0.8.0-cp310-abi3-musllinux_1_2_x86_64.whl (558.7 kB view details)

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

interpn-0.8.0-cp310-abi3-musllinux_1_2_i686.whl (588.0 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ i686

interpn-0.8.0-cp310-abi3-musllinux_1_2_armv7l.whl (657.7 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARMv7l

interpn-0.8.0-cp310-abi3-musllinux_1_2_aarch64.whl (537.0 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

interpn-0.8.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (385.4 kB view details)

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

interpn-0.8.0-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (477.4 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ s390x

interpn-0.8.0-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (401.7 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ppc64le

interpn-0.8.0-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (392.5 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARMv7l

interpn-0.8.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (355.0 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

interpn-0.8.0-cp310-abi3-macosx_11_0_arm64.whl (319.8 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

interpn-0.8.0-cp310-abi3-macosx_10_12_x86_64.whl (353.1 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for interpn-0.8.0.tar.gz
Algorithm Hash digest
SHA256 957ad52c5673af8dd7e5c0bc31616fd1344486f1310aa7664c816e4bfc4c5a5c
MD5 ea9b94247853550584877943f7f278a7
BLAKE2b-256 297f10478c6955dfe68fee5f01bf5a0156392d0516671c11658b982c6354178a

See more details on using hashes here.

File details

Details for the file interpn-0.8.0-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: interpn-0.8.0-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 285.4 kB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.9.6

File hashes

Hashes for interpn-0.8.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 76b7dce3818ab7b107138dee0e7bc0fc516ece0ce84c02413859a36547e5fe8d
MD5 c4f81a23590287b837c2dbdd08e00b06
BLAKE2b-256 306fcbc70360f9f4ab28933c0fd7fd6f91c4ed6727552d921840d8bf0f9a1964

See more details on using hashes here.

File details

Details for the file interpn-0.8.0-cp310-abi3-win32.whl.

File metadata

  • Download URL: interpn-0.8.0-cp310-abi3-win32.whl
  • Upload date:
  • Size: 299.7 kB
  • Tags: CPython 3.10+, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.9.6

File hashes

Hashes for interpn-0.8.0-cp310-abi3-win32.whl
Algorithm Hash digest
SHA256 bdc2260e4cf330b00c5a2769934a643e6083ba979c4e915492f02cc81b0ce534
MD5 cbf516ed2f6a56a7dc7b26467c5e7a8f
BLAKE2b-256 7f99c31a87f828aa5091bfd77543511b6542c440f79897e5192398e81990607c

See more details on using hashes here.

File details

Details for the file interpn-0.8.0-cp310-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for interpn-0.8.0-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 b7c10da45e5bf9649f806c33f49b583a95ebb3a9bf676328af0ee64073ee57b9
MD5 59cba404ffadea1d0a7e3ed9b74eb572
BLAKE2b-256 c65e5c7342272f94bc7744fae1015f22a1b76baec848f1d9bcef06ea65212716

See more details on using hashes here.

File details

Details for the file interpn-0.8.0-cp310-abi3-musllinux_1_2_i686.whl.

File metadata

File hashes

Hashes for interpn-0.8.0-cp310-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 e2e194efda675b298c44c6ee14fd40fe1e5854631bbf49ff54b7c0b40d4a3df3
MD5 ccd2f68526004060fc4e95d8054e87c2
BLAKE2b-256 18a2aeb78d5444f299e1d9020e517f2fe3a82ef84e7a33152ac22b9a9703e5dc

See more details on using hashes here.

File details

Details for the file interpn-0.8.0-cp310-abi3-musllinux_1_2_armv7l.whl.

File metadata

File hashes

Hashes for interpn-0.8.0-cp310-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 01917a18a748051cbd6c9b9db64a5c5d3339339bf232ab0878be7ad923a83e3d
MD5 c3aa8269e2a6b752d2eba71130813d26
BLAKE2b-256 6c69d336f595a029fa3b6a46eeb34c3a782de47fd2e39f0b29dac92e374a6628

See more details on using hashes here.

File details

Details for the file interpn-0.8.0-cp310-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for interpn-0.8.0-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 b15eb82af91eba86920f1dc037f5019729e5cf1bad14da1c543ec47bd52e7edd
MD5 fb322de19fdd53d677c7a52d548b2d0c
BLAKE2b-256 5a29b177630107ad62486743a6c89283b7cea74219c2a20ef2693313a17a3c11

See more details on using hashes here.

File details

Details for the file interpn-0.8.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for interpn-0.8.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 cb3d68a3e2976aba1d82d4309e9113d863a7e0a615bf52c5208964cee968b460
MD5 6e083a8376dc4e26ac378c66054b2f9f
BLAKE2b-256 8226cf1ef07a203a43e13615d988b4d92156776d56e99f5c9b1532c4c00a56fb

See more details on using hashes here.

File details

Details for the file interpn-0.8.0-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl.

File metadata

File hashes

Hashes for interpn-0.8.0-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 f44f14d87fd3351847c60921673a21b25a2ba6989870652320cdcb0102b056f8
MD5 c38ceccac2ee0820c432032d1c53f01d
BLAKE2b-256 88269a4779e269d6a342738431573305a82c083f137f2c44b2fafc2fec9b6a07

See more details on using hashes here.

File details

Details for the file interpn-0.8.0-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl.

File metadata

File hashes

Hashes for interpn-0.8.0-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 51d0731089acab904ac19655b233fa894a1ed6641327d3c19e6401faf3a123e4
MD5 6af15adf66cacb690402d9a5ec78b521
BLAKE2b-256 e4ceec5a569b22343ba4544f5021089b3be12490713125de05d222a21a125810

See more details on using hashes here.

File details

Details for the file interpn-0.8.0-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.

File metadata

File hashes

Hashes for interpn-0.8.0-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 d8cf8d029cc7300b169c136ec8146cc6e29495190cab892f40f9e4b8f3cddaa6
MD5 1c69cbf74c61626dce3d118e022ac6a5
BLAKE2b-256 0efcb7308edccbf14a25d56825f830d2266755abc8b5b796e9dbbf2df9bed76b

See more details on using hashes here.

File details

Details for the file interpn-0.8.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for interpn-0.8.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 7328f5df8a24621a81e9ea76ebd114918863f961192fc471b2fbbc9bbf4e9f10
MD5 ff72b607a3b92c14e8d715e74868acf7
BLAKE2b-256 0417d5e0cbf5d59a31754bebb1905cb27ecc30aa2254a205e5e999e5c426f2a4

See more details on using hashes here.

File details

Details for the file interpn-0.8.0-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for interpn-0.8.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b69329853d733cee9c40188ed3778673c9545b3a02622f1f32169da49cd2d3b2
MD5 81713a44c84c91141f891974bdede4a3
BLAKE2b-256 b62543e742a0a295d30d5a5ddb060ca85382c5b2865424eefb3a4bbdbd06b3a0

See more details on using hashes here.

File details

Details for the file interpn-0.8.0-cp310-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for interpn-0.8.0-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 3ac879e8d63f4367b080485987c9fdaf860c600646f08b96aa1206870f6398c9
MD5 a7bc0fbdce2008994c7311a873e82a52
BLAKE2b-256 61cd3096afd01d7e02dedd161b5a4bae978dd5a6a12e6c0405f297122175c476

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