Skip to content

fit.petab_v2.sciml_export

Write the neural networks of a hybrid problem as PEtab SciML.

The inverse of sbmlsim.fit.petab_v2.sciml: the hybridizations of an OptimizationProblem become the block sciml of the problem, the hybridization table, the rows of the mapping table and of the parameter table which name the networks, the NN YAML of every network and one array file per network with its arrays and the arrays of its inputs.

sbmlsim PEtab SciML
Network the NN YAML and the arrays of the array file
Hybridization.pattern pre_initialization of the network, and the observables
an input which is a formula a row of the hybridization table, evaluated along the trajectory
an input which is a parameter of its own the parameter is the petabEntityId of the input
an input with a formula per condition a change of the condition of every experiment
the formula of the other conditions a change of the conditions without a formula of their own, and the block sbmlsim
an input which is arrays array in the hybridization table, the arrays in the array file by condition
an output of RHS or PRE_INITIALIZATION a row of the hybridization table which assigns the target
an output of OBSERVABLE the symbol of the observable formula, mapped to the output
frozen and the bounds of the elements the most general rows of the parameter table
constants rows of the parameter table which are not estimated
a parameter of the model an input before the simulation or per condition uses a row of the parameter table which is not estimated

A formula which holds for every condition is a row of the hybridization table, which PEtab SciML evaluates along the trajectory; a condition sets a value once at the start of a period and uses only the parameters of the parameter table, so only the inputs with a formula per condition are changes of the conditions. The linter of petab counts a parameter of the parameter table which the model does not have, i.e. a constant of a hybridization or an estimated parameter which is no entity of the model, as used where the mapping table names it or a condition uses it, not where the hybridization table uses it: an input whose formula is such a parameter, which no other formula uses, is the parameter (its petabEntityId, as the cases of PEtab SciML write it), and such a parameter in a row of the hybridization table gets a row of the mapping table without a modelEntityId, an annotation. An input before the simulation and a condition use only parameters of the table: a parameter of the model which the fit does not estimate is written as a row which is not estimated, any other entity of the model is refused (gap sciml-input-formula). The formula of an input for the conditions without a formula of their own is written into the conditions of those experiments and into the block sbmlsim, from which the reader gives the input back as it was.

The module imports sbmlsim.sciml and with it petab_sciml, which is the extra sciml. sbmlsim.fit.petab_v2.export imports it only for a problem with hybridizations.

ArrayRow dataclass

ArrayRow(estimate, lower, upper)

The row of the parameter table an array of a network is described by.

key

key()

Get the row as a hashable key.

ExportedNetwork dataclass

ExportedNetwork(
    network,
    hybridizations,
    pre_initialization,
    arrays,
    parameter_rows=dict(),
)

A network of the problem with what is written for it.

Attributes:

Name Type Description
network Network

the network.

hybridizations list[Hybridization]

its hybridizations, one or two.

pre_initialization bool

whether it runs before the simulation.

arrays ArrayData

the array file of the network, with the arrays of its inputs.

parameter_rows dict[str, tuple[str, ArrayRow]]

the rows of the parameter table, petabEntityId -> modelEntityId and the row.

SciMLExporter

SciMLExporter(
    problem,
    hybridizations,
    simulation_ids,
    parameter_set=None,
)

Write the networks of an optimization problem as PEtab SciML.

The exporter is created by PetabExporter for a problem with hybridizations and fills the tables of the problem it builds. It validates the hybridizations against the fit parameters of the problem: every element of a network which is a fit parameter has the value of the network as its start value, the linear scale and the unit dimensionless, and the elements of one array are either all estimated with one pair of bounds or all frozen.

Attributes:

Name Type Description
problem

the problem which is exported.

simulation_ids

id of the simulation of a fit mapping -> ids of its PEtab experiments.

networks dict[str, ExportedNetwork]

the networks by their id.

element_ids set[str]

the ids of the elements of all networks, which are no rows of the parameter table and no part of the sbmlsim block.

constants dict[str, float]

the constants of the hybridizations which are rows of the parameter table, id -> value.

input_ids dict[str, str]

id of an input -> its petabEntityId, which is the parameter of an input whose formula is a parameter of its own and the id of the input otherwise.

input_formulas dict[str, dict[str, str]]

id of an input which is written as changes of the conditions -> id of the simulation -> the formula.

fallbacks dict[str, str]

id of an input -> its formula for the simulations without a formula of their own, in the math of PEtab, which the conditions repeat and the block sbmlsim carries.

row_inputs dict[str, str]

id of an input which is a row of the hybridization table -> its formula.

annotations dict[str, str]

id of a parameter which only rows of the hybridization table use -> the first input which uses it, a row of the mapping table without a modelEntityId.

fixed dict[str, float]

id of a parameter of the model which an input before the simulation or a condition uses and the fit does not estimate -> its value, a row of the parameter table which is not estimated.

hybridization_rows list[Hybridization]

the rows of the hybridization table.

Initialize the exporter.

Parameters:

