Modeling Drug Concentration Over Time Using Python

Let’s solve a $pharmacokinetics$ problem using $Python$ and visualize the results.

$Pharmacokinetics$ is the study of how drugs move through the body, and a common problem is calculating drug concentration over time.

Problem: Drug Concentration Over Time (One-Compartment Model)

We’ll assume a single dose of a drug is administered intravenously, and it distributes uniformly throughout a single compartment in the body.

Our goal is to calculate the concentration of the drug in the bloodstream over time and plot it.

Model Assumptions

  1. One-compartment model: The drug distributes instantly throughout the body.
  2. First-order elimination: The drug is metabolized and eliminated at a rate proportional to its concentration.

Equations and Parameters

The concentration $(C(t))$ of the drug in the bloodstream at time $(t)$ can be calculated with:
$$
C(t) = \frac{D}{V} e^{-k \cdot t}
$$
where:

  • $(D)$: The dose administered (in $mg$),
  • $(V)$: The volume of distribution (in $L$),
  • $(k)$: The elimination rate constant (in $1/h$), calculated as$ (k = \frac{\ln(2)}{t_{1/2}})$,
  • $(t_{1/2})$: The drug’s half-life (in hours),
  • $(t)$: Time after administration (in hours).

Python Code

Let’s write Python code to calculate and plot the concentration $(C(t))$ over a specified time period.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import numpy as np
import matplotlib.pyplot as plt

# Define parameters
D = 500 # Dose in mg
V = 50 # Volume of distribution in L
t_half = 4 # Half-life of the drug in hours

# Calculate the elimination rate constant
k = np.log(2) / t_half

# Time range (0 to 24 hours)
time = np.linspace(0, 24, 100)

# Calculate concentration over time
concentration = (D / V) * np.exp(-k * time)

# Plot the concentration-time curve
plt.plot(time, concentration, color='blue', label='Drug Concentration')
plt.xlabel('Time (hours)')
plt.ylabel('Concentration (mg/L)')
plt.title('Drug Concentration Over Time')
plt.grid(True)
plt.legend()
plt.show()

Explanation of the Code

  1. Parameter Definitions:
    • The dose $(D)$, volume of distribution $(V)$, and half-life $(t_{1/2})$ are defined based on typical values.
  2. Elimination Rate Constant:
    • We calculate $(k)$ using the half-life formula $(k = \frac{\ln(2)}{t_{1/2}})$.
  3. Concentration Calculation:
    • For each time point in the defined range ($0$ to $24$ hours), we calculate the concentration $(C(t))$ using the exponential decay formula.
  4. Plotting:
    • The concentration over time is plotted on a graph with time on the $x$-$axis$ and concentration on the $y$-$axis$.

Visualization

The output graph displays:

  • A blue curve representing the drug concentration in the bloodstream over time.
  • The concentration decreases exponentially, showing how the drug is metabolized and eliminated from the body.

This model is useful for understanding the $pharmacokinetics$ of a drug after an intravenous dose, helping in dosage planning and ensuring therapeutic levels are maintained without reaching toxic levels.