experiment.experiment¶
SimulationExperiments and helpers.
SimulationExperiment
¶
Generic simulation experiment.
Consists of models, datasets, simulations, tasks, results, processing, figures
SimulationExperiement.
:param sid: :param base_path: :param data_path: :param ureg: :param kwargs:
results
property
¶
initialize
¶
Initialize SimulationExperiment.
Initialization must be separated from object construction due to the parallel execution of the problem later on. Certain objects cannot be serialized and must be initialized. :return:
Q
¶
Create a quantity in a unit of pint and of the unit ids of the models.
The runner loads the models of an experiment before initialize, so
the unit ids of a model are known in simulations() and later, e.g.
self.Q(10, "mg_per_min"); a unit id is replaced by its expression,
see UnitsInformation.quantity. Before the models are loaded Q
takes the units of pint. sbmlsim.Q takes the units of pint only. A
unit of pint, e.g. the units of a quantity, is taken as it is.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
the magnitude, a number or an array. |
required |
unit
|
str | PlainUnit | None
|
the unit, a string or a unit of pint, dimensionless without one. |
None
|
model
|
str | None
|
the id of the model whose unit ids the unit has. Without it a unit id must have the same expression in every model of the experiment which has it. |
None
|
Returns:
| Type | Description |
|---|---|
Quantity
|
The quantity. |
Raises:
| Type | Description |
|---|---|
ValueError
|
for a |
UndefinedUnitError
|
for a unit which is neither a unit of pint nor a unit id of the models. |
datasets
¶
Define dataset definitions (experimental data).
The child classes fill out the information.
simulations
¶
Define simulation definitions.
The child classes fill out the information.
observables
¶
Define the observables of the experiment, by their id.
A Formula, PK or Custom of sbmlsim.simulation.observables. A
Data of a task reads an observable by its id and a parameter of a
PK observable as <id>.<parameter>; a task computes the observables
its data read, see _run_tasks. The child classes fill out the
information.
data
¶
Define the data of the experiment, including functions of other data.
This determines the selection in the model.
The data of tasks, fit mappings and figures determines the selections
of a simulation experiment; registering the data of a figure here is no
longer needed. Registered data of a task which no figure or mapping
reads still counts, e.g. via add_selections_data.
figures
¶
Figure definition.
The data the curves and areas of the figures read counts for the
selections of the tasks, it needs no registration in data().
Most figures do not require access to concrete data, but only abstract data concepts.
figures_mpl
¶
Matplotlib figure definition.
Selections accessed in figures and analyses must be registered beforehand via the data of the experiment.
Most figures do not require access to concrete data, but only abstract data concepts.
A figure is styled with the settings of the experiment,
FigureSettings.resolve(experiment=self.figure_settings), e.g. its
fig_titlesize; the settings of a run (figure_settings= of run)
do not reach this method.
fit_mappings
¶
Define fit mappings.
Mapping reference data on observables. Used for the optimization of parameters. The child classes fill out the information.
add_selections_data
¶
Add selections to given tasks.
The data for the selections will be part of the results.
Selections are necessary to access data from simulations. Here these selections are added to the tasks. If no tasks are given, the selections are added to all tasks.
:param reset: drop and reset all selections.
run
¶
run(
simulator,
output_path=None,
show_figures=False,
save_results=False,
figure_formats=None,
reduced_selections=True,
keep_results=True,
on_error="raise",
figure_settings=None,
)
Execute given experiment and store results.
A figure which failed in this run, and every figure of a failed experiment which this run did not write, leaves no file of an earlier run in the output path, so that no figure looks like one of this run.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
simulator
|
Simulator | None
|
the simulator of the tasks. |
required |
output_path
|
Path | None
|
directory of the outputs (datasets, results, figures and the serialization), none are written without it. |
None
|
show_figures
|
bool
|
show the matplotlib figures. |
False
|
save_results
|
bool
|
write the results of the tasks into the output path. |
False
|
figure_formats
|
list[str] | None
|
formats of the figures, |
None
|
reduced_selections
|
bool
|
simulate only the selections the experiment uses. |
True
|
keep_results
|
bool
|
keep the results of the tasks after the outputs are
written. Without them an experiment holds only what its report
needs, and a run of many experiments only the results of the one
it runs; |
True
|
on_error
|
Literal['raise', 'log']
|
what an error does. |
'raise'
|
figure_settings
|
FigureSettings | Mapping[str, Any] | None
|
the settings of the figures of the run, a
|
None
|
Returns:
| Type | Description |
|---|---|
ExperimentResult
|
The result of the experiment, which the report is created from. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if a key of |
scan_dimension
¶
Get a dimension of the scan of a task by its id, None if it has none of it.
model_units
¶
Get the units of the symbols of the model of a task, {} for a model which is not loaded.
to_json
¶
Convert experiment to JSON for exchange.
The experiment is encoded before the file is written, so a value which cannot be encoded leaves no truncated file.
:param path: path for file, if None JSON str is returned
:return:
:raises TypeError: for a value which ObjectJSONEncoder cannot encode
save_results
¶
Save the result of every task as netCDF, see ScanResult.to_netcdf.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results_path
|
Path
|
directory of the files, |
required |
create_mpl_figures
¶
Create matplotlib figures.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
on_error
|
Literal['raise', 'log']
|
|
'raise'
|
failed
|
dict[str, str] | None
|
collects the failed figures as |
None
|
figure_settings
|
FigureSettings | Mapping[str, Any] | None
|
the settings of the run, see |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, FigureMPL | Figure]
|
The figures by their key, without the failed ones. |
show_mpl_figures
¶
Show matplotlib figures.
The figures of the serializer are created without pyplot, so that
rendering does not fill its global registry; a figure it does not
manage cannot show itself. Showing is the one place which needs the
state machine, so a figure gets a manager here and only here.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mpl_figures
|
dict[str, FigureMPL]
|
the figures to show. |
required |
save_mpl_figures
¶
Save matplotlib figures.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results_path
|
Path
|
directory of the files. |
required |
mpl_figures
|
dict[str, FigureMPL]
|
the figures to save. |
required |
figure_formats
|
list[str] | None
|
formats of the files, |
None
|
on_error
|
Literal['raise', 'log']
|
|
'raise'
|
failed
|
dict[str, str] | None
|
collects the failed figures as |
None
|
save_interactive_figures
¶
Write the figures as interactive pages.
The pages are drawn by plotly, see
sbmlsim.plot.serialization_plotly: matplotlib draws the static
images a publication needs and plotly the pages a reader zooms and
hovers over. Both read the same Figure, so the two cannot disagree
about what they show.
plotly is not a dependency of sbmlsim; a run which asks for the
interactive figures without it says so and writes none.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results_path
|
Path
|
directory of the pages. |
required |
on_error
|
Literal['raise', 'log']
|
|
'raise'
|
failed
|
dict[str, str] | None
|
collects the failed figures as |
None
|
figure_settings
|
FigureSettings | Mapping[str, Any] | None
|
the settings of the run, see |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Path]
|
The path of the page of every figure, empty without plotly. |
close_mpl_figures
classmethod
¶
Close matplotlib figures.
A figure of the serializer is not in the registry of pyplot and is
released when nothing refers to it any more, for which this is a no-op.
A figure of figures_mpl() may well come from pyplot, and that one
is closed here.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mpl_figures
|
dict[str, FigureMPL]
|
the figures to close. |
required |
ExperimentRunError
¶
Bases: Exception
One or more experiments or figures of a run failed.
Raised by ExperimentRunner.run_experiments(raise_on_failure=True) after
every experiment ran.
Attributes:
| Name | Type | Description |
|---|---|---|
results |
the results of all experiments of the run, empty in a copy which was pickled. |
|
failures |
the failed experiments and figures, one line each. |
Create the error from the results of a run.