Skip to content
Snippets Groups Projects
Commit 7d5faff6 authored by Tu, Ethan's avatar Tu, Ethan
Browse files

Replace 1_Comp_Model_PK.ipynb

parent 0fe87d61
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id: tags:
``` python
# Import commands
from scipy.stats import gamma
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from scipy.integrate import odeint
import math as math
class pk1Comp:
# Derivative function
def derivs(curr_vals, time):
"""The pk1Comp object is a one compartment PK model that outputs graphs of mass of tracer over time."""
# Define value of input function Cin
Cin = rv.pdf(time)
def __init__ (self, numParam = 4, Flow = 1, Vp = 0.1, Visf = 0.5, PS = 0.15):
# Unpack the current values of the variables we wish to "update" from the curr_vals list
C = curr_vals
# Right-hand side of odes, which are used to computer the derivative
dC_dt = flow*(Cin - C)/Vol
#Cout = C
return dC_dt
# Declare Variables for initial conditions
flow = .01 # Flow into capillary
Vol = 0.05 # Volume of compartment
C0 = 0 # Initial concentration of tracer in plasma
tmax = 10 #Time in seconds
dt = 0.1 #Time step
a = 2. # Alpha for gamma distribution
rv = gamma(a, loc = 2, scale = 0.65) #input function
# Define the time array
time = ls
np.arange(0, tmax + dt, dt)
# Store the initial values in a list
init = [C0]
# Solve the odes with odeint
sol = odeint(derivs, init, time)
#Mass_plasma = Vp * sol[:,0] #mass of tracer in plasma
#Mass_isf = Visf * sol[:,1] #mass of tracer in isf
#Tp = Vp/(flow + PS) # mean transit time
#E = 1 - np.exp(-PS/flow) #extraction fraction
#Q = Mass_plasma + Mass_isf
#print('The mean transit time is ' + str(Tp))
#print('The extraction fraction is ' + str(E))
# Plot the results using the values stored in the solution variable, "sol"
# Plot Cp using the "0" element from the solution
plt.figure(1)
plt.plot(time, rv.pdf(time), color = 'blue', label = 'Input Function')
plt.plot(time, sol[:,0],color="green", label = 'Cout')
# Plot Cisf using the "1" element from the solution
#plt.plot(time, sol[:,1],color="purple", label = 'Cisf')
plt.xlabel('Time [s]')
plt.ylabel('Concentration [mM]')
plt.legend(loc = 'best')
plt.grid()
# Plot mass of tracer using the "2" element from the solution
#plt.figure(2)
#plt.plot(time, Mass_plasma,color="red", label = 'Plasma')
# Plot mass of tracer in tissue using the "3" element from the solution
#plt.plot(time, Mass_isf,color="black", label = 'Interstitial Space')
#plt.plot(time, Q, color="blue", label = 'Total mass')
#plt.xlabel('Time [s]')
#plt.ylabel('Mass [mg]')
#plt.legend(loc = 'best')
#plt.grid()
"""Initializes the model with default parameter values for flow, Vp, Visf, and PS.
Parameters
----------
numParam: int
numParam is the number of parameters you want to optimize for the model. Defaults to 4.
Flow : double
Flow is the flow of plasma through the blood vessel in mL/(mL*min). Defaults to 1.
Vp : double
Vp is the volume of plasma in mL. Defaults to 0.1.
Visf : double
Visf is the volume of interstitial fluid in mL. Defaults to 0.5.
PS : double
PS is the permeability-surface area constant in mL/(g*min). Defaults to 0.15.
"""
# Declare Variables for initial conditions
self.numParam = numParam
self.Flow = Flow
self.Vp = Vp
self.Visf = Visf
self.PS = PS
C0 = 0 # Initial concentration of tracer in plasma
tmax = 10 #Time in seconds
dt = 0.1 #Time step
a = 2. # Alpha for gamma distribution
rv = gamma(a, loc = 2, scale = 0.65) #input function
# Define the time array
time = np.arange(0, tmax + dt, dt)
# Derivative function
def derivs(curr_vals, time):
"""Finds derivatives of ODEs.
Parameters
----------
curr_vals : double[]
curr_vals it he current values of the variables we wish to "update" from the curr_vals list.
time : double[]
time is our time array from 0 to tmax with timestep dt.
Returns
-------
dc_dt : double[]
contains the derivative of concentrations with respect to time.
"""
# Define value of input function Cin
Cin = rv.pdf(time)
# Unpack the current values of the variables we wish to "update" from the curr_vals list
C = curr_vals
# Right-hand side of odes, which are used to computer the derivative
dC_dt = flow*(Cin - C)/Vol
#Cout = C
return dC_dt
def getPlot(self):
"""Plots the solution of the solved ODEs.
Parameters
----------
self : self
Passes variables needed from self.
"""
# Plot the results using the values stored in the solution variable, "sol"
# Plot Cp using the "0" element from the solution
plt.figure(1)
plt.plot(time, rv.pdf(time), color = 'blue', label = 'Input Function')
plt.plot(time, sol[:,0],color="green", label = 'Cout')
# Plot Cisf using the "1" element from the solution
#plt.plot(time, sol[:,1],color="purple", label = 'Cisf')
plt.xlabel('Time [s]')
plt.ylabel('Concentration [mM]')
plt.legend(loc = 'best')
plt.grid()
def main(self):
"""Main function to run and solve ODEs"""
# Store the initial values in a list
init = [C0]
# Solve the odes with odeint
sol = odeint(derivs, init, time)
#Mass_plasma = Vp * sol[:,0] #mass of tracer in plasma
#Mass_isf = Visf * sol[:,1] #mass of tracer in isf
#Tp = Vp/(flow + PS) # mean transit time
#E = 1 - np.exp(-PS/flow) #extraction fraction
#Q = Mass_plasma + Mass_isf
#print('The mean transit time is ' + str(Tp))
#print('The extraction fraction is ' + str(E))
# Plot mass of tracer using the "2" element from the solution
#plt.figure(2)
#plt.plot(time, Mass_plasma,color="red", label = 'Plasma')
# Plot mass of tracer in tissue using the "3" element from the solution
#plt.plot(time, Mass_isf,color="black", label = 'Interstitial Space')
#plt.plot(time, Q, color="blue", label = 'Total mass')
#plt.xlabel('Time [s]')
#plt.ylabel('Mass [mg]')
#plt.legend(loc = 'best')
#plt.grid()
```
%% Output
%% Cell type:code id: tags:
``` python
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment