Skip to main content

Multi-dimensional splines

Project description

PyPI Package latest release GitHub Actions Build Documentation status JOSS DOI Zenodo DOI

This is a Python package for multivariate B-splines with performant NumPy and C (via Cython) implementations. For a mathematical overview of tensor product B-splines, see the Splines page of the documentation.

The primary goal of this package is to provide a unified API for tensor product splines of arbitrary input and output dimension. For a list of related packages see the Comparisons page.

Installation

Install ndsplines with pip:

$ pip install ndsplines

Wheels are provided for a range of Python versions and platforms, so no compilation is required to get the better-performing Cython-based implementation in many cases.

If no matching wheel is found, pip will install build dependencies and attempt to compile the Cython-based extension module. If this is not desired, set the environment variable NDSPLINES_NUMPY_ONLY=1, e.g.:

$ NDSPLINES_NUMPY_ONLY=1 pip install ndsplines

Usage

The easiest way to use ndsplines is to use one of the make_* functions: make_interp_spline, make_interp_spline_from_tidy, or make_lsq_spline, which return an NDSpline object which can be used to evaluate the spline. For example, suppose we have data over a two-dimensional mesh.

import ndsplines
import numpy as np

# generate grid of independent variables
x = np.array([-1, -7/8, -3/4, -1/2, -1/4, -1/8, 0, 1/8, 1/4, 1/2, 3/4, 7/8, 1])*np.pi
y = np.array([-1, -1/2, 0, 1/2, 1])
meshx, meshy = np.meshgrid(x, y, indexing='ij')
gridxy = np.stack((meshx, meshy), axis=-1)

# evaluate a function to interpolate over input grid
meshf = np.sin(meshx) * (meshy-3/8)**2 + 2

We can then use make_interp_spline to create an interpolating spline and evaluate it over a denser mesh.

# create the interpolating spline
interp = ndsplines.make_interp_spline(gridxy, meshf)

# generate denser grid of independent variables to interpolate
sparse_dense = 2**7
xx = np.concatenate([np.linspace(x[i], x[i+1], sparse_dense) for i in range(x.size-1)])
yy = np.concatenate([np.linspace(y[i], y[i+1], sparse_dense) for i in range(y.size-1)])
gridxxyy = np.stack(np.meshgrid(xx, yy, indexing='ij'), axis=-1)

# evaluate spline over denser grid
meshff = interp(gridxxyy)

Generally, we construct data so that the first ndim axes index the independent variables and the remaining axes index output. This is a generalization of using rows to index time and columns to index output variables for time-series data.

We can also create an interpolating spline from a tidy data format:

tidy_data = np.dstack((gridxy, meshf)).reshape((-1,3))
tidy_interp = ndsplines.make_interp_spline_from_tidy(
    tidy_data,
    [0,1], # columns to use as independent variable data
    [2]    # columns to use as dependent variable data
)

print("\nCoefficients all same?",
      np.all(tidy_interp.coefficients == interp.coefficients))
print("Knots all same?",
      np.all([np.all(k0 == k1) for k0, k1 in zip(tidy_interp.knots, interp.knots)]))

Note however, that the tidy dataset must be over a structured rectangular grid equivalent to the N-dimensional tensor product representation. Also note that Pandas dataframes can be used, in which case lists of column names can be used instead of lists of column indices.

To see examples for creating least-squares regression splines with make_lsq_spline, see the 1D example and 2D example.

Derivatives of constructed splines can be evaluated in two ways: (1) by using the nus parameter while calling the interpolator or (2) by creating a new spline with the derivative method. In this codeblock, we show both ways of evaluating derivatives in each direction.

# two ways to evaluate derivatives x-direction: create a derivative spline or call with nus:
deriv_interp = interp.derivative(0)
deriv1 = deriv_interp(gridxxy)
deriv2 = interp(gridxy, nus=np.array([1,0]))

# two ways to evaluate derivative - y direction
deriv_interp = interp.derivative(1)
deriv1 = deriv_interp(gridxy)
deriv2 = interp(gridxxyy, nus=np.array([0,1]))

The NDSpline class also has an antiderivative method for creating a spline representative of the anti-derivative in the specified direction.

