Skip to content

fit.cli

Running fits and their reports from the command line.

A fit problem is defined by the fit experiments which enter it and the parameters which are adjusted, see FitDefinition. Everything else — creating the optimization problems for a strategy, running the optimizations and reporting them — is the same for every problem and lives here, so that a model only has to define its fits:

FIT_DEFINITIONS = {
    "PK": FitDefinition(
        fit_experiments=f_fitexp_pk,
        parameters=parameters_pk,
        base_path=MODEL_PATH,
        data_path=DATA_PATH,
    ),
}

if __name__ == "__main__":
    fit_cli(FIT_DEFINITIONS, prog="fit_mymodel")

fit_cli runs a fit and reports it, report_cli reports parameters which were stored earlier without optimizing again.

FitDefinition dataclass

FitDefinition(
    fit_experiments,
    parameters,
    base_path,
    data_path,
    settings=FitSettings(),
)

Definition of a fit problem.

This is what a model provides: which fit experiments enter the fit, which parameters are adjusted and where the experiments and their data are.

Attributes:

Name Type Description
fit_experiments Callable[[], dict[str, list[FitExperiment]]]

callable which creates the fit experiments by experiment id, e.g., a function of sbmlsim.fit.helpers. It is called when the fit runs, instantiating the experiments loads the models and the data.

parameters list[FitParameter]

parameters which are adjusted in the fit.

base_path Path

base path of the simulation experiments.

data_path Path

path of the datasets of the simulation experiments.

settings FitSettings

settings of the fit.

experiments

experiments(study_ids=None)

Create the fit experiments of the definition.

Parameters:

Name Type Description Default
study_ids Sequence[str] | None

experiments to use, all experiments by default.

None

Returns:

Type Description
list[FitExperiment]

The fit experiments.

Raises:

Type Description
KeyError

if an experiment id is not part of the definition.

problem

problem(opid, fit_experiments=None)

Create the optimization problem of the definition.

Parameters:

Name Type Description Default
opid str

id of the optimization problem.

required
fit_experiments list[FitExperiment] | None

experiments of the problem, all experiments of the definition by default.

None

Returns:

Type Description
OptimizationProblem

The uninitialized optimization problem.

FitRun dataclass

FitRun(problem, result)

A finished fit: the problem which was optimized and its result.

report

report(output_dir, name=None, **kwargs)

Create the report of the fit.

Parameters:

Name Type Description Default
output_dir Path

base directory of the reports.

required
name str | None

name of the report, the id of the problem by default.

None
kwargs Any

additional arguments of FitReport.from_optimization_result.

{}

Returns:

Type Description
Path

Path of the directory the report was written to.

run_fit

run_fit(
    definition,
    opid="all",
    strategy=ALL,
    algorithm=LEAST_SQUARE,
    size=4,
    n_cores=1,
    seed=None,
    study_ids=None,
    **kwargs,
)

Run the fit of a definition.

Parameters:

Name Type Description Default
definition FitDefinition

definition of the fit problem.

required
opid str

id of the optimization, the key of the result for ALL.

'all'
strategy OptimizationStrategy

fit all experiments together or every experiment on its own.

ALL
algorithm OptimizationAlgorithmType

optimization algorithm.

LEAST_SQUARE
size int

number of optimization runs per problem.

4
n_cores int

number of workers.

1
seed int | None

seed of the optimizations.

None
study_ids Sequence[str] | None

experiments to fit, all experiments by default.

None
kwargs Any

additional arguments of the optimizer, they replace the defaults of ALGORITHM_KWARGS.

{}

Returns:

Type Description
dict[str, FitRun]

The finished fits by optimization id.

load_parameter_sets

load_parameter_sets(paths)

Load the parameter sets of the given JSON files.

The sets of all files are combined, a set which occurs in more than one file is prefixed with the name of its directory to keep the ids unique.

Parameters:

Name Type Description Default
paths Sequence[Path]

JSON files written by ParameterSets.to_json.

required

Returns:

Type Description
ParameterSets

All parameter sets of the files.

fit_cli

fit_cli(
    definitions,
    prog="fit",
    description="Parameter fitting.",
    args=None,
)

Run a fit of one of the definitions from the command line and report it.

Parameters:

Name Type Description Default
definitions dict[str, FitDefinition]

fit problems by name, the name is the --subset argument.

required
prog str

name of the program in the help.

'fit'
description str

description of the program in the help.

'Parameter fitting.'
args Sequence[str] | None

command line arguments, sys.argv by default.

None

Returns:

Type Description
dict[str, FitRun]

The finished fits by optimization id.

Raises:

Type Description
ValueError

if no definitions are given.

report_cli

report_cli(
    definitions,
    prog="report",
    description="Report of a fit for stored parameters.",
    args=None,
)

Report stored parameters from the command line, without optimizing.

The parameters come from the parameters.json a fit wrote. Several files are combined into a single report, which compares their parameter sets.

Parameters:

Name Type Description Default
definitions dict[str, FitDefinition]

fit problems by name, the name is the --subset argument.

required
prog str

name of the program in the help.

'report'
description str

description of the program in the help.

'Report of a fit for stored parameters.'
args Sequence[str] | None

command line arguments, sys.argv by default.

None

Returns:

Type Description
Path

Path of the directory the report was written to.

Raises:

Type Description
ValueError

if no definitions are given.