Skip to main content

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+.

Documentation

The wheel ships its documentation inside the package, in gri_fitter/docs/, so it is available wherever the package is installed:

  • overview.md -- usage, failure handling, and gotchas
  • api_summary.md -- every public name and signature (generated)

Print the directory with python -c "import gri_fitter, pathlib; print(pathlib.Path(gri_fitter.__file__).parent / 'docs')". Every example in those files is run by the test suite.

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.06708] ::: 0.979x + 0.030
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.000
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.98, 0.03]
print(fit.rmse)          # 0.06708 (sqrt of the mean squared residual)
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.

Release files for gri-fitter 0.2.4

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

Source distribution (sdist)

Source distribution for gri-fitter 0.2.4
File Size Uploaded
gri_fitter-0.2.4.tar.gz 19.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for gri-fitter 0.2.4
File Interpreter ABI Platform
gri_fitter-0.2.4-py3-none-any.whl Python 3 none any Details

Total release size: 30.4 kB

Release files / gri_fitter-0.2.4.tar.gz

Download URL gri_fitter-0.2.4.tar.gz
Size 19.7 kB
Tags Source
SHA-256 checksum
How to use checksums
1c6c2f902d89b361e699b0bfc4663b4b112ac4ebcdf500ebff4707ee67387bd3
BLAKE2b-256 checksum
How to use checksums
da631997fa7f731ed648cc22ef682dd71858d54535ecb86a3301159d79f88c2f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.9.30 {"installer":{"name":"uv","version":"0.9.30","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"12","id":"bookworm","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / gri_fitter-0.2.4-py3-none-any.whl

Download URL gri_fitter-0.2.4-py3-none-any.whl
Size 10.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ba1083ce3ca0b31f80dc6abf6a4aadd63786e9ff56f0107574761db0582e8214
BLAKE2b-256 checksum
How to use checksums
e7bd6cb43e59748b81fcd9e0df5bdba02e1f4cc192355c75ee234ab28ba0f98d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.9.30 {"installer":{"name":"uv","version":"0.9.30","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"12","id":"bookworm","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

0.2.4 This release

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

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