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.2.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.2-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (538.2 kB view details)

Uploaded PyPymanylinux: glibc 2.17+ x86-64

interpn-0.8.2-pp311-pypy311_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (487.7 kB view details)

Uploaded PyPymanylinux: glibc 2.17+ ARM64

interpn-0.8.2-cp310-abi3-win_amd64.whl (506.7 kB view details)

Uploaded CPython 3.10+Windows x86-64

interpn-0.8.2-cp310-abi3-win32.whl (304.9 kB view details)

Uploaded CPython 3.10+Windows x86

interpn-0.8.2-cp310-abi3-musllinux_1_2_x86_64.whl (555.0 kB view details)

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

interpn-0.8.2-cp310-abi3-musllinux_1_2_i686.whl (588.1 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ i686

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

Uploaded CPython 3.10+musllinux: musl 1.2+ ARMv7l

interpn-0.8.2-cp310-abi3-musllinux_1_2_aarch64.whl (538.1 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

interpn-0.8.2-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (563.9 kB view details)

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

interpn-0.8.2-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (487.4 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ s390x

interpn-0.8.2-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.2-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.2-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (509.1 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

interpn-0.8.2-cp310-abi3-macosx_11_0_arm64.whl (495.6 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

interpn-0.8.2-cp310-abi3-macosx_10_12_x86_64.whl (561.2 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for interpn-0.8.2.tar.gz
Algorithm Hash digest
SHA256 297c07c20423144bbd498881e4bad08b49375b23e1712c43ea96c2fe5353de1c
MD5 0acf11c86c3aa48f3429c5da638b0e5f
BLAKE2b-256 08b9ce865437f4a1b52349c3c3f58f6423cab3f36462c42b1e6ac1f1a1720777

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.8.2-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5d07de3386cb94ec46c18278fb61e3dd7f192b0175835a681051545fc51f5ea2
MD5 12f5bd263a53b3ec3f8568e0804a7eb7
BLAKE2b-256 f2e0e132ef2630bd691b1cce2fea982776ee5e53d7ab6d5ad405484ab171d567

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.8.2-pp311-pypy311_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 96ef5e8520711dbb3967b3df5b7d963176cfd8d5b906d146ebb60e95e7c33a57
MD5 d11ed11a15bb94698824c6b1b77dd7f1
BLAKE2b-256 f603c799ade7354bd731c27511b80038fddd036a329e1cd87bdd7d187e902abd

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for interpn-0.8.2-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 c234363466403bd58712c84ba618e33fdd0413a2cd233ab049057247b9bf7045
MD5 2151455e134d468127ebedb823e15f59
BLAKE2b-256 3fe0270c6eadb91b47128da19df2d5e480bb9df2af76b884100f0e3405098a91

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for interpn-0.8.2-cp310-abi3-win32.whl
Algorithm Hash digest
SHA256 0981f951aa8bfd86a4180af648f2ba50de4bde1954e681ceb163eef95369d485
MD5 cf45745fa32d12e05c57771499b3b5da
BLAKE2b-256 db1318a9e0a100e2931e57453381b9f77c18632ed4bcc44e0cb3f945eee407c6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.8.2-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 2d92aa971b0733c5dda0956788c31ca6294959b8c43d16c7f76643ecefb3d75b
MD5 007d65a4d0340966592394d839f3f8c8
BLAKE2b-256 fcbcf29e1613ba9d66e2129b04880c80b22bf90437f15e2ee5ba83a93f67ba7f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.8.2-cp310-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 1be431ada1856f741baeaa6bb3848348980db97cbbf091eec1554441f4313b18
MD5 e4125d3871e6e88e6f649d32024a8ef3
BLAKE2b-256 36672fe23128a89d45d438d3eee1f89cd6f091e24e53767372f0602cd83e002b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.8.2-cp310-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 e967d886b3ab7220f096fd0ca7e3e0feebf67e8efd9c621dfbe666c44c3bcfcd
MD5 7b62bbbf486100093dc71ddc0c0e31cc
BLAKE2b-256 92c5044c4cb7354c350826415e82173ac38ba64241272e1cfca3c22019239a3b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.8.2-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 b22d3206435f0c008e7dea8da5800e9f4b666bf2eacf21b094e3833c7059e333
MD5 bf3fcff99a80facaacfec7ec53e7fef7
BLAKE2b-256 f20f79755bb13c4813caacb6622737e3933b46722eaec47a487c0387c939ef5b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.8.2-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c0f19fc58b2d40500101145cd420671d16479524759e27387ecc75b9a0b70bcd
MD5 8409c5b5a0aa2f87a2da3978688f107d
BLAKE2b-256 bc39005eb2c09e19d90887217610c7f53e20f171db5e4f875b78d1356de524d4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.8.2-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 ed558a2a4b5d21eb7a4f01fdee42c6244207e6744080dbf44733f34fff232ee9
MD5 5305cad5d1eef6388bc766c6a5edf54f
BLAKE2b-256 e82ef78de7e14f6ad37cb3463ebd55b064758dc6a0616e6fd7e46f2417e52d2b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.8.2-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 6b95430b2137f643faba7f42e57a3fc3b94c80f00df611728618da122799601b
MD5 3519fc4bb0af898d3a9aaa88abae47df
BLAKE2b-256 0c322e648fa80b8082e5a608be8cdd1155b388d120dbd8b4ff33ee68e5b542bb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.8.2-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 1347bf47908d2ddf93349d458a1975583fd320daae45e62eda602a8d5b15c5af
MD5 64b0c81880434a7438a5ff058a23b086
BLAKE2b-256 871983d1b4b469322d62ea13a5e1ece74cc79faafdac6f770e90546606ada4ae

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.8.2-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 28fe42cabe73e06bc707f09f1d73b02c1e06450d7dd181244cba8dcf10824f64
MD5 b9247306b272bcf0ceaedb8cced6b34a
BLAKE2b-256 bc4dee025c6e71cbf0c7f07dc33ed5283c04722f869d897f31c44751cb8137e2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.8.2-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1e67ad82aa4b8d794e0676a36ed6c5442eaf7a3d0a253d2ad1f3dba2f0c1ecd4
MD5 fa61b19e3e126dc588de5ac826e886b1
BLAKE2b-256 45476babee99519fb691700418157c3052cc13d3eec266a7c26c8d4a2e756cd7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.8.2-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 d06b83b67befbc2d992094e68b979819e2c622b7dbafae066a358f43ed789612
MD5 c71cef29b97fca5b35149774448bc3bd
BLAKE2b-256 95037038c8a3769c433b049d42102c29443b324a0787f4d1a706c76e582bb1f2

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