Skip to content

fit.petab_v2.export

Write an sbmlsim optimization problem as a PEtab v2 problem.

The export walks an initialized OptimizationProblem, i.e., a problem whose fit mappings are resolved, and builds the tables of PEtab v2 from it:

  • every model of the fit is a model of the problem, written as the model the problem was defined with when the fit simulates a derived one (compiled networks, see sbmlsim.model.provenance),
  • the fit mappings which share a model and a simulation are one experiment, its periods are the start of the Simulation and the times of its changes, and their changes are the conditions,
  • every fit mapping is one observable, named after the mapping; mappings which observe one thing in several experiments are one observable, its reference data are the measurements of that observable,
  • every fit parameter is a parameter which is estimated.

What the tables do not hold goes into the sbmlsim extension of the problem, see sbmlsim.fit.petab_v2.extension, and what is lost is reported by sbmlsim.fit.petab_v2.gaps.

PetabExporter

PetabExporter(
    problem,
    settings=None,
    kinds=None,
    required_extension=True,
    parameter_set=None,
    unsupported="raise",
    role="fit",
)

Write an optimization problem as a PEtab v2 problem.

A problem with hybridizations is written with its networks as PEtab SciML, see sbmlsim.fit.petab_v2.sciml_export, which needs the extra sciml.

Initialize the export of a problem.

Parameters:

Name Type Description Default
problem OptimizationProblem

problem to export, it is initialized if it is not.

required
settings FitSettings | None

settings of the fit, required if the problem is not initialized.

None
kinds set[MappingKind] | None

kinds of fit mappings to write, everything the problem resolved by default, i.e. the training data, the validation data and the outliers, so that a round trip keeps the fit. The data the model does not describe is not part of the problem. Which of them a fit uses is in the extension: a tool which reads the problem without it would fit everything it finds.

None
required_extension bool

mark the sbmlsim extension as required, which it is: the settings it carries are the objective of the fit, so a tool which does not know it has to reject the problem instead of fitting the same data differently. False writes a problem which other tools fit with the objective PEtab defines, see sbmlsim.fit.petab_v2.extension.SbmlsimExtension.

True
parameter_set ParameterSet | None

values of the parameters of the fit to write, e.g. the result of a fit, which must have every parameter of the problem. They are the nominal values of the parameter table, the start values of the sbmlsim block and the values of the arrays of the networks, i.e. the exported problem starts from the set: a problem which is read has one start value per parameter and the arrays of a network cannot carry a second one, so a set which was written next to the start values would read back as the elements of the set and the parameters of the start. The problem is not changed. An element of a network which the problem freezes keeps its value.

None
unsupported UnsupportedMode

what the export does about a fit mapping which PEtab cannot express (sbmlsim.fit.petab_v2.gaps.MAPPING_GAPS, e.g. a PK parameter of a simulation or a task which is a scan): raise (the default) refuses the problem in check, skip writes the other mappings and names the left out ones by their gap in skipped, in the extension and in the README. The problem which is written is then the fit without them, i.e. not the fit which was defined. The other unsupported gaps are of the whole problem and raise in either mode.

'raise'
role ProblemRole

what the written problem is of the fit, see sbmlsim.fit.petab_v2.extension.SbmlsimExtension.role: fit (the default) writes the mappings of kinds, training the training data, the problem a tool fits, and validation the validation data with the parameters of the fit not estimated at their values, i.e. the start values or the parameter set, the problem a tool evaluates the fit on. A validation problem is written from the values of a fit, e.g. its best parameters.

'fit'

Raises:

Type Description
ValueError

if the problem is not initialized and no settings are given, if the parameter set has a value for an element of a network which is no parameter of the fit, for an unknown mode of unsupported or of role, for kinds with another role than fit or if no mapping is left to write.

KeyError

if the parameter set lacks a parameter of the problem.

check

check()

Check that the written mappings of the problem can be written.

Raises:

Type Description
ValueError

for a gap which has no representation in PEtab v2, i.e. an observable which is a python function, a timecourse over something else than the time, a value per simulation or values over a dimension of a scan (scalar-observable) or a task which is a scan (scan-task), naming the mappings of the last two; or for a selector without its own id, see _check_unnamed_versions.

to_problem

to_problem()

Build the PEtab v2 problem.

Returns:

Type Description
Problem

The problem with its models, conditions, experiments, observables,

Problem

measurements and parameters, and the sbmlsim extension.

Raises:

Type Description
ValueError

if the problem uses features PEtab v2 cannot express.

write

write(output_dir, origin=None, readme=True, related=None)

Build the problem and write it with its README, see to_petab.

Parameters:

Name Type Description Default
output_dir Path

directory the problem is written to, created if it does not exist.

required
origin str | None

where the values of the parameters come from, in words, for the README.

None
readme bool

