fit.optimization¶
Optimization of parameter fitting problem.
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
¶
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.
:param opid: id for optimization problem :param mapping_collections: :param fit_parameters:
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. |
parameter_mapping_initialized
property
¶
Binding of the parameters to the simulations, created in initialize.
Raises:
| Type | Description |
|---|---|
ValueError
|
if the problem was not initialized. |
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. |
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: a fit and the report of the fit use the same problem, and resolving the data twice repeats the work and every message about the data.
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 |
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. |
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
|
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, |
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. |
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 |