Skip to content

cli

The commands of the models, wrappers of the run layer of sbmlsim.

pkpdsim installs seven console scripts which take the dotted name of a model package first and the options of the command for that model after it (main, resolve_model):

run_simulation pkdb_models.models.hctz --action factory
run_simulation pkdb_models.models.hctz --action list_experiments
run_simulation pkdb_models.models.hctz --action simulate --experiments "misc,Anderson1961" --open
run_simulation pkdb_models.models.hctz --action all --results-dir /path/to/results
run_simulation pkdb_models.models.hctz --action simulate --experiments all --uncertainty --draws 100
run_fit pkdb_models.models.hctz --subset PK --identifiability
run_fit pkdb_models.models.hctz --adopt PK_<fit id>
run_report pkdb_models.models.hctz --subset PK results/fit/PK_<fit id>/parameters.json
run_report pkdb_models.models.hctz --adopted
run_identifiability pkdb_models.models.hctz --subset PK results/fit/PK_<fit id>/parameters.json
run_credibility pkdb_models.models.hctz
run_credibility pkdb_models.models.hctz --from-json results/credibility/credibility.json -o /tmp/credibility
run_sensitivity pkdb_models.models.hctz
run_sensitivity pkdb_models.models.hctz sampling --workers 8
run_pipeline pkdb_models.models.hctz --list
run_pipeline pkdb_models.models.hctz --cores 12
run_pipeline pkdb_models.models.hctz --jobs 1

The package is imported and its model object MODEL gives everything which differs between two models; a model package registers no commands of its own.

The commands run_simulation, run_fit, run_report and run_identifiability keep their options and defaults and translate them into the spec of a kind of the run layer of sbmlsim (python -m sbmlsim.run, see sbmlsim.run), which they run with sbmlsim.run.execute on the model object of the package (the project <package>:MODEL, project_reference). A kind runs into an empty output directory and writes the records of the run into it (run.json, spec.json, summary.json, progress.jsonl), so a command runs it into the folder of its result below the directory the command wrote into before. The run writes into a staging directory next to the folder, and an earlier result is replaced only after the run succeeded, a failed run or plan leaves it untouched (_staged); an output which is an input of the command (the records of a fit, the adopted fits, the factory, a build), contains one or lies inside one is refused before anything happens (_check_out):

  • run_simulation --action simulate (run_command) runs the kind simulate into the folder of the group (simulation/<group> of the results), any other selection into simulation/custom_selection; the figures are collected into _figures of it (sbmlsim.project.collect_figure_files). --uncertainty runs the experiments over draws of the fitted parameters of the model (the field uncertainty of the spec) into simulation_uncertainty/<group>.
  • run_fit (fit_command) runs the kind fit into <output_dir>/<fit id>, with the report of the fit, the identifiability of its parameters with --identifiability (the default of a model with fit_identifiability) and the adopted fits of the other subsets recorded (record_adopted); the records of the fit, which the kind writes into its folder <fit id>, are moved into the output of the run, so they are where they were. A fit which fails keeps the records it wrote in <output_dir>/<fit id>. run_fit <package> --adopt <fit id> checks the id, runs the kind adopt into a temporary output and copies the record into the adopted fits of the model (sbmlsim.fit.records.adopt_fit); nothing is written into the fit.
  • run_report (report_command) runs the kind report on the records next to a parameters.json into <output_dir>/<name> (report by default); run_report <package> --adopted runs it with adopted for every adopted fit into <output_dir>/<fit id>.
  • run_identifiability (identifiability_command) runs the kind identifiability into analysis/ of <output_dir>/<name> (identifiability by default) and the kind report of the analysis into it, the report in the folder itself and the records of its run in report/.

Three parts call the library of sbmlsim directly instead: --action factory builds the models of the package in place with create_models, as before; --action factory --rebuild runs the kind factory, which builds them into factory/ of the results and reports the outputs which differ. The extra actions of a model write into the results of the model, which are no empty output; they are the actions of the project as well (pkpdsim.model.ExtraActionFunction), which the kind action runs. run_fit --strategy single fits every experiment on its own, which no kind does, and run_report and run_identifiability of several parameter files or of a file without the records of its fit next to it use sbmlsim.fit.cli.report_cli and identifiability_cli.

run_credibility (credibility_command) runs the kind credibility, which assesses the contexts of use of the model against its adopted fits (sbmlsim.credibility.assess, with the evidence extensions and the PK battery of pkpdsim.credibility) and writes the record, the tables and the report (sbmlsim.credibility.report) into credibility/ of its output; the command moves them up into its --output_dir, --from-json is the input record (the outputs written again from a written record) and the lock of --lock, which the kind writes into its output, is moved into the folder of the model, where the assessment compares the evidence with it. All write into the results of the model unless --output_dir is given.

