Skip to content

kb-press/ndsplines

Repository files navigation

ndsplines

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

ndsplines is available on PyPI as well as conda-forge.

pip

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

conda

Install ndsplines with conda:

$ conda install -c conda-forge 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.

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

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:

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.

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

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