-
Notifications
You must be signed in to change notification settings - Fork 8
/
parse.py
467 lines (393 loc) · 18.4 KB
/
parse.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#!/usr/bin/env python
import os
from glob import glob
import json
import pandas
import datetime
# This is a challenging dataset because it's so big!
# We will do our best to break the data into pieces
here = os.path.dirname(os.path.abspath(__file__))
folder = os.path.basename(here)
latest = '%s/latest' % here
year = datetime.datetime.today().year
output_data = os.path.join(here, 'data-latest-1.tsv')
output_year = os.path.join(here, 'data-%s-1.tsv' % year)
# Don't continue if we don't have latest folder
if not os.path.exists(latest):
print('%s does not have parsed data.' % folder)
sys.exit(0)
# Don't continue if we don't have results.json
results_json = os.path.join(latest, 'records.json')
if not os.path.exists(results_json):
print('%s does not have results.json' % folder)
sys.exit(1)
with open(results_json, 'r') as filey:
results = json.loads(filey.read())
columns = ['charge_code',
'price',
'description',
'hospital_id',
'filename',
'charge_type']
df = pandas.DataFrame(columns=columns)
seen = []
for r in range(423 ,len(results)):
result = results[r]
filename = os.path.join(latest, result['filename'])
if not os.path.exists(filename):
print('%s is not found in latest folder.' % filename)
continue
if os.stat(filename).st_size == 0:
print('%s is empty, skipping.' % filename)
continue
charge_type = 'standard'
if "drg" in filename.lower():
charge_type = "drg"
if result['filename'] in seen:
continue
seen.append(result['filename'])
print("Parsing %s" % filename)
if filename.endswith('xlsx'):
# has counts, description, procedure type (not costs)
if "common25" in filename.lower():
continue
# Unfortunately cdm_all files are inconsistent, would need custom parsing (for sheets) each
elif "cdm_all" in filename.lower():
continue
# ['Charge Code', 'Charge Description', 'Charge Amount', 'Comments']
elif "106071018_CDM" in filename or "106070988_CDM" in filename:
content = pandas.read_excel(filename)
description_key = 'Charge Description'
price_key = 'Charge Amount'
code_key = 'Charge Code'
# ['Charge Code', 'Charge Description', 'Price', 'Comments']
elif "106074039_CDM" in filename:
content = pandas.read_excel(filename)
description_key = 'Charge Description'
price_key = 'Price'
code_key = 'Charge Code'
# ['Description', 'Code', 'Unnamed: 2', 'Unnamed: 3', 'Price', 'Tier Code', 'Dept', 'Subd', 'Elem', 'Stat']
# Writing over row of dashes ----
elif "106420491_CDM" in filename:
content = pandas.read_excel(filename, skiprows=2)
content.columns = ['Description',
'Code',
'Unnamed: 2', 'Unnamed: 3',
'Price',
'Tier Code',
'Dept',
'Subd',
'Elem',
'Stat']
description_key = 'Description'
price_key = 'Price'
code_key = 'Code'
# ['Fac', 'Charge #', 'Description', 'Price', 'GL Key']
elif "106301357_CDM" in filename:
content = pandas.read_excel(filename, skiprows=5)
description_key = 'Description'
price_key = 'Price'
code_key = 'Charge #'
# ['Chrg Code', 'Chrg Desc', 'Chrg Amt IP', 'Chrg Amt OP']
elif "106430779_CDM" in filename:
content = pandas.read_excel(filename, skiprows=5)
description_key = 'Chrg Desc'
code_key = 'Chrg Code'
for row in content.iterrows():
# Outpatient
idx = df.shape[0] + 1
entry = [row[1][code_key], # charge code
row[1]['Chrg Amt OP'], # price
row[1][description_key], # description
result["hospital_id"], # hospital_id
result['filename'],
'outpatient']
df.loc[idx,:] = entry
# Inpatient
idx = df.shape[0] + 1
entry = [row[1][code_key], # charge code
row[1]['Chrg Amt IP'], # price
row[1][description_key], # description
result["hospital_id"], # hospital_id
result['filename'],
'inpatient']
df.loc[idx,:] = entry
# ['Charge code', 'Charge Description', 'Charge Cat', 'Previous Price', 'Current Price', 'UOS', 'Charges', 'Change', '% Change', 'Note'],
elif "106190449_CDM" in filename:
content = pandas.read_excel(filename, skiprows=1)
description_key = 'Charge Description'
price_key = 'Current Price'
code_key = 'Charge code'
# ['CDM NO', 'DISPENSED DESCRIPTION', 'PRICE ', 'TYPE', 'NOTE'
elif "106331152_CDM" in filename:
content = pandas.read_excel(filename)
description_key = 'DISPENSED DESCRIPTION'
price_key = 'PRICE '
code_key = 'CDM NO'
# ['Charge Description', 'Price', 'Comment']
elif "106430763_CDM" in filename:
content = pandas.read_excel(filename)
description_key = 'Charge Description'
price_key = 'Price'
code_key = None
# ['Item # ', 'Description', 'Unit of Measure', 'Patient Charge']
elif "106331168_CDM(2)" in filename:
content = pandas.read_excel(filename)
description_key = 'Description'
price_key = 'Patient Charge'
code_key = 'Item # '
# ['Procedure Code', 'Description', 'Unit Price']
elif "106331168_CDM(1)" in filename:
content = pandas.read_excel(filename, skiprows=3)
description_key = 'Description'
price_key = 'Unit Price'
code_key = 'Procedure Code'
# ['Item\nNumber', '\nDescription', '\nBegin Date ', '\nEnd Date', '\nUnits', '\nCharge By', '\nCost', 'Base Price/\nMarkup']
elif "106196404_CDM(1)" in filename:
content = pandas.read_excel(filename, skiprows=5)
description_key = '\nDescription'
price_key = '\nCost'
code_key = None
elif "106196404_CDM(2)" in filename:
content = pandas.read_excel(filename, skiprows=5)
description_key = '\nDescription'
price_key = '\nCost'
code_key = None
# ['CDMCHRG#', 'CDMDSC', 'NEWPRICE', 'STAT']
elif "106440755_CDM" in filename:
content = pandas.read_excel(filename)
description_key = 'CDMDSC'
price_key = 'NEWPRICE'
code_key = 'CDMCHRG#'
# ['Code', 'Description', 'Code.1', ' Amount ']
elif "106190256_CDM" in filename:
content = pandas.read_excel(filename, skiprows=4)
description_key = 'Description'
price_key = ' Amount '
code_key = 'Code'
# ['PROC (CDM)', 'DRUG DESCRIPTION', 'CHARGE']
elif "106364231_CDM_RX" in filename:
content = pandas.read_excel(filename, skiprows=7)
description_key = "DRUG DESCRIPTION"
price_key = "CHARGE"
code_key = 'PROC (CDM)'
# 'Charge Code', 'Description', 'CPT Code', 'Rate'
elif "106070924_CDM" in filename:
content = pandas.read_excel(filename, skiprows=3)
description_key = "Description"
price_key = "Rate"
code_key = "Charge Code"
# Code Description Code.1 Amount
elif "106190766_CDM" in filename or "106190197_CDM" in filename:
content = pandas.read_excel(filename, skiprows=4)
description_key = "Description"
price_key = "Amount"
code_key = "Code"
# ['PROC_NAME', 'CHARGE_AMOUNT', 'COMMENT', 'Unnamed: 3']
elif "106304409_CDM" in filename or "106196035_CDM" in filename or "106196403_CDM" in filename or "106361223_CDM" in filename or "106014132_CDM" in filename or "106104062_CDM" in filename or "106190429_CDM" in filename or "106394009_CDM" in filename:
content = pandas.read_excel(filename)
description_key = "PROC_NAME"
price_key = "CHARGE_AMOUNT"
code_key = None
# ['CDM#', 'CDM Description', 'Facility', 'gl_account_id', 'Rev Code', 'Price', 'CPT/HCPCS']
elif "106301188_CDM" in filename or "106301140_CDM" in filename:
content = pandas.read_excel(filename, skiprows=1)
description_key = "CDM Description"
price_key = "Price"
code_key = "CDM#"
# ['CDM #', 'Description', 'Price']
elif "106190298_CDM" in filename:
content = pandas.read_excel(filename, skiprows=4)
description_key = 'Description'
price_key = "CDM #"
code_key = 'Price'
# ['CHARGE #', 'DESC', 'REV', 'CPT', 'PRICE', 'Unnamed: 5']
elif "106220733_CDM" in filename:
content = pandas.read_excel(filename, skiprows=4)
description_key = 'DESC'
price_key = 'PRICE'
code_key = 'CHARGE #'
# ['PROC (CDM)', 'CHG CAT', 'ARMC REV DEPT', 'PROCEDURE (CDM) DESCRIPTION', 'CHARGE', 'CPT-4', 'MCLcde']
elif "106364231_CDM" in filename:
content = pandas.read_excel(filename, skiprows=7)
description_key = 'PROCEDURE (CDM) DESCRIPTION'
price_key = "CHARGE"
code_key = 'PROC (CDM)'
#['PROCEDURE', 'DESCRIPTION', 'Unnamed: 2', 'Unnamed: 3', 'Unnamed: 4', 'Unnamed: 5', 'STD AMOUNT']
elif "106010887_CDM" in filename:
content = pandas.read_excel(filename, skiprows=3)
description_key = "DESCRIPTION"
price_key = "STD AMOUNT"
code_key = "PROCEDURE"
# ['PROC_NAME', 'CHARGE_AMOUNT', 'COMMENT']
elif "106074097_CDM" in filename:
content = pandas.read_excel(filename, skiprows=1)
description_key = "PROC_NAME"
price_key = "CHARGE_AMOUNT"
code_key = None
# Service ID', 'User Gen. Service ID', 'Service Name', 'Effective Date', 'Price ($)'
elif "106474007_CDM" in filename:
content = pandas.read_excel(filename, skiprows=1)
description_key = "Service Name"
price_key = "Price ($)"
code_key = 'Service ID'
# ['Code ', 'Procedure_Name', 'Cost']
elif "106304159_CDM" in filename:
content = pandas.read_excel(filename, skiprows=2)
description_key = "Procedure_Name"
price_key = "Cost"
code_key = 'Code '
elif "106301127_CDM" in filename:
content = pandas.read_excel(filename)
description_key = "chg_desc"
price_key = "chg_amt_1"
code_key = "chg_code"
# ['PROCEDURE', 'DESCRIPTION', 'DEPARTMENT', 'CHG CAT', 'COST', 'STD AMOUNT CPT HCPC', 'A']
elif "106331194_CDM" in filename:
content = pandas.read_excel(filename)
description_key = "DESCRIPTION"
price_key = 'STD AMOUNT CPT HCPC'
code_key = "PROCEDURE"
elif "106370749_" in filename:
content = pandas.read_excel(filename)
description_key = "Description"
price_key = "Patient Price"
code_key = None
# ['Level of Care', 'Begin Date', 'End Date', 'Charge By', 'Base Price ', 'Unnamed: 5']
elif "106196404_CDM(4)" in filename:
content = pandas.read_excel(filename, skiprows=6)
description_key = 'Level of Care'
price_key = 'Base Price '
code_key = None
elif "106196404_CDM(3)" in filename:
content = pandas.read_excel(filename, skiprows=7)
description_key = 'Level of Care'
price_key = 'Base Price '
code_key = None
# ['PROC (CDM)', 'DRUG DESCRIPTION', 'CHARGE']
elif "106364231_CDM_RX" in filename:
content = pandas.read_excel(filename, skiprows=7)
description_key = "DRUG DESCRIPTION"
price_key = "CHARGE"
code_key = 'PROC (CDM)'
# ['CHARGE #', 'CHARGE DESCRIPTION', 'CPT-4', 'PT CHG $', 'INS CD']
elif "106301155_CDM" in filename:
content = pandas.read_excel(filename, skiprows=4)
code_key = 'CHARGE #'
description_key = 'CHARGE DESCRIPTION'
price_key = 'PT CHG $'
# ['Reference ID', 'Description', 'Price']
elif "106364014_CDM" in filename or "106364502_CDM" in filename or "106361246_CDM" in filename or "106334589_CDM" in filename:
content = pandas.read_excel(filename, skiprows=4)
code_key = 'Reference ID'
description_key = 'Description'
price_key = 'Price'
elif "106090793_CDM_RX" in filename:
content = pandas.read_excel(filename)
additional_row = content.columns.tolist()
idx = content.shape[0] + 1
content.loc[idx] = additional_row
content.columns = ['DESCRIPTION', "CODE", "PRICE"]
code_key = "CODE"
description_key = "DESCRIPTION"
price_key = 'PRICE'
# ['Unnamed: 0', 'CHARGE CODE', 'CHARGE DESCRIPTION', 'PRICE', 'Unnamed: 4']
elif "106130699_CDM" in filename:
content = pandas.read_excel(filename, skiprows=6)
code_key = "CHARGE CODE"
description_key = "CHARGE DESCRIPTION"
price_key = 'PRICE'
elif "106090793_CDM" in filename:
# ['EAP PROC CODE', 'CODE DESCRIPTION', 'CPT', 'REV CODE', 'UNIT CHARGE AMOUNT']
content = pandas.read_excel(filename)
code_key = "CPT"
description_key = "CODE DESCRIPTION"
price_key = 'UNIT CHARGE AMOUNT'
elif "106190555_CDM" in filename:
# 'Charge\nCode', 'Description', 'CPT/ HCPCS\nCode', 'OP/ Default Price', 'IP/ER\nPrice'
content = pandas.read_excel(filename, skiprows=4)
code_key = 'Charge\nCode'
description_key = "Description"
for row in content.iterrows():
# Outpatient
idx = df.shape[0] + 1
entry = [row[1][code_key], # charge code
row[1]['OP/ Default Price'], # price
row[1][description_key], # description
result["hospital_id"], # hospital_id
result['filename'],
'outpatient']
df.loc[idx,:] = entry
# Inpatient
idx = df.shape[0] + 1
entry = [row[1][code_key], # charge code
row[1]['IP/ER\nPrice'], # price
row[1][description_key], # description
result["hospital_id"], # hospital_id
result['filename'],
'inpatient']
df.loc[idx,:] = entry
continue
elif "cdm_" in filename.lower():
# ['2018 Charge Codes', 'Charge Codes Description', 'HCPCS Codes', 'June 2018 Prices']
content = pandas.read_excel(filename, skiprows=4)
code_key = '2018 Charge Codes'
description_key = 'Charge Codes Description'
price_key = 'June 2018 Prices'
if code_key not in content.columns.tolist():
code_key = None
description_key = 'Description'
price_key = 'Patient Price'
if description_key not in content.columns.tolist():
content = pandas.read_excel(filename)
description_key = "PROC_NAME"
price_key = "CHARGE_AMOUNT"
code_key = None
if description_key not in content.columns.tolist():
description_key = "DESCRIPTION"
price_key = "STD AMOUNT"
code_key = "PROCEDURE"
elif "cdm(" in filename.lower():
# CDM # Description Price
content = pandas.read_excel(filename, skiprows=4)
code_key = "CDM #"
description_key = "Description"
price_key = "Price"
elif "pct_chg" in filename.lower():
continue
elif "drg" in filename.lower():
#['Hospital', 'MS DRG', 'MS DRG Desc', 'Rank', 'Cases', 'Total Charges', 'Avg Chrg / Case']
content = pandas.read_excel(filename, skiprows=8)
code_key = "MS DRG"
description_key = "MS DRG Desc"
price_key = 'Avg Chrg / Case'
else:
continue
for row in content.iterrows():
if code_key != None:
code = row[1][code_key]
else:
code = None
price = row[1][price_key]
if pandas.isnull(price):
continue
idx = df.shape[0] + 1
entry = [code, # charge code
price, # price
row[1][description_key], # description
result["hospital_id"], # hospital_id
result['filename'],
charge_type]
df.loc[idx,:] = entry
# When we get to index 350 (hospital_id 'kaiser-foundation-hospital---walnut-creek')
# It's time to save and start a new data file, we just hit the max Github file size
if result['hospital_id'] == 'kaiser-foundation-hospital---walnut-creek':
# Remove empty rows
df = df.dropna(how='all')
# Save data!
print(df.shape)
df.to_csv(output_data, sep='\t', index=False)
df.to_csv(output_year, sep='\t', index=False)
output_data = os.path.join(here, 'data-latest-2.tsv')
output_year = os.path.join(here, 'data-%s-2.tsv' % year)
df = pandas.DataFrame(columns=columns)