ModelStatements#

class pharmpy.ModelStatements(statements=None)[source]#

Bases: collections.abc.MutableSequence

A sequence of symbolic statements describing the model

Two types of statements are supported: Assignment and ODESystem. A ModelStatements 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 ModelStatements) – A list of Statements or another ModelStatements to populate this object

Attributes Summary

after_odes

All statements after the ODE system

before_odes

All statements before the ODE system

free_symbols

Get a set of all free symbols

ode_system

Returns the ODE system of the model or None if the model doesn't have an ODE system

Methods Summary

copy()

Create a copy of the ModelStatements object

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

full_expression(expression)

Expand an expression into its full definition

insert(ind, value)

S.insert(index, value) -- insert value before index

insert_after(statement, new_statement)

Insert a statement directly after another statement

insert_before(statement, new_statement)

Insert a statement just before another statement

insert_before_odes(statement)

Insert a statement just before the ODE system or at the end of the model

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.

to_compartmental_system()

Convert ODE system to a compartmental system

to_explicit_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
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

copy()[source]#

Create a copy of the ModelStatements object

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

ModelStatements – 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⋅ℯ
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))
insert(ind, value)[source]#

S.insert(index, value) – insert value before index

insert_after(statement, new_statement)[source]#

Insert a statement directly after another statement

Parameters
  • statement (Statement) – Insert after this statement

  • new_statement (Statement) – Statement to insert

Examples

>>> from pharmpy import Assignment
>>> from pharmpy.modeling import load_example_model
>>> model = load_example_model("pheno")
>>> a = Assignment("WGT_G", "WGT*1000")
>>> b = model.statements.find_assignment("CL")
>>> model.statements.insert_after(b, a)
insert_before(statement, new_statement)[source]#

Insert a statement just before another statement

Parameters
  • statement (Statement) – Insert before this statement

  • new_statement (Statement) – Statement to insert

Examples

>>> from pharmpy import Assignment
>>> from pharmpy.modeling import load_example_model
>>> model = load_example_model("pheno")
>>> a = Assignment("WGT_G", "WGT*1000")
>>> b = model.statements.find_assignment("CL")
>>> model.statements.insert_before(b, a)
insert_before_odes(statement)[source]#

Insert a statement just before the ODE system or at the end of the model

Parameters

statement (Statement) – Statement to insert

Examples

>>> from pharmpy import Assignment
>>> from pharmpy.modeling import load_example_model
>>> model = load_example_model("pheno")
>>> a = Assignment("WGT_G", "WGT*1000")
>>> model.statements.insert_before_odes(a)
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

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")
>>> model.statements.subs({'WGT': 'WT'})
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”) >>> model.statements.to_explicit_system() >>> model.statements.to_compartmental_system() >>> model.statements.ode_system Bolus(AMT) ┌───────┐ ┌──────┐ │CENTRAL│──CL/V→│OUTPUT│ └───────┘ └──────┘

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.to_explicit_system()
>>> model.statements.ode_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