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.7.0.tar.gz (681.0 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.7.0-cp310-abi3-win_amd64.whl (335.2 kB view details)

Uploaded CPython 3.10+Windows x86-64

interpn-0.7.0-cp310-abi3-win32.whl (335.6 kB view details)

Uploaded CPython 3.10+Windows x86

interpn-0.7.0-cp310-abi3-musllinux_1_2_x86_64.whl (619.6 kB view details)

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

interpn-0.7.0-cp310-abi3-musllinux_1_2_i686.whl (636.3 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ i686

interpn-0.7.0-cp310-abi3-musllinux_1_2_armv7l.whl (713.9 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARMv7l

interpn-0.7.0-cp310-abi3-musllinux_1_2_aarch64.whl (590.8 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

interpn-0.7.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (446.8 kB view details)

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

interpn-0.7.0-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (517.9 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ s390x

interpn-0.7.0-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (441.3 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ppc64le

interpn-0.7.0-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (450.3 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARMv7l

interpn-0.7.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (409.2 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

interpn-0.7.0-cp310-abi3-macosx_11_0_arm64.whl (379.0 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

interpn-0.7.0-cp310-abi3-macosx_10_12_x86_64.whl (418.9 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for interpn-0.7.0.tar.gz
Algorithm Hash digest
SHA256 64d169c096242b394012727c94505b52682853ae3706191052780c97e4be4201
MD5 6ccb24a1dee95a39f60c12e8980d4755
BLAKE2b-256 5f51d45fd5f0ff6f70ae36abc57c2537440850efbd82da3e04cabe0a011c7b34

See more details on using hashes here.

File details

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

File metadata

  • Download URL: interpn-0.7.0-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 335.2 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.7.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 bb692d924327af94f0bae201fff6fb23e700cf919cfd7a105f3e9df224592548
MD5 a8ce425711ff5b40fcc748359de19951
BLAKE2b-256 e30ac91fc809375b308abce717d5f4367f4a0f7a37c359a37eecd721411f93f1

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for interpn-0.7.0-cp310-abi3-win32.whl
Algorithm Hash digest
SHA256 19f34b639d8a14f033f60c784031c9069a9c0cabe49ab8b595b82aa2eb389175
MD5 e7cd90c20a137c8e755ebac73fae8088
BLAKE2b-256 46622d94cfea1879ca8354fc4315069921a697dee17c1e7a68c230540e41afe3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.7.0-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 92a27ac4aad5998b4deac2fcfafa457c0e5b7765b40b464406c073e1b37b974b
MD5 5a329197652cee8e34f09e19b2d24d34
BLAKE2b-256 b6c7a2237b36850da7274729f135884c73c04bdcb803541b7de03d53e31cb2db

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.7.0-cp310-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 fadc184db92c669893212457b08cc20279379095c2d5a70d156c068a2d759a13
MD5 5a35d2134215d2052be4a550b00cc425
BLAKE2b-256 26b78c8fb2b8d25b21e9ce83309906cc84d08a4c831d1dfc46fa4622cdad8c8b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.7.0-cp310-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 cb31c004bc641cec0507ccac02de9bd19d1aab408ae41f0c57112f9e54ef6b5e
MD5 4c55722a324dd587de39802fa6319332
BLAKE2b-256 c08459f5e60a42df2b1238b4077d3cb481bf34607e52ae56751acb00d5934e1b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.7.0-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 5cc0be60af8ce51ce52a4bfc3d371f41730d76a3b2a1f5a701d03915531e73fd
MD5 9d3651b831f8570aa7f5c00d5bbe88b2
BLAKE2b-256 3ac0bfd1493ed704d880c8064eb9208872577ba48e4e80bb92c02c69eefe1b7b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.7.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f4fad9d289491cd29933a37bc739281521450f8303e27f2b05c2c40073799411
MD5 a8311334d7fe370e7e4b772e36c674ef
BLAKE2b-256 febfc0d0657d657c875b3e60dfd160aa21bedf43ee0601119f5f4e6ee646997d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.7.0-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 9b722047e84b3c3d3e1b9442ccde39c6c07023df0f53adffad25f6f28f8b1a58
MD5 2d5cf775867fdd9bb766fced57a3d404
BLAKE2b-256 53ade49dbe92112b4b94f41c70993b1230efc6edb2c36e9052571d9565c8bfd5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.7.0-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 6c87cdcaf945676f7f040695b4949008943d40b02f29697c01574a04c98ceebe
MD5 0c279f569e7977f5a6c02765755c117f
BLAKE2b-256 f5dd5012f1ad696b45055e857922d4e1ce5ab26a75ae51c865bed588b11337ba

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.7.0-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 53ed1a8d33c2753268771b1eb1dbcc764a5da4de003b0b397510b18d315d4683
MD5 04fc58ee157b9e961c85b2f47ab1fd3c
BLAKE2b-256 d44c365cd4914aa3c62840a6a6c0d8dfb2c65bba20d91cfa21bf1ac3030e60cd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.7.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 93d72c98a8c0812b4dfef9e3391b594a456d0aa08af5b9f4ed0dfc2d3078b6ad
MD5 71daed723601bafc4814fe8e1fa82818
BLAKE2b-256 c35e95cba35bcfa63bf6219e99f05734ce2ab75cbf8fb78070fc330b1873e8b1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.7.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3e8440e9efb2a292005e0ebc82c80013cf578171ed9e81f3c3f8d619b3323a4f
MD5 cdebc50b7d74598e63498e4bfb6ca17f
BLAKE2b-256 c4b7442c7a082251122b4e7dc3262e32c2a137d34d13ec4d6f522ab06618d5d4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.7.0-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 915c481a6198b377ab8774b993a7a2ee1c2e7969a3785f483a47c5ba7fbb54e4
MD5 e3ca8c3c5eb9007feb61840906a8d68b
BLAKE2b-256 98c4bb8daf2722cc2845a25e7dca0a63ae4df2cff932fe035f017c127f6bd092

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