CompartmentalSystem#

class pharmpy.CompartmentalSystem(builder)[source]#

Bases: ODESystem

System of ODEs descibed as a compartmental system

Examples

>>> from pharmpy import Bolus, CompartmentalSystem
>>> cb = CompartmentalSystemBuilder()
>>> dose = Bolus.create("AMT")
>>> central = Compartment("CENTRAL", dose)
>>> cb.add_compartment(central)
>>> peripheral = Compartment("PERIPHERAL")
>>> cb.add_compartment(peripheral)
>>> output = Compartment("OUTPUT")
>>> cb.add_compartment(output)
>>> cb.add_flow(central, peripheral, "K12")
>>> cb.add_flow(peripheral, central, "K21")
>>> cb.add_flow(central, output, "CL / V")
>>> CompartmentalSystem(cb)        
Bolus(AMT)
                 ┌──────────┐
                 │PERIPHERAL│
                 └──────────┘
                  ↑        │
                 K12      K21
                  │        ↓
┌───────┐      ┌──────────┐      ┌──────────┐       ┌──────┐
│CENTRAL│──K12→│PERIPHERAL│──K21→│ CENTRAL  │──CL/V→│OUTPUT│
└───────┘      └──────────┘      └──────────┘       └──────┘

Attributes Summary

amounts

Column vector of amounts for all compartments

central_compartment

The central compartment

compartment_names

Names of all compartments

compartmental_matrix

Compartmental matrix of the compartmental system

dosing_compartment

The dosing compartment

free_symbols

Get set of all free symbols in the compartmental system

output_compartment

Get the output compartment

peripheral_compartments

Find perihperal compartments

rhs_symbols

Get set of all free symbols in the right hand side expressions

zero_order_inputs

Vector of all zero order inputs to each compartment

Methods Summary

atoms(cls)

Get set of all symbolic atoms of some kind

find_compartment(name)

Find a compartment using its name

find_depot(statements)

Find the depot compartment

find_transit_compartments(statements)

Find all transit compartments

get_compartment_inflows(compartment)

Get list of all flows going in to a compartment

get_compartment_outflows(compartment)

Get list of all flows going out from a compartment

get_flow(source, destination)

Get the rate of flow between two compartments

get_n_connected(comp)

Get the number of compartments connected to a compartment

subs(substitutions)

Substitute expressions or symbols in ODE system

to_compartmental_system()

to_explicit_system([skip_output])

Get the compartmental system as an explicit ODE system

Attributes Documentation

amounts#

Column vector of amounts for all compartments

Examples

>>> from pharmpy.modeling import load_example_model
>>> import sympy
>>> model = load_example_model("pheno")
>>> sympy.pprint(model.statements.ode_system.amounts)
⎡A_CENTRAL⎤
⎢         ⎥
⎣A_OUTPUT ⎦
central_compartment#

The central compartment

The central compartment is defined to be the compartment that has an outward flow to the output compartment. Only one central compartment is supported.

Returns:

Compartment – Central compartment

Examples

>>> from pharmpy.modeling import load_example_model
>>> model = load_example_model("pheno")
>>> model.statements.ode_system.central_compartment
Compartment(CENTRAL, dose=Bolus(AMT))
compartment_names#

Names of all compartments

Examples

>>> from pharmpy.modeling import load_example_model
>>> model = load_example_model("pheno")
>>> model.statements.ode_system.compartment_names
['CENTRAL', 'OUTPUT']
compartmental_matrix#

Compartmental matrix of the compartmental system

Examples

>>> from pharmpy.modeling import load_example_model, set_first_order_absorption
>>> import sympy
>>> model = load_example_model("pheno")
>>> sympy.pprint(model.statements.ode_system.compartmental_matrix)
⎡-CL    ⎤
⎢────  0⎥
⎢ V     ⎥
⎢       ⎥
⎢ CL    ⎥
⎢ ──   0⎥
⎣ V     ⎦
dosing_compartment#

The dosing compartment

A dosing compartment is a compartment that receives an input dose. Only one dose compartment is supported.

Returns:

Compartment – Dosing compartment

Examples

>>> from pharmpy.modeling import load_example_model
>>> model = load_example_model("pheno")
>>> model.statements.ode_system.dosing_compartment
Compartment(CENTRAL, dose=Bolus(AMT))
free_symbols#

Get set of all free symbols in the compartmental system

Returns:

set – Set of symbols

Examples

>>> from pharmpy.modeling import load_example_model
>>> model = load_example_model("pheno")
>>> model.statements.ode_system.free_symbols  
{AMT, CL, V, t}
output_compartment#

Get the output compartment

An output compartment is defined to be a compartment that does not have any outward flow. A model has to have one and only one output compartment.

Returns:

Compartment – Output compartment of compartmental system

Examples

>>> from pharmpy.modeling import load_example_model
>>> model = load_example_model("pheno")
>>> model.statements.ode_system.output_compartment
Compartment(OUTPUT)
peripheral_compartments#

Find perihperal compartments

A peripheral compartment is defined as having one flow to the central compartment and one flow from the central compartment.

Returns:

list of compartments – Peripheral compartments

Examples

>>> from pharmpy.modeling import load_example_model
>>> model = load_example_model("pheno")
>>> model.statements.ode_system.peripheral_compartments
[]
rhs_symbols#

Get set of all free symbols in the right hand side expressions

Returns:

set – Set of symbols

Examples

