Skip to content

model

The description of a PBPK/PD model, which the commands of pkpdsim work with.

A model package defines one PKPDModel in its __init__.py, next to its path constants:

# not executed
MODEL = PKPDModel(
    name="hctz",
    package=__name__,
    title="HCTZ PBPK/PD model",
    path=HCTZ_PATH,
    model_path=MODEL_PATH,
    data_paths=tuple(DATA_PATHS),
    factory=".models.factory:create_hctz_models",
    experiments=".simulations:EXPERIMENTS",
    fit_definitions=".fitting.fitting:FIT_DEFINITIONS",
)

A PKPDModel is a sbmlsim.project.Project: the factory, the experiments, the fit definitions and the credibility declaration are references "module:attribute", resolved when they are used, the settings of the simulator are the sbmlsim.project.SimulatorSettings, the known exceptions of the tests the sbmlsim.project.CheckSettings and the release metadata the sbmlsim.release.settings.ReleaseSettings of sbmlsim. The keywords of the model packages (path, model_path, female_model_path, data_paths, fit_definitions, fits, figures) are mapped onto the fields of the project when a model is created, see PKPDModel; the release settings of a model are a PKPDReleaseSettings, the ones of sbmlsim with the framework pkpdsim and its release hook (pkpdsim.release.hook).

A model whose studies include women has a female build next to the male one (female_model_path, the factory builds both from one definition, see pkpdsim.pbpk.create_archive): the builds are the variants of the project by the value of their pkpdsim.sex.Sex, the male build first, the model of every caller which does not ask for a sex, and an experiment chooses the build of the sex of its subjects (pkpdsim.experiment.PKPDSimulationExperiment.sex).

FigureSettings is the sbmlsim.plot.FigureSettings of sbmlsim with the formats of the figures and the resolution of the static images, which a model splits into the figure_settings and the figures of its project; the module therefore imports sbmlsim's plotting.

PKPDReleaseSettings dataclass

PKPDReleaseSettings(
    *,
    framework="pkpdsim",
    core_libraries=("pkpdsim", "sbmlsim", "sbmlutils"),
    library_owner="matthiaskoenig",
    hook="pkpdsim.release.hook:HOOK",
)

Bases: ReleaseSettings

The release settings of a model, ReleaseSettings with the defaults of pkpdsim.

The fields of sbmlsim.release.settings.ReleaseSettings; the release is built with pkpdsim unless the model package gives another framework, so a field which the package gives keeps its value, e.g. framework="sbmlsim". The archive of the release is PKPDModel.archive_path.

Attributes:

Name Type Description
framework str

pkpdsim, whose version the release records.

core_libraries Sequence[str]

pkpdsim, sbmlsim and sbmlutils, whose release notes the release notes summarize.

library_owner str

matthiaskoenig, the owner of their repositories.

hook object

the release hook of pkpdsim, pkpdsim.release.hook:HOOK, a reference, so the model package imports none of it.

FigureSettings dataclass

FigureSettings(*, formats=('svg',), savefig_dpi=None)

Bases: FigureSettings

The figures of the simulation experiments of a model.

The settings of sbmlsim's figures (sbmlsim.plot.FigureSettings: the resolution, the sizes of the panels, the fonts and the legends, with the defaults of sbmlsim) and what a model adds: the formats of the figures and the resolution of the static images. A model splits them into the settings of its figures (sbmlsim_settings, the figure_settings of the project) and the files of its figures (outputs, the figures of the project).

The fields of sbmlsim can be given by position, as in sbmlsim; formats and savefig_dpi only by keyword.

Attributes:

Name Type Description
formats Sequence[str]

the formats of the figures, images drawn by the renderer of sbmlsim-core (svg, png); SVG only by default, a run draws PNG on request (--formats svg,png). The spec of a figure (json) is written beside it in any case.

savefig_dpi float | None

the resolution of the PNG images, None keeps the fig_dpi of the settings of each figure. It does not apply to SVG.

sbmlsim_settings

sbmlsim_settings()

Get the settings of sbmlsim, without the formats and the resolution.

A plain sbmlsim.plot.FigureSettings, which sbmlsim compares with its defaults (a run with the defaults sets nothing).

