Statements#
- class pharmpy.Statements(statements=None)[source]#
Bases:
Sequence
A sequence of symbolic statements describing the model
Two types of statements are supported: Assignment and ODESystem. A Statements object can have 0 or 1 ODESystem. The order of the statements is significant and the same symbol can be assigned to multiple times.
- Parameters:
statements (list or Statements) – A list of Statement or another Statements to populate this object
Attributes Summary
All statements after the ODE system
All statements before the ODE system
All statements after the ODE system or the whole model if no ODE system
Get a set of all free symbols
Returns the ODE system of the model or None if the model doesn't have an ODE system
Methods Summary
dependencies
(symbol)Find all dependencies of a symbol or statement
direct_dependencies
(statement)Find all direct dependencies of a statement
find_assignment
(symbol)Returns last assignment of symbol
find_assignment_index
(symbol)Returns index of last assignment of symbol
full_expression
(expression)Expand an expression into its full definition
reassign
(symbol, expression)Reassign symbol to expression
remove_symbol_definitions
(symbols, statement)Remove symbols and dependencies not used elsewhere
subs
(substitutions)Substitute symbols in all statements.
Convert ODE system to a compartmental system
Convert ODE system to an explicit ODE system
Attributes Documentation
- after_odes#
All statements after the ODE system
Examples
>>> from pharmpy.modeling import load_example_model >>> model = load_example_model("pheno") >>> model.statements.after_odes A_CENTRAL ───────── F = S₁ W = F Y = EPS(1)⋅W + F IPRED = F IRES = DV - IPRED IRES ──── IWRES = W
- before_odes#
All statements before the ODE system
Examples
>>> from pharmpy.modeling import load_example_model >>> model = load_example_model("pheno") >>> model.statements.before_odes ⎧TIME for AMT > 0 ⎨ BTIME = ⎩ 0 otherwise TAD = -BTIME + TIME TVCL = THETA(1)⋅WGT TVV = THETA(2)⋅WGT ⎧TVV⋅(THETA(3) + 1) for APGR < 5 ⎨ TVV = ⎩ TVV otherwise ETA(1) CL = TVCL⋅ℯ ETA(2) V = TVV⋅ℯ S₁ = V
- error#
All statements after the ODE system or the whole model if no ODE system
Examples
>>> from pharmpy.modeling import load_example_model >>> model = load_example_model("pheno") >>> model.statements.error A_CENTRAL ───────── F = S₁ W = F Y = EPS(1)⋅W + F IPRED = F IRES = DV - IPRED IRES ──── IWRES = W
- free_symbols#
Get a set of all free symbols
Examples
>>> from pharmpy.modeling import load_example_model >>> model = load_example_model("pheno") >>> model.statements.free_symbols {AMT, APGR, A_CENTRAL, BTIME, CL, DV, EPS(1), ETA(1), ETA(2), F, IPRED, IRES, IWRES, S1, TAD, THETA(1), THETA(2), THETA(3), TIME, TVCL, TVV, V, W, WGT, Y, t}
- ode_system#
Returns the ODE system of the model or None if the model doesn’t have an ODE system
Examples
>>> from pharmpy.modeling import load_example_model >>> model = load_example_model("pheno") >>> model.statements.ode_system Bolus(AMT) ┌───────┐ ┌──────┐ │CENTRAL│──CL/V→│OUTPUT│ └───────┘ └──────┘
Methods Documentation
- dependencies(symbol)[source]#
Find all dependencies of a symbol or statement
- Parameters:
symbol (Symbol, str or Statement) – Input symbol or statement
- Returns:
set – Set of symbols
Examples
>>> from pharmpy.modeling import load_example_model >>> model = load_example_model("pheno") >>> model.statements.dependencies("CL") {ETA(1), THETA(1), WGT}
- direct_dependencies(statement)[source]#
Find all direct dependencies of a statement
- Parameters:
statement (Statement) – Input statement
- Returns:
Statements – Direct dependency statements
Examples
>>> from pharmpy.modeling import load_example_model >>> model = load_example_model("pheno") >>> odes = model.statements.ode_system >>> model.statements.direct_dependencies(odes) ETA(1) CL = TVCL⋅ℯ ETA(2) V = TVV⋅ℯ
- find_assignment(symbol)[source]#
Returns last assignment of symbol
- Parameters:
symbol (Symbol or str) – Symbol to look for
- Returns:
Assignment – An Assignment or None if no assignment to symbol exists
Examples
>>> from pharmpy.modeling import load_example_model >>> model = load_example_model("pheno") >>> model.statements.find_assignment("CL") ETA(1) CL = TVCL⋅ℯ
- find_assignment_index(symbol)[source]#
Returns index of last assignment of symbol
- Parameters:
symbol (Symbol or str) – Symbol to look for
- Returns:
int – Index of Assignment or None if no assignment to symbol exists
Examples
>>> from pharmpy.modeling import load_example_model >>> model = load_example_model("pheno") >>> model.statements.find_assignment_index("CL") 5
- full_expression(expression)[source]#
Expand an expression into its full definition
- Parameters:
expression (expression or str) – Expression to expand. A string will be converted to an expression.
- Returns:
expression – Expanded expression
Examples
>>> from pharmpy.modeling import load_example_model >>> model = load_example_model("pheno") >>> model.statements.before_odes.full_expression("CL") THETA(1)*WGT*exp(ETA(1))
- reassign(symbol, expression)[source]#
Reassign symbol to expression
Set symbol to be expression and remove all previous assignments of symbol
- Parameters:
symbol (Symbol or str) – Symbol to reassign
expression (Expression or str) – The new expression to assign to symbol
- Returns:
Statements – Updated statements
Examples
>>> from pharmpy.modeling import load_example_model >>> model = load_example_model("pheno") >>> model.statements.reassign("CL", "TVCL + eta")
- remove_symbol_definitions(symbols, statement)[source]#
Remove symbols and dependencies not used elsewhere
If the statement no longer depends on the specified symbols, this method will make sure that the definitions of these symbols will be removed unless they are dependencies of other statements.
- Parameters:
symbols (iterable) – Iterable of symbols no longer used in the statement
statement (Statement) – Statement from which the symbols were removed
- subs(substitutions)[source]#
Substitute symbols in all statements.
- Parameters:
substitutions (dict) – Old-new pairs(can be type str or sympy symbol)
Examples
>>> from pharmpy.modeling import load_example_model >>> model = load_example_model("pheno") >>> stats = model.statements.subs({'WGT': 'WT'}) >>> stats.before_odes ⎧TIME for AMT > 0 ⎨ BTIME = ⎩ 0 otherwise TAD = -BTIME + TIME TVCL = THETA(1)⋅WT TVV = THETA(2)⋅WT ⎧TVV⋅(THETA(3) + 1) for APGR < 5 ⎨ TVV = ⎩ TVV otherwise ETA(1) CL = TVCL⋅ℯ ETA(2) V = TVV⋅ℯ S₁ = V
- to_compartmental_system()[source]#
Convert ODE system to a compartmental system
raise if not possible >>> from pharmpy.modeling import load_example_model >>> model = load_example_model(“pheno”) >>> statements = model.statements.to_compartmental_system()
- to_explicit_system()[source]#
Convert ODE system to an explicit ODE system
Example
>>> from pharmpy.modeling import load_example_model >>> model = load_example_model("pheno") >>> model.statements.ode_system Bolus(AMT) ┌───────┐ ┌──────┐ │CENTRAL│──CL/V→│OUTPUT│ └───────┘ └──────┘ >>> model.statements.ode_system.to_explicit_system() ⎧d -CL⋅A_CENTRAL(t) ⎪──(A_CENTRAL(t)) = ───────────────── ⎪dt V ⎨d CL⋅A_CENTRAL(t) ⎪──(A_OUTPUT(t)) = ─────────────── ⎪dt V ⎪A_CENTRAL(0) = AMT ⎩A_OUTPUT(0) = 0