Statements#

class pharmpy.model.Statements(statements=())[source]#

Bases: Sequence, Immutable

A sequence of symbolic statements describing the model

Two types of statements are supported: Assignment and CompartmentalSystem. A Statements object can have 0 or 1 CompartmentalSystem. 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

after_odes

All statements after the ODE system

before_odes

All statements before the ODE system

error

All statements after the ODE system or the whole model if no 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

create([statements])

dependencies(symbol_or_statement)

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

from_dict(d)

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.

to_dict()

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(t)
                ────────────
        F =      S₁
        Y = EPS₁⋅F + F
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
        TVCL = POP_CL⋅WGT
        TVV = POP_VC⋅WGT
                  ⎧TVV⋅(COVAPGR + 1)  for APGR < 5

        TVV = ⎩       TVV          otherwise
                           ETA_CL
        CL = TVCL⋅ℯ
                          ETA_VC
        VC = TVV⋅ℯ
        V = VC
        S₁ = VC
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(t)
                ────────────
        F =      S₁
        Y = EPS₁⋅F + F
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, admid=1) → CENTRAL
┌───────┐
│CENTRAL│──CL/V→
└───────┘

Methods Documentation

classmethod create(statements=None)[source]#
dependencies(symbol_or_statement)[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_CL
CL = TVCL⋅ℯ
V = VC
find_assignment(symbol)[source]#

Returns last assignment of symbol

Parameters:

symbol (Symbol or str) – Symbol to look for

Returns:

Assignment or None – 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_CL
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 or None – 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")
3
classmethod from_dict(d)[source]#
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")
POP_CL*WGT*exp(ETA_CL)
reassign(symbol, expression)[source]#

Reassign symbol to expression

Set symbol to be expression and remove all previous assignments of symbol

Parameters:
  • symbol (sympy.Symbol or str) – Symbol to reassign

  • expression (Expr 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 symbol)

Examples

>>> from pharmpy.modeling import load_example_model
>>> model = load_example_model("pheno")
>>> stats = model.statements.subs({'WGT': 'WT'})
>>> stats.before_odes
        TVCL = POP_CL⋅WT
        TVV = POP_VC⋅WT
                  ⎧TVV⋅(COVAPGR + 1)  for APGR < 5

        TVV = ⎩       TVV          otherwise
                   ETA_CL
CL = TVCL⋅ℯ
                          ETA_VC
        VC = TVV⋅ℯ
        V = VC
        S₁ = VC
to_dict()[source]#