Skip to main content

Small C extensions for local image filters (fmedian, fsigma)

Project description

ftoolss

High-performance C extensions for image processing and curve fitting.

Features

Export Description
fmedian Local median filter (2D/3D auto-dispatch)
fmedian2d, fmedian3d Direct 2D/3D median
fsigma Local ? filter (2D/3D auto-dispatch)
fsigma2d, fsigma3d Direct 2D/3D sigma
fgaussian_f32, fgaussian_f64 Gaussian profile (Accelerate-optimized)
fmpfit_pywrap Auto dtype dispatch (single spectrum)
fmpfit_f64_pywrap, fmpfit_f32_pywrap Single spectrum fitting
fmpfit_block_pywrap Auto dtype dispatch (block fitting)
fmpfit_f64_block_pywrap, fmpfit_f32_block_pywrap Block fitting
MPFitResult Named tuple for fit results

Common features: NaN handling, edge handling, optional center exclusion (fmedian/fsigma).

Installation

pip install .                        # Standard install
pip install -e .                     # Editable (development)
python setup.py build_ext --inplace  # Build extensions only

Requirements: Python 3.8+, NumPy ?1.20, C compiler. macOS Accelerate used when available.

Quick Start

import numpy as np
from ftoolss import fmedian, fsigma, fgaussian_f32, fgaussian_f64

# 2D/3D median and sigma filters
data = np.random.randn(100, 200)
median = fmedian(data, (3, 3), exclude_center=1)
sigma = fsigma(data, (3, 3), exclude_center=1)

# 3D works the same way
data_3d = np.random.randn(50, 100, 50)
median_3d = fmedian(data_3d, (3, 3, 3))

# Gaussian profiles
x = np.linspace(-10, 10, 1000, dtype=np.float32)
profile = fgaussian_f32(x, i0=1.0, mu=0.0, sigma=1.5)

Curve Fitting (fmpfit)

Single-spectrum fitting with auto dtype dispatch:

from ftoolss.fmpfit import fmpfit_pywrap
import numpy as np

x = np.linspace(-5, 5, 100)
y = 2.5 * np.exp(-0.5 * ((x - 1.0) / 0.8)**2) + np.random.randn(100) * 0.1

result = fmpfit_pywrap(
    deviate_type=0,  # Gaussian model
    parinfo=[
        {'value': 1.0, 'limits': [0.0, 10.0]},  # amplitude
        {'value': 0.0, 'limits': [-5.0, 5.0]},  # mean
        {'value': 1.0, 'limits': [0.1, 5.0]}    # sigma
    ],
    functkw={'x': x, 'y': y, 'error': np.ones_like(y) * 0.1}
)
print(f"Best-fit: {result.best_params}, ?²={result.bestnorm:.2f}")
print(f"SciPy-style errors (full Hessian): {result.xerror_scipy}")

Block fitting (multiple spectra in one call, GIL-free):

from ftoolss.fmpfit import fmpfit_block_pywrap
import numpy as np

n_spectra, mpoints, npar = 100, 200, 3
x = np.tile(np.linspace(-5, 5, mpoints), (n_spectra, 1))
y = ...  # shape (n_spectra, mpoints)
error = np.ones_like(y) * 0.1
p0 = np.tile([1.0, 0.0, 1.0], (n_spectra, 1))
bounds = np.array([[[0, 10], [-5, 5], [0.1, 5]]] * n_spectra)

results = fmpfit_block_pywrap(0, x, y, error, p0, bounds)
# results['best_params'] shape: (n_spectra, npar)

API Reference

fmedian / fsigma

fmedian(data, window_size, exclude_center=0)
fsigma(data, window_size, exclude_center=0)
  • window_size: (x, y) or (x, y, z) ? odd positive integers
  • exclude_center: 1 to exclude center from calculation (useful for outlier detection)
  • Returns: float64 array, same shape as input

fgaussian_f32 / fgaussian_f64

