Objective

class pypesto.objective.Objective(fun=None, grad=None, hess=None, hessp=None, res=None, sres=None, fun_accept_sensi_orders=False, res_accept_sensi_orders=False, options=None)

Bases: object

The objective class is a simple wrapper around the objective function, giving a standardized way of calling. Apart from that, it manages several things including fixing of parameters and history.

Parameters:
  • fun (callable, optional) –

    The objective function to be minimized. If it only computes the objective function value, it should be of the form

    fun(x) -> float

    where x is an 1-D array with shape (n,), and n is the parameter space dimension.

  • grad (callable, bool, optional) –

    Method for computing the gradient vector. If it is a callable, it should be of the form

    grad(x) -> array_like, shape (n,).

    If its value is True, then fun should return the gradient as a second output.

  • hess (callable, optional) –

    Method for computing the Hessian matrix. If it is a callable, it should be of the form

    hess(x) -> array, shape (n,n).

    If its value is True, then fun should return the gradient as a second, and the Hessian as a third output, and grad should be True as well.

  • hessp (callable, optional) –
    Method for computing the Hessian vector product, i.e.
    hessp(x, v) -> array_like, shape (n,)

    computes the product H*v of the Hessian of fun at x with v.

  • res ({callable, bool}, optional) –
    Method for computing residuals, i.e.
    res(x) -> array_like, shape(m,).
  • sres (callable, optional) –

    Method for computing residual sensitivities. If its is a callable, it should be of the form

    sres(x) -> array, shape (m,n).

    If its value is True, then res should return the residual sensitivities as a second output.

  • fun_accept_sensi_orders (bool, optional) – Flag indicating whether fun takes sensi_orders as an argument. Default: False.
  • res_accept_sensi_orders (bool, optional) – Flag indicating whether res takes sensi_orders as an argument. Default: False
  • options (pypesto.ObjectiveOptions, optional) – Options as specified in pypesto.ObjectiveOptions.
history

pypesto.ObjectiveHistory – For storing the call history. Initialized by the optimizer in reset_history().

x_names

list of str – Parameter names. The base Objective class provides None. None if no names provided, otherwise a list of str, length dim_full. Can be read by the problem.

preprocess

callable – Preprocess input values to __call__.

postprocess

callable – Postprocess output values from __call__.

sensitivity_orders

tuple – Temporary variable to store requested sensitivity orders

Notes

preprocess, postprocess are configured in update_from_problem() and can be reset using the reset() method.

__call__(x, sensi_orders: tuple = (0, ), mode='mode_fun')

Method to obtain arbitrary sensitivities. This is the central method which is always called, also by the get_* methods.

There are different ways in which an optimizer calls the objective function, and in how the objective function provides information (e.g. derivatives via separate functions or along with the function values). The different calling modes increase efficiency in space and time and make the objective flexible.

Parameters:
  • x (array_like) – The parameters for which to evaluate the objective function.
  • sensi_orders (tuple) – Specifies which sensitivities to compute, e.g. (0,1) -> fval, grad.
  • mode (str) – Whether to compute function values or residuals.
__init__(fun=None, grad=None, hess=None, hessp=None, res=None, sres=None, fun_accept_sensi_orders=False, res_accept_sensi_orders=False, options=None)

Initialize self. See help(type(self)) for accurate signature.

static as_ndarrays(result)

Convert all array_like objects to numpy arrays. This has the advantage of a uniform output datatype which offers various methods to assess the data.

check_grad(x, x_indices=None, eps=1e-05, verbosity=1, mode='mode_fun') → pandas.core.frame.DataFrame

Compare gradient evaluation: Firstly approximate via finite differences, and secondly use the objective gradient.

Parameters:
  • x (array_like) – The parameters for which to evaluate the gradient.
  • x_indices (array_like, optional) – List of index values for which to compute gradients. Default: all.
  • eps (float, optional) – Finite differences step size. Default: 1e-5.
  • verbosity (int) –
    Level of verbosity for function output
    0: no output 1: summary for all parameters 2: summary for individual parameters

    Default: 1.

  • mode (str) – Residual (MODE_RES) or objective function value (MODE_FUN, default) computation mode.
Returns:

result – gradient, finite difference approximations and error estimates.

Return type:

pd.DataFrame

finalize_history()

Finalize the history object.

get_fval(x)

Get the function value at x.

get_grad(x)

Get the gradient at x.

get_hess(x)

Get the Hessian at x.

get_res(x)

Get the residuals at x.

get_sres(x)

Get the residual sensitivities at x.

has_fun
has_grad
has_hess
has_hessp
has_res
has_sres
static output_to_dict(sensi_orders, mode, output_tuple)

