Skip to content

sbml

The SBML writing kit, the layer below the libraries of pkpdsim: the units of a model, the numbers with their provenance, the parameters and the observers these numbers become, the initial assignment of a constant with the taxon of a build, and the container which collects the objects of a part of a model. The PBPK library, the zonation engine and the machine perfusion library write SBML with these pieces, so they share them without importing each other; the numbers themselves are the parameter database.

Two names of the kit are the names a library has always called the object by and keep working: PU of the machine perfusion library (perfusion) is pkpdsim.sbml.units.OrganUnits, and CoreObjects of its core is pkpdsim.sbml.objects.SbmlObjects.

The SBML writing kit, the layer below the libraries of pkpdsim.

The PBPK library (pkpdsim.pbpk), the zonation engine (pkpdsim.zonation) and the machine perfusion library (pkpdsim.perfusion) write SBML with the same pieces: the units of a model (pkpdsim.sbml.units), the numbers with their provenance (pkpdsim.sbml.values), the parameters and the observers these numbers become (pkpdsim.sbml.parameters), the initial assignment of a constant and the taxon of a build (pkpdsim.sbml.annotation), the domain of a quantity (pkpdsim.sbml.domains) and the container which collects the objects of a part of a model (pkpdsim.sbml.objects).

The kit is the lower layer: it imports pkpdsim.database (the parameter database, for the citation of a source) and no library of pkpdsim, so the libraries share these pieces without importing each other. The layering is pinned by tests/zonation/test_limits.py, which walks the four packages and reads the imports of every module.

import pkpdsim.sbml imports none of the modules.

units

The units of the models of pkpdsim.

Time is in min, substance in mmole, volume in litre and area in m2, the model_units of the models (MODEL_UNITS). Every unit of U is a unit definition of every model of the PBPK library; a unit outside U (pkpdsim.pbpk.units.EXTRA_UNITS) is one only of a model which uses it.

OrganUnits adds the units of a model of an organ which U lacks: the volumes and the flow of a circuit as an experimenter gives them (ml, ml/min), the mass of an organ (g), the temperature, the oxygen partial pressure (kPa), the bile and the urine (µl, the bile per mass µl/g), the GFR per mass (ml/min/g, l/min/kg) and the catalytic activity of the enzymes (µkat/l = µmol/(l*s)). Every unit of it is a unit definition of every machine perfusion model and of every model of the zonation engine (pkpdsim.zonation.builder.ZonationUnits).

PU is the same class under the name the machine perfusion library has always used for it (pkpdsim.perfusion.units, which re-exports that name): an alias, so that library and its reference models are untouched by the move into this layer.

SBML Level 3 has no degree Celsius: the temperature T is in degC, a unit of the size of a kelvin, so a temperature difference is right and the absolute temperature is T + 273.15 degC, which the formulas write out.

MODEL_UNITS module-attribute

MODEL_UNITS = ModelUnits(
    time=U.min,
    extent=U.mmole,
    substance=U.mmole,
    length=U.meter,
    area=U.m2,
    volume=U.litre,
)

The model_units of every model of pkpdsim: time in min, substance and extent in mmole, length in m, area in m2 and volume in litre.

Every model of every library of pkpdsim is created with these, so that the math of one model can be read in another and a quantity needs no conversion when it crosses a boundary (a perfused organ in a circuit, a zone of a sinusoid in a liver, an organ in a body).

PU module-attribute

The unit definitions of a perfusion model, OrganUnits under the name of the machine perfusion library (pkpdsim.perfusion.units, which re-exports it). The same class, so a model built with either is the same model.

U

Bases: Units

UnitDefinitions.

OrganUnits

Bases: U

The unit definitions of a model of an organ: those of U and more.

values

Values with provenance.

A number of a model is a Value: the number, its unit and where it comes from (a key of database/sources.toml, see pkpdsim.pbpk.data). A fitted value is a Value as well, from the keyword arguments of a fit record. A value which is tied to other parameters (a metabolite with the Kp of its parent, a fixed ratio) is a Formula over ids of the top model, written as the initial assignment of its parameter. A constant which no field of a definition holds is a ModelParameter with such a value.