run_sensitivity (sensitivity_command) runs the kind sensitivity on a sensitivity analysis of the model over the groups of its simulations (sensitivities of the model object, a SensitivityRunner of sbmlsim.sensitivity.runner or a reference to one) into the folder of its name in the results (sensitivity/), the table of its parameters and a folder per analysis next to the records of the run.

run_pipeline (pipeline_command) runs the pipeline of the model (pipeline of the model object, see pkpdsim.pipeline): the commands of its steps, each in a process of its own and side by side as far as their steps after, their cores and their memory allow, with the times, the cores and the memory of every step and the output of its process in a folder of the run below pipeline/ of the results.

--profile anywhere after the model package runs a command under the sampling profiler of python 3.15 and writes its flame graph and statistics into profiling/<command>_<time>/ of the results of the model, see sbmlsim.run.profiling; the commands also run as python -m pkpdsim.cli <command> <package> ....

--progress MODE anywhere after the model package says how the progress of the runs of a command is shown: auto (the default: the bars of a terminal, a line of the log every 30 s otherwise), bar, log or none, see sbmlsim.progress. The option sets SBMLSIM_PROGRESS, so every run of the command and every process it starts reads it, and the events of the progress are written into the progress.jsonl of a run whatever is chosen: the experiments of a simulation, the points of a scan, the runs of a fit, the scans of an identifiability and the figures of a report then say where they are, how long they still take and what they use of the machine.

DOTTED_PACKAGE module-attribute

DOTTED_PACKAGE = re.compile(
    "^[A-Za-z_]\\w*(\\.[A-Za-z_]\\w*)*$"
)

The form of a dotted package, e.g. pkdb_models.models.hctz.

COMMANDS module-attribute

COMMANDS = {
    command.name: command
    for command in (
        Command(
            name="run_simulation",
            description="Run the model factory, the simulation experiments and the extra actions of a model.",
            function=run_command,
            fits=False,
        ),
        Command(
            name="run_fit",
            description="Fit the parameters of a model and analyse their identifiability, or adopt a finished fit.",
            function=fit_command,
            fits=True,
        ),
        Command(
            name="run_report",
            description="Report stored parameters of a model.",
            function=report_command,
            fits=True,
        ),
        Command(
            name="run_identifiability",
            description="Analyse the identifiability of stored parameters of a model.",
            function=identifiability_command,
            fits=True,
        ),
        Command(
            name="run_credibility",
            description="Assess the contexts of use of a model against its adopted fits.",
            function=credibility_command,
            fits=True,
        ),
        Command(
            name="run_sensitivity",
            description="Run the sensitivity analyses of a model over the groups of its simulations.",
            function=sensitivity_command,
            fits=False,
        ),
        Command(
            name="run_pipeline",
            description="Run the pipeline of a model: its commands, side by side where they may, timed and logged.",
            function=pipeline_command,
            fits=False,
        ),
    )
}

The commands by the names of their console scripts.

Action

Bases: StrEnum

The actions of the run command, pkpdsim.model.RUN_ACTIONS.

ModelNotFoundError

Bases: LookupError

A package which is not found or is no model package.

Command dataclass

Command(name, description, function, fits)

A command of the models, a console script of pkpdsim.

Attributes:

Name Type Description
name str

the name of the console script.

description str

what the command does, the description of its help.

function Callable[[PKPDModel, Sequence[str]], object]

the command as a function of the model and the arguments after the model package.

fits bool

the command needs the fit definitions of the model.

parse_formats

parse_formats(text)

Get the figure formats of a comma separated list.

Parameters:

Name Type Description Default
text str

the formats, e.g. "svg, PNG".

required

Returns:

Type Description
list[str]

The formats in lower case, without blanks and empty entries.

parse_workers

parse_workers(text)

Get the processes of the option --workers which an extra action gets.

Parameters:

Name Type Description Default
text str

model for the default of a model (pkpdsim.model.RUN_WORKERS), auto for the choice of sbmlsim (None, see sbmlsim.project.Project.run_experiments), or a positive number.

required

Returns:

Type Description
int | None

The number of processes, None for the choice of sbmlsim.

Raises:

Type Description
ValueError

for any other text.

run_workers

run_workers(text, n_experiments)

Get the workers of a run of experiments from --workers.

Parameters:

Name Type Description Default
text str

the option, see parse_workers.

required
n_experiments int

the number of experiments of the run.

required

Returns:

Type Description
int | None

The processes of the kind simulate: for model, the default of a

int | None

model, a worker per four experiments, at most one per CPU, serially

int | None

below (the rule of sbmlsim.experiment.ExperimentRunner), every

int | None

experiment with its scans serially in its worker; the number as given;

int | None

None for auto, the choice of sbmlsim, which the kind cannot take

int | None

(it resolves its workers), so the experiments run with

int | None

Project.run_experiments itself.

Raises:

