Skip to main content

No project description provided

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.1.tar.gz (40.1 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.1-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: gri_fitter-0.2.1.tar.gz
  • Upload date:
  • Size: 40.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.11 {"installer":{"name":"uv","version":"0.10.11","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.1.tar.gz
Algorithm Hash digest
SHA256 b89a98f341e8648dce344bdf4f547c75565b1c5a5b3c9bb448b36615a5d91f2d
MD5 c41ccdde2abaf28bdf62d2d989c4f20b
BLAKE2b-256 b96244907f2cc88b5d257f290983e991e276cf35aae96b02e55f1a600bf8aece

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gri_fitter-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.11 {"installer":{"name":"uv","version":"0.10.11","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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cd44c10165feb1cbbac5277270ddf904f2fff9b2af6d72fbc2f766d6d55cc6ae
MD5 a1fb6382df70bef77ec5dbfa8316f9a7
BLAKE2b-256 520f9b8d8e111ee07155d5cdd975a0a9d60b1a63f1cf1b28569f6b759694bad2

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