SID module-attribute

SID = re.compile('[A-Za-z_][A-Za-z0-9_]*')

An SBML SId, the ids of a definition (substances, processes, parameters).

CHOICE module-attribute

CHOICE = 'ModelChoice'

The source of a number the library chooses where no source gives one.

A key of database/sources.toml, which cite() resolves: a number which no publication backs says so instead of leaving its provenance empty or borrowing the key of a neighbouring row.

DIFFUSION_UNITS module-attribute

DIFFUSION_UNITS = {
    "meter^2/second": 1.0,
    "m^2/s": 1.0,
    "cm^2/second": 0.0001,
    "cm^2/s": 0.0001,
    "mm^2/second": 1e-06,
    "micrometer^2/second": 1e-12,
}

The units a diffusion coefficient may have, the factor to m**2/s, by the definition of the unit (unit_text).

Q

Q = float | Value | Formula

A number, plain or with unit and provenance, or a formula.

Value dataclass

Value(
    value, unit, source=None, notes=None, uncertainties=None
)

A number with unit and provenance.

Attributes:

Name Type Description
value float

the number.

unit UnitType

the unit of the number.

source str | None

the key of the source in database/sources.toml, fit for a fitted value.

notes str | None

a remark on the value.

uncertainties list[Uncertainty] | None

the uncertainty of a fitted value.

Formula dataclass

Formula(expression, unit, notes=None)

A value tied to other parameters: a formula over ids of the top model.

The parameter of the value gets the formula as its initial assignment, as the covariates do: a change of a parameter it reads before the simulation is followed, and an id of PBPKDefinition.dynamic keeps it an assignment rule. The ids of the formula are constant parameters and compartments of the top model (the parameters of the substances, the covariates, the physiology), also for a value of an organ, which reads them through input ports under the same ids; the build refuses an id the top model does not have and the species and observers (they change during the simulation). The unit is the unit of the parameter, it is not inferred from the formula.

One exception reads the ids of an organ instead: the steady state of a custom reaction (CustomReaction.stationary) is a Formula of the organ, as its rate law, over the elements of the organ (Vti, the parameters of the reaction, species of other substances at their start) and constants of the top model; the Formula of a ModelParameter of a custom reaction reads the constants of its organ which are no ids of the top model as well (another parameter of a custom reaction of the organ, Vti); every other Formula (a value, a parameter of the definition, a start of PBPKDefinition.starts, a baseline) reads the ids of the top model only.

Attributes:

Name Type Description
expression str

the formula (SBML L3 infix), e.g. Kp_gli or ftissue_gli * fu_ref_gli / fu_ref_gli_m1.

unit UnitType

the unit of the parameter.

notes str | None

a remark on the value, e.g. why it is tied.

ModelParameter dataclass

ModelParameter(
    sid, value, unit, name=None, notes=None, domain=None
)

A constant parameter which the model package states, with its provenance.

In PBPKDefinition.parameters a constant of the top model which no field of the library holds (a production rate which the baseline of a substance and a custom reaction of the muscle share): the formulas of the definition, its observers and the custom reactions of the organs read it under its id, an organ through an input port under the same id. In CustomReaction.parameters a constant of the organ of the reaction (a fitted constant of the mechanism): a local id of the organ, which the reactions of the organ read, <ORGAN>__<id> in the flat model.

Attributes:

Name Type Description
sid str

the id of the parameter, an id of the top model which is no id of the library, or a local id of the organ.

value Q

the number in unit, a Value (a fitted value with its uncertainty, fitted(FITS.parameter(pid, unit=...))) or a Formula over ids of the top model (its initial assignment); a Value or a Formula is in unit.

unit UnitDefinition

the unit of the parameter.

name str | None

the name of the parameter, the id by default.

notes str | None

the notes of the parameter, e.g. its meaning and source.

domain Domain | None

the interval of values the model accepts for the parameter, which the kind of a model-stated constant cannot know; written as the domain uncertainty of the element and required by the check of the definition.

