-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
219 lines (198 loc) · 6.19 KB
/
main.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
import os
import time
os.environ["TZ"] = "UTC"
time.tzset()
import sys
import logging
import argparse
from pyspark.sql import SparkSession
from ehrs import EHR_REGISTRY
logging.basicConfig(
format="%(asctime)s | %(levelname)s | %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
level=os.environ.get("LOGLEVEL", "INFO").upper(),
stream=sys.stdout,
)
logger = logging.getLogger(__name__)
def get_parser():
parser = argparse.ArgumentParser()
# task
parser.add_argument(
"--task",
type=str,
default=None,
help="specific task from the whole pipeline."
"if not set, run the whole steps.",
)
parser.add_argument(
"--dest", default="outputs", type=str, metavar="DIR", help="output directory"
)
parser.add_argument(
"--valid-percent",
default=0.1,
type=float,
metavar="D",
help="percentage of data to use as validation and test set (between 0 and 0.5)",
)
parser.add_argument("--seed", default="42", type=str, metavar="N", help="random seed")
# data
parser.add_argument(
"--ehr", type=str, required=True, choices=['mimiciii', 'mimiciv', 'eicu'],
help="name of the ehr system to be processed."
)
parser.add_argument(
"--data",
metavar="DIR",
default=None,
help="directory containing data files of the given ehr (--ehr)."
"if not given, try to download from the internet.",
)
parser.add_argument(
"--ext",
type=str,
default=None,
help="extension for ehr data to look for. "
"if not given, try to infer from --data"
)
parser.add_argument(
"--ccs",
type=str,
default=None,
help="path to `ccs_multi_dx_tool_2015.csv`"
"if not given, try to download from the internet.",
)
parser.add_argument(
"--gem",
type=str,
default=None,
help="path to `icd10cmtoicd9gem.csv`"
"if not given, try to download from the internet.",
)
parser.add_argument(
"-c", "--cache",
action="store_true",
help="whether to load data from cache if exists"
)
# misc
parser.add_argument(
"--max_event_size", type=int, default=256, help="max event size to crop to"
)
parser.add_argument(
"--min_event_size",
type=int,
default=5,
help="min event size to skip small samples",
)
parser.add_argument(
"--min_age", type=int, default=18, help="min age to skip too young patients"
)
parser.add_argument(
"--max_age", type=int, default=None, help="max age to skip too old patients"
)
parser.add_argument(
"--obs_size", type=int, default=12, help="observation window size by the hour"
)
parser.add_argument(
"--gap_size", type=int, default=0, help="time gap window size by the hour"
)
parser.add_argument(
"--pred_size", type=int, default=24, help="prediction window size by the hour"
)
parser.add_argument(
"--long_term_pred_size", type=int, default=336, help="prediction window size by the hour (for long term mortality task)"
)
parser.add_argument(
"--first_icu",
action="store_true",
help="whether to use only the first icu or not",
)
# tasks
parser.add_argument(
"--mortality", action='store_true',
help="whether to include mortality task or not"
)
parser.add_argument(
"--long_term_mortality", action='store_true',
help="whether to include long term mortality task or not"
)
parser.add_argument(
"--los_3day", action='store_true',
help="whether to include 3-day los task or not"
)
parser.add_argument(
"--los_7day", action='store_true',
help="whether to include 7-day los task or not"
)
parser.add_argument(
"--readmission", action='store_true',
help="whether to include readmission task or not"
)
parser.add_argument(
"--final_acuity", action='store_true',
help="whether to include final acuity task or not"
)
parser.add_argument(
"--imminent_discharge", action="store_true",
help="whether to include imminent discharge task or not"
)
parser.add_argument(
"--diagnosis", action='store_true',
help="whether to include diagnosis task or not"
)
parser.add_argument(
"--creatinine", action='store_true',
help="whether to include creatinine task or not"
)
parser.add_argument(
"--bilirubin", action='store_true',
help="whether to include bilirubin task or not"
)
parser.add_argument(
"--platelets", action='store_true',
help="whether to include platelets task or not"
)
parser.add_argument(
"--wbc", action='store_true',
help="whether to include blood white blood cell count task or not"
)
parser.add_argument(
"--chunk_size",
type=int,
default=1024,
help="chunk size to read large csv files",
)
parser.add_argument(
'--bins', type=int, default=20,
help='num buckets to bin time intervals by'
)
parser.add_argument(
'--max_event_token_len', type=int, default=128,
help='max token length for each event (Hierarchical)'
)
parser.add_argument(
'--max_patient_token_len', type=int, default=8192,
help='max token length for each patient (Flatten)'
)
parser.add_argument(
'--num_threads', type=int, default=8, help='number of threads to use'
)
return parser
def main(args):
task = args.task
if not os.path.exists(args.dest):
os.makedirs(args.dest)
ehr = EHR_REGISTRY[args.ehr](args)
spark = (
SparkSession.builder.master(f"local[{args.num_threads}]")
.config("spark.driver.memory", "100g")
.config("spark.driver.maxResultSize", "10g")
.config("spark.network.timeout", "100s")
.config("spark.sql.timeZone", "UTC")
.appName("Main_Preprocess")
.getOrCreate()
)
ehr.run_pipeline(spark)
if __name__ == "__main__":
parser = get_parser()
args = parser.parse_args()
main(args)