Mixed Data Fitter#

class MixedDataFitter(x_values: List | ndarray, y_values: List | ndarray, model_list: List[str], fitter_dictionary=None, max_iterations: int = 1000)[source]#

Bases: object

Class to fit a mixture of different models to data.

Parameters:
  • x_values – The x-values for the data.

  • y_values – The y-values for the data.

  • model_list – List of models to fit (e.g., LINE, GAUSSIAN, LOG_NORMAL)

  • max_iterations – The maximum number of iterations for fitting procedure.

Methods

fit(p0[, frozen])

Fit the data.

get_fitted_curve()

Gets the y-values from the fitted model.

get_model_parameters([model, errors])

Extracts parameters (and error) values for a specific model, or for all models if no model is specified.

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.

_create_model_function() Callable[source]#

Creates a composite model function based on the specified models.

Returns:

A composite model for fitting.

_expected_param_count() int[source]#

Calculates the expected number of parameters based on the model list.

Returns:

The number of parameters.

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.

_get_bounds() Tuple[ndarray, ndarray][source]#

Sets the bounds for each parameter based on the model list.

Returns:

Lower and upper bounds for the parameters.

_instantiate_fitter(model: str, return_values: str | List[str] = 'class')[source]#

Instantiate the fitter for the specified model and return requested values.

Parameters:
  • model – The model name as a string.

  • return_values – The specific attribute(s) or instance to return. Options are ‘class’, ‘n_par’, and ‘bounds’. Can be a string (for one value) or a list of strings.

Returns:

The requested values as a single value or a tuple.

Raises:

ValueError – If the model is not recognized or return_values are invalid.

_parameter_extractor(values: ndarray) dict[source]#

Extracts the parameters for each model in the model list.

Parameters:

values – The values from which the model dictionary is to be extracted.

Returns:

A dictionary where the keys are model names and the values are lists of parameters/error values.

_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 the individual fitters function.

Parameters:

plotter – The plotting axis object

_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).

fit(p0: List | ndarray, frozen: int | List[int] | None = None)[source]#

Fit the data.

Parameters:
  • p0 (Union[List, np.ndarray]) – Initial guess for the fitted parameters.

  • frozen (Union[int, List[int]]) – Parameter number of list of parameter numbers to freeze the value of.

Raises:

ValueError – If the length of the initial guess is not equal to the expected parameter count.

get_fitted_curve() ndarray[source]#

Gets the y-values from the fitted model.

Returns:

The y-values from the fitted model

Raises:

ValueError – If the model has not been fitted yet.

get_model_parameters(model: str | None = None, errors: bool = False)[source]#

Extracts parameters (and error) values for a specific model, or for all models if no model is specified.

Parameters:
  • model – Model name to extract parameters for. If unspecified, extracts parameters for all models. Defaults to None.

  • errors – If True, includes the errors in the returned output. Defaults to False.

Returns:

A dictionary containing:

  • ”parameters”: Nested dictionary of parameter values for each model if get_errors is True.

  • ”errors”: Nested dictionary of errors for each model (if get_errors=True).

Otherwise, returns just the parameters directly.

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.

Currently the MixedDataFitter supports fitting the following models together,

Full Import#

from pymultifit.fitters.mixed_f import MixedDataFitter