FrozenMap

FrozenMap(items)

Bases: Mapping[str, V]

A read-only, hashable mapping from an id to a value.

The mapping fields of the frozen dataclasses of a definition (the values per substance of the intestine, PerSubstance; the starts of the species, PBPKDefinition.starts; the regional absorption profiles per substance, whose values are profiles) are kept as this copy: it reads back as a Mapping (ka["a"], .items()), cannot be changed after the validation and is hashed by its items (a dict is unhashable).

Copy the items.

model_choice

model_choice(value, unit, notes)

Get the value of a number the library or a caller chooses.

The number with the source CHOICE: a value a caller hands over, or one the code picks, which has to say that it is a choice and not look sourced.

Parameters:

Name Type Description Default
value float

the number.

required
unit UnitType

its unit.

required
notes str

why the number is what it is, and who chose it.

required

Returns:

Type Description
Value

The value with the source ModelChoice.

derived

derived(value, unit, parts, notes)

Get the value of a number derived from other values, with their provenance.

An aggregate of sourced values (a fraction split by a haematocrit, a sum of flow fractions) is no row of a table and carries the provenance of the rows it rests on: their one source where they agree on it, as a derived row of the tables does, and CHOICE where they differ, since no single source backs the aggregate then. The notes always begin "derived: " and name the derivation, and where the sources differ they name every one of them.

Parameters:

Name Type Description Default
value float

the number.

required
unit UnitType

its unit.

required
parts Iterable[Value]

the values it is derived from, in the order they are read.

required
notes str

the derivation, and the notes of the parts worth carrying.

required

Returns:

Type Description
Value

The derived value.

fitted

fitted(kwargs, source='fit', notes=None)

Get the value of a fitted parameter.

Parameters:

Name Type Description Default
kwargs Mapping[str, Any]

the keyword arguments of AdoptedRecords.parameter(pid, unit=...): value, uncertainties and unit.

required
source str

the provenance of the value.

'fit'
notes str | None

a remark on the value, e.g. the literature of the process; the provenance of the fit is in the uncertainties.

None

Returns:

Type Description
Value

The value with the uncertainty of the fit.

equal_by_value

equal_by_value(a, b)

Compare two parts of a definition by their values.

The objects of sbmlutils which a definition holds (the Uncertainty of a fitted value, a Parameter or Species of a custom reaction) have no equality of their own and compare by identity, so two definitions which take the same fitted value from a fit record (fitted(FITS.parameter(...)) creates new uncertainties at every call) would differ. Here a dataclass is equal by its equality (PDComponent by the id of its model, its inputs and outputs) or by its fields, a mapping by its items, a list or tuple by its elements, and an object which compares by identity by the fields of its __dict__, each by value; every other object (numbers, strings, enum members, a Creator) by ==.

Parameters:

Name Type Description Default
a object

the one object.

required
b object

the other object.

required

Returns:

Type Description
bool

Whether they are equal by value.

as_value

as_value(x, unit)

Get a value of a number, a value or a formula.

Parameters:

Name Type Description Default
x Q

a number, which gets the unit, or a value or a formula, which is returned as it is.

required
unit UnitType

the unit of a number.

required

Returns:

Type Description
Value | Formula

The value or the formula.

number

number(q)

Get the number of a plain float or a Value, None for none or a Formula.

check_positive

check_positive(where, name, q)

Check that a number is positive.

check_unit

check_unit(where, name, q, unit)

Check that a Value is given in the unit of its parameter.

The numbers of a definition are in the units of the model; a Value in another unit (an exchange in l/min where one per litre of organ is meant) would be taken as it is.

check_domain

check_domain(where, name, q, domain)

Check that a number lies in the domain its parameter states.

Parameters:

Name Type Description Default
where str

what the number belongs to, e.g. parameter 'k'.

required
name str

the name of the number, e.g. value.

required
q Q | None

the number, a Value or a Formula; a Formula has no number to check.

required
domain Domain | None

the domain the parameter states, None when it states none.

required

Returns:

Type Description
list[str]