outputs

outputs()

Get the files of the figures: the formats and the resolution.

ExtraAction dataclass

ExtraAction(*, name, help, function, in_all=False)

An action a model adds to its run command, e.g. analyses of its own.

All fields are keyword-only.

Attributes:

Name Type Description
name str

the value of --action.

help str

what the action does, shown by --help.

function str

reference to the function which runs the action; it is called with the model, the parsed arguments and the directory of the results. The arguments have the settings of the simulator of the run as simulator (sbmlsim.project.SimulatorSettings, those of the model) and the processes of --workers as n_workers, which an action that simulates passes on, e.g. model.run_experiments(..., simulator=options.simulator, n_workers=options.n_workers) or options.simulator.simulator(options.n_workers).

in_all bool

run the action with --action all, after the simulations.

ExtraActionFunction dataclass

ExtraActionFunction(reference)

An extra action of a model as the function of an action of its project.

The Project.actions of a model are its extra actions, so the kind action of python -m sbmlsim.run runs them as well (see sbmlsim.project.ActionFunction): the arguments of the spec are the options of the action, over the defaults of the options of the model (PKPDModel.extra_arguments), and the action gets the settings of the simulator of the model as options.simulator, the processes of the run as options.n_workers, no draws (options.uncertainty is None) and the output of the run as its directory of the results.

Attributes:

Name Type Description
reference str

the reference to the function of the action, ExtraAction.function.

PKPDModel

PKPDModel(**data)

Bases: Project

A PBPK/PD model: what the commands, the verification and the tests need.

A sbmlsim.project.Project with what pkpdsim adds: the COMBINE archive, the builds per sex, the adopted fits, the directories of the results below the package and the actions of the run command. The keywords of the model packages are mapped onto the fields of the project when a model is created from keywords:

  • path: root, the directory of the model package, the base path of its experiments (the property path).
  • model_path, female_model_path: the variants male and female, the male build first: the model of every experiment and every caller which does not ask for a sex. The builds have the same ids, so a fit applies one parameter set to both, see model_paths.
  • data_paths: data_path, the directories of the study data (the property data_paths).
  • fit_definitions: fits, the reference to the fit problems by name (sbmlsim.fit.cli.FitDefinition).
  • fits as a sequence of fit ids: adopted_ids.
  • figures as a FigureSettings: figure_settings and figures (FigureSettings.sbmlsim_settings, FigureSettings.outputs).

The release settings (release) are a PKPDReleaseSettings, the release settings of sbmlsim with the framework, the core libraries and the release hook of pkpdsim as defaults, and archive_path is their archive.

A keyword and its field are not given together. A model is a frozen pydantic model: replace gives a changed copy, by the names of the fields.

Everything which compiles a model of the model does it at the optimization level of its simulator settings; a model which names no level is created with DEFAULT_JIT_OPTIMIZATION, the level of pkpdsim, unless JIT_OPTIMIZATION_VARIABLE names one.

Attributes:

Name Type Description
archive_path Path | None

the COMBINE archive the factory creates, linked by the factory action; the archive of the release (ReleaseSettings.archive), inside of the root of a released model.

results_path Path | None

the directory of the results, <root>/results by default.

reduced_selections bool

simulate only the selections the experiments need.

save_results bool

store the results of the simulations by default.

fit_identifiability bool

the fit command analyses the identifiability of the fitted parameters by default.

extra_actions tuple[ExtraAction, ...]

actions the model adds to its run command, also the actions of its project (ExtraActionFunction), which the kind action of python -m sbmlsim.run runs.

extra_arguments str | None

reference to a function which adds the options of the extra actions to the parser of the run command.

adopted_ids tuple[str, ...]

the ids of the adopted fits, whose records in fits_dir give the fitted values and their uncertainty, see sbmlsim.fit.records; one fit per fit subset.

credibility_settings CredibilitySettings

the credibility settings of the project, CREDIBILITY_SETTINGS (the metadata, the domain, the evidence extensions and the battery hook of pkpdsim) by default.

pipeline Any

