-
Notifications
You must be signed in to change notification settings - Fork 0
/
hormone_levels.py
330 lines (290 loc) · 12.9 KB
/
hormone_levels.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import math
import numpy as np
from drugs import *
from modelling import *
from graphing.plot import plot_drugs
import datetime
from parser.yaml_parser import *
from graphing.color_list import get_color
from sys import argv
STEP_DAYS = (5, 30, 90)
class HormoneLevels:
config: YAMLparser
drugs: Dict[str, Drug]
std_dev_count: int
p_confidence: str
model: BodyModel
lab_data_list: List[LabData]
days_into_future: int
now: Union[datetime, float]
duration_factor: float
duration: float
y_window: Tuple[float, float]
data: Tuple[np.ndarray, Dict[str, plot_data_type]]
confidence: Optional[float]
avg_levels: Dict[str, Tuple[float, float, str]]
lab_levels: Dict[str, Tuple[List[Union[int, datetime]], List[float]]]
xticks: int
start_model: datetime
def __init__(self):
# starttime = datetime.now()
self.config = YAMLparser(Path(argv[1]))
self.initialize_drugs(self.config)
self.get_std_dev_vars(self.config)
self.model = BodyModel(self.config.model['start_date'],
self.config.model['timedelta'])
self.add_drugs(self.model, self.drugs)
self.add_doses(self.model, self.config)
self.get_lab_data(self.model, self.config)
self.add_events(self.model, self.config)
self.days_into_future = self.config.model['days_into_future']
self.model.calculate_timeline(date.today() + timedelta(days=self.days_into_future))
if len(self.lab_data_list) > 0:
self.model.estimate_blood_levels(corrected_std_dev=self.config.model['corrected_std_dev'])
self.print_drug_data(self.model, self.drugs)
self.now = self.calculate_now()
self.duration_factor = self.model.step / self.config.graph['units']
self.duration = self.model.duration * self.duration_factor
# fortnight_ago = duration - (14+days_into_future)*24
# month_ago = duration - (30+days_into_future)*24
# three_months_ago = duration - (90+days_into_future)*24
# half_year_ago = duration - (183+days_into_future)*24
self.y_window = self.config.graph['y_window']
self.model.step_days = STEP_DAYS
self.data, self.confidence = self.get_data()
self.avg_levels, self.lab_levels = self.calculate_lab_levels()
self.print_estimates()
self.calculate_xticks()
self.start_model = datetime.combine(self.config.model['start_date'], time())
if not self.config.graph['confidence']:
self.confidence = None
self.full_plot()
self.plots()
self.plot_prediction_error()
# print(datetime.now() - starttime)
def initialize_drugs(self, config: YAMLparser) -> None:
self.drugs = {}
for drug_key, drug_obj in config.drugs.items():
drug_name = drug_obj['name']
drug_factor = drug_obj['factor']
drug_class = drug_db(drug_name)
if drug_class is not None:
self.drugs[drug_key] = drug_class()
self.drugs[drug_key].factor = drug_factor
else:
print(f"WARNING: Cannot find drug {drug_name} in database")
def get_std_dev_vars(self, config: YAMLparser) -> None:
self.std_dev_count = 1
self.p_confidence = ".317"
if config.graph["two_std_dev_in_band"]:
self.std_dev_count = 2
self.p_confidence = ".046"
@staticmethod
def add_drugs(model: BodyModel, drugs: Dict[str, Drug]) -> None:
for drug_key, drug_class in drugs.items():
model.add_drugs(drug_key, drug_class)
@staticmethod
def add_doses(model: BodyModel, config: YAMLparser) -> None:
for drug_name, doses in config.doses.items():
for dose in doses:
model.add_dose(drug_name, dose['dose'], dose['date'])
def get_lab_data(self, model: BodyModel, config: YAMLparser):
self.lab_data_list = []
for lab in config.labs:
lab_values = {}
for drug_key, value in lab['values'].items():
lab_values[drug_key] = value
self.lab_data_list.append(LabData(lab['date'], lab_values))
model.add_lab_data(self.lab_data_list)
@staticmethod
def add_events(model: BodyModel, config: YAMLparser) -> None:
if len(config.model['events']) > 0:
for event in config.model['events']:
model.add_event(event['event_date'], event['transition'])
def print_drug_data(self, model: BodyModel, drugs: Dict[str, Drug]) -> None:
for drug_key in drugs.keys():
ll_message = model.get_current_blood_level_message(drug_key, self.std_dev_count, self.p_confidence)
if ll_message is not None:
print(ll_message)
if model.doses_count[drug_key] > 0 and model.doses_amount[drug_key] > 0.0:
print(f"{drugs[drug_key].name}: "
f"{model.doses_amount[drug_key]:8.2f}mg over {model.doses_count[drug_key]} doses for an average "
f"dose of {model.doses_amount[drug_key] / model.doses_count[drug_key]:5.3f}mg")
def calculate_now(self) -> float:
seconds_since_start = float((datetime.today() - datetime.combine(self.model.starting_date, time())).total_seconds())
return seconds_since_start / (3600.0 * float(self.config.graph['units'] / self.config.model['timedelta']))
def get_data(self) -> Tuple[Tuple[np.ndarray, Dict[str, plot_data_type]], float]:
if self.std_dev_count == 1:
# 68% confidence at a single standard deviation
confidence = 68
data = self.model.get_plot_data(self.config.graph['units'],
True,
color=True,
offset=self.config.graph['x_offset'],
use_x_date=self.config.graph['use_x_date'])
elif self.std_dev_count == 2:
# 95% Confidence at twice the standard deviation
confidence = 95.5
data = self.model.get_plot_data(self.config.graph['units'],
True,
stddev_multiplier=2,
color=True,
offset=self.config.graph['x_offset'],
use_x_date=self.config.graph['use_x_date'])
else:
raise Exception("Can only have one or two standard deviations as banding options")
return data, confidence
def calculate_lab_levels(self) -> Tuple[Dict[str, Tuple[float, float, str]],
Dict[str, Tuple[List[Union[int, datetime]], List[float]]]
]:
avg_levels = {}
if len(self.lab_data_list) > 0:
for n, drug_key in enumerate(self.config.drugs.keys()):
stats = self.model.get_statistical_data(drug_key)
if stats is not None:
avg_level, std_dev_level = stats
print(f"Average blood level for {self.drugs[drug_key].name} "
f"is {avg_level:6.2f} "
f"± {std_dev_level * self.std_dev_count:6.2f} ng/l (P<{self.p_confidence})")
avg_levels[self.drugs[drug_key].name] = (avg_level, std_dev_level, get_color(n + len(self.drugs) + 1))
lab_levels = self.model.get_plot_lab_levels(self.config.graph['use_x_date'])
else:
lab_levels = None
return avg_levels, lab_levels
def print_estimates(self) -> None:
if len(self.config.print_estimates) > 0:
for blood_draw in self.config.print_estimates:
try:
estimate_at_last_lab = self.model.get_blood_level_at_timepoint('Estradiol', blood_draw)
except KeyError:
estimate_at_last_lab = self.model.get_blood_level_at_timepoint('Testosterone', blood_draw)
print(f"Estimate at {blood_draw}: {estimate_at_last_lab[0] * estimate_at_last_lab[1]:6.2f} ± "
f"{estimate_at_last_lab[2] * self.std_dev_count:5.2f} ng/l (P<{self.p_confidence})")
def calculate_xticks(self) -> None:
self.xticks = 7
while self.duration > self.xticks * 20:
self.xticks *= 2
def full_plot(self) -> None:
if not self.config.graph['deactivate_full_plot']:
if self.config.graph['use_x_date']:
x_win = (self.start_model, self.start_model + self.config.graph['units'] * self.duration)
self.now = datetime.now()
else:
x_win = (0, self.duration)
plot_drugs(data=self.data,
x_window=x_win,
y_window=self.y_window,
x_label=self.config.graph['x_label'],
y_label=self.config.graph['y_label'],
lab_data=self.lab_levels,
confidence_val=self.confidence,
now=self.now,
title="Full view",
x_ticks=self.xticks,
avg_levels=self.avg_levels,
plot_dates=self.config.graph['use_x_date'],
moving_average=self.model.running_average,
moving_deviation=self.model.running_stddev,
avg_length=STEP_DAYS,
)
def plots(self) -> None:
for plot in self.config.graph['plots']:
if plot['time_absolute']:
past_window = plot['begin_day']
future_window = plot['end_day']
else:
past_window = self.duration - (plot['begin_day'] + self.days_into_future)
future_window = (self.duration - self.days_into_future) + plot['end_day']
if plot['y_window'] is not None:
y_win = plot['y_window']
else:
y_win = self.y_window
if self.config.graph['use_x_date']:
x_win = (self.start_model + self.config.graph['units'] * past_window,
self.start_model + self.config.graph['units'] * future_window)
self.now = datetime.now()
else:
x_win = (past_window, future_window)
plot_drugs(data=self.data,
x_window=x_win,
y_window=y_win,
x_ticks=plot['x_ticks'],
x_label=self.config.graph['x_label'],
y_label=self.config.graph['y_label'],
lab_data=self.lab_levels,
confidence_val=self.confidence,
now=self.now,
title=plot['title'],
avg_levels=self.avg_levels,
plot_dates=self.config.graph['use_x_date'],
moving_average=self.model.running_average,
moving_deviation=self.model.running_stddev,
avg_length=STEP_DAYS,
)
def plot_prediction_error(self) -> None:
if self.config.graph['prediction_error']:
times = []
prediction_data = {}
arrays = {}
if self.config.graph['use_x_date']:
min_t = datetime(2200, 12, 31, 23, 59, 59)
max_t = datetime(1970, 1, 1, 0, 0, 0)
else:
min_t = 10000000
max_t = 0
magnitude = 0.0
x_window = (0, 0)
for lab in self.config.labs:
lab_date = lab['date']
lab_value = lab['values']
plot_start = datetime.combine(self.config.model['start_date'], time(0, 0, 0))
if self.config.graph['use_x_date']:
lab_time = lab_date
min_t = min(min_t, lab_time)
max_t = max(max_t, lab_time)
# if lab_time < min_t:
# min_t = lab_time
# if lab_time > max_t:
# max_t = lab_time
x_window = (min_t - timedelta(days=7), max_t + timedelta(days=7))
else:
lab_time = (lab_date - plot_start).total_seconds() / self.config.graph['units'].total_seconds()
min_t = min(min_t, lab_time)
max_t = max(max_t, lab_time)
x_window = (max(min_t - 7, 0), min(max_t + 7, self.duration))
times.append(lab_time)
for drug_key, lab_val in lab_value.items():
predicted = self.model.get_blood_level_at_timepoint(drug_key, lab_date)
predicted = predicted[0] * predicted[1]
if drug_key not in prediction_data:
prediction_data[drug_key] = []
# print(f"{predicted} -> {lab_val}")
val = ((lab_val - predicted) / lab_val) * 100
if val >= 0:
magnitude = max(magnitude, val)
else:
magnitude = max(magnitude, -val)
prediction_data[drug_key].append(val)
for drug_key, data in prediction_data.items():
arrays[drug_key] = (np.array(data), np.array(data), np.array(data))
if self.config.graph['use_x_date']:
duration_labs = math.ceil((max_t - min_t + timedelta(days=14)).total_seconds() / (3600 * 24))
else:
duration_labs = max_t - min_t + 14
delta = 7
tick_count = math.floor(duration_labs / delta)
while tick_count > 12:
delta *= 2
tick_count = math.floor(duration_labs / delta)
plot_drugs(data=(np.array(times), arrays),
x_window=x_window,
y_window=(-magnitude * 1.2, magnitude * 1.2),
x_ticks=delta,
x_label=self.config.graph['x_label'],
y_label="Deviation of estimation (%)",
title="Prediction accuracy",
plot_markers=True,
plot_dates=self.config.graph['use_x_date'],
)
if __name__ == '__main__':
HormoneLevels()