-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_output.py
182 lines (149 loc) · 7.3 KB
/
parse_output.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
"""
Parse Output.
Usage:
parse_output.py OUTPUT_PATH MATRIX_PATH [--straight-to-csv] [--round-to-csv] [--descriptive] [--frequency] [--demo-frequency] [--diag-frequency]
parse_output.py (-h | --help)
parse_output.py --version
Options:
--straight-to-csv Export the numpy matrix to csv as is
--round-to-csv Export the numpy matrix to csv rounding to 0 or 1.
--descriptive Prepare descriptive records for each patient/admission.
--frequency Condense descriptive records by frequency.
--demo-frequency Condense descriptive records by frequency of demographic data.
--diag-frequency Condense descriptive records by frequency of diagnoses.
--h --help Show this screen.
--version Show version.
"""
from __future__ import print_function
import csv
import pickle as pickle
import sys
from collections import namedtuple, Counter
import numpy as np
from docopt import docopt
from future import standard_library
from icd9 import ICD9
standard_library.install_aliases()
def export_to_csv(path, table, header=[]):
with open(path, "w", newline="") as csv_file:
csv_writer = csv.writer(csv_file, delimiter=",", quoting=csv.QUOTE_ALL)
# Write header
csv_writer.writerow(header)
# Write content
csv_writer.writerows(table)
def create_descriptive_recordset(input_data, one_hot_map):
# Create IC9 map of diagnoses codes & Record
descriptors = {
"admloc_": "admission_location",
"disloc_": "discharge_location",
"ins_": "insurance",
"lang_": "language",
"rel_": "religion",
"mar_": "marital_status",
"eth_": "ethnicity",
"gen_": "gender",
"dia_": "diagnoses"
}
enabled_descriptors = set()
tree = ICD9()
diagnostic_codes_map = {}
for key in one_hot_map.keys():
for descriptor in descriptors.keys():
if key.startswith(descriptor):
enabled_descriptors.add(descriptors[descriptor])
if key.startswith("dia_"):
try:
condition = key[len("dia_D_"):]
diagnostic_codes_map[key] = "{}-{}".format(condition,
tree.find(condition).description)
except:
diagnostic_codes_map[key] = key[len("dia_D_"):]
DescriptiveRecord = namedtuple("DescriptiveRecord", list(sorted(enabled_descriptors)))
# Actually process stuff
sparse_descriptive_records = []
for i, record in enumerate(input_data):
record_lists = {key: [] for key in enabled_descriptors}
# Process record
for column in record:
for column_mask, column_description in descriptors.items():
if column.startswith(column_mask):
if column_mask == "dia_":
condition = column[len("dia_D_"):]
condition_description = "{}-{}".format(condition,
diagnostic_codes_map[column])
record_lists[column_description].append(condition_description)
else:
record_lists[column_description].append(column[len(column_mask):])
# Package record
packaged_record = [tuple(record_lists[key]) for key in sorted(enabled_descriptors)]
sparse_descriptive_records.append(DescriptiveRecord(*packaged_record))
# Report progress
if (i + 1) % 1000 == 0:
print("{} records processed so far.".format(i + 1))
return sparse_descriptive_records, list(sorted(enabled_descriptors))
if __name__ == '__main__':
arguments = docopt(__doc__, version='medGAN - Parse Output 1.0')
# Load data
data = np.load("{}.npy".format(arguments["OUTPUT_PATH"]))
with open("{}.types".format(arguments["MATRIX_PATH"]), "rb") as f:
one_hot_map = pickle.load(f)
# Reverse map
index_map = {index: one_hot for one_hot, index in one_hot_map.items()}
matrix_header = [index_map[index] for index in range(0, len(index_map))]
if arguments["--straight-to-csv"]:
export_to_csv("{}.csv".format(arguments["OUTPUT_PATH"]), data, header=matrix_header)
if arguments["--round-to-csv"]:
rounded_data = [[int(round(item)) for item in row] for row in data]
export_to_csv(
"{}.csv".format(arguments["OUTPUT_PATH"]),
rounded_data,
header=matrix_header
)
if arguments["--descriptive"] or arguments["--frequency"] or arguments["--demo-frequency"] \
or arguments["--diag-frequency"]:
cleaned_data = [[index_map[i] for i, item in enumerate(row) if int(round(item)) == 1]
for row in data]
sparse_descriptive_records, descriptive_header = create_descriptive_recordset(cleaned_data,
one_hot_map)
if arguments["--descriptive"]:
processed_descriptive_records = [[";".join(item) for item in record]
for record in sparse_descriptive_records]
export_to_csv(
"{}_descriptive.csv".format(arguments["OUTPUT_PATH"]),
processed_descriptive_records,
header=descriptive_header
)
if arguments["--frequency"]:
records_counter = Counter(sparse_descriptive_records)
parsed_records_counter = [[count, *[";".join(item) for item in record]]
for record, count in records_counter.most_common()]
export_to_csv(
"{}_frequency.csv".format(arguments["OUTPUT_PATH"]),
parsed_records_counter,
header=["Count"] + descriptive_header
)
if arguments["--diag-frequency"]:
filtered_records = [record.diagnoses for record in sparse_descriptive_records]
records_counter = Counter(filtered_records)
parsed_records_counter = [[count, ";".join(record)]
for record, count in records_counter.most_common()]
export_to_csv(
"{}_diagnoses_frequency.csv".format(arguments["OUTPUT_PATH"]),
parsed_records_counter,
header=["Count", "Diagnoses"]
)
if arguments["--demo-frequency"]:
filtered_records = [
tuple({field: getattr(record, field)
for field in record._fields if field != "diagnoses"}.items())
for record in sparse_descriptive_records
]
records_counter = Counter(filtered_records)
parsed_records_counter = [[count, record]
for record, count in records_counter.most_common()]
export_to_csv(
"{}_demo_frequency.csv".format(arguments["OUTPUT_PATH"]),
[[count, *[";".join(item[1]) for item in record]]
for record, count in records_counter.most_common()],
header=["Count"] + list([key for key, values in filtered_records[0]])
)