Skip to content

easyscience.fitting

fitter

Fitter

Fitter is a class which makes it possible to undertake fitting utilizing one of the supported minimizers.

available_minimizers property

Get a list of the names of available fitting minimizers

:return: List of available fitting minimizers :rtype: List[str]

fit property

Property which wraps the current fit function from the fitting interface. This property return a wrapped fit function which converts the input data into the correct shape for the optimizer, wraps the fit function to re-constitute the independent variables and once the fit is completed, reshape the inputs to those expected.

fit_function property writable

The raw fit function that the optimizer will call (no wrapping) :return: Raw fit function

fit_object property writable

The EasyScience object which will be used as a model :return: EasyScience Model

max_evaluations property writable

Get the maximal number of evaluations for the minimizer.

:return: Maximal number of steps for the minimizer

minimizer property

Get the current fitting minimizer object.

:return: :rtype: MinimizerBase

tolerance property writable

Get the tolerance for the minimizer.

:return: Tolerance for the minimizer

create(minimizer_enum=DEFAULT_MINIMIZER)

Create the required minimizer. :param minimizer_enum: The enum of the minimization engine to create.

initialize(fit_object, fit_function)

Set the model and callable in the calculator interface.

:param fit_object: The EasyScience model object :param fit_function: The function to be optimized against.

switch_minimizer(minimizer_enum)

Switch minimizer and initialize. :param minimizer_enum: The enum of the minimizer to create and instantiate.

minimizers

minimizer_base

MinimizerBase

This template class is the basis for all minimizer engines in EasyScience.

all_methods() abstractmethod staticmethod

Return a list of all available methods for the minimizer.

:return: List of all available methods :rtype: List[str]

convert_to_par_object(obj) abstractmethod staticmethod

Convert an EasyScience.Objects.Base.Parameter object to an engine Parameter object.

convert_to_pars_obj(par_list=None) abstractmethod

Create an engine compatible container with the Parameters converted from the base object.

:param par_list: If only a single/selection of parameter is required. Specify as a list :type par_list: List[str] :return: engine Parameters compatible object

evaluate(x, minimizer_parameters=None, **kwargs)

Evaluate the fit function for values of x. Parameters used are either the latest or user supplied. If the parameters are user supplied, it must be in a dictionary of {'parameter_name': parameter_value,...}.

:param x: x values for which the fit function will be evaluated :type x: np.ndarray :param minimizer_parameters: Dictionary of parameters which will be used in the fit function. They must be in a dictionary of {'parameter_name': parameter_value,...} :type minimizer_parameters: dict :param kwargs: additional arguments :return: y values calculated at points x for a set of parameters. :rtype: np.ndarray

fit(x, y, weights=None, model=None, parameters=None, method=None, tolerance=None, max_evaluations=None, **kwargs) abstractmethod

Perform a fit using the engine.

:param x: points to be calculated at :type x: np.ndarray :param y: measured points :type y: np.ndarray :param weights: Weights for supplied measured points :type weights: np.ndarray :param model: Optional Model which is being fitted to :param parameters: Optional parameters for the fit :param method: method for the minimizer to use. :type method: str :param kwargs: Additional arguments for the fitting function. :return: Fit results

supported_methods() abstractmethod staticmethod

Return a list of supported methods for the minimizer.

:return: List of supported methods :rtype: List[str]

minimizer_bumps

Bumps

Bases: MinimizerBase

This is a wrapper to Bumps: https://bumps.readthedocs.io/ It allows for the Bumps fitting engine to use parameters declared in an EasyScience.Objects.Base.BaseObj.

__init__(obj, fit_function, minimizer_enum=None)

Initialize the fitting engine with a BaseObj and an arbitrary fitting function.

:param obj: Object containing elements of the Parameter class :type obj: BaseObj :param fit_function: function that when called returns y values. 'x' must be the first and only positional argument. Additional values can be supplied by keyword/value pairs :type fit_function: Callable

convert_to_par_object(obj) staticmethod

Convert an EasyScience.Objects.Base.Parameter object to a bumps Parameter object

:return: bumps Parameter compatible object. :rtype: BumpsParameter

convert_to_pars_obj(par_list=None)

Create a container with the Parameters converted from the base object.

