Mixed Data Fitter#

class MixedDataFitter(x_values: List | ndarray, y_values: List | ndarray, model_list: List[str], 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)

Fit the data.

format_param(value[, t_low, t_high])

Formats the parameter value based on its magnitude.

get_fit_values()

Gets the y-values from the fitted model.

get_parameters([model, get_errors])

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

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

Plots the original data, fitted model, and optionally individual components.

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

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

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

_plot_individual_fitter(plotter)[source]#

Plot the individual fitters function.

Parameters:

plotter – The plotting axis object

_validate_models()[source]#

Validate the models in the model list.

Raises:

ValueError – If any model in the model list is not recognized.

fit(p0: List | ndarray)[source]#

Fit the data.

Parameters:

p0 – Initial guess for the fitted parameters.

Raises:

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

static format_param(value: float, t_low: float = 0.001, t_high: float = 10000) str[source]#

Formats the parameter value based on its magnitude.

Parameters:
  • value – The value of the parameter to be formatted.

  • t_low – The lower bound below which the value is to be formatted.

  • t_high – The upper bound above which the value is to be formatted.

Returns:

The formatted value of the parameter

get_fit_values() 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_parameters(model: str | None = None, get_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.

  • get_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.

plot_fit(show_individuals: bool = False, x_label: str | None = None, y_label: str | None = None, title: str | None = None, data_label: str | None = None, figure_size: tuple = (12, 6)) tuple[source]#

Plots the original data, fitted model, and optionally individual components.

Parameters:
  • show_individuals – Whether to plot individual fitted functions, by default False.

  • x_label – The label for the x-axis of the plot.

  • y_label – The label for the y-axis of the plot.

  • title – The title for the plot.

  • data_label – The label for the data to be plotted.

  • figure_size – The size of the figure. Default is (12,6).

Returns:

A tuple of figure and axes object for the drawn plot

Raises:

ValueError – Raised if the plotting function is called before the fitting is done.

Currently the MixedDataFitter supports fitting the following models together,

Full Import#

from pymultifit.fitters.mixed_f import MixedDataFitter