Fitting - fit.py¶
This module defines a set of functions used to fit the species
concentrations evolution over time and optionally the charge passed
evolution over time, stored in an object of Dataset class. This fit
proceeds via the fit_dataset() function that uses the functions
residuals(), calculate_residuals() and evaluate().
After the fit is performed the print_result() function can be used to
print the fit parameters initial values and setup (min, max, vary) and
their fitted values and standard deviations.
The function evaluate() can be used to get values from a kinetic model
outside of the scope of fitting data, e.g. to test the influence of
model parameters on the concentrations evolution over time.
-
fit.calculate_residuals(df, fit, names)¶ Calculates residuals values by comparing values in df and in fit.
Parameters: - df (pandas.DataFrame) – Holds the data to be fitted. Either concentrations vs time or charge passed vs time depending on the situation.
- fit (numpy.ndarray) – Holds the fit evaluation.
- names – Names of the columns in df that hold the data to be compared to the fit values. Necessary because in some cases not all of the data stored in df is fitted.
-
fit.evaluate(derivatives, params, t)¶ Evaluate the concentration(s) evolution(s) over time.
Parameters: - derivatives (function) – A function in the form dy = f(y, t, p) used to compute d(concentration)/dt at a time t for each species. Used by scipy.integrate.odeint
- params (lmfit.parameter.Parameters) – The parameters values used to compute the derivatives function, for details on this object class see: https://lmfit.github.io/lmfit-py/parameters.html
- t (list) – Time values at which the concentrations should be evaluated.
-
fit.fit_dataset(dataset, derivatives, parameters, c0={}, c0_untracked={}, c_to_q=None)¶ Fit a dataset holding concentration vs t data and optionally charge vs t.
The parameters parameters, c0 and c0_untracked are dictionaries in which each value is a dictionary of the parameters to use in order to initialize objects of the lmfit.Parameter class. The parameters that can be passed via this dictionary are in particular: value, vary, min, max and expr. Details on the Parameter class can be found here: https://lmfit.github.io/lmfit-py/parameters.html
Parameters: - dataset (chemical_kinetics.data.Dataset) – Object holding the different DataFrames containing the data to be fitted.
- derivatives (function) – A function in the form dy = f(y, t, p) used to compute d(concentration)/dt at a time t for each species. Used by scipy.integrate.odeint
- parameters (dict) – Stores parameter names (str): parameters (dict) (e.g. value, min, max, vary) to be passed to the corresponding lmfit.Parameter. Represents all the parameters of the kinetic model.
- c0 (dict, optional) – Stores species name (str): parameters (dict) (e.g. value, min, max, vary) to be passed to the corresponding lmfit.Parameter. Represent the concentrations at initial time for the species whose concentration evolution over time is stored in dataset.df_c.
- c0_untracked (collections.OrderedDict, optional) – Stores species name (str): parameters (dict) (e.g. value, min, max, vary) to be passed to the corresponding lmfit.Parameter. Represent concentrations at initial time for the species whose concentrations evolution over time is NOT stored in dataset.df_c. An ordered dictionary is necessary in this case to be able to pass parameters properly to the scipy.integrate.odeint solver.
- c_to_q (function, optional) – Used to convert the concentrations over time evolution into charge passed.
-
fit.print_result(dataset)¶ Pretty printing of the fit parameters stored in dataset.
Parameters: dataset (chemical_kinetics.data.Dataset) – Object holding the different DataFrames containing the initial parameters and the fitted parameters.
-
fit.residuals(params, df_c, derivatives, tracked_species, df_q=None, c_to_q=None)¶ Calculates residuals for concentrations vs t and optionally charge vs t.
Parameters: - params (lmfit.parameter.Parameters) – The parameters values used to compute the derivatives function, for details on this object class see: https://lmfit.github.io/lmfit-py/parameters.html
- df_c (pandas.DataFrame) – Holds the concentration vs time data to be fitted.
- derivatives (function) – A function in the form dy = f(y, t, p) used to compute d(concentration)/dt at a time t for each species. Used by scipy.integrate.odeint
- tracked_species (list) – Column names in df_c corresponding to the fitted data (used to exclude e.g. the “t” column).
- df_q (pandas.DataFrame, optional) – Holds the charge passed vs time data to be fitted.
- c_to_q (function, optional) – Used to convert the concentrations over time evolution into charge passed.
Returns: Residuals values.
Return type: list