model.model_amici¶
The model of an SBML model in AMICI, see sbmlsim.model.model_sbml.
AmiciSBMLModel compiles the SBML of a model with AMICI (the optional extra
amici) into a python extension, which is cached on disk (compiled_dir),
and owns an instance of the compiled model and of its solver, CVODES. Its
values are read and written in the selections of roadrunner, S the amount
of a species, [S] its concentration, the id of a parameter or a
compartment, the id of a reaction its rate, the id of a species reference
its stoichiometry (a number, which AMICI folds into its equations) and
time, with the numbers of the SBML, which AMICI keeps as they are. Every
selection roadrunner makes by default is a state, a free parameter or an
expression of AMICI or a number of the SBML, so the model is compiled
without observables. A model of SBML level 1 or 2 is converted to level 3
version 2 first.
AMICI integrates the concentration of a species whose hasOnlySubstanceUnits
is false and the amount of the other species, folds a compartment without a
rule into the equations as a number and turns a parameter with an initial
assignment into an expression, which follows its math during a simulation.
The SBML which AMICI compiles (amici_sbml) therefore keeps every entity a
change can set apart:
- a parameter without a rule, an initial assignment or an event is a free parameter of AMICI, which is set directly;
- a compartment without an assignment rule and a parameter with an initial
assignment, a rate rule or an event are the targets of one event which
never fires (
TARGETS_EVENT), which makes them states of AMICI whose rate is 0 (or the one of their rate rule), so their value is the one at the initialization until a change or an event sets it; - the species are states of AMICI anyway.
The initial value of every such state is an initial assignment of two free
parameters which sbmlsim adds per entity, its value and its kind
(VALUE_PREFIX, KIND_PREFIX): piecewise(value as amount, kind == 1, value
as concentration, kind == 2, initial value of the SBML), the initial value of
the SBML being its initial assignment or its number, an amount of a species
whose state is a concentration divided by its compartment and vice versa (a
conversion factor, a constant of SBML, stays a free parameter of AMICI). A
pre-initialization sets these parameters and the free parameters, AMICI
evaluates the initial state of the model from them in the order of its
dependencies, so a target replaces its own initial assignment and every
initial value which depends on it follows, as in roadrunner
(RoadrunnerSBMLModel.initialize). A compartment whose number in the SBML is
not finite (NaN) and which has no initial assignment stays a number of
AMICI, which integrates no state which is not finite, and a change of it
raises.
The current state of the model is its time, the states of AMICI and its free parameters, which the executor sets between two integrations; the values of the assignment rules and the rates of the reactions are the expressions of AMICI, which an integration of no length at the current time evaluates.
AMICI finds the roots of the triggers of the events of the model, the
executor fires the events, see sbmlsim.simulator.executor_amici: every
event of the model keeps its trigger in the SBML AMICI compiles and, instead
of its assignments, sets two states of AMICI, that it fired in the
integration and the time of its first execution (FIRED_PREFIX,
FIRST_PREFIX, fired, first_times), which the executor sets to 0 before
every integration (reset_events), so the first root of an integration is
known exactly and the state of the model is the one before it. AMICI
evaluates a comparison exactly at its threshold at the start of an
integration as written (time <= 10 at the time 10 true, time > 10 false:
its Heaviside variables are initialized from the sign of the root functions,
a root of 0 as true) and keeps the value for the whole integration, since
CVODES does not report a root at the start and the root function does not
change its sign afterwards. The executor therefore shifts every comparison
which is exactly at its threshold at the start of an integration by a little,
so that AMICI finds the crossing when the operands move: every strict
comparison of a trigger, a > b or a < b, is compiled shifted by a free
parameter of AMICI, a > b + shift (SHIFT_PREFIX, comparisons,
set_shifts), and so is every comparison of the maths AMICI integrates, the
kinetic laws, the assignment rules and the rate rules, e.g. the condition of
a piecewise in a rate (SWITCH_PREFIX, switches, set_switch_shifts,
Switch): a > b + shift, a + shift < b, a >= b - shift and
a <= b + shift, so a shifted comparison keeps its value at its threshold
until the operands move by the shift in the direction which changes it, as
roadrunner evaluates it at every step; a == b and a != b of a math are
compiled as a <= b + shift && a >= b - shift and a < b - shift ||
a > b + shift, which is how AMICI compiles them without the shift. The
trigger of every event starts true in the compiled SBML, so AMICI fires no
event at the start of an integration; the events whose trigger is false
before the time 0 (start_events) are fired by the executor at the
start of a simulation, as the ones a change triggers. A feature which AMICI
does not compile or simulates otherwise than roadrunner raises at the load,
see _check_supported; a trigger which compares for equality, a == b or
a != b, is refused there: AMICI has no root of it, and roadrunner fires it
only where a step of its integrator lands on the equality. The priorities
of the events and the persistence of their triggers are the executor's, see
sbmlsim.simulator.changes.fire_events, so the compiled SBML has no
priority and every trigger persistent, which AMICI imports.
AmiciSBML
dataclass
¶
The SBML which AMICI compiles, see the module.
Attributes:
| Name | Type | Description |
|---|---|---|
sbml |
str
|
the SBML. |
flagged |
frozenset[str]
|
the entities whose initial value a pre-initialization sets
through the parameters |
not_a_number |
frozenset[str]
|
the compartments which are numbers of AMICI, their number in the SBML is not finite. |
start_events |
tuple[bool, ...]
|
by the position of an event of the model, whether its trigger is false before the time 0 in the SBML of the model. |
events |
tuple[str, ...]
|
the ids of the events of the model in AMICI, in the order of
the SBML, |
comparisons |
tuple[StrictComparison, ...]
|
the strict comparisons of the triggers of the events,
in the order of the shift parameters ( |
switches |
tuple[Switch, ...]
|
the comparisons of the maths of the model, in the order of
their shift parameters ( |
StrictComparison
dataclass
¶
A strict comparison of the trigger of an event, a > b or a < b.
AMICI compiles it shifted, a > b + shift or a + shift < b, see the
module; the operands are MathML of selections, which
sbmlsim.sbml_math.compile_math compiles, so the executor tells where
the comparison is exactly at its threshold, a == b.
Attributes:
| Name | Type | Description |
|---|---|---|
event |
int
|
the position of the event among the events of the model. |
left |
str
|
the MathML of the left operand |
right |
str
|
the MathML of the right operand |
Switch
dataclass
¶
A comparison of a math AMICI integrates, e.g. the condition of a piecewise in a rate.
AMICI compiles it shifted by a free parameter so that it keeps its value
at its threshold until the operands move, see the module; the operands
are MathML of selections, which sbmlsim.sbml_math.compile_math
compiles, so the executor tells where the comparison is exactly at its
threshold, a == b, and shifts it there.
Attributes:
| Name | Type | Description |
|---|---|---|
left |
str
|
the MathML of the left operand |
right |
str
|
the MathML of the right operand |
AmiciSBMLModel
¶
AmiciSBMLModel(
source,
base_path=None,
changes=None,
sid=None,
name=None,
selections=None,
ureg=None,
settings=None,
parameters=None,
)
Bases: SBMLModel
The model of an SBML model in AMICI, see the module.
Attributes:
| Name | Type | Description |
|---|---|---|
amici_model |
Any
|
the model of AMICI, whose free parameters, time and initial state an integration sets. |
solver |
Any
|
the solver of AMICI, CVODES. |
start_events |
tuple[bool, ...]
|
by the position of an event of the model, whether its
trigger is false before the time 0, which the executor fires at
the start of a simulation when it is true, see the module and
|
event_ids |
tuple[str, ...]
|
the ids of the events of the model in AMICI, in the order
of the SBML, see |
comparisons |
tuple[StrictComparison, ...]
|
the strict comparisons of the triggers of the events,
see |
switches |
tuple[Switch, ...]
|
the comparisons of the maths of the model, see |
absolute_tolerance |
AbsoluteTolerance
|
the absolute tolerance set last, see
|
Compile the model with AMICI or take it from the cache, with changes, selections and settings.
The parameters are added to the model as constant parameters, see
AbstractModel; they are not selected by default.
Raises:
| Type | Description |
|---|---|
ValueError
|
for a feature AMICI does not simulate as roadrunner, an error of AMICI when it imports the model, a selection AMICI has not, or an unknown setting. |
RuntimeError
|
if AMICI fails to compile the model. |
free_parameters
property
¶
Get a copy of the current free parameters of AMICI, in the order of their ids.
not_a_number
property
¶
Get the compartments which are numbers of AMICI, their number in the SBML is not finite.
from_abstract_model
staticmethod
¶
Create from AbstractModel, see AmiciSBMLModel.
set_time
¶
Set the current time of the model, between two integrations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time
|
float
|
the time. |
required |
set_state
¶
Set the current time and states of the model, e.g. after an integration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time
|
float
|
the time. |
required |
state
|
Sequence[float] | ndarray
|
the states of AMICI, in the order of its state ids. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the number of states is not the one of AMICI. |
prepare
¶
Set the model of AMICI to an integration from the current state.
The free parameters, the start time (the current time) and the
initial state (the current states) of the model of AMICI are the
ones of the model; the integration outputs at the time points. The
solver is a new one with the settings of the one before: CVODES keeps
a root function which is exactly 0 at the start of an integration
and still 0 after its first small step out of the root finding of
its memory (gactive of cvRcheck1), which AMICI reuses for the
next integration without setting it back (CVodeRootInit with the
same function returns at once), so the trigger would not be found
again, e.g. after an integration from a change which set a state to
the threshold of a trigger.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timepoints
|
Sequence[float] | ndarray
|
the output times, from the current time on. |
required |
rates
¶
Get the current rates of change of states.
The rate of a species is the rate of its amount, V dc/dt + c dV/dt
for a species whose state of AMICI is its concentration c in the
compartment of the size V; dV/dt is the rate of a compartment
which is a state of AMICI and 0 for one which is not (a number, a
free parameter, or an assignment rule, which is constant in a steady
state of the states it reads).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
states
|
Sequence[str]
|
the ids of species or of the targets of rate rules, see
|
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
The rate of every state. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
if AMICI fails to evaluate them. |
fired
¶
Tell the events AMICI fired in an integration from states of AMICI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
states
|
ndarray
|
states of AMICI, a row or an array of rows. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Whether every event, in the order of |
ndarray
|
records were set back ( |
first_times
¶
Get the times of the first executions of the events by AMICI from states of AMICI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
states
|
ndarray
|
states of AMICI, a row or an array of rows. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
The time of the first execution of every event, in the order of |
ndarray
|
|
ndarray
|
every row; 0 for an event which did not fire, see |
set_shifts
¶
Set the shifts of the strict comparisons of the triggers, see StrictComparison.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shifts
|
Sequence[float]
|
a shift per comparison, in the order of |
required |
set_switch_shifts
¶
Set the shifts of the comparisons of the maths of the model, see Switch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shifts
|
Sequence[float]
|
a shift per comparison, in the order of |
required |
output_transform
¶
Get the conversion of the output of AMICI into the values of selections.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selections
|
Sequence[str]
|
the selections, see |
required |
Returns:
| Type | Description |
|---|---|
Callable[[ndarray, ndarray, ndarray], ndarray]
|
A function of the times, the states and the expressions of AMICI |
Callable[[ndarray, ndarray, ndarray], ndarray]
|
of an output (a row per time), which gives the values of the |
Callable[[ndarray, ndarray, ndarray], ndarray]
|
selections, a row per time and a column per selection, with the |
Callable[[ndarray, ndarray, ndarray], ndarray]
|
current free parameters. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if AMICI has no selection of a name. |
value
¶
Get the current value of a selection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selection
|
str
|
the selection, e.g. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The value in the current state of the model. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
if AMICI has no selection of the name, as roadrunner. |
set_value
¶
Set the current value of a selection, between two integrations.
The value is written into the states or the free parameters of AMICI, the amount of a species whose state is a concentration as the concentration in the current size of its compartment and vice versa. As in roadrunner, which integrates the amounts of the species, every other species keeps its amount, also when the size of its compartment changes (a compartment which is set, or one of an assignment rule which reads what is set).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selection
|
str
|
|
required |
value
|
float
|
the value. |
required |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
if AMICI has no selection of the name, or it cannot be set: the time, a reaction, the variable of an assignment rule, as roadrunner, or a compartment which is a number of AMICI. |
initialize
¶
Initialize the model with values before its initialization.
The free parameters of AMICI are the ones as loaded with the values
which are set, a parameter of AMICI directly and an entity whose
initial value is a state of AMICI through the parameters of sbmlsim
of its value and its kind, from which AMICI evaluates the initial
state, see the module; the initialization does not depend on the one
before it. A target replaces its own initial assignment, every
initial value which depends on it follows. A compartment whose size
changes keeps the concentration of the species whose initial value
is a concentration and of a species which is set as a concentration,
and the amount of every other species, as roadrunner does
(RoadrunnerSBMLModel.initialize); the variable of an assignment
rule follows its rule, a value of it is not used.
A formula reads what it reads in roadrunner, the values as loaded with the values which are set, before the initial assignments are evaluated again, and an assignment rule on that state; the values of all formulas are evaluated before any of them is set (PEtab v2).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
assignments
|
Sequence[Assignment]
|
the pre-initialization assignments of a plan. A
formula reads parameters only, see |
required |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
for a compartment which is a number of AMICI, see the module. |
simulated
¶
Record that the model was simulated since its last initialization.
Nothing is recorded: an initialization starts from the free
parameters as loaded whatever ran before it, see initialize.
loaded_value
¶
Get the value of a parameter, a compartment or a species of the model as loaded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selection
|
str
|
the id of a parameter, a compartment or a species (its
amount), or |
required |
Returns:
| Type | Description |
|---|---|
float
|
The value as loaded. |
Raises:
| Type | Description |
|---|---|
KeyError
|
if the selection is none of these. |
has_selection
¶
Check whether AMICI has a selection of a name in the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selection
|
str
|
the name, e.g. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Whether the loaded model can select it. |
set_selections
¶
Set the selections of the simulations of the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selections
|
Sequence[str] | None
|
the selections, |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
if AMICI has no selection of a name. |
set_integrator_settings
¶
Set settings of the integrator, CVODES of AMICI.
The settings have the names of roadrunner: relative_tolerance,
absolute_tolerance, maximum_num_steps and maximum_time_step.
AMICI has one absolute tolerance, the smallest tolerance of an
AbsoluteTolerance (of the kinds and of the overrides, reported once
when they differ) times AMICI_ATOL_FACTOR, so AMICI integrates at
least as accurately as roadrunner at equal settings. A setting of
roadrunner which AMICI has no
equivalent of, e.g. variable_step_size, is ignored and reported
once per model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**settings
|
IntegratorSetting
|
the settings by their names in roadrunner. |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
if neither AMICI nor roadrunner has a setting of a name, or a value is not valid. |
integrator_state
¶
Get the name of the integrator of the model and every one of its settings.
The settings have the names of roadrunner, the absolute tolerance is
the AbsoluteTolerance of the model as it was set; the one absolute
tolerance of AMICI is its smallest tolerance, see
set_integrator_settings.
Returns:
| Type | Description |
|---|---|
tuple[str, tuple[tuple[str, Any], ...]]
|
The name of the integrator and its settings, sorted by name. |
set_integrator_state
¶
Set the settings of the integrator, see integrator_state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
tuple[str, tuple[tuple[str, Any], ...]]
|
the name of the integrator, |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the integrator is not the one of AMICI, or it has no setting of a name. |
state_ids
¶
Get the ids of the states which the integrator integrates.
The species which reactions or a rate rule change, in the order of the SBML, then the other targets of rate rules, in the order of the SBML; the states sbmlsim adds to AMICI, whose rate is 0, are not part of them.
amici_sbml
¶
Get the SBML which AMICI compiles, see the module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sbml
|
str
|
the SBML of the model, with the added parameters. |
required |
symbols
|
ModelSymbols
|
the symbols of the model. |
required |
label
|
str
|
the name of the model in a message. |
required |
Returns:
| Type | Description |
|---|---|
AmiciSBML
|
The SBML and what sbmlsim changed in it. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the model has an entity of the id of a parameter of sbmlsim, or a model of a level before 3 cannot be converted to level 3. |
shift_ids
¶
Get the ids of the parameters of the shifts of strict comparisons, see SHIFT_PREFIX.
switch_ids
¶
Get the ids of the parameters of the shifts of the comparisons of the maths, see SWITCH_PREFIX.
cache_root
¶
Get the directory of the compiled models.
The directory of using_cache while it is used, else
SBMLSIM_AMICI_CACHE if it is set, else ~/.cache/sbmlsim/amici.
using_cache
¶
Load and compile the models of AMICI in a directory of compiled models.
A worker of a pool loads a model from the directory its parent loaded it
from, whatever its own environment says: a kept pool, and the forkserver
its workers are started from, keep the environment of the time they
started, so a worker would otherwise compile the model again into
another directory after the parent changed SBMLSIM_AMICI_CACHE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root
|
Path | str | None
|
the directory, |
required |
cache_key
¶
Get the key of a compiled model in the cache.
The SHA-256 of the SBML AMICI compiles, the version of AMICI, the
version and the implementation of python, the machine, the options of the
compilation and CACHE_FORMAT.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sbml
|
str
|
the SBML AMICI compiles, see |
required |
Returns:
| Type | Description |
|---|---|
str
|
The key, a hexadecimal digest. |
compiled_dir
¶
Get the directory of the compiled model of an SBML, compiling it if it is not cached.
The model is compiled into a temporary directory next to its directory
in the cache (cache_root, cache_key), which is renamed to it when
the compilation is complete, so the directory of a model is complete if
it exists. The compilation runs under a lock of the key, so processes
which need the same model at once compile it once.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sbml
|
str
|
the SBML AMICI compiles, see |
required |
label
|
str
|
the name of the model in a message. |
'model'
|
Returns:
| Type | Description |
|---|---|
Path
|
The directory of the compiled model. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if AMICI cannot import the model. |
RuntimeError
|
if AMICI fails to compile the model. |
load_module
¶
Get the module of a compiled model, see compiled_dir.
A module is imported once per process.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directory
|
Path
|
the directory of the compiled model in the cache. |
required |
Returns:
| Type | Description |
|---|---|
ModuleType
|
The module of the model, whose |
run_simulation
¶
Get run_simulation of AMICI, which integrates a model with a solver.
The debug messages of CVODES of the integration are logged with sbmlsim,
see _DebugMessages, which is installed once on the logger of AMICI.
vector_array
¶
Copy a vector of doubles of AMICI into a new array, bitwise.
A field of AMICI's ReturnData, e.g. rdata.ptr.x (ptr is the SWIG
object of the ReturnDataView of run_simulation), is a SWIG proxy of a
std::vector<double> (DoubleVector), which numpy reads element by
element through the iterator of the proxy in python, about 0.25 µs per
element (30 ms for the expressions of 1500 time points of HCTZ), and so
does the ReturnDataView (_field_as_numpy). AMICI's stdVec2ndarray,
which wraps the data of a vector, crashes in amici 1.2: its wrapper
releases the GIL (SWIG -threads) and it creates an array without it.
The function copies the data of the vector with one memmove instead:
it reads the pointers to the begin, the end and the end of the storage of
the vector from the object of the vector (the layout of libstdc++,
libc++ and the STL of MSVC without debug iterators) and uses them only
when they are consistent with the length of the vector, and compares the
copy bitwise with the first VECTOR_PROBES elements and the last one read
through the proxy; the elements between are the contiguous storage of the
vector from the first to the last. A vector which fails a check is copied
element by element, which is logged once per process; the result is
never another than that of np.array(vector, dtype=float). Another
sequence, e.g. the tuple AMICI returns for a vector it returns by value
such as Model.get_initial_state(), is converted with np.array.
The vector must be alive and unchanged during the call: the caller keeps
the object which owns it, e.g. the ReturnDataView.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vector
|
Any
|
a |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
A new array of the values, one dimensional. |