pypesto.history
History
Objetive function call history. The history tracks and stores function evaluations performed by e.g. the optimizer and other routines, allowing to e.g. recover results from failed runs, fill in further details, and evaluate performance.
- class pypesto.history.CountHistory[source]
Bases:
CountHistoryBase
History that can only count, other functions cannot be invoked.
- get_fval_trace(ix=None, trim=False)[source]
Return function values.
Takes as parameter an index or indices and returns corresponding trace values. If only a single value is requested, the list is flattened.
- get_grad_trace(ix=None, trim=False)[source]
Return gradients.
Takes as parameter an index or indices and returns corresponding trace values. If only a single value is requested, the list is flattened.
- get_hess_trace(ix=None, trim=False)[source]
Return hessians.
Takes as parameter an index or indices and returns corresponding trace values. If only a single value is requested, the list is flattened.
- get_res_trace(ix=None, trim=False)[source]
Residuals.
Takes as parameter an index or indices and returns corresponding trace values. If only a single value is requested, the list is flattened.
- get_sres_trace(ix=None, trim=False)[source]
Residual sensitivities.
Takes as parameter an index or indices and returns corresponding trace values. If only a single value is requested, the list is flattened.
- get_time_trace(ix=None, trim=False)[source]
Cumulative execution times [s].
Takes as parameter an index or indices and returns corresponding trace values. If only a single value is requested, the list is flattened.
- class pypesto.history.CountHistoryBase[source]
Bases:
HistoryBase
Abstract class tracking counts of function evaluations.
Needs a separate implementation of trace.
- __init__(options=None)[source]
- Parameters:
options (HistoryOptions | dict) –
- finalize(message=None, exitflag=None)[source]
Finalize history. Called after a run. Default: Do nothing.
- update(x, sensi_orders, mode, result)[source]
Update history after a function evaluation.
- Parameters:
x (
ndarray
) – The parameter vector.sensi_orders (
tuple
[int
,...
]) – The sensitivity orders computed.mode (
Literal
['mode_fun'
,'mode_res'
]) – The objective function mode computed (function value or residuals).result (
dict
[str
,Union
[float
,ndarray
]]) – The objective function values for parameters x, sensitivities sensi_orders and mode mode.
- Return type:
- class pypesto.history.CsvAmiciHistory[source]
Bases:
CsvHistory
Stores history extended by AMICI-specific time traces in a CSV file.
Stores AMICI-specific traces of total simulation time, pre-equilibration time and post-equilibration time.
- Parameters:
- get_cpu_time_total_trace(ix=None, trim=False)[source]
Cumulative simulation CPU time [s].
Takes as parameter an index or indices and returns corresponding trace values. If only a single value is requested, the list is flattened.
- get_posteq_timeB_trace(ix=None, trim=False)[source]
Cumulative post-equilibration time of the backward problem [s].
Takes as parameter an index or indices and returns corresponding trace values. If only a single value is requested, the list is flattened.
- get_posteq_time_trace(ix=None, trim=False)[source]
Cumulative post-equilibration time [s].
Takes as parameter an index or indices and returns corresponding trace values. If only a single value is requested, the list is flattened.
- get_preeq_timeB_trace(ix=None, trim=False)[source]
Cumulative pre-equilibration time of the backward problem [s].
Takes as parameter an index or indices and returns corresponding trace values. If only a single value is requested, the list is flattened.
- class pypesto.history.CsvHistory[source]
Bases:
CountHistoryBase
Stores a representation of the history in a CSV file.
- Parameters:
- exception pypesto.history.CsvHistoryTemplateError[source]
Bases:
ValueError
Error raised when no template is given for CSV history.
- class pypesto.history.Hdf5AmiciHistory[source]
Bases:
Hdf5History
Stores history extended by AMICI-specific time traces in an HDF5 file.
Stores AMICI-specific traces of total simulation time, pre-equilibration time and post-equilibration time.
- Parameters:
- __init__(id, file, options=None)[source]
- Parameters:
id (str) –
options (HistoryOptions | dict | None) –
- get_cpu_time_total_trace(ix=None, trim=False)[source]
Cumulative simulation CPU time [s].
Takes as parameter an index or indices and returns corresponding trace values. If only a single value is requested, the list is flattened.
- get_posteq_timeB_trace(ix=None, trim=False)[source]
Cumulative post-equilibration time of the backward problem [s].
Takes as parameter an index or indices and returns corresponding trace values. If only a single value is requested, the list is flattened.
- get_posteq_time_trace(ix=None, trim=False)[source]
Cumulative post-equilibration time [s].
Takes as parameter an index or indices and returns corresponding trace values. If only a single value is requested, the list is flattened.
- get_preeq_timeB_trace(ix=None, trim=False)[source]
Cumulative pre-equilibration time of the backward problem, [s].
Takes as parameter an index or indices and returns corresponding trace values. If only a single value is requested, the list is flattened.
- class pypesto.history.Hdf5History[source]
Bases:
HistoryBase
Stores a representation of the history in an HDF5 file.
- Parameters:
- __init__(id, file, options=None)[source]
- Parameters:
id (str) –
options (HistoryOptions | dict | None) –
- finalize(message=None, exitflag=None)[source]
See
HistoryBase.finalize
.
- static from_history(other, file, id_, overwrite=False)[source]
Write some History to HDF5.
- Parameters:
- Return type:
- Returns:
The newly created
Hdf5History
.
- static load(id, file, options=None)[source]
Load the History object from memory.
- Return type:
- Parameters:
id (str) –
options (HistoryOptions | dict) –
- property n_fval: int
See
HistoryBase.n_fval()
.
- property n_grad: int
See
HistoryBase.n_grad()
.
- property n_hess: int
See
HistoryBase.n_hess()
.
- property n_res: int
See
HistoryBase.n_res()
.
- property n_sres: int
See
HistoryBase.n_sres()
.
- recover_options(file)[source]
Recover options when loading the hdf5 history from memory.
Done by testing which entries were recorded.
- class pypesto.history.HistoryBase[source]
Bases:
ABC
Abstract base class for histories.
- ALL_KEYS = ('x', 'fval', 'grad', 'hess', 'res', 'sres', 'time')
- RESULT_KEYS = ('fval', 'grad', 'hess', 'res', 'sres')
- __init__(options=None)[source]
- Parameters:
options (HistoryOptions | None) –
- finalize(message=None, exitflag=None)[source]
Finalize history. Called after a run. Default: Do nothing.
- get_chi2_trace(ix=None, trim=False)[source]
Chi2 values.
Takes as parameter an index or indices and returns corresponding trace values. If only a single value is requested, the list is flattened.
- abstract get_fval_trace(ix=None, trim=False)[source]
Return function values.
Takes as parameter an index or indices and returns corresponding trace values. If only a single value is requested, the list is flattened.
- abstract get_grad_trace(ix=None, trim=False)[source]
Return gradients.
Takes as parameter an index or indices and returns corresponding trace values. If only a single value is requested, the list is flattened.
- abstract get_hess_trace(ix=None, trim=False)[source]
Return hessians.
Takes as parameter an index or indices and returns corresponding trace values. If only a single value is requested, the list is flattened.
- abstract get_res_trace(ix=None, trim=False)[source]
Residuals.
Takes as parameter an index or indices and returns corresponding trace values. If only a single value is requested, the list is flattened.
- get_schi2_trace(ix=None, trim=False)[source]
Chi2 sensitivities.
Takes as parameter an index or indices and returns corresponding trace values. If only a single value is requested, the list is flattened.
- abstract get_sres_trace(ix=None, trim=False)[source]
Residual sensitivities.
Takes as parameter an index or indices and returns corresponding trace values. If only a single value is requested, the list is flattened.
- abstract get_time_trace(ix=None, trim=False)[source]
Cumulative execution times [s].
Takes as parameter an index or indices and returns corresponding trace values. If only a single value is requested, the list is flattened.
- abstract get_x_trace(ix=None, trim=False)[source]
Return parameters.
Takes as parameter an index or indices and returns corresponding trace values. If only a single value is requested, the list is flattened.
- abstract update(x, sensi_orders, mode, result)[source]
Update history after a function evaluation.
- Parameters:
x (
ndarray
) – The parameter vector.sensi_orders (
tuple
[int
,...
]) – The sensitivity orders computed.mode (
Literal
['mode_fun'
,'mode_res'
]) – The objective function mode computed (function value or residuals).result (
dict
[str
,Union
[float
,ndarray
]]) – The objective function values for parameters x, sensitivities sensi_orders and mode mode.
- Return type:
- class pypesto.history.HistoryOptions[source]
Bases:
dict
Options for what values to record.
In addition implements a factory pattern to generate history objects.
- Parameters:
trace_record (
bool
) – Flag indicating whether to record the trace of function calls. The trace_record_* flags only become effective if trace_record is True. Defaults toFalse
.trace_record_grad (
bool
) – Flag indicating whether to record the gradient in the trace. Defaults toTrue
.trace_record_hess (
bool
) – Flag indicating whether to record the Hessian in the trace. Defaults toTrue
.trace_record_res (
bool
) – Flag indicating whether to record the residual in the trace. Defaults toTrue
.trace_record_sres (
bool
) – Flag indicating whether to record the residual sensitivities in the trace. Defaults toTrue
.trace_save_iter (
int
) – After how many iterations to store the trace. Defaults to10
.storage_file (
Union
[str
,Path
,None
]) – File to save the history to. Can be any of None, a “{filename}.csv”, or a “{filename}.hdf5” file. Depending on the values, thecreate_history()
method creates the appropriate object. Occurrences of “{id}” in the file name are replaced by the id upon creation of a history, if applicable. Defaults toNone
.
- __init__(trace_record=False, trace_record_grad=True, trace_record_hess=True, trace_record_res=True, trace_record_sres=True, trace_save_iter=10, storage_file=None)[source]
- static assert_instance(maybe_options)[source]
Return a valid options object.
- Parameters:
maybe_options (
HistoryOptions
or dict) –- Return type:
- exception pypesto.history.HistoryTypeError[source]
Bases:
ValueError
Error raised when an unsupported history type is requested.
- class pypesto.history.MemoryHistory[source]
Bases:
CountHistoryBase
Class for optimization history stored in memory.
Tracks number of function evaluations and keeps an in-memory trace of function evaluations.
- Parameters:
options (
Union
[HistoryOptions
,dict
,None
]) – History options, seepypesto.history.HistoryOptions
. Defaults to None, which implies default options.
- __init__(options=None)[source]
- Parameters:
options (HistoryOptions | dict | None) –
- class pypesto.history.NoHistory[source]
Bases:
HistoryBase
Dummy history that does not do anything.
Can be used whenever a history object is needed, but no history is desired. Can be created, but not queried.
- get_fval_trace(ix=None, trim=False)[source]
Return function values.
Takes as parameter an index or indices and returns corresponding trace values. If only a single value is requested, the list is flattened.
- get_grad_trace(ix=None, trim=False)[source]
Return gradients.
Takes as parameter an index or indices and returns corresponding trace values. If only a single value is requested, the list is flattened.
- get_hess_trace(ix=None, trim=False)[source]
Return hessians.
Takes as parameter an index or indices and returns corresponding trace values. If only a single value is requested, the list is flattened.
- get_res_trace(ix=None, trim=False)[source]
Residuals.
Takes as parameter an index or indices and returns corresponding trace values. If only a single value is requested, the list is flattened.
- get_sres_trace(ix=None, trim=False)[source]
Residual sensitivities.
Takes as parameter an index or indices and returns corresponding trace values. If only a single value is requested, the list is flattened.
- get_time_trace(ix=None, trim=False)[source]
Cumulative execution times [s].
Takes as parameter an index or indices and returns corresponding trace values. If only a single value is requested, the list is flattened.
- get_x_trace(ix=None, trim=False)[source]
Return parameters.
Takes as parameter an index or indices and returns corresponding trace values. If only a single value is requested, the list is flattened.
- update(x, sensi_orders, mode, result)[source]
Update history after a function evaluation.
- Parameters:
x (
ndarray
) – The parameter vector.sensi_orders (
tuple
[int
,...
]) – The sensitivity orders computed.mode (
Literal
['mode_fun'
,'mode_res'
]) – The objective function mode computed (function value or residuals).result (
dict
[str
,Union
[float
,ndarray
]]) – The objective function values for parameters x, sensitivities sensi_orders and mode mode.
- Return type:
- class pypesto.history.OptimizerHistory[source]
Bases:
object
Optimizer objective call history.
Container around a History object, additionally keeping track of optimal values.
- fval0, fval_min
Initial and best function value found.
- x0, x_min
Initial and best parameters found.
- grad_min
gradient for best parameters
- hess_min
hessian (approximation) for best parameters
- res_min
residuals for best parameters
- sres_min
residual sensitivities for best parameters
- Parameters:
history (
HistoryBase
) – History object to attach to this container. This history object implements the storage of the actual history.x0 (
ndarray
) – Initial values for optimization.lb (
ndarray
) – Lower and upper bound. Used for checking validity of optimal points.ub (
ndarray
) – Lower and upper bound. Used for checking validity of optimal points.generate_from_history (
bool
) – If set to true, this function will try to fill attributes of this function based on the provided history. Defaults toFalse
.
- MIN_KEYS = ('x', 'fval', 'grad', 'hess', 'res', 'sres')
- __init__(history, x0, lb, ub, generate_from_history=False)[source]
- Parameters:
history (HistoryBase) –
x0 (ndarray) –
lb (ndarray) –
ub (ndarray) –
generate_from_history (bool) –
- Return type:
None
- pypesto.history.create_history(id, x_names, options)[source]
Create a
HistoryBase
object; Factory method.- Parameters:
id (
str
) – Identifier for the history.options (
HistoryOptions
) – History options.
- Return type:
- Returns:
A history object corresponding to the inputs.