Data Fits

class ionworkspipeline.data_fits.DataFit(objectives, source='', parameters=None, cost=None, optimizer=None, options=None, cost_logger=None, multistarts=None, initial_guess_sampler=None, num_workers=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.

multistartsint, optional

Number of times to run the optimization from different initial guesses. If None, only runs once from the provided initial guess.

initial_guess_samplerionworkspipeline.samplers.Sampler, optional

Sampler to use for generating initial guesses when using multistarts. Default is iwp.samplers.LatinHypercube.

num_workersint, optional

Number of worker processes to use for parallel optimization. Not supported on Windows. If num_workers = 1, then multiprocessing is disabled. If num_workers = None, then the number of workers is set to the number of CPU cores.

Extends: ionworkspipeline.pipeline.PipelineElement

property all_fit_parameters

Get the list of all parameters being fit.

Returns

list

List of Parameter objects.

property bounds

Get the parameter bounds dictionary.

Returns

dict

Dictionary of parameter bounds.

property bounds_arrays

Get the upper and lower bounds for the parameters being fit.

Returns

tuple

Tuple containing (lower_bounds, upper_bounds) arrays.

check_multistart_initial_guesses()

Check that the initial parameter guesses are different for multistarts.

Raises

ValueError

If initial guesses are not unique or number of guesses does not match jobs.

property cost

Get the cost function.

Returns

ionworkspipeline.costs.Cost

Cost function object.

get_fit_results()

Get the results of the fit.

Returns

dict

Dictionary containing fit results for each objective.

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.

eq_constraintscallable, optional

Equality constraint functions.

Returns

ionworkspipeline.optimizers.Optimizer

Configured optimizer object.

property initial_guess_sampler

Get the initial guess sampler.

Returns

ionworkspipeline.samplers.Sampler

Sampler object for generating initial guesses.

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.

Returns

list

List of initial parameter values.

property job_id

Get the job ID.

Raises

AttributeError

Parent tasks do not have job IDs.

property jobs

Get the list of optimization jobs.

Returns

list

List of DataFitJob objects.

Raises

AttributeError

If jobs have not been created yet.

property multiprocessing

Whether multiprocessing is used. This property is set by the parent DataFit object.

Returns

bool

True if multiprocessing is enabled, False otherwise

property num_jobs

Get the number of optimization jobs.

Returns

int

Number of jobs.

property num_workers

Get the number of worker processes.

Returns

int

Number of workers.

property objectives

Get the objectives dictionary.

Returns

dict

Dictionary of optimization objectives.

property optimizer

Get the optimizer object.

Returns

ionworkspipeline.optimizers.Optimizer

Optimizer object.

property optimizer_result

Get the optimizer result.

Returns

iwp.Result

Optimization result object.

Raises

AttributeError

If optimization has not been run yet.

property options

Get the options dictionary.

Returns

dict

Dictionary of options.

property parameters

Get the parameters dictionary.

Returns

dict

Dictionary of parameters.

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.

Returns

dict

Dictionary containing figure and axes objects for each objective.

plot_trace()

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

Returns

tuple

Tuple containing figure and axes objects.

process_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()).

property results

Get the optimization results.

Returns

iwp.Result

Results object.

run(parameter_values)

Run the optimization to fit the model to data.

Parameters

parameter_valuesdict

Dictionary of parameter values to use for the fit.

Returns

iwp.Result

Results object containing fitted parameters and optimization results.

sample_initial_guesses(parameter_values, multistarts)

Generate multiple initial parameter guesses for multistart optimization.

Parameters

parameter_valuesdict

Dictionary of parameter values.

multistartsint

Number of initial guesses to generate.

Returns

tuple

Tuple containing (parameters_array, parameter_values_array) with initial guesses.

set_up_depickling()

Restore objects after unpickling when using multiprocessing.

set_up_pickling(parameter_values)

Prepare objects for pickling when using multiprocessing.

Parameters

parameter_valuesdict

Dictionary of parameter values.

spawn_jobs(parameters_array, parameter_values_array)

Create job objects for each optimization run.

Parameters

parameters_arraylist

List of parameter dictionaries for each job.

parameter_values_arraylist

List of parameter value dictionaries for each job.

Returns

list

List of DataFitJob objects.

timeseries_preprocessing()

Set up time stepping for the fit.

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

check_multistart_initial_guesses()

Check that initial guesses can be sampled for all data fits.

property data_fits

Get the dictionary of DataFit objects.

Returns

dict

Dictionary mapping independent variable values to DataFit objects

run(parameter_values)

Run the optimization for each independent variable value.

Parameters

parameter_valuesdict

Dictionary of parameter values to use for the fit

Returns

iwp.Result

Results object containing fitted parameters and optimization results

class ionworkspipeline.data_fits.CostLogger(plot_every=None, print_every=None)

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

Parameters

plot_everyfloat, optional

The number of seconds 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.

print_everyfloat, optional

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

argmin_costs()

Get the index of the job with minimum cost.

Returns

int

Index of minimum cost

argsort_costs()

Sort the jobs in ascending order of cost.

Returns

numpy.ndarray

Indices that would sort the costs

property children

List of child CostLoggerJob objects.

clear_axes()

Clear the plot axes.

property cost

List of logged costs.

property fig_axes

Get or create figure and axes objects.

Returns

tuple

Figure and axes objects, and whether they existed

finish()

Finish logging.

property finished

Whether logging is finished.

get_log()

Get the logged data.

Returns

dict

Dictionary of logged values

property is_parent

Whether this is a parent logger.

property iteration

List of iteration numbers.

log(logs=None, job_id=None)

Log cost and parameter values.

Parameters

logsdict, optional

Dictionary of values to log

job_idint, optional

ID of the job being logged

property multiprocessing

Whether multiprocessing is enabled.

property num_jobs

Number of jobs being logged.

property parent

Parent CostLogger object.

plot()

Plot the cost and parameters.

Returns

tuple

Figure and axes objects

property plot_every

Seconds between plot updates.

property plot_flag

Plot update flag.

plot_refresh(force_plot=False)

Update the plot of cost and parameters.

Parameters

force_plotbool, optional

If True, force a plot update regardless of timing

Returns

tuple

Figure and axes objects

property plot_variables

List of variables to plot.

property print_every

Seconds between print updates.

reset()

Reset all logging data and counters.

set_multiprocessing(multiprocessing)

Set whether multiprocessing is being used.

Parameters

multiprocessingbool

Whether multiprocessing is enabled

set_parameters(parameters)

Set the parameters to be logged.

Parameters

parameterslist

List of Parameter objects

property show_plot_iterative

Whether to show plots during optimization.

property show_print_iterative

Whether to print updates during optimization.

spawn_children(num_jobs)

Create child CostLoggerJob objects.

Parameters

num_jobsint

Number of child loggers to create

Returns

list

List of CostLoggerJob objects

start()

Start logging.

property timer

Timer object.