>>> from pharmpy.modeling import load_example_model
>>> model = load_example_model("pheno")
>>> model.statements.ode_system.rhs_symbols   
{AMT, CL, V, t}
zero_order_inputs#

Vector of all zero order inputs to each compartment

Example

>>> from pharmpy.modeling import load_example_model, set_zero_order_absorption
>>> import sympy
>>> model = load_example_model("pheno")
>>> sympy.pprint(model.statements.ode_system.zero_order_inputs)
⎡0⎤
⎢ ⎥
⎣0⎦
>>> set_zero_order_absorption(model)    
<...>
>>> sympy.pprint(model.statements.ode_system.zero_order_inputs)
⎡⎧ AMT                ⎤
⎢⎪─────  for t < 2⋅MAT⎥
⎢⎨2⋅MAT               ⎥
⎢⎪                    ⎥
⎢⎩  0      otherwise  ⎥
⎢                     ⎥
⎣          0          ⎦

Methods Documentation

atoms(cls)[source]#

Get set of all symbolic atoms of some kind

For more information see https://docs.sympy.org/latest/modules/core.html#sympy.core.basic.Basic.atoms

Parameters:

cls (type) – Type of atoms to find

Returns:

set – Set of symbolic atoms

find_compartment(name)[source]#

Find a compartment using its name

Parameters:

name (str) – Name of compartment to find

Returns:

Compartment – Compartment named name or None if not found

Examples

>>> from pharmpy.modeling import load_example_model
>>> model = load_example_model("pheno")
>>> central = model.statements.ode_system.find_compartment("CENTRAL")
>>> central
Compartment(CENTRAL, dose=Bolus(AMT))
find_depot(statements)[source]#

Find the depot compartment

The depot compartment is defined to be the compartment that only has out flow to the central compartment, but no flow from the central compartment.

Returns:

Compartment – Depot compartment

Examples

>>> from pharmpy.modeling import load_example_model, set_first_order_absorption
>>> model = load_example_model("pheno")
>>> set_first_order_absorption(model)       
<...>
>>> model.statements.ode_system.find_depot(model.statements)
Compartment(DEPOT, dose=Bolus(AMT))
find_transit_compartments(statements)[source]#

Find all transit compartments

Transit compartments are a chain of compartments with the same out rate starting from the dose compartment. Because one single transit compartment cannot be distinguished from one depot compartment such compartment will be defined to be a depot and not a transit compartment.

Returns:

list of compartments – Transit compartments

Examples

>>> from pharmpy.modeling import load_example_model
>>> model = load_example_model("pheno")
>>> model.statements.ode_system.find_transit_compartments(model.statements)
[]
get_compartment_inflows(compartment)[source]#

Get list of all flows going in to a compartment

Parameters:

compartment (Compartment or str) – Get inflows to this compartment

Returns:

list – Pairs of compartments and symbolic rates

Examples

>>> from pharmpy.modeling import load_example_model
>>> model = load_example_model("pheno")
>>> model.statements.ode_system.get_compartment_inflows("OUTPUT")
[(Compartment(CENTRAL, dose=Bolus(AMT)), CL/V)]
get_compartment_outflows(compartment)[source]#

Get list of all flows going out from a compartment

Parameters:

compartment (Compartment or str) – Get outflows for this compartment

Returns:

list – Pairs of compartments and symbolic rates

Examples

>>> from pharmpy.modeling import load_example_model
>>> model = load_example_model("pheno")
>>> model.statements.ode_system.get_compartment_outflows("CENTRAL")
[(Compartment(OUTPUT), CL/V)]
get_flow(source, destination)[source]#

Get the rate of flow between two compartments

Parameters:
  • source (Compartment) – Source compartment

  • destination (Compartment) – Destination compartment

Returns:

Expression – Symbolic rate

Examples

>>> from pharmpy import CompartmentalSystem, Compartment
>>> cb = CompartmentalSystemBuilder()
>>> depot = Compartment("DEPOT")
>>> cb.add_compartment(depot)
>>> central = Compartment("CENTRAL")
>>> cb.add_compartment(central)
>>> cb.add_flow(depot, central, "KA")
>>> odes = CompartmentalSystem(cb)
>>> odes.get_flow(depot, central)
KA
>>> odes.get_flow(central, depot)
get_n_connected(comp)[source]#

Get the number of compartments connected to a compartment

Parameters:

comp (Compartment or str) – The compartment

Returns:

int – Number of compartments connected to comp

Examples

>>> from pharmpy.modeling import load_example_model
>>> model = load_example_model("pheno")
>>> model.statements.ode_system.get_n_connected("CENTRAL")
1
subs(substitutions)[source]#

Substitute expressions or symbols in ODE system

Examples

>>> from pharmpy.modeling import load_example_model
>>> model = load_example_model("pheno")
>>> model.statements.ode_system.subs({'AMT': 'DOSE'})
Bolus(DOSE)
┌───────┐       ┌──────┐
│CENTRAL│──CL/V→│OUTPUT│
└───────┘       └──────┘
to_compartmental_system()[source]#
to_explicit_system(skip_output=False)[source]#

Get the compartmental system as an explicit ODE system

Parameters:
  • skip_output (boolean) – Set to true to leave the output compartment out

  • Results

  • ——-

  • ExplicitODESystem – The same ODE system with explicit equations and initial conditions

Example

>>> from pharmpy.modeling import load_example_model
>>> model = load_example_model("pheno")
>>> odes = model.statements.ode_system.to_explicit_system()
>>> odes
⎧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