Convert output tuple to dict.

static output_to_tuple(sensi_orders, mode, **kwargs)

Return values as requested by the caller, since usually only a subset is demanded. One output is returned as-is, more than one output are returned as a tuple in order (fval, grad, hess).

reset()

Completely reset the objective, i.e. undo the modifications in update_from_problem().

reset_history(index=None)

Reset the objective history and specify temporary saving options.

Parameters:index (As in ObjectiveHistory.index.) –
update_from_problem(dim_full, x_free_indices, x_fixed_indices, x_fixed_vals)

Handle fixed parameters. Later, the objective will be given parameter vectors x of dimension dim, which have to be filled up with fixed parameter values to form a vector of dimension dim_full >= dim. This vector is then used to compute function value and derivaties. The derivatives must later be reduced again to dimension dim.

This is so as to make the fixing of parameters transparent to the caller.

The methods preprocess, postprocess are overwritten for the above functionality, respectively.

Parameters:
  • dim_full (int) – Dimension of the full vector including fixed parameters.
  • x_free_indices (array_like of int) – Vector containing the indices (zero-based) of free parameters (complimentary to x_fixed_indices).
  • x_fixed_indices (array_like of int, optional) – Vector containing the indices (zero-based) of parameter components that are not to be optimized.
  • x_fixed_vals (array_like, optional) – Vector of the same length as x_fixed_indices, containing the values of the fixed parameters.
class pypesto.objective.ObjectiveOptions(trace_record=False, trace_record_grad=True, trace_record_hess=False, trace_record_res=False, trace_record_sres=False, trace_record_chi2=True, trace_record_schi2=True, trace_all=True, trace_file=None, trace_save_iter=10)

Bases: dict

Options for the objective that are used in optimization, profiles and sampling.

Parameters:
  • trace_record (bool, optional) – Flag indicating whether to record the trace of function calls. The trace_record_* flags only become effective if trace_record is True. Default: False.
  • trace_record_grad (bool, optional) – Flag indicating whether to record the gradient in the trace. Default: True.
  • trace_record_hess (bool, optional) – Flag indicating whether to record the Hessian in the trace. Default: False.
  • trace_record_res (bool, optional) – Flag indicating whether to record the residual in the trace. Default: False.
  • trace_record_sres (bool, optional.) – Flag indicating whether to record the residual sensitivities in the trace. Default: False.
  • trace_record_chi2 (bool, optional) – Flag indicating whether to record the chi2 in the trace. Default: True.
  • trace_record_schi2 (bool, optional) – Flag indicating whether to record the chi2 sensitivities in the trace. Default: True.
  • trace_all (bool, optional) – Flag indicating whether to record all (True, default) or only better (False) values.
  • trace_file (str or True, optional) – Either pass a string here denoting the file name for storing the trace, or True, in which case the default file name “tmp_trace_{index}.dat” is used. A contained substring {index} is converted to the multistart index. Default: None, i.e. no file is created.
  • index, optional (trace_save_iter.) – Trace is saved every tr_save_iter iterations. Default: 10.
__init__(trace_record=False, trace_record_grad=True, trace_record_hess=False, trace_record_res=False, trace_record_sres=False, trace_record_chi2=True, trace_record_schi2=True, trace_all=True, trace_file=None, trace_save_iter=10)

Initialize self. See help(type(self)) for accurate signature.

static assert_instance(maybe_options)

Returns a valid options object.

Parameters:maybe_options (ObjectiveOptions or dict) –
pypesto.objective.res_to_chi2(res)

We assume that the residuals res are related to an objective function value fval = chi2 via:

fval = 0.5 * sum(res**2)

which is the ‘Linear’ formulation in scipy.

pypesto.objective.sres_to_schi2(res, sres)

In line with the assumptions in res_to_chi2.

class pypesto.objective.AmiciObjective(amici_model, amici_solver, edata, max_sensi_order=None, preprocess_edata=True, options=None)

Bases: pypesto.objective.objective.Objective

This is a convenience class to compute an objective function from an AMICI model.

Parameters:
  • amici_model (amici.Model) – The amici model.
  • amici_solver (amici.Solver) – The solver to use for the numeric integration of the model.
  • edata – The experimental data.
  • max_sensi_order (int) – Maximum sensitivity order supported by the model.
__init__(amici_model, amici_solver, edata, max_sensi_order=None, preprocess_edata=True, options=None)

Initialize self. See help(type(self)) for accurate signature.

get_error_output(sensi_orders, mode)
postprocess_preequilibration(data, original_value_dict)
preprocess_edata(edata_vector)
preprocess_preequilibration(data)