forked from ARCH-commons/i2p-transform
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathi2p_tasks.py
498 lines (354 loc) · 16.6 KB
/
i2p_tasks.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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
"""i2p_tasks -- Luigi CDM task support.
"""
from typing import cast, List, Type
import luigi
from sqlalchemy.engine import RowProxy
from sqlalchemy.exc import DatabaseError
from csv_load import LoadCSV
from etl_tasks import CDMStatusTask, SqlScriptTask
from param_val import IntParam, StrParam
from script_lib import Script
from sql_syntax import Environment, Params
import csv
import subprocess
import urllib.request
class I2PConfig(luigi.Config):
datamart_id = StrParam(description='see client.cfg')
datamart_name = StrParam(description='see client.cfg')
enrollment_months_back = StrParam(description='see client.cfg')
i2b2_data_schema = StrParam(description='see client.cfg')
i2b2_etl_schema = StrParam(description='see client.cfg')
i2b2_meta_schema = StrParam(description='see client.cfg')
min_pat_list_date_dd_mon_rrrr = StrParam(description='see client.cfg')
min_visit_date_dd_mon_rrrr = StrParam(description='see client.cfg')
network_id = StrParam(description='see client.cfg')
network_name = StrParam(description='see client.cfg')
token_encryption_key = StrParam(description='see client.cfg')
class I2PScriptTask(SqlScriptTask):
@property
def variables(self) -> Environment:
return dict(datamart_id=I2PConfig().datamart_id, datamart_name=I2PConfig().datamart_name,
i2b2_data_schema=I2PConfig().i2b2_data_schema,
min_pat_list_date_dd_mon_rrrr=I2PConfig().min_pat_list_date_dd_mon_rrrr,
min_visit_date_dd_mon_rrrr=I2PConfig().min_visit_date_dd_mon_rrrr,
i2b2_meta_schema=I2PConfig().i2b2_meta_schema,
enrollment_months_back=I2PConfig().enrollment_months_back, network_id=I2PConfig().network_id,
network_name=I2PConfig().network_name, i2b2_etl_schema=I2PConfig().i2b2_etl_schema,
token_encryption_key=I2PConfig().token_encryption_key)
class condition(I2PScriptTask):
script = Script.condition
def requires(self) -> List[luigi.Task]:
return [encounter()]
class lds_address_history(I2PScriptTask):
script = Script.lds_address_history
def requires(self) -> List[luigi.Task]:
return [demographic()]
class death(I2PScriptTask):
script = Script.death
def requires(self) -> List[luigi.Task]:
return [demographic()]
class death_cause(I2PScriptTask):
script = Script.death_cause
def requires(self) -> List[luigi.Task]:
return [pcornet_init()]
class demographic(I2PScriptTask):
script = Script.demographic
def requires(self) -> List[luigi.Task]:
return [loadLanguage(), pcornet_init()]
class diagnosis(I2PScriptTask):
script = Script.diagnosis
def requires(self) -> List[luigi.Task]:
return [encounter()]
class dispensing(I2PScriptTask):
script = Script.dispensing
def requires(self) -> List[luigi.Task]:
return [encounter(), loadRouteMap(), loadUnitMap()]
class encounter(I2PScriptTask):
script = Script.encounter
def requires(self) -> List[luigi.Task]:
return [demographic(), loadPayerMap()]
class enrollment(I2PScriptTask):
script = Script.enrollment
def requires(self) -> List[luigi.Task]:
return [pcornet_init()]
class harvest(I2PScriptTask):
script = Script.harvest
def requires(self) -> List[luigi.Task]:
return [condition(), death(), death_cause(), diagnosis(), dispensing(), enrollment(),
lab_result_cm(), loadHarvestLocal(), med_admin(), obs_clin(), obs_gen(), pcornet_trial(),
prescribing(), pro_cm(), procedures(), provider(), vital(), lab_history(), lds_address_history()]
class Covid19a(I2PScriptTask):
script = Script.harvest
def requires(self) -> List[luigi.Task]:
return [death(), death_cause(), diagnosis(), dispensing(), enrollment(),
lab_result_cm(), loadHarvestLocal(), med_admin(), pcornet_trial(),
prescribing(), pro_cm(), procedures(), provider(), vital(), lab_history(), lds_address_history()]
class lab_result_cm(I2PScriptTask):
script = Script.lab_result_cm
def requires(self) -> List[luigi.Task]:
return [encounter(), loadLabNormal(), loadSpecimenSourceMap(),loadCodesPotentialPpi()]
class med_admin(I2PScriptTask):
script = Script.med_admin
def requires(self) -> List[luigi.Task]:
return [encounter(), loadRouteMap(), loadUnitMap()]
class obs_clin(I2PScriptTask):
script = Script.obs_clin
def requires(self) -> List[luigi.Task]:
return [lab_result_cm(), pcornet_init()]
class obs_gen(I2PScriptTask):
script = Script.obs_gen
def requires(self) -> List[luigi.Task]:
return [encounter(), demographic(), pcornet_init()]
class lab_history(I2PScriptTask):
script = Script.lab_history
def requires(self) -> List[luigi.Task]:
return [lab_result_cm(), pcornet_init()]
class I2PPatientGroupTask(I2PScriptTask):
patient_num_first = IntParam()
patient_num_last = IntParam()
patient_num_qty = IntParam(significant=False, default=-1)
group_num = IntParam(significant=False, default=-1)
group_qty = IntParam(significant=False, default=-1)
def run(self) -> None:
SqlScriptTask.run_bound(self, script_params=dict(
patient_num_first=self.patient_num_first, patient_num_last=self.patient_num_last))
class _PatientNumGrouped(luigi.WrapperTask):
group_tasks = cast(List[Type[I2PPatientGroupTask]], []) # abstract
def requires(self) -> List[luigi.Task]:
deps = [] # type: List[luigi.Task]
for group_task in self.group_tasks:
survey = patient_chunks_survey()
deps += [survey]
results = survey.results()
if results:
deps += [
group_task(
group_num=ntile.chunk_num,
group_qty=len(results),
patient_num_qty=ntile.patient_num_qty,
patient_num_first=ntile.patient_num_first,
patient_num_last=ntile.patient_num_last)
for ntile in results
]
return deps
class patient_chunks_survey(SqlScriptTask):
script = Script.patient_chunks_survey
patient_chunks = IntParam(default=20)
patient_chunk_max = IntParam(default=None)
@property
def variables(self) -> Environment:
return dict(chunk_qty=str(self.patient_chunks))
def run(self) -> None:
SqlScriptTask.run_bound(self, script_params=dict(chunk_qty=str(self.patient_chunks)))
def results(self) -> List[RowProxy]:
with self.connection(event='survey results') as lc:
q = '''
select patient_num
, patient_num_qty
, patient_num_first
, patient_num_last
from patient_chunks
where chunk_qty = :chunk_qty
and (:chunk_max is null or
chunk_num <= :chunk_max)
order by chunk_num
'''
Params
params = dict(chunk_max=self.patient_chunk_max, chunk_qty=self.patient_chunks) # type: Params
try:
return lc.execute(q, params=params).fetchall()
except DatabaseError:
return []
# TODO: pcornet_init drops and recreates the cdm_status table, forcing all tasks to wait until init is done.
# Moving this operation to a distinct task would allow some other tasks (e.g. mapping tasks) to proceed, while init
# performs other labor intensive SQL operations.
# In the mean time, don't forget to make all tasks that use the status table dependent on pcornet_init.
# TODO: On a related matter, if the cdm_status table is missing (e.g. on a db where CDM has never been run), running
# the full pipeline, starting at pcornet_loader, will fail. It would be nice to detect this situation and first
# build the status table before attempting other tasks.
class pcornet_init(I2PScriptTask):
script = Script.pcornet_init
def requires(self) -> List[luigi.Task]:
return []
class pcornet_loader(I2PScriptTask):
script = Script.pcornet_loader
def requires(self) -> List[luigi.Task]:
return [pcornet_init(), harvest()]
class pcornet_trial(I2PScriptTask):
script = Script.pcornet_trial
def requires(self) -> List[luigi.Task]:
return [pcornet_init()]
class prescribing(I2PScriptTask):
script = Script.prescribing
def requires(self) -> List[luigi.Task]:
return [encounter(), loadRouteMap(), loadUnitMap()]
class pro_cm(I2PScriptTask):
script = Script.pro_cm
def requires(self) -> List[luigi.Task]:
return [pcornet_init()]
class procedures(I2PScriptTask):
script = Script.procedures
def requires(self) -> List[luigi.Task]:
return [encounter()]
class provider(I2PScriptTask):
script = Script.provider
def requires(self) -> List[luigi.Task]:
return [loadSpecialtyCode(), encounter()]
class vital(I2PScriptTask):
script = Script.vital
def requires(self) -> List[luigi.Task]:
return [encounter()]
class loadPayerMap(LoadCSV):
taskName = 'PAYER_MAP'
# payer_map.csv is a combination of the CDM spec's payer_type spreadsheet
# with payer_name and financial_class values from epic's clarity_epm table.
# payer_name and financial_class are used together to determine the CDM's
# payer type code.
# TODO: incorporte IDX.
csvname = 'curated_data/payer_map.csv'
def requires(self) -> List[luigi.Task]:
return [pcornet_init()]
class loadLabNormal(LoadCSV):
taskName = 'LABNORMAL'
csvname = 'curated_data/labnormal.csv'
def requires(self) -> List[luigi.Task]:
return [pcornet_init()]
class loadLabRUnit(LoadCSV):
taskName = 'LABRUNIT'
csvname = 'curated_data/resultunit_manualcuration.csv'
def requires(self) -> List[luigi.Task]:
return [pcornet_init()]
class loadHarvestLocal(LoadCSV):
taskName = 'HARVEST_LOCAL'
csvname = 'curated_data/harvest_local.csv'
def requires(self) -> List[luigi.Task]:
return [pcornet_init()]
class loadLanguage(LoadCSV):
taskName = 'LANGUAGE_CODE'
# language.csv is a copy of the CDM spec's patient_pref_language_spoken spreadsheet.
# It maps a language code to descriptive text. When the spreadsheet mapped more than
# one text value to a code, duplicate codes where created in language.csv.
csvname = 'curated_data/language.csv'
def requires(self) -> List[luigi.Task]:
return [pcornet_init()]
class loadSpecimenSourceMap(LoadCSV):
taskName = 'SPECIMEN_SOURCE_MAP'
# specimen_source_map.csv matches values in the CDM spec's specimen_source spreadsheet
# to specimen type values from Epic's order_proc table.
csvname = 'curated_data/specimen_source_map.csv'
def requires(self) -> List[luigi.Task]:
return [pcornet_init()]
class loadRouteMap(LoadCSV):
taskName = 'ROUTE_MAP'
# route_map.csv matches values in the CDM spec's _route spreadsheet
# to route values from Epic's zc_admin_route table.
csvname = 'curated_data/route_map.csv'
def requires(self) -> List[luigi.Task]:
return [pcornet_init()]
class loadUnitMap(LoadCSV):
taskName = 'UNIT_MAP'
# unit_map.csv matches values in the CDM spec's _unit spreadsheet
# to unit values from Epic's zc_med_unit table.
csvname = 'curated_data/unit_map.csv'
def requires(self) -> List[luigi.Task]:
return [pcornet_init()]
class loadCodesPotentialPpi(LoadCSV):
taskName = 'CODES_POTENTIAL_PPI'
csvname = 'curated_data/codes_potential_ppi.csv'
def requires(self) -> List[luigi.Task]:
return [pcornet_init()]
class NPIDownloadConfig(luigi.Config):
# The configured 'path' and 'npi' variables are used by the downloadNPI method to fetch and
# store the NPPES zip file. Changes to these may require changes to the file system.
# TODO: Update code to discover the npi_csv automatically.
dl_path = StrParam(description='Path where the NPPES zip file will be stored and unzipped.')
extract_path = StrParam(description='Path where the extract')
npi_csv = StrParam(description='CSV file in the NPPES zip that contains NPI data.')
npi_url = StrParam(description='URL for the NPPES download site.')
npi_zip = StrParam(description='Name of the NPPES zip file.')
# The configured 'col' and 'ct' variables reflect the layout of the NPI data file.
# The extracNPI method uses these values to parse the NPI data file.
# Changes to these may require code changes.
# Complete overkill making these configurable. Consider reverting to hard coded values.
taxonomy_col = StrParam(description='Header for the taxonomy columns in the NPI data file.')
switch_col = StrParam(description='Header for the switch columns in the NPI data file.')
npi_col = StrParam(description='Header for the NPI column in the NPI data file.')
taxonomy_ct = IntParam(description='Number of taxonomy columns in the NPI data file.')
class loadSpecialtyMap(LoadCSV):
taskName = 'PROVIDER_SPECIALTY_MAP'
# provider_specialty_map.csv is created on demand by the extractNPI method.
# It maps a National Provider Identifier (NPI) to a primary provider specialty code.
csvname = 'curated_data/provider_specialty_map.csv'
def requires(self) -> List[luigi.Task]:
return [pcornet_init(), extractNPI()]
class loadSpecialtyCode(LoadCSV):
taskName = 'PROVIDER_SPECIALTY_CODE'
# provider_specialty_code.csv is a copy of the CDM spec's provider_primary_specialty spreadsheet.
# It maps a provider specialty code to a descriptive text and grouping.
csvname = 'curated_data/provider_specialty_code.csv'
def requires(self) -> List[luigi.Task]:
return [pcornet_init()]
class downloadNPI(CDMStatusTask):
'''
Download the NPPES zip file and extract the NPI data file.
'''
taskName = 'NPI_DOWNLOAD'
expectedRecords = 0
dl_path = NPIDownloadConfig().dl_path
npi_url = NPIDownloadConfig().npi_url
npi_zip = NPIDownloadConfig().npi_zip
def run(self) -> None:
self.setTaskStart()
self.fetch()
self.unzip()
self.setTaskEnd(self.expectedRecords)
def fetch(self) -> None:
r = urllib.request.urlopen(self.npi_url + self.npi_zip)
with open(self.dl_path + self.npi_zip, 'wb') as fout:
fout.write(r.read())
def unzip(self) -> None:
subprocess.call(['unzip', '-o', self.dl_path + self.npi_zip, '-d', self.dl_path]) # ISSUE: ambient
def requires(self) -> List[luigi.Task]:
return [pcornet_init()]
class extractNPI(CDMStatusTask):
'''
Extract the Nation Provider Identifier (NPI) and primary specialty from the NPPES download file
and write the data to a separate csv file.
'''
taskName = 'NPI_EXTRACT'
specialty_csv = 'provider_specialty_map.csv'
dl_path = NPIDownloadConfig().dl_path
extract_path = NPIDownloadConfig().extract_path
npi_col = NPIDownloadConfig().npi_col
npi_csv = NPIDownloadConfig().npi_csv
switch_col = NPIDownloadConfig().switch_col
taxonomy_col = NPIDownloadConfig().taxonomy_col
taxonomy_ct = NPIDownloadConfig().taxonomy_ct
def run(self) -> None:
self.setTaskStart()
self.extract()
self.setTaskEnd(self.expectedRecords)
def requires(self) -> List[luigi.Task]:
return [downloadNPI()]
def extract(self) -> None:
self.expectedRecords = 0
with open(self.dl_path + self.npi_csv, 'r', encoding='utf-8') as fin:
with open(self.extract_path + self.specialty_csv, 'w', newline='') as fout:
reader = csv.DictReader(fin)
writer = csv.writer(fout)
writer.writerow(['NPI', 'SPECIALTY'])
for row in reader:
self.expectedRecords = self.expectedRecords + 1
useDefault = True
# Search the taxonomy columns for a primary specialty.
for i in range(1, self.taxonomy_ct + 1):
# 'Y' in the switch column indicates that the current taxonomy column contains
# the primary specialty.
if row[self.switch_col + str(i)] == 'Y':
useDefault = False
writer.writerow([row[self.npi_col], row[self.taxonomy_col + str(i)]])
continue
# If true, the NPI file did not explicitly identify a primary specialty. Use the value
# in taxonomy column one as the default.
if (useDefault):
writer.writerow([row[self.npi_col], row[self.taxonomy_col + str(1)]])