Skip to main content
`Ctrl`+`K`

# `fittheory`: Fit theory definition

This module defines the `FitTheory` object that is used by `silx.math.fit.FitManager` to define fit functions and background models.

class FitTheory(function, parameters, estimate=None, configure=None, derivative=None, description=None, pymca_legacy=False, is_background=False)

  This class defines a fit theory, which consists of:

  - a model function, the actual function to be fitted

  - parameters names

  - an estimation function, that return the estimated initial parameters that serve as input for `silx.math.fit.leastsq()`

  - an optional configuration function, that can be used to modify configuration parameters to alter the behavior of the fit function and the estimation function

  - an optional derivative function, that replaces the default model derivative used in `silx.math.fit.leastsq()`

  function

    Regular fit functions must have the signature `f(x, *params) -> y`, where x is a 1D array of values for the independent variable, params are the parameters to be fitted and y is the output array that we want to have the best fit to a series of data points.

    Background functions used by `FitManager` must have a slightly different signature: `f(x, y0, *params) -> bg`, where y0 is the array of original data points and bg is the background signal that we want to subtract from the data array prior to fitting the regular fit function.

    The number of parameters must be the same as in `parameters`, or a multiple of this number if the function is defined as a sum of a variable number of base functions and if `estimate` is designed to be able to estimate the number of needed base functions.

  parameters

    List of parameters names.

    This list can contain the minimum number of parameters, if the function takes a variable number of parameters, and if the estimation function is responsible for finding the number of required parameters

  estimate

    The estimation function should have the following signature:

        f(x, y) -> (estimated_param, constraints)

    Parameters:

    > - `x` is a sequence of values for the independent variable

    > - `y` is a sequence of the same length as `x` containing the data to be fitted

    Return values:

    > - `estimated_param` is a sequence of estimated fit parameters to be used as initial values for an iterative fit.

    > - `constraints` is a sequence of shape (n, 3), where n is the number of estimated parameters, containing the constraints for each parameter to be fitted. See `silx.math.fit.leastsq()` for more explanations about constraints.

  configure

    The optional configuration function must conform to the signature `f(**kw) -> dict` (i.e it must accept any named argument and return a dictionary). It can be used to modify configuration parameters to alter the behavior of the fit function and the estimation function.

  derivative

    The optional derivative function must conform to the signature `model_deriv(xdata, parameters, index)`, where parameters is a sequence with the current values of the fitting parameters, index is the fitting parameter index for which the the derivative has to be provided in the supplied array of xdata points.

  description

    Optional description string for this particular fit theory.

  pymca_legacy

    This attribute can be set to True to indicate that the theory is a PyMca legacy theory.

    This tells `silx.math.fit.fitmanager` that the signature of the estimate function is:

        f(x, y, bg, xscaling, yscaling) -> (estimated_param, constraints)

  is_background

    Flag to indicate that the theory is background theory.

    A background function is an secondary function that needs to be added to the main fit function to better fit the original data. If this flag is set to True, modules using this theory are informed that `function` has the signature `f(x, y0, *params) -> bg`, instead of the usual fit function signature.

  default_estimate(x=None, y=None, bg=None)

    Default estimate function. Return an array of ones as the initial estimated parameters, and set all constraints to zero (FREE)