import petl as etl from fhir_petl.util import preprocess, resolve, mkdirp import random mkdirp(resolve('work')) selection = etl.io.csv.fromtsv(resolve('sa1cases.txt')).columns()['STUDYID'] selection = set(random.sample(selection, 1000)) preprocess( etl.io.csv.fromcsv(resolve('case_demog_27065.csv')).selectin( 'STUDYID', selection), 'STUDYID').tocsv(resolve('work/Patient.csv')) preprocess( etl.io.csv.fromtsv(resolve('dx_case_inst.txt')).selectin( 'STUDYID', selection), 'STUDYID', ).tocsv(resolve('work/Condition.csv')) preprocess( etl.io.csv.fromtsv(resolve('lab_case_inst.txt')).selectin( 'STUDYID', selection), 'STUDYID').tocsv(resolve('work/Observation.csv')) preprocess( etl.io.csv.fromtsv(resolve('med_case_inst_gpi.txt')).selectin( 'CASE_ID', selection), 'CASE_ID').tocsv(resolve('work/MedicationDispense.csv')) preprocess( etl.io.csv.fromtsv(resolve('order_case_inst_gpi.txt')).selectin(
'subject': 'subject' }, True)) def medications2(rec): group = rec['DRUG_GROUP'].strip('*') clazz = rec['DRUG_CLASS'].strip('*') return [ ('http://hl7.org/fhir/sid/ndc', rec['NDC'], rec['ORDER_NAME']), ('urn:oid:2.16.840.1.113883.6.68', rec['GPI'], rec['ORDER_NAME']), ('drug-class', clazz, clazz), ('drug-group', group, group) ] med_requests = (etl.io.csv.fromcsv(resolve('work/MedicationRequest.csv')) .hashjoin(index, lkey='STUDYID', rkey='STUDYID') .fieldmap({ 'id': 'ID', 'date': lambda rec: rec['index_date'] + timedelta(int(rec['DAYS_ORDER_INDEX'])), 'medication': medications2, 'subject': 'subject' }, True)) mkdirp(resolve('fhir')) to_json(patients, 'Patient', resolve('fhir/Patient.json')) to_json(procedures, 'Procedure', resolve('fhir/Procedure.json')) to_json(conditions, 'Condition', resolve('fhir/Condition.json')) to_json(observations, 'Observation', resolve('fhir/Observation.json')) to_json(med_dispenses, 'MedicationDispense', resolve('fhir/MedicationDispense.json')) to_json(med_requests, 'MedicationRequest', resolve('fhir/MedicationRequest.json'))
# ), # # "route": ("ROUTE", map_rx_route), # "subject": "subject", # # # "status": ( # # "ONGOING", # # lambda active: "active" # # if active == "TRUE" # # else "completed" # # if active == "FALSE" # # else None, # # ), # # "note": "STATUS_REASON", # "start_date": ("START_DATE", parse_date), # # "indication": ("INDICATION", map_indication), # # "end_date": ("END_DATE", date), # # "nct": ("NCT"), # }, # True, # ) # ) mkdirp(resolve("fhir")) # to_json(patients, "Patient", resolve("fhir/Patient_ktb_updated.json")) # to_json(procedures, 'Procedure', resolve('fhir/Procedure.json')) to_json(observations, "Observation", resolve("fhir/Observation_bmi_gs.json")) # to_json(conditions, "Condition", resolve("fhir/Condition_ktb.json")) # to_json(med_requests, 'MedicationRequest', resolve('fhir/MedicationRequest.json')) # to_json( # med_statements, "MedicationStatement", resolve("fhir/MedicationStatement_ktb.json") # )