the pipeline of the model, its commands in order, which run_pipeline runs: a pkpdsim.pipeline.Pipeline or a reference module:attribute to it, relative to the package if it starts with a dot (.pipeline:PIPELINE), see load_pipeline.

Create a model from the keywords of a model package or the fields.

The keywords of the model packages are mapped onto the fields of the project, see the class.

Raises:

Type Description
ValidationError

for invalid keywords or fields.

path property

path

The directory of the model package, root.

data_paths property

data_paths

The directories of the study data, data_path as a tuple.

model_paths property

model_paths

The builds of the model by sex, the male build first.

A model is built for one sex (the reference values of the PBPK library are those of a sex, pkpdsim.pbpk.PBPKDefinition.sex); a model with a female build has both, a model without one the male build only. The builds have the same ids, so the changes of an experiment and the parameters of a fit apply to both.

female_model_path property

female_model_path

The female build, None for a model without one.

results_dir property

results_dir

The directory of the results.

simulation_dir property

simulation_dir

The directory of the results of the simulation experiments.

uncertainty_dir property

uncertainty_dir

The directory of the results of the experiments over the draws of the fits.

fit_dir property

fit_dir

The directory of the results of the fits.

report_dir property

report_dir

The directory of the reports of stored parameters.

identifiability_dir property

identifiability_dir

The directory of the identifiability analyses of stored parameters.

credibility_dir property

credibility_dir

The directory of the credibility assessments.

default_app

default_app(sbml)

Get the default web app of the model: its doses, diseases and scans.

See pkpdsim.webapp: pages of small panels of the readouts its experiments compare with data, under the knobs of its doses and dosing protocols, of the diseases it is studied in and of its other scans, and the variants of the model.

Parameters:

Name Type Description Default
sbml Path

the SBML of the default variant, which the factory just wrote.

required

Returns:

Type Description
tuple[dict[str, Panel], App | None]

The panels by their id and the app.

default_variant_panels

default_variant_panels(panels, variants)

Get the default panels of the builds of the other sexes.

See pkpdsim.webapp.default_variant_panels: the classes of a renal impairment at the healthy GFR of the build of each sex.

Parameters:

Name Type Description Default
panels Mapping[str, Panel]

the panels of default_app by their id.

required
variants Mapping[str, Path]

the SBML file of every variant, the default first.

required

Returns:

Type Description
dict[str, dict[str, Panel]]

The panels of a variant by its name and the id of the panel, only

dict[str, dict[str, Panel]]

the panels which differ.

replace

replace(**changes)

Get a copy of the model with other fields, validated again.

See sbmlsim.project.Project.replace. A FigureSettings as figures replaces the settings of the figures as well, figure_settings. A copy with other builds (variants) or another root derives the directory of the factory and the folder of the adopted fits again, unless they are given or were not the derived ones: the directory of the male build and fits next to the directory of the factory.

Raises:

Type Description
ValidationError

for invalid fields.

model_path_of

model_path_of(sex)

Get the build of a sex.

Parameters:

Name Type Description Default
sex Sex | str

the sex.

required

Returns:

Type Description
Path

The SBML model of the sex.

Raises:

Type Description
ValueError

if the model has no build of the sex.

load_roadrunner

load_roadrunner(variant=None)

Load the build of a sex in roadrunner at the level of the model.

See sbmlsim.project.Project.load_roadrunner: the loader of the tests of a model.

Parameters:

Name Type Description Default
variant str | None

the sex of the build (pkpdsim.sex.Sex), the male build without.

None

Returns:

Type Description
RoadRunner

The roadrunner instance.

Raises:

Type Description
ValueError

if the model has no build of the sex.

adopted_fits

adopted_fits()

Get the adopted fits by their subset, those of adopted_ids.

Returns:

Type Description
dict[str, str]

The fit id by subset; the adopted fits of the file of the project

dict[str, str]

for a model without adopted_ids.

Raises:

Type Description
ValueError

if an adopted fit has no id of a fit, or two adopted fits are fits of one subset.

load_pipeline

load_pipeline()

Get the pipeline of the model, resolved.

Raises:

Type Description
ValueError

if the model has no pipeline.

TypeError

