Skip to main content
`Ctrl`+`K`

# `FitWidget`

This module provides a widget designed to configure and run a fitting process with constraints on parameters.

The main class is `FitWidget`. It relies on `silx.math.fit.fitmanager`, which relies on `silx.math.fit.leastsq()`.

The user can choose between functions before running the fit. These function can be user defined, or by default are loaded from `silx.math.fit.fittheories`.

For a tutorial on how to use `FitWidget`, see Using FitWidget.

## API

class FitWidget(parent=None, title=None, fitmngr=None, enableconfig=True, enablestatus=True, enablebuttons=True)

  This widget can be used to configure, run and display results of a fitting process.

  The standard steps for using this widget is to initialize it, then load the data to be fitted.

  Optionally, you can also load user defined fit theories. If you skip this step, a series of default fit functions will be presented (gaussian-like functions), and you can later load your custom fit theories from an external file using the GUI.

  A fit theory is a fit function and its associated features:

  > - estimation function,

  > - list of parameter names

  > - numerical derivative algorithm

  > - configuration widget

  Once the widget is up and running, the user may select a fit theory and a background theory, change configuration parameters specific to the theory run the estimation, set constraints on parameters and run the actual fit.

  The results are displayed in a table.

  __init__(parent=None, title=None, fitmngr=None, enableconfig=True, enablestatus=True, enablebuttons=True)
    Parameters:

      - parent – Parent widget

      - title – Window title

      - fitmngr – User defined instance of `silx.math.fit.fitmanager.FitManager`, or `None`

      - enableconfig – If `True`, activate widgets to modify the fit configuration (select between several fit functions or background functions, apply global constraints, peak search parameters…)

      - enablestatus – If `True`, add a fit status widget, to display a message when fit estimation is available and when fit results are available, as well as a measure of the fit error.

      - enablebuttons – If `True`, add buttons to run estimation and fitting.

  configdialogs

    This dictionary defines the fit configuration widgets associated with the fit theories in `fitmanager.theories`

    Keys must correspond to existing theory names, i.e. existing keys in `fitmanager.theories`.

    Values must be instances of QDialog widgets with an additional output attribute, a dictionary storing configuration parameters interpreted by the corresponding fit theory.

    The dialog can also define a setDefault method to initialize the widget values with values in a dictionary passed as a parameter. This will be executed first.

    In case the widget does not actually inherit `QDialog`, it must at least implement the following methods (executed in this particular order):

    > - `show()`: should cause the widget to become visible to the user)

    > - `exec()`: should run while the user is interacting with the widget, interrupting the rest of the program. It should typically end (return) when the user clicks an OK or a Cancel button.

    > - `result()`: must return `True` if the new configuration in attribute `output` is to be accepted (user clicked OK), or return `False` if `output` is to be rejected (user clicked Cancel)

    To associate a custom configuration widget with a fit theory, use `associateConfigDialog()`. E.g.:

        fw = FitWidget()
        my_config_widget = MyGaussianConfigWidget(parent=fw)
        fw.associateConfigDialog(theory_name="Gaussians",
                                 config_widget=my_config_widget)

  setData(x=None, y=None, sigmay=None, xmin=None, xmax=None)

    Set data to be fitted.

    Parameters:

      - x (Sequence or numpy array or None) – Abscissa data. If `None`, `xdata`` is set to `numpy.array([0.0, 1.0, 2.0, ..., len(y)-1])`

      - y (Sequence or numpy array or None) – The dependant data `y = f(x)`. `y` must have the same shape as `x` if `x` is not `None`.

      - sigmay (Sequence or numpy array or None) – The uncertainties in the `ydata` array. These are used as weights in the least-squares problem. If `None`, the uncertainties are assumed to be 1.

      - xmin – Lower value of x values to use for fitting

      - xmax – Upper value of x values to use for fitting

  associateConfigDialog(theory_name, config_widget, theory_is_background=False)

    Associate an instance of custom configuration dialog widget to a fit theory or to a background theory.

    This adds or modifies an item in the correspondence table `configdialogs` or `bgconfigdialogs`.

    Parameters:

      - theory_name (str) – Name of fit theory. This must be a key of dict `fitmanager.theories`

      - config_widget – Custom configuration widget. See documentation for `configdialogs`

      - theory_is_background (bool) – If flag is True, add dialog to `bgconfigdialogs` rather than `configdialogs` (default).

    Raise:

      KeyError if parameter `theory_name` does not match an existing fit theory or background theory in `fitmanager`.

    Raise:

      AttributeError if the widget does not implement the mandatory methods (show, exec, result, setDefault) or the mandatory attribute (output).