forked from Down-To-Programme/PK_Model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteractive_pkmodel.py
198 lines (174 loc) · 6.77 KB
/
interactive_pkmodel.py
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import matplotlib.pylab as plt
import pathlib
def make_model():
from pkmodel import model
from pkmodel import protocol
from pkmodel import solution
Vc, Vps, Qps, CL = input_model()
model = model.Model(Vc=Vc, Vps=Vps, Qps=Qps, CL=CL)
dose, sub, k_a, cont, cont_period, inst, dose_times = input_protocol()
protocol = protocol.Protocol(dose, sub, k_a, cont, cont_period,
inst, dose_times)
sol = solution.Solution(model, protocol)
return sol
def input_model():
print('\nEnter volume of central compartment:')
Vc = float(input("Vc (mL) = "))
print('\nEnter number of peripheral compartments:')
n = int(input("n = "))
Vps = []
Qps = []
for i in range(n):
print('\nEnter volume of peripheral compartment %d:' % (i + 1))
Vp = input("Vp (mL) = ")
Vps.append(float(Vp))
print('\nEnter transition rate between central and '
'peripheral compartment %d:' % (i + 1))
Qp = input("Qp (mL/h) = ")
Qps.append(float(Qp))
print('\nEnter clearance rate from central compartment:')
CL = float(input("CL (mL/h) = "))
return Vc, Vps, Qps, CL
def input_k_a():
print('\nDosing protocol can be either subcutaneous (s) '
'OR intravenous bolus (i) ')
protocol = str(input('Enter dosing protocol: (s/i) [i] ') or 'i')
if (protocol != 's' and protocol != 'i'):
print('Could not interpret protocol. '
'Running with default (intravenous)')
sub = False
k_a = 1
if protocol == 's':
sub = True
print('\nEnter absorption rate of drug '
'administration for subcutaneous dosing:')
k_a = input('k_a (/h): [1] ') or 1
try:
k_a = float(k_a)
except ValueError:
print('Could not interpret k_a. Running with default (1 /h)')
k_a = 1
return sub, k_a
def input_inst():
print('\nEnter the number of instantaneous doses '
'of X ng (default=1): ')
n_inst = input('n = ') or 1
dose_times = []
inst = True
try:
n_inst = int(n_inst)
except ValueError:
print('Could not interpret n. Running with default (1)')
n_inst = 1
if n_inst < 1:
inst = False
n_inst = 0
else:
d0 = input('Time (in hours) of first dose: [0] ') or 0
try:
d0 = float(d0)
except ValueError:
print('Could not interpret time. Running with default (0)')
d0 = 0
dose_times.append(d0)
for i in range(n_inst - 1):
d = input('Time (in hours) of dose %d: ' % (i + 2))
try:
d = float(d)
dose_times.append(d)
except ValueError:
print('Could not interpret time. Running with default (None)')
return inst, dose_times
def input_cont():
print('\nEnter whether the dose is applied '
'at a continuous rate of X ng per hour: ')
cont = str(input('Continuous?: (y/n) [n] ') or 'n')
if (cont != 'y' and cont != 'n'):
print('Could not interpret protocol. '
'Running with default (not continuous)')
continuous = False
cont_period = [0, 0]
if cont == 'y':
continuous = True
print('Enter time in hours at which continuous '
'dosing begins (default=0):')
t0 = float(input('t0 = ') or 0)
print('Enter time in hours at which continuous '
'dosing ends (default=0):')
tf = float(input('tf = ') or 0)
cont_period = [t0, tf]
return continuous, cont_period
def input_protocol():
print('\nEnter protocol of drug administration:')
dose = float(input('Enter the dose amount in ng: [1] ') or 1.)
try:
dose = float(dose)
except ValueError:
print('Could not dose amount. Running with default (1 ng)')
dose = 1
sub, k_a = input_k_a()
continuous, cont_period = input_cont()
inst, dose_times = input_inst()
return dose, sub, k_a, continuous, cont_period, inst, dose_times
def ask_show():
show = str(input('\nShow the plot in pop-up window? (y/n) [y] \n') or 'y')
if show != 'n' and show != 'y':
print('Could not interpret input. Running with default (y)')
return True
if show == 'y':
return True
elif show == 'n':
return False
def ask_save():
save = str(input('\nSave the figure? (y/n) [n] \n') or 'n')
if save != 'n' and save != 'y':
print('Could not interpret input. Running with default (n)')
return False
if save == 'n':
return False
elif save == 'y':
default_path = str(pathlib.Path(__file__).parent.absolute()) + '/'
filename = input('Enter filename for figure [\'pkplot.pdf\']: ')
path = input('Enter path for figure [%s]: ' % default_path)
if path:
filepath = '' + path + filename
else:
filepath = '' + default_path + filename
print('Saving image at %s.pdf' % filepath)
return filepath
def print_intro():
print('\n=====================================')
print('PK MODEL: SIMULATING PHARMACOKINETICS')
print('=====================================\n \n')
print('This is a package to run a user-specifed pharmacokinetic '
'model (PK model).')
print('The user can specify the number of peripheral compartments '
'around a central compartment,')
print('a dosing type (I.V. or S.C.), and a dosing protocol. '
'A solver will solve the differential')
print('equations that model the pharmacokinetics of the compartments, '
'and graphs comparing the')
print('solutions of different model parameters will be outputted.')
print('Default values are within brackets (e.g. [0]).\n \n')
print('Enter the parameters of the main model and protocol:')
print('___________________________________________________')
if __name__ == "__main__":
print_intro()
solution1 = make_model()
print(' \n================ \nPreparing plots. \n================')
separate = input('\nSeparate the plots by compartment? (y/n) [n] ') or 'n'
compare = input('\nCompare the plot with another model? (y/n) [n] ') or 'n'
if compare == 'y':
print('\nEnter the parameters of the second model and protocol.')
print('_____________________________________________________')
solution2 = make_model()
if separate == 'y':
fig = solution1.generate_plot(solution2, True, False, ask_save())
else:
fig = solution1.generate_plot(solution2, False, False, ask_save())
elif separate == 'y':
fig = solution1.generate_plot(None, True, False, ask_save())
else:
fig = solution1.generate_plot(None, False, False, ask_save())
if ask_show():
plt.show()