Type Description
ValueError

for an invalid option, see parse_workers.

resolve_experiments

resolve_experiments(registry, names)

Resolve the names of groups and experiments to the experiment classes.

Parameters:

Name Type Description Default
registry Mapping[str, Sequence[type[SimulationExperiment]]]

the groups of experiments of a model.

required
names Sequence[str]

names of groups or of experiment classes.

required

Returns:

Type Description
list[type[SimulationExperiment]]

The experiment classes, every class once in the order of the names, and

list[str]

the names which are neither a group nor an experiment.

project_reference

project_reference(model)

Get the reference of a model as the project of the run layer.

The kinds of sbmlsim.run load their project by its reference, so a command runs the model object of the package, <package>:MODEL.

Parameters:

Name Type Description Default
model PKPDModel

the model.

required

Returns:

Type Description
str

The reference <package>:MODEL.

Raises:

Type Description
ValueError

if the reference does not give the model, e.g. for a copy of the model object with other fields.

run_parser

run_parser(model)

Get the parser of the run command of a model.

run_command

run_command(model, args=None)

Run the action of the command line of run_simulation.

Parameters:

Name Type Description Default
model PKPDModel

the model, the model object of its package.

required
args Sequence[str] | None

the command line arguments, sys.argv[1:] by default.

None

A failure of the simulations or a rebuild which differs from the models of the package does not stop the command: the remaining actions run, then the failures are printed and the command exits with 1. An extra action gets the settings of the simulator of the model as options.simulator, the processes of --workers as options.n_workers (parse_workers), and the settings of --uncertainty as options.uncertainty (None without).

with_output_dir

with_output_dir(args, output_dir)

Add the output directory of the model to command line arguments.

The commands of sbmlsim.fit.cli write into the working directory by default, a model writes into its results instead.

Parameters:

Name Type Description Default
args Sequence[str]

command line arguments without the program name.

required
output_dir Path

output directory used if the arguments do not name one.

required

Returns:

Type Description
list[str]

The arguments with the output directory in front of them. argparse keeps

list[str]

the last value of an option, so an output directory in the arguments,

list[str]

also an abbreviation like --out, wins.

fit_parser

fit_parser(model, definitions)

Get the parser of the fit command of a model.

The fit arguments are those of sbmlsim.fit.cli.fit_cli, the identifiability arguments are the settings of the profile likelihood which follows the fit.

analyse_identifiability

analyse_identifiability(run, options)

Analyse the identifiability of the parameters of a finished fit.

The profile likelihood is the exact answer and the Fisher information the cheap local one next to it; both are computed around the best parameter set of the fit and end up in the identifiability section of its report.

Parameters:

Name Type Description Default
run FitRun

finished fit whose best parameter set is analysed.

required
options Namespace

command line arguments of fit_parser.

required

Returns:

Type Description
tuple[IdentifiabilityResult | None, FisherInformation | None]

The profiles and the Fisher information, None for what was skipped.

fit_command

fit_command(model, args=None)

Run a fit of a model and analyse the identifiability of its parameters.

The experiments of the problem are fitted together with the kind fit of the run layer into <output_dir>/<fit id>, every experiment on its own (--strategy single) with sbmlsim.fit.cli.run_fit; --adopt adopts a finished fit with the kind adopt.

Parameters:

Name Type Description Default
model PKPDModel

the model, the model object of its package.

required
args Sequence[str] | None

command line arguments, sys.argv[1:] by default.

None

Returns:

Type Description
dict[str, Path]

The records of the finished fits by their ids, none for --adopt and

dict[str, Path]

--cross-validation.

report_parser

report_parser(model, definitions)

Get the parser of the report command of a model, of parameter files.

report_command

report_command(model, args=None)

Report stored parameter sets of a model.

The kind report writes the report of the records next to a parameters.json into <output_dir>/<name>, report by default, with the context of the model, which names the fixed adopted fits of the record (sbmlsim.fit.cli.records_context). Several parameter files, or a file which is no parameters.json of the records of a fit, are reported with sbmlsim.fit.cli.report_cli. With --adopted the report of every adopted fit of the model is written from its record, see _adopted_reports.

Parameters:

Name Type Description Default
model PKPDModel

the model, the model object of its package.

required
args Sequence[str] | None

command line arguments, sys.argv[1:] by default.

None

Returns:

Type Description
Path

The directory of the report, with --adopted the directory of the

Path

reports.

identifiability_parser

identifiability_parser(model, definitions)

Get the parser of the identifiability command of a model.

The options of sbmlsim.fit.cli.identifiability_cli.

identifiability_command

identifiability_command(model, args=None)

Analyse the identifiability of stored parameters of a model.

