Skip to main content

Structured wrapper around scipy.optimize.curve_fit with automatic coefficient extraction

Project description

GeoSol Research Logo

Fitter (Equation Fitting)

Structured wrapper around scipy.optimize.curve_fit that pairs equations with their fitted results. Provides automatic coefficient extraction from function signatures, printable equation representations with value substitution, and RMSE reporting.

Overview

gri-fitter separates equation definition from curve fitting into two composable classes. The Equation class wraps any Python function and introspects its signature to identify the independent variable (first parameter) and coefficients (remaining parameters). The Fit class takes data and an Equation, runs scipy.optimize.curve_fit, and stores the results -- coefficients, fitted y-values, and RMSE -- for downstream use.

Requires Python 3.12+.

Installation

pip install gri-fitter

For development:

git clone https://gitlab.com/geosol-foss/python/gri-fitter.git
cd gri-fitter
. .init_venv.sh

Quick Start

from gri_fitter import Equation, Fit

# Define the equation: first param is independent variable, rest are coefficients
equ = Equation(lambda x, m, b: m * x + b, "mx + b")

# Fit to data
fit = Fit((0, 1, 2, 3), (0, 1.1, 1.9, 3), equ, "LineFit")
coeffs, rmse = fit.fit()

# Print results
print(fit)          # LineFit RMSE [0.00900] ::: 0.99x + 0.05
print(fit.y_fit)    # Fitted y values at the input x points

Equation Class

Equation wraps a function (lambda or def) and extracts variable names from its signature. The first parameter is treated as the independent variable; all others are coefficients.

# Using a lambda
equ = Equation(lambda x, a, b, c: a * x**2 + b * x + c, "ax^2 + bx + c")

print(equ.independent)   # "x"
print(equ.coefficients)  # ["a", "b", "c"]

# Using a defined function
def exponential(x, a, k):
    return a * np.exp(k * x)

equ = Equation(exponential, "a * exp(kx)")

# Without a string representation (replace() will not be available)
equ = Equation(lambda x, m: m * x)

The replace() method substitutes coefficient values into the equation string for display. The p parameter controls decimal precision (truncation, not rounding):

equ = Equation(lambda x, m, b: m * x + b, "mx + b")
print(equ.replace((4.9876, 1.0)))         # 4.987x + 1.0
print(equ.replace((4.9876, 1.0), p=1))    # 4.9x + 1.0

Fit Class

Fit takes x data, y data, an Equation, and an optional title. Call fit() to run scipy.optimize.curve_fit.

equ = Equation(lambda x, m, b: m * x + b, "mx + b")
fit = Fit(x_data, y_data, equ, "MyFit")

# Returns (coefficients, rmse) on success, ([], None) on failure
coeffs, rmse = fit.fit()

# After fitting, results are stored as attributes
print(fit.coefficients)  # [0.99, 0.05]
print(fit.rmse)          # 0.009
print(fit.y_fit)         # Fitted y values at each input x

When the fit fails (e.g., data does not match the equation form), fit() returns ([], None) and str(fit) displays the failure:

print(fit)  # MyFit ***NO FIT*** ::: mx + b

Dependencies

  • numpy: Array operations
  • scipy: scipy.optimize.curve_fit for nonlinear least squares fitting

Other Projects

Current list of other GRI FOSS Projects we are building and maintaining.

License

MIT License. See LICENSE for details.

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

gri_fitter-0.2.2.tar.gz (40.7 kB view details)

Uploaded Source

Built Distribution

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

gri_fitter-0.2.2-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file gri_fitter-0.2.2.tar.gz.

File metadata

  • Download URL: gri_fitter-0.2.2.tar.gz
  • Upload date:
  • Size: 40.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for gri_fitter-0.2.2.tar.gz
Algorithm Hash digest
SHA256 579a1239ca8b44e03a80dd06b494db971484d2760e61ad66d7a6c789c0841ad1
MD5 fb2be10f5dc5d2ccb5f929ceecbed65d
BLAKE2b-256 9b7d35ca4e0381215cac2af20d65606e1e7370191d2658340a3bef54ebedf216

See more details on using hashes here.

File details

Details for the file gri_fitter-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: gri_fitter-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for gri_fitter-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b8bc20e2fa3302f89e51b177d707903da79df44ff88b7d15b442afd110b5a9ec
MD5 bf9b6afdc3cd1e11be9e671f9ad1396a
BLAKE2b-256 e943260a884502168ae0fcd1d0bde98a46756786190c564ae54b967489fff976

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