Computes: i0 * exp(-((x - mu)² / (2 * sigma²))

fgaussian_f32(x, i0, mu, sigma)  # float32, fastest
fgaussian_f64(x, i0, mu, sigma)  # float64

fmpfit

Levenberg-Marquardt least-squares fitting. See src/ftoolss/fmpfit/README.md for details.

Function Description
fmpfit_pywrap Auto dtype dispatch (single spectrum)
fmpfit_f64_pywrap float64 single spectrum
fmpfit_f32_pywrap float32 single spectrum
fmpfit_block_pywrap Auto dtype dispatch (block of spectra)
fmpfit_f64_block_pywrap float64 block fitting
fmpfit_f32_block_pywrap float32 block fitting

Examples

See examples/ directory for complete working examples.

Testing

pytest                              # Run all tests
pytest --cov=ftoolss --cov-report=html  # With coverage

Performance

Module Speedup vs NumPy Notes
fgaussian_f32 5-10× Accelerate vvexpf
fgaussian_f64 2-3× Accelerate vvexp
fmedian/fsigma ? Sorting networks for small windows
fmpfit ? GIL-free, 4× speedup with 6 threads

License

MIT

Credits

  • Sorting networks: Bert Dobbelaere
  • MPFIT: Craig Markwardt (cmpfit, MINPACK-1 derivative)

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

ftoolss-6.0.26.tar.gz (135.5 kB view details)

Uploaded Source

Built Distributions

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

ftoolss-6.0.26-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (790.5 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

ftoolss-6.0.26-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (789.8 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

ftoolss-6.0.26-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (782.3 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

ftoolss-6.0.26-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (775.7 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

File details

Details for the file ftoolss-6.0.26.tar.gz.

File metadata

  • Download URL: ftoolss-6.0.26.tar.gz
  • Upload date:
  • Size: 135.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ftoolss-6.0.26.tar.gz
Algorithm Hash digest
SHA256 796db0c694d0e0f48a25d0dc5899849e26740be632ce6faefc1a524b03c97a14
MD5 7b7adb62967443c52fbf15b267201e98
BLAKE2b-256 9104c321adb03b6b501868c3f2427609d6e5b46996bc8c1ed0a2f1ff1829e18b

See more details on using hashes here.

Provenance

The following attestation bundles were made for ftoolss-6.0.26.tar.gz:

Publisher: release.yml on steinhh/ftools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ftoolss-6.0.26-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ftoolss-6.0.26-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0a933f25950b4ab1c4597f9de287123242cb97aec227ec4364161fc91d01a4b7
MD5 0adee2835728b217586d11236b321220
BLAKE2b-256 e025b8385a5cbfcc2488ce01b5e29f863615c7b257ec11bc23fb717de807d822

See more details on using hashes here.

Provenance

The following attestation bundles were made for ftoolss-6.0.26-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on steinhh/ftools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ftoolss-6.0.26-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ftoolss-6.0.26-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 70855535c62f96f33f2fdeaf42d349a8082ef427165b9fc44426bebdb405b31f
MD5 3ee946da2a422c206462b60cddae2693
BLAKE2b-256 bc22dd940dbdc861125e05717f1254d4f71fc2c37c24832df500b64c3d9f79e4

See more details on using hashes here.

Provenance

The following attestation bundles were made for ftoolss-6.0.26-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on steinhh/ftools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ftoolss-6.0.26-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ftoolss-6.0.26-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 3d90c487a1c02d70ef9555953017c35fbcae1150f3bf32e1b0c82a89bcb3c7ad
MD5 17a83e4d3e9c1d52de3747e59be27b88
BLAKE2b-256 8fd86cb6a9f17138aeeae10a951c8087effaec50f3faf0492955159d76bacbf7

See more details on using hashes here.

Provenance

The following attestation bundles were made for ftoolss-6.0.26-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on steinhh/ftools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ftoolss-6.0.26-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ftoolss-6.0.26-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 39a586caadd4b3978f1038be1fa4ba52321c8b421b1aa0a1853c9d719f601e00
MD5 e0b4b4cbdb1e5b7899001a17cd06d577
BLAKE2b-256 2c2c78124da8287109bb3b63a710c774db0e8467b8297dd9454b0bf8cebab34e

See more details on using hashes here.

Provenance

The following attestation bundles were made for ftoolss-6.0.26-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on steinhh/ftools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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