This modules provides a set of fit functions and associated estimation functions in a format that can be imported into a silx.math.fit.FitManager instance.
These functions are well suited for fitting multiple gaussian shaped peaks typically found in spectroscopy data. The estimation functions are designed to detect how many peaks are present in the data, and provide an initial estimate for their height, their center location and their full-width at half maximum (fwhm).
The limitation of these estimation algorithms is that only gaussians having a similar fwhm can be detected by the peak search algorithm. This search fwhm can be defined by the user, if he knows the characteristics of his data, or can be automatically estimated based on the fwhm of the largest peak in the data.
The source code of this module can serve as template for defining your own fit functions.
The functions to be imported by FitManager.loadtheories() are defined by a dictionary THEORY: with the following structure:
from silx.math.fit.fittheory import FitTheory
THEORY = {
'theory_name_1': FitTheory(
description='Description of theory 1',
function=fitfunction1,
parameters=('param name 1', 'param name 2', …),
estimate=estimation_function1,
configure=configuration_function1,
derivative=derivative_function1),
'theory_name_2': FitTheory(…),
}
Note
Consider using an OrderedDict instead of a regular dictionary, when defining your own theory dictionary, if the order matters to you. This will likely be the case if you intend to load a selection of functions in a GUI such as silx.gui.fit.FitManager.
Theory names can be customized (e.g. gauss, lorentz, splitgauss…).
The mandatory parameters for FitTheory are function and parameters.
You can also define an INIT function that will be executed by FitManager.loadtheories().
See the documentation of silx.math.fit.fittheory.FitTheory for more information.
This dictionary defines default configuration parameters that have effects on fit functions and estimation functions, mainly on fit constraints. This dictionary is accessible as attribute FitTheories.config, which can be modified by configuration functions defined in CONFIGURE.
Class wrapping functions from silx.math.fit.functions and providing estimate functions for all of these fit functions.
Wrapping of silx.math.fit.functions.sum_ahypermet() without the tail flags in the function signature.
Depending on the value of self.config[‘HypermetTails’], one can activate or deactivate the various terms of the hypermet function.
self.config[‘HypermetTails’] must be an integer between 0 and 15. It is a set of 4 binary flags, one for activating each one of the hypermet terms: gaussian function, short tail, long tail, step.
For example, 15 can be expressed as 1111 in base 2, so a flag of 15 means all terms are active.
Order n polynomial. The order of the polynomial is defined by the number of coefficients (*pars).
Estimate polynomial coefficients for a degree n polynomial.
Return the strip background of y, using parameters from config dictionary (StripBackgroundFlag, StripWidth, StripIterations, StripThresholdFactor)
Estimate scaling for y prior to peak search. A smoothing filter is applied to y to estimate the noise level (chi-squared)
Parameters: | y – Data array |
---|---|
Returns: | Scaling factor |
Search for peaks in y array, after padding the array and multiplying its value by a scaling factor.
Parameters: |
|
---|---|
Returns: | List of peak indices |
Estimation of Height, Position, FWHM of peaks, for gaussian-like curves.
This functions finds how many parameters are needed, based on the number of peaks detected. Then it estimates the fit parameters with a few iterations of fitting gaussian functions.
Parameters: |
|
---|---|
Returns: | Tuple of estimated fit parameters and fit constraints. Parameters to be estimated for each peak are: Height, Position, FWHM. Fit constraints depend on config. |
Estimation of Area, Position, FWHM of peaks, for gaussian-like curves.
This functions uses estimate_height_position_fwhm(), then converts the height parameters to area under the curve with the formula area = sqrt(2*pi) * height * fwhm / (2 * sqrt(2 * log(2))
Parameters: |
|
---|---|
Returns: | Tuple of estimated fit parameters and fit constraints. Parameters to be estimated for each peak are: Area, Position, FWHM. Fit constraints depend on config. |
Estimation of Area, Position, FWHM of peaks, for Lorentzian curves.
This functions uses estimate_height_position_fwhm(), then converts the height parameters to area under the curve with the formula area = height * fwhm * 0.5 * pi
Parameters: |
|
---|---|
Returns: | Tuple of estimated fit parameters and fit constraints. Parameters to be estimated for each peak are: Area, Position, FWHM. Fit constraints depend on config. |
Estimation of Height, Position, FWHM1, FWHM2 of peaks, for asymmetric gaussian-like curves.
This functions uses estimate_height_position_fwhm(), then adds a second (identical) estimation of FWHM to the fit parameters for each peak, and the corresponding constraint.
Parameters: |
|
---|---|
Returns: | Tuple of estimated fit parameters and fit constraints. Parameters to be estimated for each peak are: Height, Position, FWHM1, FWHM2. Fit constraints depend on config. |
Estimation of Height, Position, FWHM, eta of peaks, for pseudo-Voigt curves.
Pseudo-Voigt are a sum of a gaussian curve G(x) and a lorentzian curve L(x) with the same height, center, fwhm parameters: y(x) = eta * G(x) + (1-eta) * L(x)
This functions uses estimate_height_position_fwhm(), then adds a constant estimation of eta (0.5) to the fit parameters for each peak, and the corresponding constraint.
Parameters: |
|
---|---|
Returns: | Tuple of estimated fit parameters and fit constraints. Parameters to be estimated for each peak are: Height, Position, FWHM, eta. Constraint for the eta parameter can be set to QUOTED (0.–1.) by setting config`['QuotedEtaFlag'] to ``True`. If this is not the case, the constraint code is set to FREE. |
Estimation of Height, Position, FWHM1, FWHM2, eta of peaks, for asymmetric pseudo-Voigt curves.
This functions uses estimate_height_position_fwhm(), then adds an identical FWHM2 parameter and a constant estimation of eta (0.5) to the fit parameters for each peak, and the corresponding constraints.
Constraint for the eta parameter can be set to QUOTED (0.–1.) by setting config`['QuotedEtaFlag'] to ``True`. If this is not the case, the constraint code is set to FREE.
Parameters: |
|
---|---|
Returns: | Tuple of estimated fit parameters and fit constraints. Parameters to be estimated for each peak are: Height, Position, FWHM1, FWHM2, eta. |
Estimation of Area, Position, FWHM1, eta of peaks, for pseudo-Voigt curves.
This functions uses estimate_pvoigt(), then converts the height parameter to area.
Parameters: |
|
---|---|
Returns: | Tuple of estimated fit parameters and fit constraints. Parameters to be estimated for each peak are: Area, Position, FWHM, eta. |
Estimation of area, position, fwhm, st_area_r, st_slope_r, lt_area_r, lt_slope_r, step_height_r of peaks, for hypermet curves.
Parameters: |
|
---|---|
Returns: | Tuple of estimated fit parameters and fit constraints. Parameters to be estimated for each peak are: area, position, fwhm, st_area_r, st_slope_r, lt_area_r, lt_slope_r, step_height_r . |
Estimation of parameters for stepdown curves.
The functions estimates gaussian parameters for the derivative of the data, takes the largest gaussian peak and uses its estimated parameters to define the center of the step and its fwhm. The estimated amplitude returned is simply max(y) - min(y).
Parameters: |
|
---|---|
Returns: | Tuple of estimated fit parameters and fit newconstraints. Parameters to be estimated for each stepdown are: height, centroid, fwhm . |
Estimation of parameters for slit curves.
The functions estimates stepup and stepdown parameters for the largest steps, and uses them for calculating the center (middle between stepup and stepdown), the height (maximum amplitude in data), the fwhm (distance between the up- and down-step centers) and the beamfwhm (average of FWHM for up- and down-step).
Parameters: |
|
---|---|
Returns: | Tuple of estimated fit parameters and fit constraints. Parameters to be estimated for each slit are: height, position, fwhm, beamfwhm . |
Estimation of parameters for a single step up curve.
The functions estimates gaussian parameters for the derivative of the data, takes the largest gaussian peak and uses its estimated parameters to define the center of the step and its fwhm. The estimated amplitude returned is simply max(y) - min(y).
Parameters: |
|
---|---|
Returns: | Tuple of estimated fit parameters and fit constraints. Parameters to be estimated for each stepup are: height, centroid, fwhm . |
Estimation of parameters for periodic gaussian curves: number of peaks, distance between peaks, height, position of the first peak, fwhm
The functions detects all peaks, then computes the parameters the following way:
distance: average of distances between detected peaks
height: average height of detected peaks
- fwhm: fwhm of the highest peak (in number of samples) if
field 'AutoFwhm' in config is True, else take the default value (field 'FwhmPoints' in config)
Parameters: |
|
---|---|
Returns: | Tuple of estimated fit parameters and fit constraints. |
Dictionary of fit theories: fit functions and their associated estimation function, parameters list, configuration function and description.