simulator.formula¶
Formulas of the math of PEtab over the selections of roadrunner.
A formula is a string of the math of PEtab whose symbols are selections of
roadrunner: S is the amount of a species, [S] its concentration and time
the time. The brackets are not math, so [S] is replaced by an identifier
before the formula is parsed and mapped back afterwards; so is an identifier
with a dot, the parameter of a PK observable, e.g. hctz.cmax.
A formula is compiled to a numpy function once and cached, a CompiledFormula
is not pickled: a plan keeps the formula as a string. The parser of PEtab
decides what is valid math, parse_math, and the function is written from
its parse tree with the meaning the package gives the math of SBML as well
(sbmlsim.sbml_math): a number is the double of its text and the operations
are the ones written, in the order written, x / 90 is one division by 90 and
0.1 + 0.2 is 0.30000000000000004, see _NumpyCode.
The formulas of observables extend the math by four reductions over the time
of one simulation, see reduce_formula and evaluate_reduced: max(x) and
min(x) with a single argument (with two or more they are the elementwise
functions of PEtab), mean(x), the time weighted mean, and at(x, t), the
value at a time. A value is an array whose last axis is the time of a
simulation, so a scan of many simulations reduces every simulation on its
own. The formulas of data share reduce_formula and have only max and
min, which reduce a labelled array along its dimension of the time or of
the rows of a dataset by name, see sbmlsim.data.evaluate_function.
A formula which is linear in some of its symbols is also read as a form,
constant + Σ factor * symbol, see linear_form: the engine of sbmlsim-core
evaluates such a form itself, at a change from its own state before it
(sbmlsim.simulator.executor_core.form_of) and in a condition of a fit at the
values its optimizer is at (sbmlsim.fit.fit_core), so the value it carries
follows the simulation and the parameters where a number folded into the
request would hold it where that request was built.
CompiledFormula
dataclass
¶
A formula compiled to a numpy function of its symbols.
Attributes:
| Name | Type | Description |
|---|---|---|
formula |
str
|
the formula as it was given. |
symbols |
tuple[str, ...]
|
the selections the formula reads, sorted. |
evaluate
¶
Evaluate the formula.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
Sequence[float]
|
the values of the symbols, in the order of |
required |
Returns:
| Type | Description |
|---|---|
float
|
The value of the formula. |
apply
¶
Evaluate the formula on values of any type numpy operates on.
Unlike evaluate, the result is not converted to a float, arrays and
the quantities of pint keep their shape and their units.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
Sequence[Any]
|
the values of the symbols, in the order of |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The value of the formula. |
evaluate_array
¶
Evaluate the formula on arrays of the values of its symbols.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
Sequence[ndarray]
|
the values of the symbols, in the order of |
required |
size
|
int
|
the number of values, which a formula without symbols needs. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
The |
Reduction
dataclass
¶
A reduction over the time of a simulation, see reduce_formula.
Attributes:
| Name | Type | Description |
|---|---|---|
symbol |
str
|
the identifier which stands for its value in the formula. |
function |
str
|
|
arguments |
tuple[str, ...]
|
the formulas of its arguments, which read the symbols of the reductions inside of them. |
ReducedFormula
dataclass
¶
A formula whose reductions are replaced by symbols, see reduce_formula.
Attributes:
| Name | Type | Description |
|---|---|---|
formula |
str
|
the formula as it was given. |
outer |
str
|
the formula with every reduction replaced by its symbol. |
reductions |
tuple[Reduction, ...]
|
the reductions, an inner one before the one whose argument it is. |
outer_symbols
property
¶
Get the identifiers the formula reads outside of its reductions.
parse_math
¶
Parse the math of PEtab into a sympy expression, petab.v2.math.sympify_petab.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
formula
|
str
|
the math, without the brackets of a concentration. |
required |
Returns:
| Type | Description |
|---|---|
Basic
|
The expression. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the formula is not valid math of PEtab. |
compile_formula
cached
¶
Compile a formula, see the module.
Whether a formula is valid math is decided by the parser of PEtab with
the evaluation of sympy, parse_math, as before: it refuses what sympy
evaluates to a value which is not real, e.g. 1/0 and sqrt(-1), and
accepts what sympy folds to a number, e.g. (2 > 1) * x and log(0).
Its expression is not compiled, sympy evaluates on construction with
numbers of 15 digits and writes x / 90 as 0.0111111111111111 * x.
The function is compiled from the parse tree by _NumpyCode instead:
every number is the double of its text and every operation the one
written, in the order written, e.g. x / 90 divides once by 90.0 and
0.1 + 0.2 is 0.30000000000000004. It computes in the doubles of numpy,
as the math of SBML does (sbmlsim.sbml_math.compile_math): a number it
is given, a python float included, is a double of numpy, so a division by
zero is infinite and the root of a negative number is NaN, without the
warnings of numpy; arrays, the quantities of pint and the arrays of
xarray are passed as they are.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
formula
|
str
|
the formula. |
required |
Returns:
| Type | Description |
|---|---|
CompiledFormula
|
The compiled formula, which reads every identifier of the formula. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the formula is not valid math of PEtab. |
linear_form
¶
Get a formula as a linear form over the symbols a caller names.
A linear form is constant + Σ factor * symbol. Two callers read one off
the formula of an assignment, and this is the one place which tells such a
formula from any other: sbmlsim.simulator.executor_core over the
entities of a model, which the engine evaluates at a change from its own
state before it (the field forms of a change of the contract
simulation/1), and sbmlsim.fit.fit_core over the parameters a fit
estimates, which the engine evaluates before every simulation at the
values the optimizer is at (the field derived of a condition of
fit/1).
A formula is the math of PEtab over the symbols of the model, and a form is read over the symbols themselves, for a fit over the entity each parameter writes and not over the quantity of the parameter: so a coefficient of the form is the coefficient of the formula and carries no factor of a unit either way.
The symbols are the selections of roadrunner, the concentration of a
species and the dotted name of an observable included: the formula is
escaped for the parser as compile_formula escapes it and every symbol
of the parsed expression is mapped back, so [A] + 5 is a form over
[A] and is no longer refused for being unreadable (#630).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
formula
|
str
|
the formula of the assignment. |
required |
targets
|
Mapping[str, int]
|
the position in the terms of every symbol the form may read, an entity of the model for the executor and the entity each parameter of a fit writes for a fit. |
required |
constants
|
Mapping[str, float] | None
|
the value of every other symbol the form may read, which
is substituted before the form is read: the numbers a
pre-initialization sets and the constant parameters of the model,
which |
None
|
Returns:
| Type | Description |
|---|---|
tuple[float, list[tuple[int, float]]] | None
|
The constant and the factor of every symbol the form reads, by its |
tuple[float, list[tuple[int, float]]] | None
|
position, or |
tuple[float, list[tuple[int, float]]] | None
|
symbols alone: one which reads anything else, which is not |
tuple[float, list[tuple[int, float]]] | None
|
polynomial in them, which is of a higher degree, or whose |
tuple[float, list[tuple[int, float]]] | None
|
coefficients are not finite numbers. A formula of no symbol is |
tuple[float, list[tuple[int, float]]] | None
|
|
formula_mathml
¶
Write a formula of the math of PEtab as the MathML of SBML.
The MathML is written from the parse tree compile_formula compiles
(_NumpyCode), with the same meaning, so a model which carries it
computes what the compiled formula computes: a number is the double of its
text and every operation is the one written, in the order written, x /
90 is one division by 90 and a + b + c adds a and b first. A
comparison, &&, || and ! are the elements of MathML, piecewise is a
piecewise whose last value is its otherwise, a truth value read as a
number is piecewise(1, b, 0) and a number read as a truth value is x !=
0, as the parser of PEtab reads them. sign(x) is the piecewise of 1, -1
and 0 and else x, which is NaN for NaN as numpy.sign, min(a, b)
and max(a, b) are the piecewise of a <= b and a >= b, which a model
of every level of SBML carries, log(x, b) is log with its logbase,
log10 and log2 as well, sec(x) is 1 / cos(x) and arcsec(x) is
arccos(1 / x), and so on for the other reciprocal functions, pow is
power, and time is the symbol of the time of SBML.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
formula
|
str
|
the formula, whose identifiers are SIds; a concentration
|
required |
names
|
Mapping[str, str] | None
|
the identifier of the model of an identifier of the formula,
e.g. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The MathML, a |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the formula is not valid math of PEtab, and for a concentration or an identifier with a dot. |
reduce_formula
cached
¶
Find the reductions of a formula, see the module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
formula
|
str
|
the formula. |
required |
Returns:
| Type | Description |
|---|---|
ReducedFormula
|
The formula with its reductions. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the parentheses are not balanced, a reduction has not its number of arguments or a part of the formula is not valid math. |
evaluate_reduced
¶
Evaluate a formula with reductions over the time of every simulation.
A value is a number, a numpy array or a quantity of pint. The last axis of
an array is the time of a simulation and the axes before it are the
simulations, e.g. (n_points, n_rows) padded with NaN in a worker or
(*dims, time) of a scan. A number, or an array whose last
axis has one element, is a value per simulation, which is constant in
time. A reduction keeps its axis with one element, so its value broadcasts
against the timecourses, and keeps the unit of its argument:
max(x),min(x): the largest and the smallest value, ignoringNaN;mean(x): the trapezoidal integral divided by the time between the first and the last time point, the value itself for a single time point; the two time points of the states before and after a change at its time are an interval without time, which adds nothing;at(x, t): the value at the timet, a number or a value per simulation, interpolated linearly; the value after a change or an event of the model at its time, toward the state before a change at a time before it (time points at mosttoleranceapart are a jump, seesbmlsim.result.timecourse.grid_weights) andNaNoutside of the time points.
The times which are not finite, the padding (NaN) and the steady state
after the end (inf), are no time points of a reduction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
formula
|
str
|
the formula. |
required |
values
|
Mapping[str, Any]
|
the values of its identifiers. |
required |
time
|
ndarray | None
|
the time points of the timecourses, of their shape; |
None
|
tolerance
|
float | ndarray
|
the resolution of the time of every simulation, a number or
an array of the shape of the simulations, e.g. |
0.0
|
changes
|
Sequence[ndarray] | None
|
the times of the changes after the start of every
simulation, in the order of the simulations flattened, see
|
None
|
Returns:
| Type | Description |
|---|---|
Any
|
The value of the formula. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the formula is not valid, reads an identifier without a
value, reduces a timecourse with |