# Calculus demonstration
interp1 = deriv_interp.antiderivative(0)
coeff_diff = interp1.coefficients - interp.coefficients
print("\nAntiderivative of derivative:\n","Coefficients differ by constant?",
      np.allclose(interp1.coefficients+2.0, interp.coefficients))
print("Knots all same?",
      np.all([np.all(k0 == k1) for k0, k1 in zip(interp1.knots, interp.knots)]))

antideriv_interp = interp.antiderivative(0)
interp2 = antideriv_interp.derivative(0)
print("\nDerivative of antiderivative:\n","Coefficients the same?",
      np.allclose(interp2.coefficients, interp.coefficients))
print("Knots all same?",
      np.all([np.all(k0 == k1) for k0, k1 in zip(interp2.knots, interp.knots)]))

Contributing

Please feel free to share any thoughts or opinions about the design and implementation of this software by opening an issue on GitHub. Constructive feedback is welcomed and appreciated.

Bug fix pull requests are always welcome. For feature additions, breaking changes, etc. check if there is an open issue discussing the change and reference it in the pull request. If there isn’t one, it is recommended to open one with your rationale for the change before spending significant time preparing the pull request.

Ideally, new/changed functionality should come with tests and documentation. If you are new to contributing, it is perfectly fine to open a work-in-progress pull request and have it iteratively reviewed.

Testing

To test, install the package with the test extras and use pytest:

$ pip install .[test]
$ pytest

Documentation

Documentation is based on Sphinx and built and served by Read the Docs. To build locally, install the docs requirements:

$ pip install .[docs]
$ cd docs
$ make html

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

