Created
October 17, 2022 10:25
-
-
Save fredkingham/30d0aa0f9dbfc1ea1d44f3baf8a0b7c9 to your computer and use it in GitHub Desktop.
load transfer histories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
from django.utils import timezone | |
from django.db import transaction | |
from intrahospital_api.apis.prod_api import ProdApi as ProdAPI | |
from elcid.models import Demographics | |
from plugins.admissions import loader | |
import csv, os | |
from plugins.admissions.models import TransferHistory | |
from elcid.episode_categories import InfectionService | |
from intrahospital_api.loader import create_rfh_patient_from_hospital_number | |
# process 1 | |
def get_mrns(): | |
query = """ | |
SELECT DISTINCT(LOCAL_PATIENT_IDENTIFIER) FROM INP.TRANSFER_HISTORY_EL_CID WITH (NOLOCK) | |
""" | |
api = ProdAPI() | |
result = api.execute_warehouse_query(query) | |
return [i["LOCAL_PATIENT_IDENTIFIER"] for i in result] | |
def write_mrns(): | |
mrns = get_mrns() | |
mrns = sorted([i for i in list(set(mrns)) if i]) | |
with open("mrns_to_process.csv" "w") as some_file: | |
import csv | |
writer = csv.DictWriter(some_file, fieldnames="MRN") | |
writer.writeheader() | |
to_write = [{"MRN": mrn} for mrn in mrns] | |
writer.writerows(to_write) | |
# the in 1 shell | |
def load_patients(): | |
## load patients in the mrns that don't exist | |
pass | |
def load_history_for_patients(mrns): | |
if len(mrns) < 20: | |
histories = [] | |
for mrn in mrns: | |
histories.extend(get_histories_for_mrn(mrn)) | |
else: | |
histories = get_histories_for_mrns(mrns) | |
return histories | |
def get_histories_for_mrns(mrns): | |
api = ProdAPI() | |
Q_GET_TRANSFERS_FOR_MRNS = """ | |
SELECT * | |
FROM INP.TRANSFER_HISTORY_EL_CID WITH (NOLOCK) | |
WHERE | |
LOCAL_PATIENT_IDENTIFIER in ( | |
@mrn_0, | |
@mrn_1, | |
@mrn_2, | |
@mrn_3, | |
@mrn_4, | |
@mrn_5, | |
@mrn_6, | |
@mrn_7, | |
@mrn_8, | |
@mrn_9, | |
@mrn_10, | |
@mrn_11, | |
@mrn_12, | |
@mrn_13, | |
@mrn_14, | |
@mrn_15, | |
@mrn_16, | |
@mrn_17, | |
@mrn_18, | |
@mrn_19, | |
@mrn_20, | |
@mrn_21, | |
@mrn_22, | |
@mrn_23, | |
@mrn_24, | |
@mrn_25, | |
@mrn_26, | |
@mrn_27, | |
@mrn_28, | |
@mrn_29, | |
@mrn_30, | |
@mrn_31, | |
@mrn_32, | |
@mrn_33, | |
@mrn_34, | |
@mrn_35, | |
@mrn_36, | |
@mrn_37, | |
@mrn_38, | |
@mrn_39, | |
@mrn_40, | |
@mrn_41, | |
@mrn_42, | |
@mrn_43, | |
@mrn_44, | |
@mrn_45, | |
@mrn_46, | |
@mrn_47, | |
@mrn_48, | |
@mrn_49 | |
) | |
""" | |
params = {f"mrn_{i}": v for i, v in enumerate(mrns)} | |
return api.execute_warehouse_query( | |
Q_GET_TRANSFERS_FOR_MRNS, params=params | |
) | |
def get_histories_for_mrn(mrn): | |
api = ProdAPI() | |
Q_GET_TRANSFERS_FOR_MRN = """ | |
SELECT * | |
FROM INP.TRANSFER_HISTORY_EL_CID WITH (NOLOCK) | |
WHERE | |
LOCAL_PATIENT_IDENTIFIER = @mrn | |
""" | |
return api.execute_warehouse_query( | |
Q_GET_TRANSFERS_FOR_MRN, params={"mrn": mrn} | |
) | |
def get_top_1(): | |
api = ProdAPI() | |
Q_GET_TRANSFERS_FOR_MRN = """ | |
SELECT TOP(1) * | |
FROM INP.TRANSFER_HISTORY_EL_CID WITH (NOLOCK) | |
""" | |
return api.execute_warehouse_query( | |
Q_GET_TRANSFERS_FOR_MRN | |
)[0] | |
def load_history_for_mrns(mrns): | |
if len(mrns) < 50: | |
histories = [] | |
for mrn in mrns: | |
histories.extend(get_histories_for_mrn(mrn)) | |
else: | |
histories = get_histories_for_mrns(mrns) | |
return histories | |
def load_all_by_patients(): | |
mrns = list(set(get_mrns())) | |
for idx, mrn in enumerate(mrns): | |
print(f'looking at {idx+1}/{len(mrns)}, {mrn}') | |
result = load_for_patient(mrn) | |
print(f'{len(result)} results') | |
loader.create_transfer_histories_from_upstream_result(result) | |
@transaction.atomic | |
def save_histories(rows): | |
demographics = Demographics.objects.filter( | |
hospital_number__in=set([i["LOCAL_PATIENT_IDENTIFIER"] for i in rows]) | |
).filter(patient__transferhistory=None).select_related('patient') | |
mrn_to_patient = {i.hospital_number: i.patient for i in demographics} | |
transfer_histories = [] | |
for history in rows: | |
patient = mrn_to_patient.get(history["LOCAL_PATIENT_IDENTIFIER"]) | |
if not patient: | |
continue | |
hist = TransferHistory(patient=mrn_to_patient[history["LOCAL_PATIENT_IDENTIFIER"]]) | |
for k, v in history.items(): | |
if k in TransferHistory.UPSTREAM_FIELDS_TO_MODEL_FIELDS: | |
if isinstance(v, datetime.datetime): | |
v = timezone.make_aware(v) | |
setattr( | |
hist, | |
TransferHistory.UPSTREAM_FIELDS_TO_MODEL_FIELDS[k], | |
v | |
) | |
transfer_histories.append(hist) | |
TransferHistory.objects.bulk_create(transfer_histories) | |
def filter_mrns(mrns): | |
""" | |
Ignore MRNS already loaded or those for new patients | |
""" | |
to_ignore = set( | |
Demographics.objects.filter(hospital_number__in=mrns).exclude( | |
patient__transferhistory=None | |
).values_list('hospital_number', flat=True) | |
) | |
return list(set(mrns) - to_ignore) | |
def get_mrns_to_process(): | |
with open("mrns_to_process.csv") as some_file: | |
reader = csv.DictReader(some_file) | |
rows = list([i["MRN"] for i in reader]) | |
return rows | |
def load_histories(): | |
rows = filter_mrns(get_mrns_to_process()) | |
print(f'starting to process with {len(rows)} rows') | |
for i in range(0, len(rows), 50): | |
to_process = rows[i:i+50] | |
histories = load_history_for_mrns(to_process) | |
save_histories(histories) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment