simulator.simulator¶
The simulator: simulations and scans of models, serially or in a pool.
A scan of the engine core is planned and run by sbmlsim-core, see
sbmlsim.simulator.scan_core: the request scan/1 of the scan goes to the
planner of the bindings, which simulates its points on the threads of the
engine in this process, and its answer becomes the ScanResult described
here. Simulator.run(model, scan) of roadrunner, COPASI and AMICI runs every
point of a scan in four steps:
- Compile: a plan per combination of the simulations and the models of the
scan (one plan without a dimension of simulations or of models), the
values of the dimensions of values in the units of their targets in every
model and the times of the dimensions with
atin the time unit of every model, compiled by sbmlsim-core (sbmlsim.simulator.plans), once; the selections and the grid of the output; the observables of the run, ordered, checked against the first model and with their kinds and units, seesbmlsim.simulator.observables. - Points: a point is a tuple of indices, nothing is built per point in the
parent. The points are cut into chunks which share a plan, at most
ceil(n_points / (4 * n_workers))and at mostMAX_CHUNKpoints each, four per process so that a process which finishes early takes another chunk. - Worker: a chunk applies the values of each point to its plan and runs it,
see
sbmlsim.simulator.worker; the same function runs serially in the calling process and in a worker of a pool ofsbmlsim.parallel; the observables are evaluated on the native solutions of the points of a chunk before the kept timecourses are interpolated onto a grid. A worker of a pool hands a large array of the result of a chunk over in shared memory instead of the pipe of the pool, on POSIX, seesbmlsim.simulator.shared; the segments of a run never outlive it. - Assembly: the arrays of a chunk are written into the arrays of the result
when the chunk arrives and the chunk is released, see
_Assembly, so a run holds its result once; the arrays are reshaped into the dimensions of the scan, aScanResult.
A run of a block of points (points=range(start, stop)) runs only these
points of the scan, in the same chunks of a plan, and gives a result over the
dimension _index of their flat indices in C order; ScanResult.combine
combines the results of the blocks of a scan to the result of the whole scan,
bitwise, so a scan runs in pieces, e.g. on several machines.
The result does not depend on the number of workers or the size of the chunks: the values of a scan are fixed before the run. A run which raises raises the error of the first point in the order of the scan which fails, and a run which flags the points which fail keeps their errors in that order.
The integrator settings of a simulator apply to every model it runs: the model of a run, every model of a dimension of models and every model a worker loads, each with its own tolerance per state. A setting of a model which the simulator does not set stays, and a worker loads the model with the integrator and the settings the model has in the calling process.
The engine of a simulator, sbmlsim-core by default
(sbmlsim.engine.DEFAULT_ENGINE), roadrunner, COPASI or AMICI, simulates
every model it runs: a model of another engine is loaded
again in the engine of the simulator, see
sbmlsim.model.model_sbml.load_model, and a plan runs with the executor of
the engine, see sbmlsim.simulator.engines. The engine is the attribute
engine of the result. A run of sbmlsim-core starts no pool: the planner
runs its points on the threads of scan_core.core_threads(n_workers,
n_points) in one python process, and its result does not depend on them
either. A feature the engine does not simulate raises its
NotImplementedError, also with on_error="flag", when the scan is compiled,
before a point runs or a pool starts, see
sbmlsim.simulator.engines.check_plan. A result of COPASI depends on the
number of workers within the integration and not bitwise: COPASI sums the
rates of the reactions of a species in an order which depends on the memory
its process allocated before, so two loads of a model in the parent and in a
worker differ in the last bit of a rate, which the integration amplifies. A
result of AMICI does not depend on the number of workers: a worker imports
the module the parent compiled, from the directory of compiled models the
parent loaded it from (ModelSpec.cache), whatever the environment of the
worker says, see sbmlsim.model.model_amici.using_cache.
ScanError
¶
Bases: RuntimeError
A point of a scan failed; the message names its labels and values.
Simulator
¶
Runs simulations and scans of models, see the module.
Attributes:
| Name | Type | Description |
|---|---|---|
n_workers |
the processes of a run, see
|
|
engine |
Engine
|
the engine which simulates every model. |
integrator_settings |
dict[str, IntegratorSetting]
|
the settings of the integrator of every model. |
Create a simulator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_workers
|
int | None
|
|
None
|
engine
|
Engine | str
|
the engine or its name, |
DEFAULT_ENGINE
|
**integrator_settings
|
Unpack[IntegratorSettings]
|
the settings of the integrator by their
names in roadrunner, see |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
if |
ImportError
|
for the engine COPASI without python-copasi, for the
engine AMICI without AMICI and for sbmlsim-core without the
bindings of sbmlsim-core, which are no extra of sbmlsim, see
|
set_integrator_settings
¶
Set settings of the integrator for every model run after.
Every name is taken, also one which is no IntegratorSettings, e.g. a
setting of another integrator of roadrunner, so a dictionary
dict[str, IntegratorSetting] is set here.
A name the integrator does not have raises when a model is loaded, i.e. before a pool starts.
load
¶
Get a model of the engine with the integrator settings of the simulator.
A model of the engine is used as it is and gets the settings; a model
of another engine, an AbstractModel, with its selections, or a path
is loaded in the engine, see sbmlsim.model.model_sbml.load_model.
Raises:
| Type | Description |
|---|---|
ValueError
|
if the model is of no supported type, or the integrator has no setting of a name. |
compile
¶
Compile a simulation against a model.
The changes of the model are defaults of the pre-initialization
changes, see Simulation.with_preinit_defaults: a change of the model
applies unless the simulation sets the target.
The plan is compiled by sbmlsim-core, see sbmlsim.simulator.plans.
Raises:
| Type | Description |
|---|---|
ValueError
|
if the simulation does not fit the model, e.g. a target which is no target of the model or a quantity which cannot be converted into the unit of its target. |
UnsupportedModelError
|
for a model sbmlsim-core cannot compile. |
simulate
¶
Run one simulation with the selections of the model.
The native solution of one simulation, which the fit and the test
suites use; a scan is run. It runs on the executor of the engine,
also for the engine core, whose scans the planner of sbmlsim-core
runs: the executor raises the error of its cause (a RuntimeError,
a SteadyStateError, the deadline of the runtime) and keeps a later
column time of a model with an entity time (case 01820), which
the result of a scan has no column for.
Raises:
| Type | Description |
|---|---|
ValueError
|
if the simulation does not fit the model. |
RuntimeError
|
if the engine fails to integrate. |
NotImplementedError
|
for a feature the engine does not simulate. |
sensitivities
¶
Run one simulation with the forward sensitivities of the parameters.
The derivatives of the columns of the simulation in the values the
parameters have at its start, which sbmlsim-core integrates along
with the states: one simulation answers what a central difference
answers with two simulations per parameter, and to the tolerance of
the integration instead of the step of a difference, which is what
sbmlsim.sensitivity.forward reads. Only sbmlsim-core carries
them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
ModelLike
|
the model, see |
required |
simulation
|
Simulation | Plan
|
the simulation or a plan of it. |
required |
parameters
|
Sequence[str]
|
the ids the derivatives are taken in, in their order:
a parameter of the model, a compartment, or a species as its
amount ( |
required |
selections
|
Sequence[str] | None
|
the columns of the result, the selections of the model by default. |
None
|
Returns:
| Type | Description |
|---|---|
TimecourseSensitivities
|
The result and the derivatives of its columns. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the simulation does not fit the model, or no parameter is given. |
NotImplementedError
|
if the engine of the simulator is not the
sbmlsim-core, or for a plan or a model whose sensitivities the
engine does not carry ( |
RuntimeError
|
if the engine fails to integrate. |
run
¶
run(
model,
scan,
observables=None,
*,
time=None,
keep=None,
on_error="raise",
progress=None,
points=None,
)
Run every point of a scan, or a block of its points, see the module.
The variables of the result are the kept observables; without observables they are the selections of the model, of the first model of a dimension of models, each a timecourse of its own name.
A block of points is a piece of the scan, e.g. a unit of work of a run
on several machines: its result has the dimension _index of the flat
indices of its points in C order instead of the dimensions of the scan,
every coordinate of a dimension along _index and the attribute
block, see ScanResult.combine, which combines the results of the
blocks of a scan to the result of the whole scan, bitwise. The methods
of a result over the dimensions of the scan, dims, summary,
interpolate, to and q, are for the combined result: summary
and interpolate raise for a block. A run of a block allocates per
point of the block only, also for a scan of more points than fit
into memory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
ModelLike | None
|
the model of the run; |
required |
scan
|
Scan | Simulation
|
the scan, a simulation is a scan without dimensions. |
required |
observables
|
Sequence[Observable] | None
|
what the run computes from every simulation, see
|
None
|
time
|
ArrayLike | Quantity | None
|
a grid of times every timecourse is interpolated onto,
numbers in the time unit of the model or a quantity, see
|
None
|
keep
|
Sequence[str] | None
|
the observables of the result, every one by default; the id of a PK observable keeps all its parameters; without observables the selections of the result. The others are evaluated where a kept one needs them and dropped. |
None
|
on_error
|
OnError
|
|
'raise'
|
progress
|
bool | None
|
show a progress bar; |
None
|
points
|
range | None
|
the flat indices of the points in C order which run, a
|
None
|
Returns:
| Type | Description |
|---|---|
ScanResult
|
The result, see |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the scan does not fit its models, see |
ScanError
|
for a point which fails with |
RuntimeError
|
if a worker of the pool dies. |
NotImplementedError
|
for a feature the engine does not simulate,
see |
scan_point_plans
¶
Get the plans of points of a scan of value dimensions on a compiled plan.
The plan of a point is the plan with the values and times of the point,
scan_point_values, applied by point_plan, the function the workers of a
run apply them with. The fit computes the values once per simulation group
and applies them to the plan of every evaluation, which carries the fitted
values first, so the values of a dimension win over a fitted value of the
same target, as in a run of the scan.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scan
|
Scan
|
the scan, whose dimensions all set values. |
required |
model
|
SBMLModel
|
the loaded model, whose units the values are converted to. |
required |
plan
|
Plan
|
the compiled plan of |
required |
positions
|
ndarray
|
the index of every point along every dimension, a row per point; the one point of a scan without dimensions is a row of length 0. |
required |
Returns:
| Type | Description |
|---|---|
list[Plan]
|
The plan of every point, in the order of |
Raises:
| Type | Description |
|---|---|
ValueError
|
for a dimension of simulations or models, values which do
not fit the model, a time of a dimension with |
scan_point_values
¶
Get the values and times of points of a scan of value dimensions.
The values are in the units of the model and the times in its time unit,
computed as a run of the scan computes them. point_plan(plan, values,
timed, k) applies them to a plan, see scan_point_plans; the fit computes
them once per simulation group and applies them to the plan of every
evaluation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scan
|
Scan
|
the scan, whose dimensions all set values. |
required |
model
|
SBMLModel
|
the loaded model, whose units the values are converted to;
the times of the dimensions with |
required |
positions
|
ndarray
|
the index of every point along every dimension, a row per point; the one point of a scan without dimensions is a row of length 0. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, ndarray]
|
target -> the value of every point, and time -> target -> the value of |
dict[float, dict[str, ndarray]]
|
every point for the dimensions with |
Raises:
| Type | Description |
|---|---|
ValueError
|
for a dimension of simulations or models, values which do
not fit the model, a time of a dimension with |
settings_record
¶
Get the integrator settings as JSON types, the provenance of a result.