CompartmentalSystem#

class pharmpy.model.CompartmentalSystem(builder, t=t)[source]#

Bases: Statement

System of ODEs descibed as a compartmental system

Examples

>>> from pharmpy.model import Bolus, Compartment, output
>>> from pharmpy.model import CompartmentalSystemBuilder, CompartmentalSystem
>>> cb = CompartmentalSystemBuilder()
>>> dose = Bolus.create("AMT")
>>> central = Compartment.create("CENTRAL", doses=(dose,))
>>> cb.add_compartment(central)
>>> peripheral = Compartment.create("PERIPHERAL")
>>> cb.add_compartment(peripheral)
>>> 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→
└───────┘      └──────────┘      └──────────┘

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_compartments

The dosing compartment(s)

eqs

Tuple of equations

free_symbols

Get set of all free symbols in the compartmental system

rhs_symbols

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

t

Independent variable of CompartmentalSystem

zero_order_inputs

Vector of all zero order inputs to each compartment

Methods Summary

create(builder[, t])

find_compartment(name)

Find a compartment using its name

find_compartment_or_raise(comp)

find_depot(statements)

Find the depot compartment

find_peripheral_compartments([name])

Find perihperal compartments

find_transit_compartments(statements)

Find all transit compartments

from_dict(d)

get_bidirectionals(compartment)

Get list of all compartments with bidirectional flow from/to a compartment

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

replace(**kwargs)

subs(substitutions)

Substitute expressions or symbols in ODE system

to_dict()

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(t)]
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, amount=A_CENTRAL(t), doses=Bolus(AMT, admid=1))
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']
compartmental_matrix#

Compartmental matrix of the compartmental system

Examples

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

The dosing compartment(s)

A dosing compartment is a compartment that receives an input dose. Multiple dose compartments are supported. The order of dose compartments is defined to put the central compartment last.

Returns:

tuple – A tuple of dose compartments

Examples

>>> from pharmpy.modeling import load_example_model
>>> model = load_example_model("pheno")
>>> model.statements.ode_system.dosing_compartments
(Compartment(CENTRAL, amount=A_CENTRAL(t), doses=Bolus(AMT, admid=1)),)
eqs#

Tuple of equations

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}
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}
t#

Independent variable of CompartmentalSystem

zero_order_inputs#

Vector of all zero order inputs to each compartment

Example

>>> from pharmpy.modeling import load_example_model, set_zero_order_absorption
>>> model = load_example_model("pheno")
>>> model.statements.ode_system.zero_order_inputs
[0]

Methods Documentation

classmethod create(builder, t=t)[source]#
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, amount=A_CENTRAL(t), doses=Bolus(AMT, admid=1))
find_compartment_or_raise(comp)[source]#
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.

For drug metabolite models however, it is possible to have outflow with unidirectional flow to both the central and metabolite compartment. In this case, the central compartment is found based on name.

Returns:

Compartment – Depot compartment

Examples

>>> from pharmpy.modeling import load_example_model, set_first_order_absorption
>>> model = load_example_model("pheno")
>>> model = set_first_order_absorption(model)
>>> model.statements.ode_system.find_depot(model.statements)
Compartment(DEPOT, amount=A_DEPOT(t), doses=Bolus(AMT, admid=1))
find_peripheral_compartments(name=None)[source]#

Find perihperal compartments

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

If name is set, peripheral compartments connected to the compartment with the associated name is returned.

Returns:

list of compartments – Peripheral compartments

Examples

>>> from pharmpy.modeling import load_example_model
>>> model = load_example_model("pheno")
>>> model.statements.ode_system.find_peripheral_compartments()
[]
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)
[]
classmethod from_dict(d)[source]#
get_bidirectionals(compartment)[source]#

Get list of all compartments with bidirectional flow from/to a compartment

Parameters:

compartment (Compartment or str) – Compartment of interest

Returns:

list – Compartments with bidirectional flow

Examples

>>> from pharmpy.modeling import load_example_model
>>> model = load_example_model("pheno")
>>> central = model.statements.ode_system.central_compartment
>>> model.statements.ode_system.get_bidirectionals(central)
[]
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
>>> from pharmpy.model import output
>>> model = load_example_model("pheno")
>>> model.statements.ode_system.get_compartment_inflows(output)
[(Compartment(CENTRAL, amount=A_CENTRAL(t), doses=Bolus(AMT, admid=1)), 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")
[(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.model import Compartment, CompartmentalSystemBuilder
>>> from pharmpy.model import CompartmentalSystem
>>> cb = CompartmentalSystemBuilder()
>>> depot = Compartment.create("DEPOT")
>>> cb.add_compartment(depot)
>>> central = Compartment.create("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)
0
get_n_connected(comp)[source]#

Get the number of compartments connected to a compartment

Parameters:

comp (Compartment) – 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")
0
replace(**kwargs)[source]#
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, admid=1) → CENTRAL
┌───────┐
│CENTRAL│──CL/V→
└───────┘
to_dict()[source]#