Name Type Description Default
problem OptimizationProblem

the initialized problem.

required
hybridizations Sequence[Any]

the hybridizations of the problem.

required
simulation_ids Mapping[str, Sequence[str]]

id of the simulation of a fit mapping (the condition of the inputs) -> ids of its experiments of PEtab: a simulation whose fit mappings are in several collections is several experiments, which have the same inputs.

required
parameter_set ParameterSet | None

values of the parameters of the fit, e.g. the result of a fit. The arrays of the networks are written with the values of the elements in the set instead of the values the networks have, the elements which are not in the set keep theirs; a value for a frozen element is refused.

None

Raises:

Type Description
ValueError

if a hybridization is not a Hybridization of sbmlsim.sciml, if a network is before the simulation and in the model, if two hybridizations of a network differ in the network, the model or the inputs, if an element which is a fit parameter differs from the network, if the elements of an array are not described by one row, if two hybridizations give a constant different values, if an input has no formula for a simulation of the problem, or if the parameter set has a value for an element which is no parameter of the fit.

place_inputs

place_inputs(models, model_changes)

Decide where every input of a formula is written, see the module.

Fills input_ids, input_formulas, fallbacks, row_inputs, annotations, fixed and hybridization_rows; PetabExporter calls it once the models of the problem are added.

Parameters:

Name Type Description Default
models Mapping[str, Any]

id of a model of the problem -> its SbmlModel of petab, the model without the networks.

required
model_changes Mapping[str, Mapping[str, float]]

id of a model -> the changes the fit applies to it, id -> value.

required

Raises:

Type Description
ValueError

if an input has no formula for a simulation of the problem, if the model of a network is not a model of the problem, or if an input which a condition sets or which runs before the simulation uses an entity of the model which is no parameter of the parameter table (gap 'sciml-input-formula').

conditions_of

conditions_of(simulation)

Get the ids of the conditions of the first periods of a simulation.

Parameters:

Name Type Description Default
simulation str

id of the simulation.

required

Returns:

Type Description
list[str]

The id of the condition of the first period of every experiment of

list[str]

the simulation, which PetabExporter._periods gives it; none for

list[str]

a simulation none of whose fit mappings is written.

input_changes

input_changes(simulation)

Get the changes of the condition of a simulation which set inputs.

Parameters:

Name Type Description Default
simulation str

id of the simulation.

required

Returns:

Type Description
list[Change]

One change per input which is written as changes of the

list[Change]

conditions, see place_inputs.

needs_condition

needs_condition()

Check whether the first period of the experiments needs a condition.

An input which differs between the conditions needs the condition of every experiment: its formula is a change of it, its arrays are keyed by it in the array file.

mapping_rows

mapping_rows()

Get the rows of the mapping table which name parts of the networks.

Returns:

Type Description
list[Mapping]

A row per input, per used output and per row of the parameter

list[Mapping]

table of the networks. The petabEntityId of an input and of an

list[Mapping]

output which sets an entity is its id in sbmlsim, the one of an

list[Mapping]

input which is a constant of its own is the constant, the one of

list[Mapping]

an output an observable uses is its target, i.e. the symbol of

list[Mapping]

the observable formula.

parameter_rows

parameter_rows()

Get the rows of the parameter table of the networks and the constants.

Returns:

Type Description
list[Parameter]

The rows of the arrays of every network with the nominal value

list[Parameter]

array, and one row per constant of the hybridizations which is

list[Parameter]

not estimated.

config

config()

Get the block of the extension sciml of the problem.

write

write(output_dir)

Write the files of the networks into the directory of the problem.

Parameters:

Name Type Description Default
output_dir Path

the directory of the problem. The NN YAML and the array file of every network and the hybridization table are written next to problem.yaml.

required

network_yaml

network_yaml(sid)

Get the file of the NN YAML of a network.

network_arrays

network_arrays(sid)

Get the array file of a network, with its arrays and its inputs.

petab_math

petab_math(formula)

Get the math of PEtab of an L3 formula of SBML.

Parameters:

Name Type Description Default
formula str

the formula, e.g. alpha + (prey - 1.3).

required

Returns:

Type Description
str

The expression in the math of PEtab, in which log is the natural

str

logarithm. A decimal of the formula is written as a decimal, not as

str

the fraction the parser reads it as.

Raises:

Type Description
ValueError

if the formula is not valid math.

petab_index

petab_index(index, shape)

Get the index of an element of an output as PEtab SciML writes it.

Parameters:

Name Type Description Default
index tuple[int, ...]

the index of the element in the output.

required
shape tuple[int, ...]

the shape of the output.

required

Returns:

Type Description
str

The index, e.g. [0][1], without the leading axes of length one,

str

which are the axes of the batch, see sbmlsim.fit.petab_v2.sciml.

parameters_id

parameters_id(network, layer=None, array=None)

Get the petabEntityId of the parameters of a network, a layer or an array.

model_entity_id

model_entity_id(network, layer=None, array=None)

Get the modelEntityId of the parameters of a network, a layer or an array.