fit module

File : fit.py Author : Rolf Verberg <rolfverberg AT gmail dot com> Description: General curve fitting module

class Component(model, prefix='')

Bases: object

A model fit component.

class Components

Bases: dict

The dictionary of model fit components.

add(model, prefix='')

Add a model to the model fit component dictionary.

property components

Return the model fit component dictionary.

class Fit(nxdata, config)

Bases: object

Wrapper class for scipy/lmfit.

add_model(model, prefix)

Add a model component to the fit model.

add_parameter(parameter)

Add a fit parameter to the fit model.

property best_errors

Return errors in the best fit parameters.

property best_fit

Return the best fit.

property best_parameters

Return the best fit parameters.

property best_values

Return values of the best fit parameters.

property chisqr

Return the chisqr value of the best fit.

property components

Return the fit model components info.

property covar

Return the covarience matrix of the best fit parameters.

eval(x, result=None)

Evaluate the best fit.

fit(config=None, **kwargs)

Fit the model to the input data.

static guess_init_peak(x, y, *args, center_guess=None, use_max_for_center=True)

Return a guess for the initial height, center and fwhm for a single peak.

property init_parameters

Return the initial parameters for the fit model.

property init_values

Return the initial values for the fit parameters.

property normalization_offset

Return the normalization_offset for the fit model.

property num_func_eval

Return the number of function evaluations for the best fit.

property parameters

Return the fit parameter info.

plot(y=None, y_title=None, title=None, result=None, skip_init=False, plot_comp=True, plot_comp_legends=False, plot_residual=False, plot_masked_data=True, **kwargs)

Plot the best fit.

print_fit_report(result=None, show_correl=False)

Print a fit report.

property redchi

Return the redchi value of the best fit.

property residual

Return the residual in the best fit.

property success

Return the success value for the fit.

property var_names

Return the variable names for the covarience matrix property.

property x

Return the input x-array.

property y

Return the input y-array.

class FitMap(nxdata, config)

Bases: Fit

Wrapper to the Fit class to fit data on a N-dimensional map.

property best_errors

Return errors in the best fit parameters.

property best_fit

Return the best fits.

best_parameters(dims=None)

Return the best fit parameters.

property best_values

Return values of the best fit parameters.

property chisqr

Return the chisqr value of each best fit.

property components

Return the fit model components info.

property covar

Return the covarience matrices of the best fit parameters.

fit(config=None, **kwargs)

Fit the model to the input data.

freemem()

Free memory allocated for parallel processing.

property max_nfev

Return if the maximum number of function evaluations is reached for each fit.

property num_func_eval

Return the number of function evaluations for each best fit.

property out_of_bounds

Return the out_of_bounds value of each best fit.

plot(dims=None, y_title=None, plot_residual=False, plot_comp_legends=False, plot_masked_data=True, **kwargs)

Plot the best fits.

property redchi

Return the redchi value of each best fit.

property residual

Return the residual in each best fit.

property success

Return the success value for each fit.

property var_names

Return the variable names for the covarience matrix property.

property y

Return the input y-array.

property ymap

Return the input y-array map.

class FitProcessor

Bases: Processor

A processor to perform a fit on a data set or data map.

static create_multipeak_model(model_config)

Create a multipeak model.

process(data, config=None)

Fit the data and return a CHAP.utils.fit.Fit or CHAP.utils.fit.FitMap object depending on the dimensionality of the input data. The input data should contain a NeXus NXdata object, with properly defined signal and axis.

Parameters:
  • data (list[PipelineData]) – Input data containing the nexusformat.nexus.NXdata object to fit.

  • config (dict, optional) – Fit configuration.

Raises:

ValueError – Invalid input or configuration parameter.

Returns:

The fitted data object.

Return type:

Union[CHAP.utils.fit.Fit, CHAP.utils.fit.FitMap]

class ModelResult(model, parameters, x=None, y=None, method=None, ast=None, res_par_exprs=None, res_par_indices=None, res_par_names=None, result=None)

Bases: object

The result of a model fit, mimicking the functionality of a similarly named class in the lmfit library.

eval_components(x=None, parameters=None)

Evaluate each component of a composite model function.

Parameters:
  • x (Union[list, np.ndarray], optional) – Independent variable, defaults to None, in which case the class variable x is used.

  • parameters (Parameters, optional) – Composite model parameters, defaults to None, in which case the class variable params is used.

Returns:

A dictionary with component name and evealuated function values key, value pairs.

Return type:

dict

fit_report(show_correl=False)

Generates a report of the fitting results with their best parameter values and uncertainties.

Parameters:

show_correl (bool, optional) – Whether to show list of correlations, defaults to False.

class Parameters

Bases: dict

A dictionary of FitParameter objects, mimicking the functionality of a similarly named class in the lmfit library.

add(parameter, prefix='')

Add a fit parameter.

Parameters:
  • parameter (Union[str, FitParameter]) – The fit parameter to add to the dictionary.

  • prefix (str, optional) – The prefix for the model to which this parameter belongs, defaults to ‘’.