if the pipeline is no pkpdsim.pipeline.Pipeline.

load_credibility

load_credibility()

Get the declaration of the contexts of use.

Raises:

Type Description
ValueError

if the model has no declaration.

TypeError

if the reference is no Credibility.

run_experiments

run_experiments(
    experiment_classes,
    output_dir,
    *,
    save_results=None,
    show_report=False,
    figure_formats=None,
    simulator=None,
    n_workers=None,
    scan_workers=RUN_WORKERS,
    uncertainty=None,
)

Run simulation experiments and create their report.

The run of the model packages: Project.run_experiments with the settings of the model, which returns the directory of the results and resolves a relative directory below the results of the model. A run with a failed experiment or figure raises sbmlsim.experiment.ExperimentRunError after its report is written. The messages of the run are shown on the rich console.

Parameters:

Name Type Description Default
experiment_classes type[SimulationExperiment] | Sequence[type[SimulationExperiment]]

the experiment class or classes to run.

required
output_dir str | Path

the directory of the results; a relative directory is below simulation_dir, or uncertainty_dir with uncertainty.

required
save_results bool | None

store the results of the simulations, save_results of the model by default.

None
show_report bool

open the report in a web browser.

False
figure_formats Sequence[str] | None

the formats of the figures, those of the model by default or when empty.

None
simulator SimulatorSettings | None

the settings of the simulator, those of the model by default.

None
n_workers int | None

the processes of the pool of the experiments, every experiment with its scans in its worker; by default a worker per four experiments, at most one per CPU, and serially below (None), 1 runs every experiment in this process, see Project.run_experiments. A pool needs the guard if __name__ == "__main__": in the script of the run.

None
scan_workers int | None

the processes of the scans of an experiment, serially by default (RUN_WORKERS); None takes n_workers, the processes of the whole run as sbmlsim counts them.

RUN_WORKERS
uncertainty UncertaintySettings | None

run the experiments over the draws of the fitted parameters of the model (sbmlsim.experiment.draws), None runs them once.

None

Returns:

Type Description
Path

The directory of the results.

Raises:

Type Description
TypeError

if figure_formats is a single string.

ExperimentRunError

after the report is written, if an experiment or a figure failed.

run_simulation_experiments

run_simulation_experiments(
    experiment_classes,
    output_dir,
    *,
    show_report=False,
    figure_formats=None,
    simulator=None,
    n_workers=None,
    scan_workers=RUN_WORKERS,
    uncertainty=None,
)

Run simulation experiments and collect their figures.

run_experiments which copies the static figures of every experiment into sbmlsim.project.FIGURES_DIR of the output directory, also those of a run with failures, before it raises.

Parameters:

Name Type Description Default
experiment_classes Sequence[type[SimulationExperiment]]

the experiments to run.

required
output_dir str | Path

the directory of the results, see run_experiments.

required
show_report bool

open the report in a web browser.

False
figure_formats Sequence[str] | None

the formats of the figures, those of the model by default.

None
simulator SimulatorSettings | None

the settings of the simulator, those of the model by default.

None
n_workers int | None

the processes of the experiments, see run_experiments.

None
scan_workers int | None

the processes of the scans, see run_experiments.

RUN_WORKERS
uncertainty UncertaintySettings | None

run the experiments over the draws of the fitted parameters, see run_experiments.

None

Returns:

Type Description
Path

The directory of the collected figures.

Raises:

Type Description
ExperimentRunError

after the figures are collected, if an experiment or a figure failed.

sex

The sex of a model build.

A model is built for one sex: the PBPK library takes the reference values of the sex when it builds a model (pkpdsim.pbpk.PBPKDefinition.sex, the ICRP 89 anatomy, the hematocrit, the partition coefficients, the GFR), so a study of women runs on a female build and a study of men on a male one. The model object holds a build per sex (pkpdsim.model.PKPDModel.model_paths, the male build the default) and an experiment chooses one (pkpdsim.experiment.PKPDSimulationExperiment.sex).

The module imports nothing else of pkpdsim, so that the model object and the library (which imports no simulator) share the enum.

Sex

Bases: StrEnum

The sex of a model build, the sex of its reference individual.