write the README.md of the problem.

True
related Mapping[str, str] | None

the other problems of the fit by their role, as paths relative to the directory, which the README names.

None

Returns:

Type Description
Path

Path of the YAML file of the problem.

Raises:

Type Description
ValueError

if the problem uses features PEtab v2 cannot express or a file of the problem would be written outside the directory.

petab_id

petab_id(*parts)

Get a PEtab identifier from the parts of an sbmlsim key.

Parameters:

Name Type Description Default
parts str

parts of the identifier, joined with __.

()

Returns:

Type Description
str

An identifier of letters, digits and underscores which does not start

str

with a digit.

period_condition_id

period_condition_id(experiment_id, k)

Get the id of the condition of a period of an experiment.

Parameters:

Name Type Description Default
experiment_id str

id of the experiment.

required
k int

index of the period of the experiment.

required

Returns:

Type Description
str

The id, e.g. e1__tc0, which the arrays of the inputs of the

str

networks are keyed by for the first period.

to_petab

to_petab(
    problem,
    output_dir,
    settings=None,
    kinds=None,
    required_extension=True,
    parameter_set=None,
    unsupported="raise",
    origin=None,
    readme=True,
)

Write an optimization problem as a PEtab v2 problem.

Next to the YAML file and the tables the export writes a README.md, which describes the problem in words: the values it starts from, the data which is written and which is not, every gap of the problem with what it means for another tool, the measurements at the time of a change and how the problem is read back, see sbmlsim.fit.petab_v2.readme.

Parameters:

Name Type Description Default
problem OptimizationProblem

problem to write, it is initialized if it is not.

required
output_dir Path

directory the problem is written to, created if it does not exist.

required
settings FitSettings | None

settings of the fit, required if the problem is not initialized.

None
kinds set[MappingKind] | None

kinds of fit mappings to write, everything the problem resolved by default, i.e. the training data, the validation data and the outliers.

None
required_extension bool

mark the sbmlsim extension as required, which it is: the settings it carries are the objective of the fit. False writes a problem other tools fit with the objective of PEtab.

True
parameter_set ParameterSet | None

values of the parameters of the fit to write, e.g. the result of a fit: the exported problem starts from them, see PetabExporter. The start values of the problem are written if there is none.

None
unsupported UnsupportedMode

what the export does about a fit mapping PEtab v2 cannot express, e.g. a PK parameter of a simulation: raise (the default) refuses the problem, skip writes the problem without the mapping and names it in the extension and the README, see PetabExporter.

'raise'
origin str | None

where the values of the parameters come from, in words, for the README, e.g. the best parameters of the fit PK_....

None
readme bool

write the README.md of the problem.

True

Returns:

Type Description
Path

Path of the YAML file of the problem.

Raises:

Type Description
ValueError

if the problem uses features PEtab v2 cannot express (of a fit mapping only with unsupported="raise"), or if the parameter set has a value for an element of a network which is no parameter of the fit.

KeyError

if the parameter set lacks a parameter of the problem.

to_petab_problems

to_petab_problems(
    problem,
    output_dir,
    settings=None,
    required_extension=True,
    parameter_set=None,
    unsupported="raise",
    origin=None,
)

Write a fit as its training and its validation problem of PEtab v2.

The training problem (training/problem.yaml) has the training data and estimates the parameters of the fit, starting from their values; the validation problem (validation/problem.yaml) has the validation data and the parameters of the fit fixed at these values (estimate=false), i.e. it evaluates the fit on data it was not fitted to, see PetabExporter with role. The outliers are in neither, the excluded data is no part of the fit. A fit without validation data has the training problem only. Each problem has its README, which names the other one. PEtab v2 has no collection of problems, so they are two problems with their own files.

Parameters:

Name Type Description Default
problem OptimizationProblem

problem to write, it is initialized if it is not.

required
output_dir Path

directory of the problems, created if it does not exist.

required
settings FitSettings | None

settings of the fit, required if the problem is not initialized.

None
required_extension bool

mark the sbmlsim extension as required.

True
parameter_set ParameterSet | None

values of the parameters, e.g. the best parameters of a fit: the start of the training problem and the values of the validation problem; the start values of the problem without.

None
unsupported UnsupportedMode

what the export does about a fit mapping PEtab v2 cannot express, see PetabExporter.

'raise'
origin str | None

where the values of the parameters come from, in words, for the READMEs.

None

Returns:

Type Description
dict[str, PetabExporter]

The export of every problem which is written by its role, training

dict[str, PetabExporter]

and, with validation data, validation; the problem of a role is in

dict[str, PetabExporter]

output_dir / PROBLEM_DIRS[role] / YAML_FILE.

Raises:

Type Description
ValueError

if the problem uses features PEtab v2 cannot express, see to_petab, or has no training data.