The problems: no domain, or a number it does not admit.

unit_text

unit_text(unit)

The definition of a unit, e.g. 1/min, the id of a unit of a kind (dimensionless).

check_literals

check_literals(obj)

Check the fields of a dataclass which are typed with a Literal.

A definition written in a script or a notebook may give any text: a text which is none of the choices would select no branch of the builder (a route without doses, an erythrocyte mode treated as another), so it raises.

Parameters:

Name Type Description Default
obj object

the dataclass instance.

required

Raises:

Type Description
ValueError

if a field holds none of its choices, naming the field and the allowed values.

parameters

The parameters and the observers every library of pkpdsim writes.

A constant of a model is the parameter of a number with its provenance (value_parameter, whose notes carry the citation of the source and whose distrib uncertainty carries the uncertainty of a fitted value, value_notes); a constant which is tied to other parameters of the model is a FormulaParameter, whose value is the initial assignment of its formula (formula_assignment). An observer is a parameter which an assignment rule sets (observer, RULE): read-only, so a model with observers has the same states as a model without them.

FormulaParameter

FormulaParameter(pid, formula, name, sbo, notes)

Bases: Parameter

A constant parameter whose value is a Formula over ids of the top model.

The parameter has no value; the build gives it the initial assignment of the formula (formula_assignment): the top model in its objects, an organ in its assignments, with an input port for every id of the top model the formula reads (OrganBuilder.model_definition).

Attributes:

Name Type Description
formula

the formula.

Create the parameter of a formula.

formula_assignment

formula_assignment(p)

Get the initial assignment of the parameter of a formula.

value_parameter

value_parameter(pid, q, unit, name, sbo, text='')

Get a constant parameter of a number with its provenance.

Parameters:

Name Type Description Default
pid str

the id of the parameter.

required
q Q

the number, plain (in unit) or a Value with unit and source, or a Formula (a FormulaParameter, the notes name the formula).

required
unit UnitDefinition

the unit of a plain number.

required
name str

the name of the parameter.

required
sbo str

the SBO term.

required
text str

the description of the parameter.

''

Returns:

Type Description
Parameter

The parameter, the citation of the source in the notes and the

Parameter

uncertainty of a fitted value.

with_domain

with_domain(p, domain)

State the domain of a parameter as its domain uncertainty.

Parameters:

Name Type Description Default
p Parameter

the parameter.

required
domain Domain | None

the domain the model states for it, None states none.

required

Returns:

Type Description
Parameter

The parameter, with the uncertainty of the domain after the

Parameter

uncertainties it has (the one of a fitted value).

value_notes

value_notes(v, text='')

Get the notes of a number with its provenance.

Parameters:

Name Type Description Default
v Value

the value.

required
text str

the description of the element which carries the value.

''

Returns:

Type Description
str | None

The description, the notes of the value and the citation of its

str | None

source (Fitted value. for a fit), None without any.

observer

observer(sid, formula, unit, name, sbo)

Get the parameter of an observer and its assignment rule.

objects

The container of the SBML objects of a part of a model.

A generator of a library creates the species, reactions, parameters, rules and assignments of one part of a model and hands them over as one object (SbmlObjects), which adds to the objects of the other parts; the builder puts them into the model. The container is the SBML writing kit's, so that the generators of the libraries hand over the same object: the machine perfusion library knows it as pkpdsim.perfusion.core.CoreObjects.

SbmlObjects dataclass

SbmlObjects(
    species=list(),
    reactions=list(),
    parameters=list(),
    rules=list(),
    rate_rules=list(),
    assignments=list(),
)

The SBML objects of a part of a model.

Attributes:

Name Type Description
species list[Species]

the species.

reactions list[Reaction]

the reactions.

parameters list[Parameter]

the parameters.

rules list[AssignmentRule]

the assignment rules.

rate_rules list[RateRule]

the rate rules.

assignments list[InitialAssignment]

the initial assignments.

annotation

The annotations every library of pkpdsim writes.

