Skip to main content

N-dimensional interpolation/extrapolation methods

Project description

InterpN

Writeup | Repo | Python Docs | Rust Docs

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

Available as a rust crate and python library.

Features

Feature →
↓ Interpolant Method
Regular
Grid
Rectilinear
Grid
Json
Serialization
Thread Parallelism
Nearest-Neighbor
Linear
Cubic Hermite
Cubic B-spline

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

For larger jobs such as image processing, the non-allocating evaluation pattern results in a nearly-linear thread speedup, limited only by thread spawning overhead and memory bandwidth.

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, do sh ./scripts/distr_pgo.sh after installing this extra compiler dependency:

rustup component add llvm-tools-preview

An LLVM install matching the version used by rustc is also required for doing PGO; see the ./scripts/distr_pgo.sh or CI workflows for the exact version.

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.11.1.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.11.1-cp310-abi3-win_amd64.whl (617.1 kB view details)

Uploaded CPython 3.10+Windows x86-64

interpn-0.11.1-cp310-abi3-win32.whl (536.1 kB view details)

Uploaded CPython 3.10+Windows x86

interpn-0.11.1-cp310-abi3-musllinux_1_2_x86_64.whl (822.5 kB view details)

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

interpn-0.11.1-cp310-abi3-musllinux_1_2_i686.whl (887.8 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ i686

interpn-0.11.1-cp310-abi3-musllinux_1_2_armv7l.whl (887.9 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARMv7l

interpn-0.11.1-cp310-abi3-musllinux_1_2_aarch64.whl (745.9 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

interpn-0.11.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (688.6 kB view details)

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

interpn-0.11.1-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (673.5 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ s390x

interpn-0.11.1-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (628.7 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ppc64le

interpn-0.11.1-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (610.7 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARMv7l

interpn-0.11.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (646.9 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

interpn-0.11.1-cp310-abi3-macosx_11_0_arm64.whl (611.3 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

interpn-0.11.1-cp310-abi3-macosx_10_12_x86_64.whl (658.8 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for interpn-0.11.1.tar.gz
Algorithm Hash digest
SHA256 9f825fe8bb9de8c29a57f04bef7957ab319b5c1c8bda6cafb9544eff617a2ec7
MD5 4f6caab2dd4237bf0fcafc6319ce8802
BLAKE2b-256 5638d6608855482dcc7db79fae372b88a77e0e3efd0a06946fc3593a68c4718c

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for interpn-0.11.1-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 2fa6b5ff09622d3a0152c80b530b0958aa1306dbd3333e240b138e3f158472fc
MD5 75339df16eaadbd33c79580ce75e876d
BLAKE2b-256 9e87f2c5a8226b7b1e891e4bc3876ff5e92c86a7e2c96232491624423f3ce885

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for interpn-0.11.1-cp310-abi3-win32.whl
Algorithm Hash digest
SHA256 778d24bdd3d2422f25fb4e76c72b48842bbba7e244a9949eeeab74374081d238
MD5 cce769c64730260c5d9e7abc01d87742
BLAKE2b-256 2ac61f6c73bda60c28bd10905cf3ce4117a84cab712a8227b17aac1a76b09c48

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.11.1-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 0e9216e335e64133fb1c8ca04d52f4108604732959692bd78ead51543fcf62da
MD5 6cabc01ac73a75639c966d3ddf90b0ad
BLAKE2b-256 6c7dc4a167ff28f0f14e9114480aebf181ded49d23c69bc9f05dd9ad421c9c94

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.11.1-cp310-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 828b405ecf93c1ec6238b5d2d4ed16912f5c5d68dd2af9239f2e92a965251d3c
MD5 0a96d09697acdc090fff12eb6bb92a99
BLAKE2b-256 9f21334348eb7b6060247e03946a422c38f020a23c5d81c8f12fde902bc5ac41

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.11.1-cp310-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 f57448569f223340c64dd17da7905cbe2dde9b4b800904095b0ee747400b893d
MD5 95443933ff785d3ba260d3b1c076db00
BLAKE2b-256 f08c788fec1935730b57986b7d12e5bedc46c3259281f3bc163be53f8cbbe0f8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.11.1-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 9aab49a0d4f6c8fe6f5f446b4cb9f188c03f6fc4d1257bff94f6a68c49cd876e
MD5 80b29263d92d82c4342940aa32f4b439
BLAKE2b-256 fb1e4c7ac8edc4a43515ffb465d24317cfc0c97b43028abc4d64325ac2140278

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.11.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 cc20d866ba20785050ac370441346d9ddfbf60bede0ea40c94aa8911a9d04286
MD5 f8ad257f9e94e937a575145830b4c222
BLAKE2b-256 4ed1712f2212440365da811f578216a920a00efa30b98c446bc856daa217f78e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.11.1-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 758eef5c69e2f92e832f18868de5490f6dee55a38d6cc8fb59cf2158e9fe8df8
MD5 47519daf2ffd35bd2a45a89e2ef5f92c
BLAKE2b-256 2be86c0cca230f6a07558cded466ff6041ffc6a0bad3b4a8011fda4b30756725

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.11.1-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 fe98771bcb9f896eeb746e0d5bb7d591e28d09356b9919149a9572e4aa96fc0d
MD5 d4bbf630f9fcb10320881be0c76f7a2a
BLAKE2b-256 c77719e907c081eb3f9203945c6761a386863aad41ff652bfce085b3d1cedf14

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.11.1-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 cbbb3484fc043e802fbc906143441df4f21d6cce743793fed857d71a95e2b3ec
MD5 feebcc978c1614d9fa53b57d9709a052
BLAKE2b-256 56b00540b25f2427959ade7c8f3b73980cef0e5e4356b5aceb75b040d4df2914

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.11.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 da124e05b66fb046d8e7a64979870cec66a63bb2d691c8bcf4ffa98c9ceaf348
MD5 10d192724d309b461b12da62c24f37ca
BLAKE2b-256 557f6e6f11ae84e41d7d2b9499a94b26a8882f1d483268a70360bb2990f0cc8d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.11.1-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 69c5c24172373981f59e4a3296f44d490cbb813f48dd5d7af8525843a1f2a93f
MD5 ef5ead918553900dc293ca1df9e3cd72
BLAKE2b-256 aef357d2eca44ef835b32dcf7734f08a38af388c2425b2c1f2eacc8575f6ce51

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.11.1-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 bac8680a31455a7b6ce5fe281f22f88cb204ddeaeb8cede239740c1e619f4460
MD5 0dde783c2e6a0cd88109b8f5c2ca7b4a
BLAKE2b-256 d9cea7d3e74d0afbe1d7a0991d10a9b9d677c47db8026640b7309c2d407e6742

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