-
Notifications
You must be signed in to change notification settings - Fork 1
/
calculator.py
199 lines (135 loc) · 5.31 KB
/
calculator.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
def lbs_to_kg(weight):
"""convert a weight in pounds to a weight in kg
:params:
weight(lbs) - Float
:returns:
weight(kg) - Float
Examples:
>>> lbs_to_kg(10)
4.535923700000001
>>> lbs_to_kg(100)
45.359237
"""
return weight * 0.45359237
def get_amount_in_mg(weight, dose):
"""Calculates the amount of drug needed in mg for a given weight and dose.
:params:
weight - Float
dose - Float
:returns:
Float - amount of drug needed in mg.
Examples:
>>> get_amount_in_mg(10.2, 13.75)
140.25
>>> get_amount_in_mg(5, 0.5)
2.5
"""
return weight * dose
def get_amount_in_ml(dose, concentration):
"""Calculates the amount of liquid drug to be given in mls, given a dose(mg/kg) and concentration (mg/ml.
:params:
dose(mg/kg) - Float
concentration - Float
:returns:
Amount in mls - Float
Examples:
>>> get_amount_in_ml(2.5, 50)
0.05
>>> get_amount_in_ml(80, 50)
1.6
"""
return dose/concentration
def get_num_tablets_per_dose(dose, strength, divisions=1):
"""Calculates the number of tablets to give based on the dose given and the strength of the tablets.
Tablets can be divided in quarters.
:params:
dose (Float) - The dose in mg of what the patient should be receiving
strength (Float) - The strength in mg of the chosen tablets.
divisions (Int) - the number of sections you can divide your tablet into
:returns:
Number of tablets per dose (Float) - fixed to one of the points of the divisions
>>> get_num_tablets_per_dose(120, 100, 2)
1.0
>>> get_num_tablets_per_dose(240, 100, 2)
2.5
#Divide into quarters
>>> get_num_tablets_per_dose(120, 100, 4)
1.25
"""
num_tabs = dose/strength
return round(num_tabs * divisions)/divisions
def total_amount_needed(amount, duration, frequency):
"""Calculates the total amount of the drug that is needed to send home with the client.
Params:
amount(Float) - Could be in mls or the number of tablets per dose.
duration(Integer) - The number of days to give the medication for
frequency(Integer) - The interval between each dosing in hrs.
:returns:
Total amount of the drug(float) (either in mls or number of tablets) to be sent home.
>>> total_amount_needed(0.5, 7, 12)
7.0
>>> total_amount_needed(1.5, 14, 8)
63.0
>>> total_amount_needed(2, 60, 48)
60.0
"""
# Number of times per day * days of treatment * amount per each dose
return 24 / frequency * duration * amount
def get_amount_ml_per_dose(dose, concentration, weight):
"""Gets the amount in ml required per dose"""
amount_in_mg = get_amount_in_mg(weight, dose)
return get_amount_in_ml(amount_in_mg, concentration)
def get_instructions(weight, dose, concentration, duration, frequency, form, divide=1):
"""Take in arguments obtained from the input form and output a dictionary with points of interest.
:params:
weight (Float)
dose(mg/kg) (Float)
duration - number of days to continue treatment (Int)
frequency - how many hours in between each treatment (Int)
concentration - concentration of the drug (mg/ml) being used (Float)
:returns:
Dictionary with information on amount to give per dose, the total amount to send home, how many hours to leave
between each dose and the frequency of the dose per day.
Examples:
>>> get_instructions(10, 2, 0.5, 7, 12, 'liq')
{'amount_per_dose': 40.0, 'total_amount': 560.0, 'frequency_hrs': 12, 'frequency_day': '2 times daily', 'duration': 7, 'form': 'liq'}
>>> get_instructions(50, 2.5, 100, 14, 8, 'tab', 4)
{'amount_per_dose': 1.25, 'total_amount': 52.5, 'frequency_hrs': 8, 'frequency_day': '3 times daily', 'duration': 14, 'form': 'tab'}
"""
amount_in_mg = get_amount_in_mg(weight, dose)
amount_per_dose = 0
if form == 'liq':
amount_per_dose = get_amount_in_ml(amount_in_mg, concentration)
elif form == 'tab':
amount_per_dose = get_num_tablets_per_dose(amount_in_mg, concentration, divide)
total_amount = total_amount_needed(amount_per_dose, duration, frequency)
frequency_per_day = 24/frequency
if frequency_per_day == 0.5:
frequency_day = "Every other day"
elif round(frequency_per_day,2) == 0.33:
frequency_day = "Every 3rd day"
else:
frequency_day = f"{int(frequency_per_day)} times daily"
instruction_info = {
'amount_per_dose' : round(amount_per_dose,2),
'total_amount' : round(total_amount,2),
'frequency_hrs' : int(frequency),
'frequency_day' : frequency_day,
'duration' : int(duration),
'form' : form
}
return instruction_info
def generate_instructions(instruction_info):
"""Generates an instruction string from a dictionary of instruction info given.
:params instruction_info: Dictionary
:returns: String of instructions
"""
return f"""Give {instruction_info['amount_per_dose']}
{'tablets' if instruction_info['form'] == 'tab' else 'ml'},
{instruction_info['frequency_day']}.
(every {instruction_info['frequency_hrs']} hrs)
for {instruction_info['duration']} days."""
if __name__ == "__main__":
from doctest import testmod
if testmod().failed == 0:
print("ALL DOCTESTS TESTS PASSED!")