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
uv sync

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.3.tar.gz (43.9 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.3-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: gri_fitter-0.2.3.tar.gz
  • Upload date:
  • Size: 43.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","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.3.tar.gz
Algorithm Hash digest
SHA256 57dcdd2df45aa47d1c868c3481d2767e8cfc42a82a32ba491a92de8749102d8a
MD5 7de589be21a57bfdf89905711ef2d8ec
BLAKE2b-256 6f73f378310095590068988e396cad33f3201bd6ca8cfd470873513810a71b5a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gri_fitter-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 9d1b0270b8ce7cf4ef98d84e33b2b4d2399f2e6e4e3012015d1f0dcadfce6cb1
MD5 b416276a52ef548b24fc1a401f67ee29
BLAKE2b-256 6188b00ca02274c457caf4d847fecd737eb484fc600da76b0757a3809ff0a1e1

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