-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicd9.py
executable file
·68 lines (55 loc) · 1.86 KB
/
icd9.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
#!/usr/bin/python3
import os
import re
import time
import touch_icd9 as touch
# read input data
inputFile = input("Please enter the input CSV file name: ")
# inputFile = "sample_icd9.csv"
try:
fhand0 = open(inputFile)
except:
print("Error: failed to open/find", inputFile)
exit()
# define output file name
basePath = os.path.basename(inputFile)
outputFile = re.sub(".csv", "_touch.csv", basePath)
# read in dictionaries
touch.dicts_icd9()
# read input and write output line by line
fout = open(outputFile, "w")
firstObs = 1
for line in fhand0:
tmpLine = line.strip().lower()
tmpLine = re.sub('"|[ ]', '', tmpLine)
oneLine = tmpLine.split(",")
if firstObs:
input_colNames = oneLine
output_colNames = [touch.dicts_icd9.colNames[i].upper()
for i in range(len(touch.dicts_icd9.colNames))]
fout.write(",".join(output_colNames) + '\n')
dx_idx = []
drg_idx = None
for i in range(len(input_colNames)):
if input_colNames[i].startswith("dx"):
dx_idx.append(i)
if input_colNames[i].startswith("drg"):
drg_idx = i
firstObs = 0
# quick check on dx_idx and drg_idx
if len(dx_idx) <= 1:
print("Error: failed to locate (secondary) diagnoses code",
"in the input file:", inputFile)
exit()
if drg_idx is None:
print("Error: failed to locate DRG code",
"in the input file:", inputFile)
exit()
else:
tmp = touch.icd9(oneLine, drg_idx, dx_idx, touch.dicts_icd9)
fout.write(",".join(list(map(str, tmp))) + "\n")
fout.close()
# output message
print("Comorbidity measures have been successfully generated.")
print("Output file:", outputFile)
print("The system and user CPU time: %.3f seconds." % time.process_time())