fit.optimization¶
Optimization of parameter fitting problem.
ObservationKind
¶
Bases: StrEnum
What the observable of a fit mapping is, see OptimizationProblem.initialize.
The kind is decided from the dimensions of the observable after its sel:
a timecourse over the time (or another selection), a value per
simulation of a plain simulation or of a scan with every dimension
selected, or values over the one dimension of a scan which is left, which
are matched to the reference data by the values of the dimension.
GroupResult
dataclass
¶
The simulation of a group of fit mappings which keeps its output.
The output has the states before and after a change after the start at
its time, see sbmlsim.simulator.executor, also at a change between the
output times of explicit times, which the plan of a group with a
timecourse gets (OptimizationProblem._compile_plans).
Attributes:
| Name | Type | Description |
|---|---|---|
time |
ndarray
|
the time points of every simulated point of the group, |
outputs |
dict[str, ndarray]
|
the observables and selections the mappings of the group read,
a timecourse |
changes |
ndarray
|
the times of the changes after the start of the first point
in the time of the output, see |
tolerance |
float
|
the resolution of the time of the simulation, time points
of the output closer than it are a jump, see
|
DataTimesResult
dataclass
¶
The simulation of a group of fit mappings which outputs the times of its data.
Attributes:
| Name | Type | Description |
|---|---|---|
output |
TimecourseResult
|
a row per output time of the data and per time of a change
within them, two rows at a change after the start: the state
before the change and the state after it; the data takes one of
them, see |
MappingEvaluation
dataclass
¶
The simulation of a fit mapping at its reference data.
Attributes:
| Name | Type | Description |
|---|---|---|
prediction |
ndarray
|
the observable at every data point. |
selections |
dict[str, ndarray]
|
the selections the noise model of the mapping reads, at
every data point, see |
FitTimeout
¶
Bases: Exception
A single optimization ran longer than its budget.
The optimizers of scipy cannot be interrupted, so the objective raises this
once the budget is spent, which ends the optimization. The runs which
finished are kept, see OptimizationProblem.optimize.
RuntimeErrorOptimizeResult
¶
RuntimeErrorOptimizeResult(
x=None,
x0=None,
cost=inf,
message="RuntimeError in ODE integration.",
duration=-1.0,
)
Bases: OptimizeResult
Result of an optimization which did not finish.
An optimization fails with an error of the integrator, with a timeout or
with any other error of the objective. This is a
scipy.optimize.OptimizeResult, i.e., a dictionary with attribute access,
so that a failed run is stored, serialized and reported like a successful
one and a fit keeps the runs which worked.
Initialize the result of an optimization which did not finish.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray | None
|
parameters the optimization reached. |
None
|
x0
|
ndarray | None
|
parameters it started from. |
None
|
cost
|
float
|
cost of |
inf
|
message
|
str
|
what went wrong. |
'RuntimeError in ODE integration.'
|
duration
|
float
|
seconds the optimization ran. |
-1.0
|
OptimizationProblem
¶
OptimizationProblem(
opid,
mapping_collections,
fit_parameters,
base_path=None,
data_path=None,
hybridizations=None,
)
Bases: ObjectJSONEncoder
Parameter optimization problem.
Optimization problem.
The problem must be pickable for parallelization ! So initialize must be run to create the non-pickable instances.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
opid
|
str
|
id for optimization problem. |
required |
mapping_collections
|
list[FitMappingCollection]
|
the fit mappings of the problem. |
required |
fit_parameters
|
list[FitParameter]
|
the parameters of the fit. |
required |
base_path
|
Path | None
|
directory the models of the experiments are relative to. |
None
|
data_path
|
Path | Sequence[Path] | None
|
directory or directories of the data of the experiments. |
None
|
hybridizations
|
Sequence[DerivedChanges] | None
|
the derived changes of the problem, i.e. changes
of a simulation which are calculated from the parameters of
the fit, see |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
if the problem has no parameters or two parameters of one id. |
TypeError
|
if a hybridization does not provide the derived changes. |
validation_indices
property
¶
Indices of the fit mappings which are only evaluated.
outlier_indices
property
¶
Indices of the fit mappings whose data a fit dropped as unusable.
is_initialized
property
¶
Check if the problem was initialized, i.e., the data is resolved.
settings_initialized
property
¶
Settings of the problem, set in initialize.
Raises:
| Type | Description |
|---|---|
ValueError
|
if the problem was not initialized. |
scales_initialized
property
¶
Get the space the optimizer searches every parameter in.
The scale of a parameter is its FitParameter.scale and the
parameter_scale of the settings for a parameter without one.
Raises:
| Type | Description |
|---|---|
ValueError
|
if the problem was not initialized. |
parameter_units
property
¶
Get the unit of every parameter in the units of pint, by parameter id.
A unit id of the model of the target of a parameter is replaced by its
expression, see ParameterMapping.parameter_units, so the units are
the ones of the values of the parameter sets, the Fisher information
and the designs of sbmlsim.simulation.sampling, which have no model.
None is the unit of the model.
Raises:
| Type | Description |
|---|---|
ValueError
|
if the problem was not initialized. |
parameter_mapping_initialized
property
¶
Binding of the parameters to the simulations, created in initialize.
Raises:
| Type | Description |
|---|---|
ValueError
|
if the problem was not initialized. |
files
¶
Get the files a fit of the problem reads, the models and the data.
The models are the SBML files of the experiments, the data are the files
below data_path without python code and caches (data_files). An
uninitialized problem is initialized first, with the settings of the
problem or the default settings.
Returns:
| Type | Description |
|---|---|
list[Path]
|
The sorted paths of the files. |
indices
¶
Get the indices of the fit mappings of a kind.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kind
|
MappingKind | None
|
kind of the mappings, all mappings if |
None
|
Returns:
| Type | Description |
|---|---|
list[int]
|
Indices into the resolved data of the mappings. |
times_before_changes
¶
Get the times of the data of a mapping compared before a change.
A data point of a timecourse over the time whose time is the time of
a change after the start of its simulation, up to the resolution of
the time (sbmlsim.simulator.plan.time_tolerance), is compared with
the state before the change, except the first point of the curve,
see FitSettings.data_at_change.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k
|
int
|
index of the fit mapping. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
The times of the reference data which are compared with the state |
ndarray
|
before a change, in the time of the data; empty for a mapping |
ndarray
|
which is no timecourse over the time and with |
ndarray
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
if the problem is not initialized. |
report
¶
Print and write report.
Can only be called after initialization.
initialize
¶
Initialize the optimization problem for the given settings.
Resolves the data of the fit mappings, converts it to the units of the model, calculates the weights and attaches a simulator. The problem is only initialized once for a given set of settings. New runtime state is committed after initialization succeeds. Reports copy the definition and initialize their own runtime, preserving the original fit session.
Every fit mapping is one of three kinds, see ObservationKind,
decided from the dimensions of its observable after its sel
(observation_kinds, observation_dims): a timecourse, whose
reference x are times; a value per simulation, whose reference has
no x (x_references is NaN) and every row of which compares with
the one simulated value; or values over one dimension of a scan,
whose reference x are values of the target of the dimension, which
the values of the dimension must cover. The groups, the plans of the
points of a scan and the observables are built here, also in every
worker of a parallel fit, which initializes the pickled definition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
FitSettings
|
settings of the fit, they decide how the residuals and the weights are calculated. |
required |
force
|
bool
|
initialize again even if the settings did not change. |
False
|
Raises:
| Type | Description |
|---|---|
TypeError
|
if the settings are not a |
ValueError
|
if a mapping is none of the kinds (see
|
to_scale
¶
Transform parameters of the model into the space of the optimizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Any
|
one value per parameter, in the units of the model. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
The values in the space the optimizer searches, every parameter |
ndarray
|
with its own scale. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if |
from_scale
¶
Transform parameters of the optimizer into the units of the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Any
|
one value per parameter, in the space the optimizer searches. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
The values in the units of the model. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if |
parameter_set_model
¶
Get the initial values of the fitted parameters in the model.
The set is the reference a fitted set is compared against in a report.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sid
|
str
|
identifier of the set. |
'model'
|
Returns:
| Type | Description |
|---|---|
ParameterSet
|
Parameter set of the values the models start from. |
bind_parameter_set
¶
Convert a set to fit units, resolving declared model unit aliases.
optimize
¶
optimize(
size=5,
algorithm=LEAST_SQUARE,
sampling=UNIFORM,
seed=None,
timeout=None,
on_run_finished=None,
**kwargs,
)
Run parameter optimization.
The problem must be initialized, i.e., the settings of the fit are the settings it was initialized with.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
int
|
number of optimizations, every one starts from its own sample. |
5
|
algorithm
|
OptimizationAlgorithmType
|
optimization algorithm. |
LEAST_SQUARE
|
sampling
|
SamplingType
|
sampling of the start values of the local optimizer. |
UNIFORM
|
seed
|
int | None
|
seed of the sampling. |
None
|
timeout
|
float | None
|
seconds a single optimization may run, no limit if |
None
|
on_run_finished
|
Callable[[int, OptimizeResult, list[float]], None] | None
|
called with the index, the fit and the trajectory of every finished optimization, i.e., to report the progress of a fit and to store the runs while it runs. |
None
|
**kwargs
|
Any
|
additional arguments of the optimizer. |
{}
|
Returns:
| Type | Description |
|---|---|
list[OptimizeResult]
|
The fits and the trajectories of the optimizations. A run which |
list[list[float]]
|
failed is a |
tuple[list[OptimizeResult], list[list[float]]]
|
other runs are unaffected. |
start_values
¶
Create the start values of the optimization runs.
The start values are created for all runs at once, so that they only depend on the seed and the number of runs and not on how the runs are distributed over the workers of a parallel fit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
int
|
number of optimizations. |
required |
algorithm
|
OptimizationAlgorithmType
|
optimization algorithm. The global optimizer draws its
own samples, its runs start from |
LEAST_SQUARE
|
sampling
|
SamplingType
|
sampling of the start values of the local optimizer. |
UNIFORM
|
seed
|
int | None
|
seed of the sampling. |
None
|
Returns:
| Type | Description |
|---|---|
list[ndarray | None]
|
One start vector per run, |
run_seeds
staticmethod
¶
Create the seed of every optimization run.
The global optimizer draws its own population, so every run needs its own seed: with one seed for all runs they all return the same result. The local optimizer is deterministic, its runs differ in the start values and do not need a seed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
int
|
number of optimizations. |
required |
algorithm
|
OptimizationAlgorithmType
|
optimization algorithm. |
LEAST_SQUARE
|
seed
|
int | None
|
seed of the fit, |
None
|
Returns:
| Type | Description |
|---|---|
list[int | None]
|
One seed per run, |
check_optimizer_arguments
staticmethod
¶
Check that the optimizer of the algorithm accepts the arguments.
The additional arguments of a fit are handed to scipy.optimize.least_squares
or scipy.optimize.differential_evolution in every run. An argument they
do not accept would fail every run, so it is found before the first run.
What the problem passes itself, i.e., the objective, the start values and
the bounds, is not an additional argument.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
algorithm
|
OptimizationAlgorithmType
|
optimization algorithm. |
required |
arguments
|
Iterable[str] | Mapping[str, Any]
|
names of the additional arguments of the optimizer. |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
if an argument is not an argument of the optimizer. |
optimize_run
¶
optimize_run(
x0=None,
algorithm=LEAST_SQUARE,
timeout=None,
run=0,
size=1,
run_seed=None,
**kwargs,
)
Run a single optimization, which never raises.
This is one repeat of a fit, i.e., what the serial runner loops over and
what a worker of a parallel fit executes. An optimization which fails is
a RuntimeErrorOptimizeResult with its message, so that the repeat is
stored and reported like a successful one and the other repeats are
unaffected.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x0
|
ndarray | None
|
start values of the run, |
None
|
algorithm
|
OptimizationAlgorithmType
|
optimization algorithm. |
LEAST_SQUARE
|
timeout
|
float | None
|
seconds the optimization may run, no limit if |
None
|
run
|
int
|
index of the run, for the log messages. |
0
|
size
|
int
|
number of runs, for the log messages. |
1
|
run_seed
|
int | None
|
seed of the run, |
None
|
**kwargs
|
Any
|
additional arguments of the optimizer. |
{}
|
Returns:
| Type | Description |
|---|---|
tuple[OptimizeResult, list[float]]
|
The fit and the cost of every step of the optimization. |
evaluated_plan
¶
Get the plan a group is simulated with for the given parameters.
The values of the parameters are applied first; for a group of a scan the values of the point follow, so a dimension wins over a fitted value of its target; the derived changes come last and read the values of the point, e.g. a covariate of a population dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k_group
|
int
|
index of the simulation group. |
required |
x
|
ndarray
|
parameter values in the units of the parameters. |
required |
quantities
|
Sequence[Quantity] | None
|
the quantity of every parameter, built from |
None
|
point
|
int
|
the point of a group of a scan, in the order of its points; a group of a simulation has one. |
0
|
Returns:
| Type | Description |
|---|---|
Plan
|
The plan of the group with the values of the parameters, of the |
Plan
|
point and the derived changes in the units of the model. |
predictions
¶
Get the simulation of fit mappings at their reference data.
See evaluations, whose predictions these are; a prediction without a
value is NaN.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
values of the parameters in the units of the model. |
required |
indices
|
Sequence[int] | None
|
indices of the fit mappings, the training data by default. |
None
|
Returns:
| Type | Description |
|---|---|
dict[int, ndarray]
|
The prediction at the reference data of every mapping, by the |
dict[int, ndarray]
|
index of the mapping. |
evaluations
¶
Get the simulation of fit mappings at their reference data.
The predictions are the values of the observable as they are
simulated, i.e. the baseline of a curve is not subtracted, whatever
the residual of the settings is. A prediction without a value, e.g. a
PK parameter of a point without a terminal phase, stays NaN here, so
that a likelihood sees it, while residuals gives the failure residual
for it, which keeps an optimizer going.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
values of the parameters in the units of the model, i.e. on the linear scale, in the order of the parameters of the problem. |
required |
indices
|
Sequence[int] | None
|
indices of the fit mappings, the training data by default. |
None
|
Returns:
| Type | Description |
|---|---|
dict[int, MappingEvaluation]
|
The prediction and the selections of the noise model at the |
dict[int, MappingEvaluation]
|
reference data of every mapping, by the index of the mapping. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the problem is not initialized, if no simulator is
set, if |
residuals
¶
Calculate residuals for given parameter vector.
Optimization is performed in logarithmic parameter space to account for xtol in largely varying parameters. see https://github.com/scipy/scipy/issues/7632
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xlog
|
ndarray
|
logarithmic parameter vector. |
required |
complete_data
|
bool
|
return the simulations, residuals and costs of every fit mapping instead of the vector of weighted residuals. |
False
|
Returns:
| Type | Description |
|---|---|
ndarray | dict[str, list[Any]]
|
Vector of weighted residuals, or the complete data of the mappings. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if no simulator is set or the residuals are not supported. |
apply_loss_function
¶
Apply the loss function to the residuals.
The cost of the optimization is 0.5 * sum(rho(residuals**2)) with rho the
loss function. The optimizers minimize 0.5 * sum(f**2), so the residuals are
transformed to sign(r) * sqrt(rho(r**2)), which gives exactly this cost and
keeps the sign of the residual. The loss functions are the loss functions of
scipy.optimize.least_squares.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
residuals
|
ndarray
|
weighted residuals of a fit mapping. |
required |
loss_function
|
LossFunctionType
|
loss function to apply. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Transformed residuals. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the loss function is not supported. |
minimal_result
¶
Reduce the result of an optimization to what is reported.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
opt_result
|
OptimizeResult
|
result of one of the optimizers of scipy. |
required |
Returns:
| Type | Description |
|---|---|
OptimizeResult
|
A result with the keys of |