Skip to content

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 x.

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,
)

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:

training_indices property

training_indices

Indices of the fit mappings which are fitted.

validation_indices property

validation_indices

Indices of the fit mappings which are only evaluated.

outlier_indices property

outlier_indices

Indices of the fit mappings whose data a fit dropped as unusable.

is_initialized property

is_initialized

Check if the problem was initialized, i.e., the data is resolved.

settings_initialized property

settings_initialized

Settings of the problem, set in initialize.

Raises:

Type Description
ValueError

if the problem was not initialized.

residual property

residual

Handling of the residuals, see FitSettings.

loss_function property

loss_function

Loss function of the fit, see FitSettings.

weighting_curves property

weighting_curves

Weighting of the curves, see FitSettings.

weighting_points property

weighting_points

Weighting of the data points, see FitSettings.

parameter_scale property

parameter_scale

Get the space the optimizer searches the parameters in.

runner_initialized property

runner_initialized

Runner of the problem, created in initialize.

parameter_mapping_initialized property

parameter_mapping_initialized

Binding of the parameters to the simulations, created in initialize.

Raises:

Type Description
ValueError

if the problem was not initialized.

indices

indices(kind=None)

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.

None

Returns:

Type Description
list[int]

Indices into the resolved data of the mappings.

mapping_counts

mapping_counts()

Get the number of resolved fit mappings per kind.

to_dict

to_dict()

Convert to dictionary.

to_json

to_json(path=None)

Store OptimizationResult as json.

Uses the to_dict method.

report

report(path=None, print_output=True)

Print and write report.

Can only be called after initialization.

initialize

initialize(settings, force=False)

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 FitSettings.

to_scale

to_scale(x)

Transform parameters of the model into the space of the optimizer.

from_scale

from_scale(x)

Transform parameters of the optimizer into the units of the model.

parameter_set_model

parameter_set_model(sid='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.

set_simulator

set_simulator(simulator)

Set the simulator on the runner and the experiments.

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. A run which is out of time keeps the parameters it reached.

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 RuntimeErrorOptimizeResult with its message, the

tuple[list[OptimizeResult], list[list[float]]]

other runs are unaffected.

start_values

start_values(
    size,
    algorithm=LEAST_SQUARE,
    sampling=UNIFORM,
    seed=None,
)

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 None.

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, None for the global optimizer.

run_seeds staticmethod

run_seeds(size, algorithm=LEAST_SQUARE, seed=None)

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 for runs which are not reproducible.

None

Returns:

Type Description
list[int | None]

One seed per run, None if the runs do not need one.

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 for the global optimizer.

None
algorithm OptimizationAlgorithmType

optimization algorithm.

LEAST_SQUARE
timeout float | None

seconds the optimization may run, no limit if None.

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 if it does not need one.

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.

cost_least_square

cost_least_square(xlog)

Get least square costs for parameters.

residuals

residuals(xlog, complete_data=False)

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_loss_function(residuals, 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

minimal_result(opt_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 RESULT_KEYS which are present.