diff --git a/pk_optimizer/pk1Comp.py b/pk_optimizer/pk1Comp.py new file mode 100644 index 0000000000000000000000000000000000000000..f7daa6a7bbf4992f0b5756d4bd82c308fca6d444 --- /dev/null +++ b/pk_optimizer/pk1Comp.py @@ -0,0 +1,140 @@ +#!/usr/bin/env python +# coding: utf-8 + +# In[1]: + + +# 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: + + """The pk1Comp object is a one compartment PK model that outputs graphs of mass of tracer over time.""" + + def __init__ (self, numParam = 4, Flow = 1, Vp = 0.1, Visf = 0.5, PS = 0.15): + + """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() + + +# In[ ]: + + + +