-
Notifications
You must be signed in to change notification settings - Fork 0
/
populate_db.py
executable file
·355 lines (317 loc) · 12.5 KB
/
populate_db.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/env python
# encoding: utf-8
"""
populate_db.py
Created by Dr. H on 2010-06-28.
Copyright (c) 2010 UTHSC School of Health Information Sciences. All rights reserved.
"""
import sys
import os
import psycopg2
import random
from decimal import *
AGE_MIN=18
AGE_MAX=99
AGE_NORMAL=25
AGE_SIGMA=20
#Commit the transaction every so many patients
COMMIT_EVERY=5000
#VISITS_PER_PATIENT_NORMAL=3
#VISITS_PER_PATIENT_SIGMA=5
VISITS_PER_PATIENT_PARETO_ALPHA=1
LABS_PER_CONDITION_NORMAL=2
LABS_PER_CONDITION_SIGMA=2
EXTRA_LABS_PER_VISIT_NORMAL=0
EXTRA_LABS_PER_VISIT_SIGMA=1
OLDEST_VISIT=10*365 # In days ago
# Probabilities
PROVIDER_NOT_AVAILABLE=0.07
MAX_PROBABILTY_PROVIDER_MESSES_UP_PER_VISIT=0.02
PROBABILITY_CONDITION_MISSED=0.3 # Otherwise, the provider will add a wrong condition
#Probability another lab is abnormal
PROBABILITY_SPURIOUS_ABNORMAL_LAB=0.01
def normal_random_with_min_bounds(normal, sigma, min_acceptable=0.0):
variable=min_acceptable-1
while variable<min_acceptable:
variable=random.normalvariate(normal, sigma)
return variable
def normal_int_with_min_bounds(normal, sigma, min_acceptable=0):
return int(normal_random_with_min_bounds(normal, sigma, min_acceptable))
class lab_test(object):
def __init__(self, name, minimum, maximum, abs_min=0.0):
self._name=name
self._minimum=float(minimum)
self._maximum=float(maximum)
self._normal=float(self._minimum+self._maximum)/2.0
self._sigma=abs(float(self._maximum-self._minimum))/4.0
self._abs_min=0.0
def name():
doc = "The name property."
def fget(self):
return self._name
return locals()
name = property(**name())
def minimum():
doc = "The minimum property."
def fget(self):
return self._minimum
return locals()
minimum = property(**minimum())
def maximum():
doc = "The maximum property."
def fget(self):
return self._maximum
return locals()
maximum = property(**maximum())
def normal_result(self):
return normal_random_with_min_bounds(self._normal,
self._sigma,
self._abs_min)
def abnormal_result(self):
# Low or high?
if random.random() < 0.5:
# Low
return normal_random_with_min_bounds(self._minimum-3.0*self._sigma,
self._sigma,
self._abs_min)
else:
# High
return normal_random_with_min_bounds(self._maximum+3.0*self._sigma,
self._sigma,
self._abs_min)
def __repr__(self):
return "<Lab test: %s>" % (self.name,)
def __eq__(self, other):
return self._name==other._name
class disease(object):
def __init__(self, code, name, prevalence, probability_treated_here):
self._name=name
self._code=code
self._prevalence=float(prevalence)
self._p_treated_here=float(probability_treated_here)
def name():
doc = "The name property."
def fget(self):
return self._name
return locals()
name = property(**name())
def code():
doc = "The code property."
def fget(self):
return self._code
return locals()
code = property(**code())
def prevalence():
doc = "The prevalence property."
def fget(self):
return self._prevalence
return locals()
prevalence = property(**prevalence())
def probability_treated_here():
doc = "The probability_treated_here property."
def fget(self):
return self._p_treated_here
return locals()
probability_treated_here = property(**probability_treated_here())
def __repr__(self):
return "<Condition %s: %s>" % (self.code, self.name)
def __eq__(self, other):
return self._code==other._code
def setup_connection(DBNAME):
return psycopg2.connect('dbname=%s' % (DBNAME,))
def get_valid_providers(connection):
cur=connection.cursor()
cur.execute("SELECT name FROM providers")
return [x[0] for x in cur.fetchall()]
def get_valid_races(connection):
cur=connection.cursor()
cur.execute("SELECT name FROM races")
return [x[0] for x in cur.fetchall()]
def get_valid_lab_tests(connection):
cur=connection.cursor()
cur.execute("SELECT lab_testid, normal_min, normal_max FROM lab_tests")
return [lab_test(*x) for x in cur.fetchall()]
def get_diseases(connection):
cur=connection.cursor()
cur.execute("SELECT * FROM icd_codes")
return [disease(*x) for x in cur.fetchall()]
def create_patient(cursor, races):
age=0
while age < AGE_MIN or age > AGE_MAX:
age=int(random.normalvariate(AGE_NORMAL, AGE_SIGMA))
cursor.execute("SELECT NewPatient(%s, %s)", (age, random.choice(races)))
return cursor.fetchone()[0]
def chance_providers_mess_up(providers):
"""Creates a dictionary specifying the probability that each provider makes
a diagnostic mistake."""
prob={}
for provider in providers:
prob[provider]=random.random() * \
MAX_PROBABILTY_PROVIDER_MESSES_UP_PER_VISIT
return prob
def generate_order_sets(conditions, labs):
labs_per_condition={}
for c in conditions:
num_labs=normal_int_with_min_bounds(LABS_PER_CONDITION_NORMAL,
LABS_PER_CONDITION_SIGMA)
labs_per_condition[c]=set(random.sample(labs, num_labs))
return labs_per_condition
def save_order_sets(cursor, order_sets):
for condition, labs in order_sets.iteritems():
for lab in labs:
cursor.execute("INSERT INTO order_sets (icd_code, lab_test) "
"VALUES (%s, %s)", (condition.code, lab.name))
return
def decide_which_lab_is_abnormal_for_each_condition(order_sets):
abnormal_labs={}
for o in order_sets:
if len(order_sets[o])>0:
abnormal_labs[o]=random.choice(list(order_sets[o]))
return abnormal_labs
def visits_one_patient():
#return normal_int_with_min_bounds(VISITS_PER_PATIENT_NORMAL, VISITS_PER_PATIENT_SIGMA, 1)
return int(random.paretovariate(VISITS_PER_PATIENT_PARETO_ALPHA))
def labs_one_visit(patient_conditions, order_sets, lab_tests):
labs=set([])
for c in patient_conditions:
labs|=order_sets[c[0]]
# add an extra lab here or there
extra_labs=normal_int_with_min_bounds(EXTRA_LABS_PER_VISIT_NORMAL,
EXTRA_LABS_PER_VISIT_SIGMA)
try:
labs|=set(random.sample(lab_tests, extra_labs))
except ValueError:
# Tried to sample too many
labs|=set(lab_tests)
return labs
def assign_lab_values(labs_this_patient, conditions, abnormal_labs):
labs_with_values=[]
to_be_abnormal=set([])
for c in conditions:
try:
to_be_abnormal.add(abnormal_labs[c[0]])
except KeyError:
# This condition has no abnormal labs
pass
for l in labs_this_patient:
if l in to_be_abnormal:
labs_with_values.append((l, l.abnormal_result()))
else:
if random.random()<PROBABILITY_SPURIOUS_ABNORMAL_LAB:
labs_with_values.append((l, l.abnormal_result()))
else:
labs_with_values.append((l, l.normal_result()))
return labs_with_values
def save_visit(cursor, patient_number, provider, date):
cursor.execute("SELECT NewVisit(%s, %s, %s)", (provider, patient_number, date))
return cursor.fetchone()[0]
def save_problems(cursor, visitid, conditions):
for c in conditions:
cursor.execute("INSERT INTO problems (visit, icd_code) VALUES (%s, %s)",
(visitid, c[0].code))
return
def save_labs(cursor, visitid, labs):
for l in labs:
cursor.execute("INSERT INTO labs (visit, lab_test, value) VALUES (%s, %s, %s)", (visitid, l[0].name, l[1]))
return
def save_billing(cursor, visitid, conditions):
for c in conditions:
if c[1]:
cursor.execute("INSERT INTO billing (visit, icd_code) "
"VALUES (%s, %s)", (visitid, c[0].code))
return
def save_patient_ground_truth(cursor, patientid, patient_conditions):
for c in patient_conditions:
cursor.execute("INSERT INTO patients_ground_truth"
" (patient, icd_code, treated_here) VALUES (%s, %s, %s)",
(patientid, c[0].code, c[1]))
return
def generate_patient_conditions(diseases):
conditions=[]
for d in diseases:
if random.random() < d.prevalence:
treated_here=(random.random() < d.probability_treated_here)
conditions.append((d, treated_here))
return conditions
def generate_patient_history(cursor, patient_number, diseases, providers, lab_tests, order_sets, abnormal_labs):
# This is what the patient actually has
patient_conditions=generate_patient_conditions(diseases)
save_patient_ground_truth(cursor, patient_number, patient_conditions)
patient_provider=random.choice(providers.keys())
num_visits=visits_one_patient()
max_visit_spacing=OLDEST_VISIT/num_visits
visit_date=int(random.random()*max_visit_spacing*num_visits)
for i in xrange(num_visits):
this_visits_provider=patient_provider
if random.random() < PROVIDER_NOT_AVAILABLE:
# Replaced by someone else for this visit
this_visits_provider=random.choice(providers.keys())
this_visits_conditions=patient_conditions[:] # Copy
if random.random() < providers[this_visits_provider]:
# Provider screwed up.
if random.random() < PROBABILITY_CONDITION_MISSED:
if len(this_visits_conditions)>0:
this_visits_conditions.remove(
random.choice(this_visits_conditions))
else:
# Add a new condition to this visit
this_visits_conditions.append(
random.choice([(x, random.random() <
x.probability_treated_here)
for x in diseases if x not in
[y[0] for y in this_visits_conditions]]))
visit_id=save_visit(cursor, patient_number,
this_visits_provider, visit_date)
visit_date-=int(random.random() * max_visit_spacing)
save_problems(cursor, visit_id, this_visits_conditions)
save_billing(cursor, visit_id, this_visits_conditions)
this_visits_labs=labs_one_visit(this_visits_conditions,
order_sets, lab_tests)
lab_results=assign_lab_values(this_visits_labs,
this_visits_conditions, abnormal_labs)
save_labs(cursor, visit_id, lab_results)
return
def draw_spinner(SPINNER="/-\\|", steps=100, pos=[None]):
if pos[0] is not None:
sys.stdout.write(chr(8))
else:
pos[0]=0
if pos[0] % steps==0:
sys.stdout.write('-')
sys.stdout.write(SPINNER[pos[0] % len(SPINNER)])
sys.stdout.flush()
pos[0]+=1
def main():
DBNAME=sys.argv[1]
NUM_PATIENTS=int(sys.argv[2])
random.seed()
conn=setup_connection(DBNAME)
providers=get_valid_providers(conn)
races=get_valid_races(conn)
lab_tests=get_valid_lab_tests(conn)
diseases=get_diseases(conn)
print "Providers=", providers
failure_rates=chance_providers_mess_up(providers)
print "Failure rates=", failure_rates
print "Diseases=", diseases
print "Races=", races
print "Lab tests=", lab_tests
order_sets=generate_order_sets(diseases, lab_tests)
print "Order sets=", order_sets
abnormal_labs=decide_which_lab_is_abnormal_for_each_condition(order_sets)
print "Abnormal labs=", abnormal_labs
print "Inserting %d patients" % NUM_PATIENTS
cur=conn.cursor()
save_order_sets(cur, order_sets)
print "Working: ",
for x in xrange(NUM_PATIENTS): # REPLACE WITH NUM_PATIENTS
this_patient=create_patient(cur, races)
generate_patient_history(cur, this_patient, diseases, failure_rates, lab_tests, order_sets, abnormal_labs)
draw_spinner()
if x % COMMIT_EVERY==0:
# Commmit every 5000
conn.commit()
print "\nDone."
conn.commit()
if __name__ == '__main__':
main()