The initial assignment of a constant (initial_assignment, the SBO term of a mathematical expression and the name of its target) and the NCBI taxon of the species of a build (TAXA, the BQB.HAS_TAXON of a model).

The annotation of an element by its kind is the PBPK library's (pkpdsim.pbpk.annotation.annotate, which resolves the PBPKO term of a kind at its place through the vocabulary of the library): no other library of pkpdsim annotates by kind, so the table and its resolver stay there.

TAXA module-attribute

TAXA = {
    Species.HUMAN: "taxonomy/9606",
    Species.RAT: "taxonomy/10116",
    Species.MOUSE: "taxonomy/10090",
    Species.PIG: "taxonomy/9823",
}

The NCBI taxon of every species of a build.

initial_assignment

initial_assignment(symbol, formula, unit, name)

Get the initial assignment of a constant, with a name and an SBO term.

Every quantity of the library which depends on parameters only is a constant with an initial assignment; the assignment carries the name of its target and the SBO term of a mathematical expression, so the factory has nothing to hint about.

Parameters:

Name Type Description Default
symbol str

the id of the target.

required
formula str

the formula.

required
unit UnitType

the unit of the formula.

required
name str

the name of the target.

required

Returns:

Type Description
InitialAssignment

The initial assignment.

domains

The writer of the domain contract, see Domains. The reader is the one of sbmlsim, which this module re-exports, so Domain, Values, domain_of and domains are documented in domains of sbmlsim and imported from here unchanged.

The domain of a quantity of a model: the writer of the contract.

A domain is the interval of values the model accepts for a quantity, and the value set inside that interval: a fraction lies in [0, 1], a volume or a rate in [0, inf), a severity in the integers of [0, 3], a switch is 0 or 1. It is written into the SBML as a distrib uncertainty named DOMAIN whose single span is of type range, on the parameter, compartment or species it constrains, with an externalParameter whose definitionURL is the XSD datatype of the value set for an integer or boolean domain; a continuous domain writes none, so a document written before the value sets keeps its meaning. Nothing enforces the value set at run time, it is the contract for the tools which read it.

pkpdsim writes the models, so domain_uncertainty lives here, and the reader of the contract is the one of sbmlsim (sbmlsim.domains), which every tool that samples, scans, fits or checks a model reads it with: sampling.local of sbmlsim clamps the step of a parameter to the domain of its target, and a design must not import pkpdsim. Domain, Values, domain_of and domains are that reader, re-exported here next to the writer, so one contract has one implementation of its reader (#245). The named domains of the library and integers are the vocabulary of the writer and stay here.

A domain is not the uncertainty of a source (pkpdsim.pbpk.data.sbml_uncertainty, a confidence interval around a literature value) and not the distribution of a fit: those are statements in their own uncertainty elements, which coexist with the domain on the same element. The reader finds the domain by its name and its span type and ignores every other uncertainty.

A reader of a model which states no domain learns nothing: domain_of gives None and domains leaves the element out, there is no default. This module imports libsbml, sbmlutils and the reader of sbmlsim, which imports libsbml and the standard library only, no simulator and no library of pkpdsim: it belongs to the layer below them (see pkpdsim.sbml).

integers

integers(lower, upper)

The integer domain of an interval, e.g. a class of 0 to 3.

Parameters:

Name Type Description Default
lower float

Lower bound.

required
upper float

Upper bound.

required

Returns:

Type Description
Domain

The domain which accepts the integers of the interval.

Raises:

Type Description
ValueError

If no integer lies in [lower, upper].

domain_uncertainty

domain_uncertainty(d)

Build the uncertainty which writes a domain.

Parameters:

Name Type Description Default
d Domain

The domain.

required

Returns:

Type Description
Uncertainty

An uncertainty named DOMAIN with one range span and, for an

Uncertainty

integer or boolean domain, one externalParameter whose

Uncertainty

definitionURL is the XSD datatype of the value set. The child

Uncertainty

carries the value 1 although only its URL means something: libsbml

Uncertainty

does not need it, but libAntimony, which the archive of a model loads

Uncertainty

it with, fails without it (sys-bio/antimony#190).