:param par_list: If only a single/selection of parameter is required. Specify as a list :type par_list: List[str] :return: bumps Parameters list :rtype: List[BumpsParameter]

fit(x, y, weights=None, model=None, parameters=None, method=None, tolerance=None, max_evaluations=None, minimizer_kwargs=None, engine_kwargs=None, **kwargs)

Perform a fit using the lmfit engine.

:param x: points to be calculated at :type x: np.ndarray :param y: measured points :type y: np.ndarray :param weights: Weights for supplied measured points * Not really optional* :type weights: np.ndarray :param model: Optional Model which is being fitted to :type model: lmModel :param parameters: Optional parameters for the fit :type parameters: List[BumpsParameter] :param kwargs: Additional arguments for the fitting function. :param method: Method for minimization :type method: str :return: Fit results :rtype: ModelResult

minimizer_dfo

DFO

Bases: MinimizerBase

This is a wrapper to Derivative Free Optimisation for Least Square: https://numericalalgorithmsgroup.github.io/dfols/

__init__(obj, fit_function, minimizer_enum=None)

Initialize the fitting engine with a BaseObj and an arbitrary fitting function.

:param obj: Object containing elements of the Parameter class :type obj: BaseObj :param fit_function: function that when called returns y values. 'x' must be the first and only positional argument. Additional values can be supplied by keyword/value pairs :type fit_function: Callable

convert_to_par_object(obj) staticmethod

Required by interface but not needed for DFO-LS

convert_to_pars_obj(par_list=None)

Required by interface but not needed for DFO-LS

fit(x, y, weights=None, model=None, parameters=None, method=None, tolerance=None, max_evaluations=None, **kwargs)

Perform a fit using the DFO-ls engine.

:param x: points to be calculated at :type x: np.ndarray :param y: measured points :type y: np.ndarray :param weights: Weights for supplied measured points * Not really optional* :type weights: np.ndarray :param model: Optional Model which is being fitted to :type model: lmModel :param parameters: Optional parameters for the fit :type parameters: List[bumpsParameter] :param kwargs: Additional arguments for the fitting function. :param method: Method for minimization :type method: str :return: Fit results :rtype: ModelResult

minimizer_lmfit

LMFit

Bases: MinimizerBase

This is a wrapper to the extended Levenberg-Marquardt Fit: https://lmfit.github.io/lmfit-py/ It allows for the lmfit fitting engine to use parameters declared in an EasyScience.Objects.Base.BaseObj.

__init__(obj, fit_function, minimizer_enum=None)

Initialize the minimizer with the BaseObj and the fit_function to be used.

:param obj: Base object which contains the parameters to be fitted :type obj: BaseObj :param fit_function: Function which will be fitted to the data :type fit_function: Callable :param method: Method to be used by the minimizer :type method: str

convert_to_par_object(parameter) staticmethod

Convert an EasyScience.Objects.Base.Parameter object to a lmfit Parameter object.

:return: lmfit Parameter compatible object. :rtype: LMParameter

convert_to_pars_obj(parameters=None)

Create an lmfit compatible container with the Parameters converted from the base object.

:param parameters: If only a single/selection of parameter is required. Specify as a list :return: lmfit Parameters compatible object

fit(x, y, weights=None, model=None, parameters=None, method=None, tolerance=None, max_evaluations=None, minimizer_kwargs=None, engine_kwargs=None, **kwargs)

Perform a fit using the lmfit engine.

:param method: :type method: :param x: points to be calculated at :type x: np.ndarray :param y: measured points :type y: np.ndarray :param weights: Weights for supplied measured points :type weights: np.ndarray :param model: Optional Model which is being fitted to :type model: LMModel :param parameters: Optional parameters for the fit :type parameters: LMParameters :param minimizer_kwargs: Arguments to be passed directly to the minimizer :type minimizer_kwargs: dict :param kwargs: Additional arguments for the fitting function. :return: Fit results :rtype: ModelResult

utils

FitResults

At the moment this is just a dummy way of unifying the returned fit parameters.

multi_fitter

MultiFitter

Bases: Fitter

Extension of Fitter to enable multiple dataset/fit function fitting. We can fit these types of data simultaneously: - Multiple models on multiple datasets.