ndsplines-0.2.0rc1.tar.gz (188.6 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

ndsplines-0.2.0rc1-cp312-cp312-win_amd64.whl (241.3 kB view details)

Uploaded CPython 3.12Windows x86-64

ndsplines-0.2.0rc1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (626.3 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

ndsplines-0.2.0rc1-cp312-cp312-macosx_10_9_x86_64.whl (248.8 kB view details)

Uploaded CPython 3.12macOS 10.9+ x86-64

ndsplines-0.2.0rc1-cp311-cp311-win_amd64.whl (240.1 kB view details)

Uploaded CPython 3.11Windows x86-64

ndsplines-0.2.0rc1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (632.4 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

ndsplines-0.2.0rc1-cp311-cp311-macosx_10_9_x86_64.whl (247.1 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

ndsplines-0.2.0rc1-cp310-cp310-win_amd64.whl (240.1 kB view details)

Uploaded CPython 3.10Windows x86-64

ndsplines-0.2.0rc1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (596.5 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

ndsplines-0.2.0rc1-cp310-cp310-macosx_10_9_x86_64.whl (247.3 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

ndsplines-0.2.0rc1-cp39-cp39-win_amd64.whl (239.8 kB view details)

Uploaded CPython 3.9Windows x86-64

ndsplines-0.2.0rc1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (598.7 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

ndsplines-0.2.0rc1-cp39-cp39-macosx_10_9_x86_64.whl (247.2 kB view details)

Uploaded CPython 3.9macOS 10.9+ x86-64

ndsplines-0.2.0rc1-cp38-cp38-win_amd64.whl (240.7 kB view details)

Uploaded CPython 3.8Windows x86-64

ndsplines-0.2.0rc1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (606.9 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

ndsplines-0.2.0rc1-cp38-cp38-macosx_10_9_x86_64.whl (247.4 kB view details)

Uploaded CPython 3.8macOS 10.9+ x86-64

ndsplines-0.2.0rc1-cp37-cp37m-win_amd64.whl (240.6 kB view details)

Uploaded CPython 3.7mWindows x86-64

ndsplines-0.2.0rc1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (574.3 kB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.17+ x86-64

ndsplines-0.2.0rc1-cp37-cp37m-macosx_10_9_x86_64.whl (248.2 kB view details)

Uploaded CPython 3.7mmacOS 10.9+ x86-64

File details

Details for the file ndsplines-0.2.0rc1.tar.gz.

File metadata

  • Download URL: ndsplines-0.2.0rc1.tar.gz
  • Upload date:
  • Size: 188.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for ndsplines-0.2.0rc1.tar.gz
Algorithm Hash digest
SHA256 f2b7800887b41fe8a2d686b5ea7f73bf84914564fd89650e95b5589778f4e7bd
MD5 460939f6be0beebcbabbb9e8d990e4d6
BLAKE2b-256 275e502dea91929e35a2645c65b015af942733da981cde085ba32bad8e2da874

See more details on using hashes here.

File details

Details for the file ndsplines-0.2.0rc1-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for ndsplines-0.2.0rc1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 f6b3b4c2540b3ddfdbf72f3f6eb85e1a850ba49616d2c3cc14fefc7b1b1431d1
MD5 24c5da99d4fc5969b0eb8ac1b9f9f028
BLAKE2b-256 7aff30cdab1bbcb88a28e6d4668c79dbbb6e6144145f1aaafd74b71390eb4a92

See more details on using hashes here.

File details

Details for the file ndsplines-0.2.0rc1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for ndsplines-0.2.0rc1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 95eec2fee7e2378221b924e6b8143eb14310f556eb8c869a4aed85eab17112ac
MD5 c7a37a0460f13133bd04927aba7525c8
BLAKE2b-256 3566789bde5bbe1b6eb703f8be1e0e19aa91e98f30e03c13e95c7b026ae08a37

See more details on using hashes here.

File details

Details for the file ndsplines-0.2.0rc1-cp312-cp312-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for ndsplines-0.2.0rc1-cp312-cp312-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 81f43c94171d707dde2e4adb5aae115672caefa88eca5c79cfe7e195a2588c40
MD5 6ab8058f8399ada1a9300a346ebe1a62
BLAKE2b-256 7da57bf8754d0a0b5c12833e320a24129407f6deca8e240c2b2005f7644c5d01

See more details on using hashes here.

File details

Details for the file ndsplines-0.2.0rc1-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for ndsplines-0.2.0rc1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 5b127068f6a57e1df4688cc0ca73a91d81cc2618d45abea984d3c437e19ffbca
MD5 628568a0add3e27eacd888c534f924a2
BLAKE2b-256 0a13e8658e764f44067a5b51bf3c82726de668e5f47a3421c308f351f0547f90

See more details on using hashes here.

File details

Details for the file ndsplines-0.2.0rc1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for ndsplines-0.2.0rc1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f83eca9db427575924746f20479ae0cdec63171a8accb22f835314165f450f3f
MD5 2f96a37a213d9378fe51c5b2f053d7a7
BLAKE2b-256 70140658bf1a39e9e104e3facf121132ddcbd53055f78af76b1e5e8c754cdbf4

See more details on using hashes here.

File details

Details for the file ndsplines-0.2.0rc1-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for ndsplines-0.2.0rc1-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 f73090540c80b461c912ea4ff3cfa4dbd27263def148e955240f1145ef706b86
MD5 ddbb051565a90c9eb69dc6e8b5256814
BLAKE2b-256 9e8626b39dd6c6d5af5f93c7ca013085b1389f12e41d39deddf66a3b0155a4ea

See more details on using hashes here.

File details

Details for the file ndsplines-0.2.0rc1-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for ndsplines-0.2.0rc1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 9e05b3a37145ffdfa84d01cd2b0009167d8307ab8539dbbc1b3daef6cab94104
MD5 01b276f188bc065023ec922338518251
BLAKE2b-256 93912e4782ed85d60a8a1a007f293bc920029f00ff349475ac58580ae1e09ff1

See more details on using hashes here.

File details

Details for the file ndsplines-0.2.0rc1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for ndsplines-0.2.0rc1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 18af2767a9ad44505c6d5e302bd03ca9bd084f401ca8547dd0891bdf5572fe79
MD5 3839cb9344f3862af7cb7da52581646a
BLAKE2b-256 db0a2473afb3c0b5db3dadf052848a22e6a2cc62f226d86eb53df0ba3bfc50d4

See more details on using hashes here.

File details

Details for the file ndsplines-0.2.0rc1-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for ndsplines-0.2.0rc1-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 f939daf7c61d00494e80c15f2f727b154e06b4987e63f636a3eb80fdc166628d
MD5 3f714404393faa17054468d3914c68d3
BLAKE2b-256 a31aaa429e275653976b9035c7765ef1c0a94d00b502cc243bede672e71c2345

See more details on using hashes here.

File details

Details for the file ndsplines-0.2.0rc1-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: ndsplines-0.2.0rc1-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 239.8 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for ndsplines-0.2.0rc1-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 44dcabf6b9c08c642447bb3f31f70b7f81744bd78dbc62c095808260487947c3
MD5 94255f0b6219c92c1a9bb5e8f91240c8
BLAKE2b-256 a25894c9fceb34cebf232b82a9536f56f10eb750f521d5448456ba8190a0611b

See more details on using hashes here.

File details

Details for the file ndsplines-0.2.0rc1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for ndsplines-0.2.0rc1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 cb21b806afffecb56ffd5e3b16e728e6f10e0dec6db829c2a56954c7fef01735
MD5 8d57739423cf690b83040d4bbaabe14a
BLAKE2b-256 8afc4261392efc1a2d00e2799ca42095c77c154727df97f33a1c22dd68c3fed9

See more details on using hashes here.

File details

Details for the file ndsplines-0.2.0rc1-cp39-cp39-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for ndsplines-0.2.0rc1-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 48d9abec5809b634f0ed102ae937c228b4ac47823983ecc1883c6d114a3d8189
MD5 fade1730a64bdac39cb7ef86bbfad599
BLAKE2b-256 5e5f531b676aadab20aed9240a79608832f89f25a666bdcd0480396433ba9666

See more details on using hashes here.

File details

Details for the file ndsplines-0.2.0rc1-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: ndsplines-0.2.0rc1-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 240.7 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for ndsplines-0.2.0rc1-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 c37d8bdbdfdbd0f00bdd2b7cfb69b7601d52199fb32481c30241d295b3fd051b
MD5 26e3ebf44a6ff7a86e7721fedc9bbfd7
BLAKE2b-256 a679f87993528005c935b673e69e796d51fd08ebc3d44038c836d85a5bd7a854

See more details on using hashes here.

File details

Details for the file ndsplines-0.2.0rc1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for ndsplines-0.2.0rc1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 aad758bd17a5de52532ef3d7fa4b8257e48935fc4bd6f51910687bfc3bb2728d
MD5 f9f94338d2fbb293a1859e08df12d1f5
BLAKE2b-256 0bd874cd208582602b6784d855a88be4c69ebadd34d94a301bebccef93ffd2ee

See more details on using hashes here.

File details

Details for the file ndsplines-0.2.0rc1-cp38-cp38-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for ndsplines-0.2.0rc1-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 82b7536e5a03686fe741a3acac010484aa8e8eeeb4f6081686d2ea49e310192b
MD5 b91ea1e8a08de1e4c5e7db759e602880
BLAKE2b-256 1f15147f5f9f4324b97d7abbcc9e849681b38e966f562aba83cd155ddb74aa75

See more details on using hashes here.

File details

Details for the file ndsplines-0.2.0rc1-cp37-cp37m-win_amd64.whl.

File metadata

File hashes

Hashes for ndsplines-0.2.0rc1-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 2c571054b9b38bfa2b7f122538a23c6bbf50ae38221855083551f8417c87c8e6
MD5 e4f05341c10af50399a77eac477c65d7
BLAKE2b-256 05fde7cc6bb9afaca3344cbaa74fa14032d76c8004759359d9501d02c5938e5b

See more details on using hashes here.

File details

Details for the file ndsplines-0.2.0rc1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for ndsplines-0.2.0rc1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a77c90111fe9a5162ca92945d0c2a1975fa9ddc80a1b55d080ca5c1dc58c3294
MD5 851d7f76898d3625a6de70768650f726
BLAKE2b-256 2d99744e43667abd4fe52bc4fb5621dbc64275dcada5ac5298cadb546b559eda

See more details on using hashes here.

File details

Details for the file ndsplines-0.2.0rc1-cp37-cp37m-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for ndsplines-0.2.0rc1-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 9cb5a5a7c5e001a338fc9fe8a5bb65721fbde01584621fb2db9eb60b6df7c5e9
MD5 a574267365c5023732ea39f565fd0544
BLAKE2b-256 4cd6014ed16bd1fdfa1a8b847d5410c25b965a916fbf47733ecf2f60f7cfddb8

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