Skip to main content

pyboundfit

Boundary fits using polynomials or splines, based on the method described in Cardiel 2009. This code is a Python implementation of part of the functionality implemented in the original Fortran 77 code boundfit.

The numerical minimization of the fit to splines is performed with the help of the package lmfit.

Warning: this code is under development. Major changes are still being introduced.

Instaling the code

In order to keep your Python installation clean, it is highly recommended to first build a specific Python 3 virtual enviroment

Creating and activating the Python virtual environment

$ python3 -m venv venv_pyboundfit
$ . venv_pyboundfit/bin/activate
(venv_pyboundfit) $ 

Installing the package

The latest stable version is available via de PyPI repository:

(venv_pyboundfit) $ pip install pyboundfit

Note: This command can also be employed in a Windows terminal opened through the CMD.exe prompt icon available in Anaconda Navigator.

The latest development version is available through GitHub:

(venv_pyboundfit) $ pip install git+https://github.com/nicocardiel/pyboundfit.git@main#egg=pyboundfit

Testing the installation

(venv_pyboundfit) $ pip show pyboundfit
(venv_pyboundfit) $ ipython
In [1]: import pyboundfit

In [2]: print(pyboundfit.__version__)
0.3.0

In [3]: pol1, pol2, spl1, spl2 = pyboundfit.demo()
Computing upper boundary (polynomial fit)... OK!
Computing lower boundary (polynomial fit)... OK!
Computing upper boundary (splines fit)... OK!
Computing lower boundary (splines fit)... OK!

In [4]: pol1.coef
Out[4]: 
array([  22.43919975,  -41.5216713 ,   -7.32451917,  140.23297889,
         41.59855497, -148.66578342])
...

The demo function computes the boundary fits to some example data and generates the following plot: pyboundfit demo plot

Usage

Import the required packages

import matplotlib.pyplot as plt
import numpy as np
import pyboundfit as bf

Generate some random data to be fitted

npoints = 300
rng = np.random.default_rng(seed=1234)
xfit = np.linspace(1.0, 3.5, npoints)
yfit = np.sin(xfit) + rng.normal(loc=0, scale=0.1, size=npoints)

Fit upper and lower boundaries using polynomials

pol_upper = bf.boundfit_poly(
    x=xfit, y=yfit, deg=5, 
    xi=10, niter=100, expweight=True,
    boundary='upper'
)
pol_lower = bf.boundfit_poly(
    x=xfit, y=yfit, deg=5, 
    xi=10, niter=100, expweight=True,
    boundary='lower'
)

Fit upper and lower boundaries using splines (the numerical minimization takes some time).

spl_upper = bf.boundfit_adaptive_splines(
    x=xfit, y=yfit, t=5, 
    xi=100, niter=100, boundary='upper',
    adaptive=False,
    expweight=True
)
spl_lower = bf.boundfit_adaptive_splines(
    x=xfit, y=yfit, t=5, 
    xi=100, niter=100, boundary='lower',
    adaptive=False,
    expweight=True
)

Display the result

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(xfit, yfit, 'ko')
ax.plot(xfit, pol_upper(xfit), '-', label='upper boundary (polynomial)')
ax.plot(xfit, pol_lower(xfit), '-', label='lower boundary (polynomial)')
ax.plot(xfit, spl_upper(xfit), ':', label='upper boundary (splines)')
ax.plot(xfit, spl_lower(xfit), ':', label='lower boundary (splines)')
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.legend()
plt.tight_layout()
plt.show()

pyboundfit demo plot

Please, note that the results are quite sensitive to the fitting parameters.

Citation

If you find this package useful, please cite Cardiel 2009.

@ARTICLE{2009MNRAS.396..680C,
       author = {{Cardiel}, N.},
        title = "{Data boundary fitting using a generalized least-squares method}",
      journal = {\mnras},
     keywords = {methods: data analysis, methods: numerical, Astrophysics - Instrumentation and Methods for Astrophysics},
         year = 2009,
        month = jun,
       volume = {396},
       number = {2},
        pages = {680-695},
          doi = {10.1111/j.1365-2966.2009.14749.x},
archivePrefix = {arXiv},
       eprint = {0903.2068},
 primaryClass = {astro-ph.IM},
       adsurl = {https://ui.adsabs.harvard.edu/abs/2009MNRAS.396..680C},
      adsnote = {Provided by the SAO/NASA Astrophysics Data Syste

Metadata

Release files for pyboundfit 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyboundfit 0.3.0
File Size Uploaded Provenance Actions
pyboundfit-0.3.0.tar.gz 24.8 kB Without provenance Details for pyboundfit-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pyboundfit 0.3.0
File Interpreter ABI Platform Actions
pyboundfit-0.3.0-py3-none-any.whl Python 3 none any Details for pyboundfit-0.3.0-py3-none-any.whl

Total release size: 50.6 kB

Release files / pyboundfit-0.3.0.tar.gz

Download URL pyboundfit-0.3.0.tar.gz
Size 24.8 kB
Tags Source
SHA-256 checksum
How to use checksums
7cae7f5f99067809ab9e9523c110c4c47be74be7d1c4d3fa33629aa1ffa005db
BLAKE2b-256 checksum
How to use checksums
790abef96ff431190e00027d7cf7ad7b96802c58c0070a656cec0308798ccc22
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.7

Release files / pyboundfit-0.3.0-py3-none-any.whl

Download URL pyboundfit-0.3.0-py3-none-any.whl
Size 25.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7f1be5fdf6f372aa3dc600ea6cf8dbf273f180ed412e7cab2a989e8d40148472
BLAKE2b-256 checksum
How to use checksums
1bc60269b4207c81a3b97eeb51c110ea65d75b9b561dfd037ee700ce76d37dc8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.7

Security for pyboundfit 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page