-
Notifications
You must be signed in to change notification settings - Fork 0
/
df.py
290 lines (229 loc) · 10 KB
/
df.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
#!/usr/bin/env python3
import os
import json
import collections
import docker
import click
import dfpipelines
from dfpipelines import DbConfig
from logger import LOGGER
# GET THE CONFIGURATIONS AND INPUT FOLDERS
with open('config.json') as json_data_file:
CONFIG = json.load(json_data_file)
def getfolders(rootfolder):
"""Returns the subfolders in a folder."""
return [name for name in os.listdir(rootfolder)
if os.path.isdir(os.path.join(rootfolder, name))]
EHR_INPUT_ROOT = CONFIG['mipmap']['input_folders']['ehr']
IMG_INPUT_ROOT = CONFIG['mipmap']['input_folders']['imaging']
MRI_RAW_ROOT = CONFIG['mri']['input_folders']['nifti']['raw']
CFG_PRE_ROOT = CONFIG['mipmap']['preprocess']['root']
CFG_CAP_ROOT = CONFIG['mipmap']['capture']['root']
CFG_HAR_ROOT = CONFIG['mipmap']['harmonize']['root']
EHR_INPUT = getfolders(EHR_INPUT_ROOT)
IMG_INPUT = getfolders(IMG_INPUT_ROOT)
MRI_INPUT = getfolders(MRI_RAW_ROOT)
CFG_PRE = getfolders(CFG_PRE_ROOT)
CFG_CAP = getfolders(CFG_CAP_ROOT)
CFG_HAR = getfolders(CFG_HAR_ROOT)
FLAT_STRATEGIES = list(CONFIG['flattening']['strategy'].keys())
FLAT_STRATEGIES_ANON = list(CONFIG['anonymization']['strategy'].keys())
@click.group()
@click.pass_context
def main(ctx):
# GET DATAFACTORY CONFIGURATION
with open('config.json') as json_data_file:
config = json.load(json_data_file)
# test if postgres docker container is running
container_name = config['db_docker']['container_name']
client = docker.from_env(timeout=10800)
try:
pg_container = client.containers.get(container_name)
LOGGER.info('Found postgres container: %s' % container_name)
except:
LOGGER.warning('Unable to find db container: %s' % container_name)
exit()
# docker db credentials
dbconfig = DbConfig(pg_container,
config['db_docker']['postgres_port'],
config['db_docker']['postgres_user'],
config['db_docker']['postgres_pwd'])
ctx.obj = {
'cfgjson': config,
'dbconfig': dbconfig,
}
@main.group()
@click.pass_context
def ingest(ctx):
pass
@main.group()
@click.pass_context
def anonymize(ctx):
pass
@main.group()
@click.option('--input_folder', prompt=True,
type=click.Choice(MRI_INPUT))
@click.option('--loris', is_flag=True)
def mri(ctx, input_folder, loris):
dfpipelines.mri_wrapper(ctx, input_folder, from_loris=loris)
@main.command()
@click.argument('output_folder')
@click.option('-s', '--strategy', prompt=True,
type=click.Choice(FLAT_STRATEGIES),
help='Flattening strategy from i2b2 to csv')
@click.option('--local', is_flag=True)
@click.option('--csv_name',
help='overrides the defalut strategy\'s flat csv name')
@click.option('-d', '--dataset', prompt=True,
help='value in the final csv under the column \'Dataset\'')
@click.pass_context
def export(ctx, output_folder, strategy, local, csv_name, dataset):
dfpipelines.export_flat_csv(ctx, output_folder, strategy, local, csv_name, dataset)
@ingest.group()
@click.pass_context
def ehr(ctx):
pass
@ehr.command()
@click.option('--input_folder', prompt=True, type=click.Choice(EHR_INPUT),
help='batch folder name that contains EHR csv files')
@click.option('--config_folder', prompt=True, type=click.Choice(CFG_PRE),
help='config folder for preprosessing step')
@click.pass_context
def preprocess(ctx, input_folder, config_folder):
"""
Creates the auxilary files for the capture DF EHR step.
"""
dfpipelines.ehr_preprocess(ctx, input_folder, config_folder)
@ehr.command()
@click.option('--input_folder', prompt=True, type=click.Choice(EHR_INPUT),
help='batch folder name that contains EHR csv files')
@click.option('--config_folder', prompt=True, type=click.Choice(CFG_CAP),
help='config folder for capture step')
@click.pass_context
def capture(ctx, input_folder, config_folder):
"""
Ingest EHR data into capture i2b2.
"""
dfpipelines.ehr_capture(ctx, input_folder, config_folder)
@ehr.command()
@click.option('--config_folder', prompt=True, type=click.Choice(CFG_HAR),
help='config folder for harmonization step')
@click.pass_context
def harmonize(ctx, config_folder):
"""
Ingests and harmonizes EHR data from capture to harmonize i2b2.
"""
dfpipelines.ehr_harmonize(ctx, config_folder)
@ingest.command()
@click.option('--input_folder', prompt=True, type=click.Choice(IMG_INPUT),
help='batch folder with the output of imaging pipeline')
@click.pass_context
def imaging(ctx, input_folder):
"""
Arguments
input_folder: folder name of the which contains volumes.csv
and mri_visits.csv files
"""
dfpipelines.ingest_imaging(ctx, input_folder)
@anonymize.command()
@click.argument('output_folder')
@click.option('-s', '--strategy', prompt=True,
type=click.Choice(FLAT_STRATEGIES_ANON),
help='Flattening strategy from i2b2 to csv')
@click.option('--hash_function', default='sha224',
type=click.Choice(['md5', 'sha224']),
help='Hashing function used for anonymization')
@click.option('--csv_name',
help='overrides the defalut strategy\'s flat csv name')
@click.option('-d', '--dataset', prompt=True,
help='value in the final csv under the column \'Dataset\'')
@click.pass_context
def db(ctx, output_folder, hash_function, strategy, csv_name, dataset):
dfpipelines.export_anonymized_db(ctx, output_folder, hash_function, strategy, csv_name, dataset)
@anonymize.command()
@click.argument('input_path', type=click.Path(exists=True, file_okay=True))
@click.argument('output_folder')
@click.option('--csv_name', prompt=True,
help='anonymized flat csv name')
@click.option('--hash_function', default='sha224',
type=click.Choice(['md5', 'sha224']),
help='Hashing function used for anonymization')
@click.option('-d', '--dataset', prompt=True,
help='value in the final csv under the column \'Dataset\'')
@click.pass_context
def csv(ctx, input_path, output_folder, hash_function, csv_name, dataset):
dfpipelines.export_anonymized_csv(ctx, input_path, output_folder, hash_function, csv_name, dataset)
@main.command()
@click.pass_context
def interactive(ctx):
click.clear()
click.echo('DataFactory EHR pipeline - interactive mode')
click.confirm('Do you want to continue?', abort=True)
# PREPROCESS
if click.confirm('Proceed with the EHR preprocess step?', abort=False):
click.clear()
inp_num = [str(x) for x in range(len(EHR_INPUT))]
prompt_msg = 'Select input folder number: '
prompt_msg = enum_options(prompt_msg, EHR_INPUT)
choice = click.prompt(prompt_msg, type=click.Choice(inp_num))
input_idx = int(choice)
input_folder = EHR_INPUT[input_idx]
click.clear()
inp_num = [str(x) for x in range(len(CFG_PRE))]
prompt_msg = 'Select configuration folder number: '
prompt_msg = enum_options(prompt_msg, CFG_PRE)
choice = click.prompt(prompt_msg, type=click.Choice(inp_num))
input_idx = int(choice)
config_folder = CFG_PRE[input_idx]
dfpipelines.ehr_preprocess(ctx, input_folder, config_folder)
# CAPTURE
if click.confirm('Proceed with the EHR capture step?', abort=False):
click.clear()
inp_num = [str(x) for x in range(len(EHR_INPUT))]
prompt_msg = 'Select input folder number: '
prompt_msg = enum_options(prompt_msg, EHR_INPUT)
choice = click.prompt(prompt_msg, type=click.Choice(inp_num))
input_idx = int(choice)
input_folder = EHR_INPUT[input_idx]
click.clear()
inp_num = [str(x) for x in range(len(CFG_CAP))]
prompt_msg = 'Select configuration folder number: '
prompt_msg = enum_options(prompt_msg, CFG_CAP)
choice = click.prompt(prompt_msg, type=click.Choice(inp_num))
input_idx = int(choice)
config_folder = CFG_CAP[input_idx]
dfpipelines.ehr_capture(ctx, input_folder, config_folder)
# HARMONIZE
if click.confirm('Proceed with the EHR harmonization step?', abort=False):
click.clear()
inp_num = [str(x) for x in range(len(CFG_HAR))]
prompt_msg = 'Select configuration folder number: '
prompt_msg = enum_options(prompt_msg, CFG_HAR)
choice = click.prompt(prompt_msg, type=click.Choice(inp_num))
input_idx = int(choice)
config_folder = CFG_HAR[input_idx]
dfpipelines.ehr_harmonize(ctx, config_folder)
# EXPORT
if click.confirm('Proceed with the export step?', abort=False):
click.clear()
if click.confirm('Do you want anonymized export?', abort=False):
output_folder = click.prompt('Enter name for the output folder: ', type=str)
hash_function = click.prompt('Select flattening strategy: ', click.Choice(['md5', 'sha224']))
flat_strategy= click.prompt('Select flattening strategy: ', click.Choice(FLAT_STRATEGIES_ANON))
dataset = click.prompt('Enter dataset name: ', type=str)
dfpipelines.export_anonymized_db(ctx, output_folder, hash_function, flat_strategy, dataset)
else:
output_folder = click.prompt('Enter name for the output folder: ', type=str)
hash_function = click.prompt('Select flattening strategy: ', click.Choice(['md5', 'sha224']))
flat_strategy= click.prompt('Select flattening strategy: ', click.Choice(FLAT_STRATEGIES))
dataset = click.prompt('Enter dataset name: ', type=str)
local = click.confirm('Use local hospital variables instead of CDE variables', abort=False)
dfpipelines.export_flat_csv(ctx, output_folder, flat_strategy, local, dataset)
def enum_options(prompt_msg, options):
opt_str = ''
for num, opt in enumerate(options):
s = '%d: %s \n' % (num, opt)
opt_str += s
return opt_str + prompt_msg
if __name__ == '__main__':
main()