CHAP.utils.fit

File : fit.py Author : Rolf Verberg Description: General curve fitting module

Module Contents

Classes

FitProcessor

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

Component

Components

Parameters

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

ModelResult

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

Fit

Wrapper class for scipy/lmfit.

FitMap

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

Data

logger

FLOAT_MIN

FLOAT_MAX

FLOAT_EPS

fwhm_factor

height_factor

API

CHAP.utils.fit.logger

‘getLogger(…)’

CHAP.utils.fit.FLOAT_MIN

None

CHAP.utils.fit.FLOAT_MAX

None

CHAP.utils.fit.FLOAT_EPS

None

CHAP.utils.fit.fwhm_factor

None

CHAP.utils.fit.height_factor

None

class CHAP.utils.fit.FitProcessor

Bases: CHAP.processor.Processor

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

Initialization

Constructor of PipelineItem class

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.

Raises:

ValueError – Invalid input or configuration parameter.

Returns:

The fitted data object.

Return type:

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

static create_multipeak_model(model_config)

Create a multipeak model.

class CHAP.utils.fit.Component(model, prefix='')

Initialization

class CHAP.utils.fit.Components

Bases: dict

__setitem__(key, value)
add(model, prefix='')
property components
class CHAP.utils.fit.Parameters

Bases: dict

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

Initialization

Initialize self. See help(type(self)) for accurate signature.

__setitem__(key, value)
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 ''.

class CHAP.utils.fit.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)

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

Initialization

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 CHAP.utils.fit.Fit(nxdata, config)

Wrapper class for scipy/lmfit.

Initialization

Initialize Fit.

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
property covar

Return the covarience matrix of the best fit parameters.

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.

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.

print_fit_report(result=None, show_correl=False)

Print a fit report.

add_parameter(parameter)
add_model(model, prefix)

Add a model component to the fit model.

eval(x, result=None)

Evaluate the best fit.

fit(config=None, **kwargs)

Fit the model to the input data.

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.

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.

_create_prefixes(models)
_setup_fit_model(parameters, models)

Setup the fit model.

_setup_fit(config, guess=False)

Setup the fit.

_check_linearity_model()

Identify the linearity of all model parameters and check if the model is linear or not.

_fit_linear_model(x, y)

Perform a linear fit by direct matrix solution with numpy.

_fit_nonlinear_model(x, y, **kwargs)

Perform a nonlinear fit with spipy or lmfit

_normalize()

Normalize the data and initial parameters.

_renormalize()

Renormalize the data and results.

_reset_par_at_boundary()
_residual(pars, x, y)
class CHAP.utils.fit.FitMap(nxdata, config)

Bases: CHAP.utils.fit.Fit

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

Initialization

Initialize FitMap.

property best_errors

Return errors in the best fit parameters.

property best_fit

Return the best fits.

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.

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.

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.

best_parameters(dims=None)

Return the best fit parameters.

freemem()

Free memory allocated for parallel processing.

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

Plot the best fits.

fit(config=None, **kwargs)

Fit the model to the input data.

_fit_parallel(current_best_values, num, n_start, **kwargs)
_fit(n, current_best_values, return_result=False, **kwargs)
_fit_with_bounds_check(n, current_best_values, **kwargs)
_renormalize(n, result)