Skip to main content

Multi-dimensional splines

Project description

PyPI Package latest release Documentation status JOSS DOI Zenodo DOI Azure Pipelines build status Codecov test coverage

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

or from source:

$ git clone https://github.com/kb-press/ndsplines
$ cd ndsplines
$ pip install .

Note: If installing from source, to use the C implementation, install with the build_ext feature (i.e., $ pip install .[build_ext]) or install Cython (i.e., $ pip install cython) before installing ndsplines. The system must have a C compiler configured before installing.

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 splane
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 developer requirements and use pytest:

$ pip install -r requirements-dev.txt
$ pip install -e .
$ pytest

Documentation

To build the docs, install the docs feature requirements (a subset of the developer requirements above):

$ pip install -e .[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.1.2.tar.gz (135.0 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.1.2-cp38-cp38-win_amd64.whl (80.8 kB view details)

Uploaded CPython 3.8Windows x86-64

ndsplines-0.1.2-cp38-cp38-manylinux2010_x86_64.whl (366.2 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.12+ x86-64

ndsplines-0.1.2-cp38-cp38-macosx_10_14_x86_64.whl (81.4 kB view details)

Uploaded CPython 3.8macOS 10.14+ x86-64

ndsplines-0.1.2-cp37-cp37m-win_amd64.whl (79.7 kB view details)

Uploaded CPython 3.7mWindows x86-64

ndsplines-0.1.2-cp37-cp37m-manylinux2010_x86_64.whl (333.7 kB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.12+ x86-64

ndsplines-0.1.2-cp37-cp37m-macosx_10_14_x86_64.whl (82.2 kB view details)

Uploaded CPython 3.7mmacOS 10.14+ x86-64

ndsplines-0.1.2-cp36-cp36m-win_amd64.whl (79.7 kB view details)

Uploaded CPython 3.6mWindows x86-64

ndsplines-0.1.2-cp36-cp36m-manylinux2010_x86_64.whl (333.9 kB view details)

Uploaded CPython 3.6mmanylinux: glibc 2.12+ x86-64

ndsplines-0.1.2-cp36-cp36m-macosx_10_14_x86_64.whl (82.0 kB view details)

Uploaded CPython 3.6mmacOS 10.14+ x86-64

File details

Details for the file ndsplines-0.1.2.tar.gz.

File metadata

  • Download URL: ndsplines-0.1.2.tar.gz
  • Upload date:
  • Size: 135.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3

File hashes

Hashes for ndsplines-0.1.2.tar.gz
Algorithm Hash digest
SHA256 7dd6eba5a9a300d9166a0650df98567302efcd6954a01356e8ed9519e8099b1f
MD5 a822be49c3742006f083093668191ad4
BLAKE2b-256 d7658089ce87cbbe7332d92dc94d83372dfbdefc4942f3266cca31ce23832665

See more details on using hashes here.

File details

Details for the file ndsplines-0.1.2-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: ndsplines-0.1.2-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 80.8 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3

File hashes

Hashes for ndsplines-0.1.2-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 0b5e60a65cca200119a3c99d4ef25b855b5b802250733c22cf65c2834708ebf8
MD5 2eecc073bcd347aab17b668b3ec19350
BLAKE2b-256 23ad1338950c15cff9d44e845b40fd88e6ca13976e3a3877f13d9ee70b53c439

See more details on using hashes here.

File details

Details for the file ndsplines-0.1.2-cp38-cp38-manylinux2010_x86_64.whl.

File metadata

  • Download URL: ndsplines-0.1.2-cp38-cp38-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 366.2 kB
  • Tags: CPython 3.8, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3

File hashes

Hashes for ndsplines-0.1.2-cp38-cp38-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 09d6f1c4afd16760ed865d0f0de715edde5132f71689df5ea42856967734ba00
MD5 83ac453d7718f07032c0143ea4ddd4cd
BLAKE2b-256 70c7b9c9de8b5085448fc69b04ec66f277816f0f4b32c6a9e715778c6d3714db

See more details on using hashes here.

File details

Details for the file ndsplines-0.1.2-cp38-cp38-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: ndsplines-0.1.2-cp38-cp38-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 81.4 kB
  • Tags: CPython 3.8, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3

File hashes

Hashes for ndsplines-0.1.2-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 98e2f6083876c255f83c8da0d807e399279607a5e8395f535a705311e184dc06
MD5 fbf8c5f6b961dfc726d983441071c1f0
BLAKE2b-256 0089d64d0f992b6fd72045040dfafd04d63d3e2fce83e67409b3a1d2cea4d32a

See more details on using hashes here.

File details

Details for the file ndsplines-0.1.2-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: ndsplines-0.1.2-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 79.7 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3

File hashes

Hashes for ndsplines-0.1.2-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 5cfbd5a9fb799ff0e6854815d90be9ee97215a3080a895d332c7c58c63c361ea
MD5 972509d3ae9c2afe9a4a3b3903f607c8
BLAKE2b-256 05f67847644944ca9c3d8b66493456aea105c9e183a7c1277b37f598e7c95a6f

See more details on using hashes here.

File details

Details for the file ndsplines-0.1.2-cp37-cp37m-manylinux2010_x86_64.whl.

File metadata

  • Download URL: ndsplines-0.1.2-cp37-cp37m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 333.7 kB
  • Tags: CPython 3.7m, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3

File hashes

Hashes for ndsplines-0.1.2-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 c2e58612be7a707c390fe54ca1e1dfe9222b1890fde35a5b5c12f7f2924783b0
MD5 52bbe11a5b576e7a7519cb87c0df6ba6
BLAKE2b-256 0096f5842b8c47531522fbca27b3ee657b7f3bfd6401eb58cd74135428149dfd

See more details on using hashes here.

File details

Details for the file ndsplines-0.1.2-cp37-cp37m-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: ndsplines-0.1.2-cp37-cp37m-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 82.2 kB
  • Tags: CPython 3.7m, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3

File hashes

Hashes for ndsplines-0.1.2-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 2d3d07452ec8af58ebb208f206041b32d8b817b75fb9497c9236a324c13dcfec
MD5 62abc70027dac71647b874d215825e3f
BLAKE2b-256 cb5f0a9be015d6f22c140349b1ffd105cc685fcd0617c312dac5e158aa04b77b

See more details on using hashes here.

File details

Details for the file ndsplines-0.1.2-cp36-cp36m-win_amd64.whl.

File metadata

  • Download URL: ndsplines-0.1.2-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 79.7 kB
  • Tags: CPython 3.6m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3

File hashes

Hashes for ndsplines-0.1.2-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 98026e110d077286a5abc6ae32c6e4464d3e9d33540e72a6d7bc8a28ccb3781b
MD5 79025fd6b742e6298e012eaf5cab740c
BLAKE2b-256 912da982b65d06cbbe3e1a7f67e85b94bcaa0ccc774ab11decc583d4ff9a9fc9

See more details on using hashes here.

File details

Details for the file ndsplines-0.1.2-cp36-cp36m-manylinux2010_x86_64.whl.

File metadata

  • Download URL: ndsplines-0.1.2-cp36-cp36m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 333.9 kB
  • Tags: CPython 3.6m, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3

File hashes

Hashes for ndsplines-0.1.2-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 fef4d91359b2bdda20edc7fa731f31b4fff76507217ac988a718dd73d86ba426
MD5 cb1482275bee21f6ee92735657ab3cfd
BLAKE2b-256 1ddf574108783679bb01b20fe0bfd76781e0429adee33abe12c7b9643586aeb1

See more details on using hashes here.

File details

Details for the file ndsplines-0.1.2-cp36-cp36m-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: ndsplines-0.1.2-cp36-cp36m-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 82.0 kB
  • Tags: CPython 3.6m, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3

File hashes

Hashes for ndsplines-0.1.2-cp36-cp36m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 065c014a67352e465f505d897185771accd6e6b1013f22ae0818b60b104b739e
MD5 2da28fbbe0210fc35bd8e044b3031df5
BLAKE2b-256 e95f4ca714653748398a254c0cdcaa22525d40c4e7556457848c1b314477c74e

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