Data Fits

class ionworkspipeline.data_fits.DataFit(objectives, source='', parameters=None, cost=None, optimizer=None, options=None, cost_logger=None)

A pipeline element that fits a model to data.

Parameters

objectivesionworkspipeline.objectives.Objective or dict

The objective(s) to use for the fit. This can be a single objective, or a dictionary of objectives. Each objective can be any class that implements the method build, which creates a function Objective.run_() that takes a dictionary of parameters and returns a scalar or vector cost. In general, we subclass ionworkspipeline.objectives.Objective to implement a particular objective.

sourcestr

A string describing the source of the data.

parametersdict, optional
A dictionary of parameters to fit. The values can be:
  • an iwp.Parameter object, e.g. iwp.Parameter(“x”)

  • a pybamm expression, in which case the other parameters should also be explicitly provided as iwp.Parameter objects, e.g.

    {

    “param”: 2 * pybamm.Parameter(“half-param”),

    “half-param”: iwp.Parameter(“half-param”)

    }

    works, but

    {“param”: 2 * iwp.Parameter(“half-param”)} would not work.

  • a function, containing other parameters, in which case the other parameters should again also be explicitly provided as iwp.Parameter objects, e.g.

    {

    “main parameter”: lambda x: pybamm.Parameter(“other parameter”) * x**2,

    “other parameter”: iwp.Parameter(“other parameter”)

    }

The name of the input parameter does not need to match the name of the parameter. In all cases, the DataFit class will automatically process this input to fit for “x”.

costionworkspipeline.costs.Cost, optional

The cost function to use when constructing the objective. If None, uses the optimizer’s default cost function.

optimizerionworkspipeline.optimizers.Optimizer, optional

The optimizer to use for the fit. Default is set by the DataFit subclasses.

optionsdict, optional

A dictionary of options to pass to the data fit.

cost_loggeroptional

A cost logger to use for logging the cost and parameters during the fit. Default is iwp.data_fits.CostLogger with default options.

Extends: ionworkspipeline.pipeline.PipelineElement

get_fit_results()

Get the results of the fit.

get_fitted_parameters(x_fit, parameter_values)

Get the dictionary of fitted parameters from the vector of fitted input parameters. For example, if the parameters are {“x”: 2 * iwp.Parameter(“x_scaled”)} and the fitted input parameters are [0.25], then the fitted parameters will be {“x”: 0.5}.

Parameters

x_fitarray_like

The fitted input parameters vector

parameter_valuespybamm.ParameterValues

Other parameter values, which in some cases are required to calculate new parameter values that depend on existing parameter values

Returns

dict

A dictionary of fitted parameters.

get_optimizer(objective, eq_constraints=None)

Return the optimizer with the objective and bounds set.

Parameters

objectivecallable

The objective function to be minimized.

property initial_values_dict

Get the initial values for the parameters to be used for the fit.

Returns

dict

A dictionary of initial values for the parameters being fit.

property initial_values_list

Like initial_values_dict(), but returns a list of initial values instead of a dictionary, in the same order as the parameters vector, for use by the optimizer.

plot_fit_results()

Plot the results of the fit by calling the plot_fit_results method of each internal callback in each objective. Any user-defined callbacks should be called manually instead of using this method.

plot_trace()

Plot the cost and each parameter as a function of iteration number.

process_parameters(parameters)

Process the parameters to be used for the fit. This function extracts all the ionworkspipeline.Parameter objects from the parameters dictionary, and saves their names in a list. This is used to convert the parameters vector to a dictionary of parameters (in x_to_inputs()).

x_to_inputs(x)

Convert the parameters vector to a dictionary of parameters that can be passed as inputs to a PyBaMM solver. This function is used by the fit to convert the parameters vector to a dictionary of parameters that can be passed to the model.

Parameters

xarray_like

The parameters vector.

Returns

dict

A dictionary of parameters.

class ionworkspipeline.data_fits.ArrayDataFit(objectives, **kwargs)

A pipeline element that fits a model to data for multiple independent variable values. The data for each independent variable value is fitted separately. The independent variable values should be given as the keys of the objectives dictionary. The value of each key should be a ionworkspipeline.objectives.Objective object. This objective will be used to fit the data for the corresponding independent variable value.

The user-supplied objectives should assign the independent variable value to the custom_parameters attribute of the objective as appropriate. This class simply calls a separate ionworkspipeline.DataFit for each provided objective. It does not pass the independent variable value to the objective, so the user must ensure that the objective is set up to use the independent variable value correctly.

For example, this can be used to fit a model to data at multiple temperatures, or fit each pulse of a GITT experiment separately, with post-processing to extract functional relationships between parameters and the independent variable.

The rest of the parameters are the same as for ionworkspipeline.DataFit.

Extends: ionworkspipeline.data_fits.data_fit.DataFit

class ionworkspipeline.data_fits.CostLogger(plot_every=None)

A class to log the cost and parameters during a fit, and plot the results.

Parameters

plot_everyint, optional

The number of iterations between each plot of the cost and parameters. If None, no plots are generated during the fit, but the final plot can be generated using the plot() method.