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

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.0.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.11.0-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (600.4 kB view details)

Uploaded PyPymanylinux: glibc 2.17+ x86-64

interpn-0.11.0-pp311-pypy311_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (523.7 kB view details)

Uploaded PyPymanylinux: glibc 2.17+ ARM64

interpn-0.11.0-cp310-abi3-win_amd64.whl (485.5 kB view details)

Uploaded CPython 3.10+Windows x86-64

interpn-0.11.0-cp310-abi3-win32.whl (356.7 kB view details)

Uploaded CPython 3.10+Windows x86

interpn-0.11.0-cp310-abi3-musllinux_1_2_x86_64.whl (687.3 kB view details)

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

interpn-0.11.0-cp310-abi3-musllinux_1_2_i686.whl (703.3 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ i686

interpn-0.11.0-cp310-abi3-musllinux_1_2_armv7l.whl (720.3 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARMv7l

interpn-0.11.0-cp310-abi3-musllinux_1_2_aarch64.whl (605.1 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

interpn-0.11.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (588.1 kB view details)

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

interpn-0.11.0-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (596.2 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ s390x

interpn-0.11.0-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (513.0 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ppc64le

interpn-0.11.0-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (448.7 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARMv7l

interpn-0.11.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (517.3 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

interpn-0.11.0-cp310-abi3-macosx_11_0_arm64.whl (482.4 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

interpn-0.11.0-cp310-abi3-macosx_10_12_x86_64.whl (545.5 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for interpn-0.11.0.tar.gz
Algorithm Hash digest
SHA256 e04b099116c4a75749386031e3ed8359d3ecb012337a20f6bf37cf8452c8ce1e
MD5 d28ba0ab52ef933035155f16a0a37bbf
BLAKE2b-256 aeee3fe156634991867e0b92e01e9a303ce753693570df5bb22b4c0c6516047f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.11.0-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ddd56d5f9165f24fb262ccba94515f81d46002f31474c4ddf7f9e2d07de028f0
MD5 fd8cdac4392e2b76bad1ab9a3c1f1c66
BLAKE2b-256 b3f0ef459b0c26c2efbe7943a6b9d8bcc367673da4687d12faff87ade9de1c4c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.11.0-pp311-pypy311_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 d4eee5792cc446a87dd9666324aa042499a3028670f5669c12de2b5ae3ecf325
MD5 28364fa4315de013f68b9f2efcf5c1fd
BLAKE2b-256 527023bfc8e8ac07136a2190588da2968f3e06a810c2e1bda20e462e793cb216

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for interpn-0.11.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 e1bc43c864740bca65db2dcc523cdf28aa73989658f934006ab8b235ce87f88d
MD5 837ed8c6b5d1a770dc480b8bfb44b91a
BLAKE2b-256 9f21079fb68176e452e6633e4b4a0886b666c39d34bf874fae6e4a966349f2d0

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for interpn-0.11.0-cp310-abi3-win32.whl
Algorithm Hash digest
SHA256 898f0f832093e14a104f704976ca69439898ac0fc7a741e562dc0581b1e23e83
MD5 5f9526002df48d8c9da8a3cfaa00a77c
BLAKE2b-256 f820e6739ba11caed725affbccf2a56834e8be81cc7ccd8b03cd085fc9777b77

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.11.0-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 8602d68482090b59f8c0f8471afd47d9ced37b8887d529b9e6cde65c1a4be314
MD5 92a7f94527c19810c5f099ad4e2e83f0
BLAKE2b-256 d65ff503622788bee77bfe0fed095ee12ef32b3154bdcc62e163097593ccf230

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.11.0-cp310-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 16b77289ee00c57aecbdb9fe131215ca7dd3ecea85738afff6e02dbf45b85a3a
MD5 0859477c0de749ff88f99edb00d2cb3a
BLAKE2b-256 87d6d9e56acfdad1e38adf94c0e4fa4fc188085433b98030c3e4303c25c4791c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.11.0-cp310-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 272155e016bf65e77f8b9f7cb510ce45cea6ff09372f1af1aac2356cf10a63dc
MD5 b31d14db3329562c835873d9b9e25343
BLAKE2b-256 81957b315b74719a7c658bd35b30fa88acdee0fec7b30065adbbbfe3ddcbc929

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.11.0-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 5be1c541297dbcaaf6104e7f034e4f9d7df8d8ac4e3cd242eb5fc31dff463ece
MD5 927a98187f3b290015b12ad0c7a907b2
BLAKE2b-256 a6668fa9a27559c021a94dc100994383f1c08b8b69486d2b97644b1c0fb67a5d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.11.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 578b40fb08613ecf71465c80752b8cbe8ae9ace4872775abb2ae9cf9a4a30a46
MD5 9257296af2e138de03a5f3ecf83cad5f
BLAKE2b-256 02caae9e15c510ce9e1830e237852b6ac7be3d547cf62a6626bc54d2ccd4d31c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.11.0-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 66f94e44c528daaaa50af0d95f6bc53b11c6a9523ef62d93ac343adb32ff1a09
MD5 9f6b43cbc1d428f693086532aed832ae
BLAKE2b-256 57df6ccbc05d6ca5af52d0abb5b7340c849bfceb164db5d20af8bd970ffa305c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.11.0-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 edf6983d78309737a9593b28749e2ea4df71fe3fec4542e675b485e8e7af21a9
MD5 bd10b106ebe5fae3f5f1b7bb27d01691
BLAKE2b-256 21d434481534001ead2128e9de27d593a7d81838db12a867b06576742f39caf8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.11.0-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 2a153299a9674ab48ba033fcbc76078ccdb070027b55dadb874628d30b7bda63
MD5 35589df3ee71ef8d9633e77623e27468
BLAKE2b-256 a1a6373efd7ba9fcca576db5f14b86248293cdbf8073f1551940efe31d177a46

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.11.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 f49cd6478d8a5a35b7220b9b9c319bb8a8a01fd1b5a431b899b7878cd79293a6
MD5 4a09a9a31e34a54a4b014f31bcc364b7
BLAKE2b-256 13e8ad3cc73328e9d0f1817b4af284ea002f85fbd0a43341813b7bfdae66df76

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.11.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f290218754006ab2120fafeb5e9b298897ecc668ee0780235011d9315b6ed0ab
MD5 5166d8c4b63f594008a5df5a5623eae9
BLAKE2b-256 e4838cccf1d0b66cec8fe7c4e537dfd75339a22208ac3745b9b91228e78f4cf1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.11.0-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 76d569a6a27e7bfb3283c1a3603756abe4392d7a178147648e517b30c58778b5
MD5 fc683fbef007ff2395deef1322078ad8
BLAKE2b-256 c8959d1fbc234efa7d33d4ac28596bb608f0235ecea120ecfb299c65521fd1b4

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