BaseFitter#

class BaseFitter(x_values: list | ndarray, y_values: list | ndarray, max_iterations: int = 1000)[source]#

The base class for multi-fitting functionality.

Methods

dry_run([axis])

Plot the x and y data for a quick visual inspection of the data.

fit(p0[, frozen])

Fit the data.

fit_boundaries()

Defines the distribution boundaries to be used by fitter.

fitter(x, params)

Fitter function for multi-fitting.

get_fitted_curve()

Get the fitted values of the model.

get_model_parameters([select, errors])

Extract specific parameter values or their uncertainties from the fitting process.

get_value_error_pair([mean_values, std_values])

Retrieve the value/error pairs for the fitted parameters.

plot_fit([show_individuals, x_label, ...])

Plot the fitted models.

_adjust_parameters(p0: List[Tuple[float, ...]])[source]#

Adjust input parameters to include defaults for secondary parameters if missing.

Parameters:
p0: List[List[float]]

A list of initial guesses for the parameters.

Returns:
adjusted_p0: List[List[float]]

Adjusted parameter list with default values for missing secondary parameters.

_covariance()[source]#

Store the covariance matrix of the fitted model.

Returns:
np.ndarray

An array containing the covariance matrix of the fitted model.

_fit_preprocessing(p0: List[Tuple[float, ...]] | ndarray, frozen: List[bool])[source]#

Process frozen parameters and adjust bounds.

Parameters:
p0: listOfTuplesOrArray

A list of initial guesses for the parameters of the models. For example: [(1, 1, 0), (3, 3, 2)].

frozen: List[bool]

A list of booleans indicating which parameters are frozen. For example: [False, False, True] for 3 parameters.

Returns:
Tuple[np.ndarray, np.ndarray, np.ndarray]

Adjusted lower and upper bounds, and flattened initial guesses.

static _format_param(value, t_low=0.001, t_high=10000) str[source]#

Formats the parameter value to scientific notation based on its magnitude.

Parameters:
value: float

The value of the parameter to be formatted.

t_low: float, optional

The lower bound below which the formatting should be applied to the value. Defaults to 0.001.

t_high: float, optional

The upper bound above which the formatting should be applied to the value. Defaults to 10,000.

Returns:
str:

A formatted string of the parameter value.

_n_fitter(x: ndarray, *params: Iterable) ndarray[source]#

Perform N-fitting by summing over multiple parameter sets.

Parameters:
xnp.ndarray

Input array of values for which the composite function is evaluated.

paramstuple

A tuple with all parameters to be fitted in an array of size (self.n_fits, self.n_par) where: - self.n_fits is the number of individual fits. - self.n_par is the number of parameters per fit.

Returns:
np.ndarray

An array containing the composite fitted values for the input x.

_params() ndarray[source]#

Store the fitted parameters of the fitted model.

Returns:
np.ndarray

The parameters obtained after performing the fit.

Raises:
RuntimeError

If the fit has not been performed yet (i.e., self.params is None).

Notes

This method assumes that the fitting process assigns values to self.params.

_plot_individual_fitter(plotter)[source]#

Plot individual fits from the composite fitter.

Parameters:
plottermatplotlib.axes.Axes

The axis object where the plots will be rendered.

Notes

  • self.params must contain the fitted parameters reshaped into (self.n_fits, self.n_par).

  • Each plot will be labeled with the class name and the index of the fit, along with the formatted parameters.

_standard_errors() ndarray[source]#

Store the standard errors of the fitted parameters.

Returns:
np.ndarray

An array containing the standard errors of the fitted parameters.

Raises:
RuntimeError

If the fit has not been performed yet (i.e., self.covariance is None).

dry_run(axis=None)[source]#

Plot the x and y data for a quick visual inspection of the data.

Parameters:
axis:

The axis to plot the data on.

fit(p0: List[Tuple[float, ...]] | ndarray, frozen: List[bool] | None = None)[source]#

Fit the data.

Parameters:
p0: listOfTuplesOrArray

A list of initial guesses for the parameters of the models. For example: [(1, 1, 0), (3, 3, 2)].

frozen: List[bool]

A list of booleans indicating whether each parameter is frozen. For example: [False, False, True] for 3 parameters.

static fit_boundaries()[source]#

Defines the distribution boundaries to be used by fitter.

static fitter(x: ndarray, params: Tuple[float, Any])[source]#

Fitter function for multi-fitting.

Parameters:
x: np.ndarray

The x-array on which the fitting is to be performed.

params: List[float]

A list of parameters to fit.

get_fitted_curve() ndarray[source]#

Get the fitted values of the model.

Returns:
np.ndarray

An array of fitted values.

get_model_parameters(select: Tuple[int, Any] | None = None, errors: bool = False)[source]#

Extract specific parameter values or their uncertainties from the fitting process.

This method allows for retrieving the fitted parameters or their corresponding standard errors for specific

sub-models, or for all sub-models if no selection is provided.

Parameters:
selectlist of int or None, optional

A list of indices specifying which sub-models to extract parameters for. Indexing starts at 1. If None, parameters for all sub-models are returned. Defaults to None.

errorsbool, optional

If True, both the parameter values and their standard errors are returned. Defaults to False.

Returns:
np.ndarray or tuple of np.ndarray
  • If errors is False:
    • A 2D array of shape (n_parameters, selected_models) with parameter values for the selected models.

  • If errors is True: A tuple of two 2D arrays:
    • The first array contains the parameter values of shape (n_parameters, selected_models).

    • The second array contains the standard errors of the parameters, with the same shape.

Raises:
ValueError

If the input select is not a valid list of indices or is incompatible with the model structure.

Notes

  • The select parameter allows filtering by specific sub-model indices. If None, use all sub-models.

  • When errors is True, both the parameter means and their uncertainties are returned as separate arrays.

get_value_error_pair(mean_values: bool = True, std_values: bool = False) ndarray[source]#

Retrieve the value/error pairs for the fitted parameters.

This method provides the fitted parameter values and their corresponding standard errors as a combined array or individually based on the input flags.

Parameters:
mean_valuesbool, optional

If True, return only the values of the fitted parameters. Defaults to True.

std_valuesbool, optional

If True, return only the standard errors of the fitted parameters. Defaults to False.

Returns:
np.ndarray
  • If mean_values and std_values are both True: A 2D array of shape (n_parameters, 2),

    where each row is [value, error].

  • If mean_values is True and std_values is False: A 1D array of parameter values.

  • If std_values is True and mean_values is False: A 1D array of standard errors.

  • If both flags are False: An error message.

Raises:
ValueError

If both mean_values and std_values are False.

plot_fit(show_individuals: bool = False, x_label: str | None = None, y_label: str | None = None, data_label: list[str] | str | None = None, title: str | None = None, axis: Axes | None = None)[source]#

Plot the fitted models.

Parameters:
show_individuals: bool, optional

Whether to show individually fitted models or not.

x_label: str, optional

The label for the x-axis.

y_label: str, optional

The label for the y-axis.

title: str, optional

The title for the plot.

data_label: str, optional

The label for the data.

axis: Axes, optional

Axes to plot instead of the entire figure. Defaults to None.

Returns:
plotter

The plotter handle for the drawn plot.