The kind identifiability profiles the parameters of the records next to a parameters.json into analysis/ of <output_dir>/<name> (identifiability by default), with the Fisher information unless --no-fisher is given, and the kind report writes the report of the analysis into the folder, the records of its run in report/. A file which is no parameters.json of the records of a fit is analysed with sbmlsim.fit.cli.identifiability_cli.

Parameters:

Name Type Description Default
model PKPDModel

the model, the model object of its package.

required
args Sequence[str] | None

command line arguments, sys.argv[1:] by default.

None

Returns:

Type Description
Path

The directory of the report.

credibility_command

credibility_command(model, args=None)

Assess the contexts of use of a model against its adopted fits.

Runs the kind credibility of sbmlsim.run (sbmlsim.credibility.assess with the evidence extensions and the PK battery of the model, see pkpdsim.model.CREDIBILITY_SETTINGS) into the output directory through a staged swap: the outputs of the kind (credibility/ of its output) are moved up into the directory, next to the records of the run. The adopted fits are evaluated on the current model and data, --from-json instead reads a written record (the input record) without evaluating a fit. The decisive predictions are verified (sbmlsim.credibility.verification) unless --no-verification is given. --lock writes the lock of the evidence, which the kind writes into its output, into the folder of the model. --source-commit records the commit of the source repository in the provenance, which a release passes, since it assesses the model in its release repository. The outputs replace an earlier assessment in the directory, a directory with other files is refused; an error of the declaration, the evidence, the records or the directory is printed and exits with 1.

Parameters:

Name Type Description Default
model PKPDModel

the model.

required
args Sequence[str] | None

command line arguments, sys.argv[1:] by default.

None

Returns:

Type Description
Path

The directory of the outputs.

sensitivity_parser

sensitivity_parser(model)

Get the parser of the sensitivity command of a model.

sensitivity_command

sensitivity_command(model, args=None)

Run the sensitivity analyses of a model.

Runs the kind sensitivity of sbmlsim.run on the sensitivity analysis of the model object (Project.sensitivities, by --name) into the output directory through a staged swap: the table of the parameters and a folder per analysis, next to the records of the run.

Parameters:

Name Type Description Default
model PKPDModel

the model, the model object of its package.

required
args Sequence[str] | None

command line arguments, sys.argv[1:] by default.

None

Returns:

Type Description
Path

The directory of the results.

pipeline_parser

pipeline_parser(model, steps)

Get the parser of the pipeline command of a model.

pipeline_command

pipeline_command(model, args=None)

Run the pipeline of a model, see pkpdsim.pipeline.

Parameters:

Name Type Description Default
model PKPDModel

the model, the model object of its package.

required
args Sequence[str] | None

command line arguments, sys.argv[1:] by default.

None

Returns:

Type Description
Path

The folder of the run.

Raises:

Type Description
SystemExit

with 1 if a step failed, with 130 on an interrupt.

resolve_model

resolve_model(package)

Get the model object MODEL of a model package.

Parameters:

Name Type Description Default
package str

the dotted name of the model package, e.g. pkdb_models.models.hctz.

required

Returns:

Type Description
PKPDModel

The model object of the package.

Raises:

Type Description
ModelNotFoundError

if the name is no dotted package, the package is not found, it has no MODEL, its MODEL is no PKPDModel or belongs to another package. Any other error of the import of the package, e.g. a missing module which the package imports, is raised as it is, with its traceback.

profile_dir

profile_dir(model, command)

Get a new folder of the profile of a command in the results of a model.

Parameters:

Name Type Description Default
model PKPDModel

the model.

required
command str

the name of the command, a key of COMMANDS.

required

Returns:

Type Description
Path

profiling/<command>_<time> of the results of the model, not created.

command_parser

command_parser(command)

Get the parser of a command before its model is known.

The options of a command depend on the model, so this parser only prints the general usage and the errors of the model package.

main

main(name, argv=None)

Run a command with the model package as the first argument.

The model is resolved first, since the options of the command depend on it; the arguments after the package are the arguments of the command.

Parameters:

Name Type Description Default
name str

the name of the command, a key of COMMANDS.

required
argv Sequence[str] | None

the command line arguments, sys.argv[1:] by default.

None

run_simulation_main

run_simulation_main(argv=None)

The console script run_simulation, see run_command.

run_fit_main

run_fit_main(argv=None)

The console script run_fit, see fit_command.

Returns nothing: a console script exits with the return value of the function it calls, and 1 for anything which is not None or an int.

run_report_main

run_report_main(argv=None)

The console script run_report, see report_command.

run_identifiability_main

run_identifiability_main(argv=None)

The console script run_identifiability, see identifiability_command.

run_credibility_main

run_credibility_main(argv=None)

The console script run_credibility, see credibility_command.

run_sensitivity_main

run_sensitivity_main(argv=None)

The console script run_sensitivity, see sensitivity_command.

run_pipeline_main

run_pipeline_main(argv=None)

The console script run_pipeline, see pipeline_command.