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.1.tar.gz (1.4 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.1-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (537.4 kB view details)

Uploaded PyPymanylinux: glibc 2.17+ x86-64

interpn-0.8.1-pp311-pypy311_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (484.8 kB view details)

Uploaded PyPymanylinux: glibc 2.17+ ARM64

interpn-0.8.1-cp310-abi3-win_amd64.whl (506.0 kB view details)

Uploaded CPython 3.10+Windows x86-64

interpn-0.8.1-cp310-abi3-win32.whl (305.1 kB view details)

Uploaded CPython 3.10+Windows x86

interpn-0.8.1-cp310-abi3-musllinux_1_2_x86_64.whl (558.6 kB view details)

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

interpn-0.8.1-cp310-abi3-musllinux_1_2_i686.whl (588.3 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ i686

interpn-0.8.1-cp310-abi3-musllinux_1_2_armv7l.whl (663.2 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARMv7l

interpn-0.8.1-cp310-abi3-musllinux_1_2_aarch64.whl (538.2 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

interpn-0.8.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (565.8 kB view details)

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

interpn-0.8.1-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (486.9 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ s390x

interpn-0.8.1-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (398.5 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ppc64le

interpn-0.8.1-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (394.1 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARMv7l

interpn-0.8.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (507.3 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

interpn-0.8.1-cp310-abi3-macosx_11_0_arm64.whl (496.3 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

interpn-0.8.1-cp310-abi3-macosx_10_12_x86_64.whl (558.9 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for interpn-0.8.1.tar.gz
Algorithm Hash digest
SHA256 b3f541fc991c4027d10a0262a0faffe6c93864ac196345ca2873210007935316
MD5 641f589c931cca09563f9c8f48184dfc
BLAKE2b-256 f71b5c7096a32b704c9478cd6d1fc328e5d0419cf5afa0fafc5eca961bed7d7f

See more details on using hashes here.

File details

Details for the file interpn-0.8.1-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for interpn-0.8.1-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 563421c03ffc2eeb0daf2fa2d78131e630c2a3900faf8f96746beb173ca75a7c
MD5 14d0982abf980c1c6ff18d7b7bf44891
BLAKE2b-256 0805115b99eb3ac2567b774262728126eec4e41c2939948ed82c20cb22e2c16d

See more details on using hashes here.

File details

Details for the file interpn-0.8.1-pp311-pypy311_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for interpn-0.8.1-pp311-pypy311_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 650ac07f37c5c4de00d91dcecd07696dfde728506792b74c85f8dce40bde1b0f
MD5 d9fa24ce537ae2c667e3a091d133e1f4
BLAKE2b-256 af9efd251b6d17537a8384f3126209a475c9ebe287ac68429237731419eb0a81

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for interpn-0.8.1-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 1274290f8d670d718f7c9cb530e9023c4bef2b32b6a522e694dced077b2af90d
MD5 38cf649c5db722d7435e3dce03699fa5
BLAKE2b-256 165a6be6d1acea98dba737391bb30df86e9f28cf63eab59c603bd3a0908f23f1

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for interpn-0.8.1-cp310-abi3-win32.whl
Algorithm Hash digest
SHA256 a3f5ecfb590a30ed53b692dd7f2e508c7d3c9ef591bd80b1c4e5082f1b631332
MD5 e578b102e18a025223a6538ad44f0c1e
BLAKE2b-256 e996e89ec61d22a32c0115463cf712085d3d6384d49ee6f162a3eaa701f82b9e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.8.1-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 eac1bc0672cdc880e435e466b5d692067c4f5215d76d1cedf1433247a30abb27
MD5 a056ad6c941759b20a57fd633f7268ce
BLAKE2b-256 7efb61efdcc95f9eed0ebe5e3ccdc846f67ee8f3aa51c4278a5c2893ed8f6da8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.8.1-cp310-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 e86fd235afeef06fde60c19d3326cf45e3d221e50d0e73b33fa4521fcb0bef9f
MD5 35f51749f2711091baa06dbcaa01c69d
BLAKE2b-256 ba1a471bdd7c55716f7fed69ca44ee004c6ebc4e569e0785a6edd3e69d6d032b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.8.1-cp310-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 804d6ac486d4a3c97ca96ebd376b4e1590e979b1ea9e4ac467e053142de1953e
MD5 29a39a9124256b87a6c806126f7f6975
BLAKE2b-256 cd4fc131fada452b3c0d27b54e7a6ea40042385c3c254fc78588c99577c62061

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.8.1-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 6c633df6c554b933fa90f6cbb259001940d8b713149d8722826880283c1483f1
MD5 eb9553fefd779aacb7975b1d6ff37b6e
BLAKE2b-256 41ee9495b23d07bdc29abe113846235d5835d5c3d5bcac594ea642a7acb295fd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.8.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 78429dedc22ec66faf9c9c3c75c0f6fb0e15b772a4605f141c6cffa0ae2bcb11
MD5 2df9eaf6f27255ed3083b09d3134c8ba
BLAKE2b-256 3faa03f8571a3bcab14db2aad08a09d331049ddb7bf2bebd6b05e5582d49c6c5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.8.1-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 88884a033f85b403271f6ae132371226ed3727539e067ba7ccebdac3dd236068
MD5 fab78039568a8411be92f940bb200843
BLAKE2b-256 73df6d3fb4296148a019e6877cd6c383bf4b21501d41ebfb1a86e6358513048c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.8.1-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 846cc5b8771690a782141137d44841191ea1b336d2d03b6901a142439b545816
MD5 fe2a31a5d97a7b644d5545ec3bde74f9
BLAKE2b-256 7514e8b114c7abfcc86abd984ab49c4daea6e74ea4601a4721a360e2d74d65e8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.8.1-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 d42d6cd1bcaf6afeca0fd77b2cd928b34b5ff6b923b043759a8c5ae32d1c72b2
MD5 bc075228094bdad3ea323038e906ba00
BLAKE2b-256 e3539103193d0fc14fc0fd9cc807ee5dca5ab5a34c4f550d29f4333c51404340

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.8.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 1c18b50ed083ca5f5f0e8eb3fe37f7d466a4963b3f0ec9610da354bf4b44fe52
MD5 5c9b911a47a786a500a2fd5baa23c499
BLAKE2b-256 d32e3c9aa1a13be84e9db805780bf6e5b0e77acb2b03ac9020b32ebf3e6421ee

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.8.1-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0b250bd5a019c6d79211c7fb44c4bfae8b25fabe4aba295eb04753e875782fa7
MD5 4ad924aed229216e72906137735710fa
BLAKE2b-256 9b01712dd5a04350fe6a4ae3fe9ed55ba89a393429f7c0ca714fe492860183b1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.8.1-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 d75fdd029d8e539ef29a4681cd0a29c363cbb3fa6fe0d3bde1427c510443f8d3
MD5 c04a38572e08ef6a0b3673caa71f0a8c
BLAKE2b-256 2cda1f8a4af83e9b5c156d5ee8f2b07f57e12577fdf97319d3e066bb0886d0ef

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