Skip to content

Instantly share code, notes, and snippets.

@fredkingham
Last active December 21, 2022 10:45
Show Gist options
  • Save fredkingham/cddf4ea37094fb23202b5860dc2dfbd2 to your computer and use it in GitHub Desktop.
Save fredkingham/cddf4ea37094fb23202b5860dc2dfbd2 to your computer and use it in GitHub Desktop.
delete zero prefixed patients or their counterparts where at least one does not have elcid native entries
from plugins.covid import calculator
from opal.models import Patient
from elcid.models import Demographics
from intrahospital_api import loader
from opal.core import subrecords
from django.db import transaction
from django.core.management import call_command
from plugins.dischargesummary import loader as dicharge_summary_loader
SUBRECORDS_TO_IGNORE = set([
'InitialPatientLoad',
'Demographics',
'ContactInformation',
'NextOfKinDetails',
'GPDetails',
'ExternalDemographics',
])
@transaction.atomic
def update_patients_with_leading_zero_with_no_counter_part():
# patients with leading 0s but no duplicate, remove the 0, re-sync all upstream
cnt = 0
demos = Demographics.objects.filter(hospital_number__startswith='0').select_related('patient')
for demo in demos:
mrn = demo.hospital_number.lstrip('0')
print(f'Changing stripping the zero from the MRN of patient id {demo.patient_id}')
if mrn and not Demographics.objects.filter(hospital_number=mrn).exists():
demo.hospital_number = mrn
demo.save()
cnt += 1
loader.load_patient(demo.patient, run_async=False)
dicharge_summary_loader.load_dischargesummaries(demo.patient)
print(f'updated {cnt} patients who had no non zero')
def delete_only_zero_patients():
# patients with all 00s and no other data, delete the patients
cnt = 0
demos = Demographics.objects.filter(hospital_number__startswith='0').filter(hospital_number__endswith='0').select_related(
'patient'
)
for demo in demos:
if not demo.hospital_number.lstrip('0'):
print(f'Deleting patient id {demo.patient_id} as it does not have a real MRN')
demo.patient.delete()
cnt += 1
print(f'updated {cnt} patients who were only zeros')
@transaction.atomic
def has_elcid_native_records(patient):
result = []
for subrecord in subrecords.episode_subrecords():
if subrecord._is_singleton:
result.extend(subrecord.objects.filter(episode__patient_id=patient.id).exclude(updated=None))
else:
result.extend(subrecord.objects.filter(episode__patient_id=patient.id))
for subrecord in subrecords.patient_subrecords():
if subrecord.__name__ in SUBRECORDS_TO_IGNORE:
continue
if subrecord._is_singleton:
result.extend(subrecord.objects.filter(patient_id=patient.id).exclude(updated=None))
else:
result.extend(subrecord.objects.filter(patient_id=patient.id))
return result
@transaction.atomic
def delete_non_populated_non_zero_counter_parts():
deleted_count = 0
demos = Demographics.objects.filter(hospital_number__startswith='0')
for demo in demos:
without_hn = demo.hospital_number.lstrip('0')
if not without_hn:
continue
patients = Patient.objects.filter(demographics__hospital_number=without_hn)
cnt = patients.count()
if not cnt:
continue
if patients.count() > 1:
raise ValueError(f'hn {without_hn} is attached to multiple patients')
patient = patients.get()
if not has_elcid_native_records(patient):
patient.delete()
deleted_count += 1
print(
f'Deleting patient_id {patient.id} as it does not have subrecords and zero prefixed {patient.id} does'
)
print(f'deleted {deleted_count} non populated non zero patients and updated their zero prefixed counterparts')
update_patients_with_leading_zero_with_no_counter_part()
@transaction.atomic
def delete_non_populated_zeros_where_there_is_a_populated_zero_counterpart():
demos = Demographics.objects.filter(hospital_number__startswith='0')
deleted_count = 0
for demo in demos:
without_hn = demo.hospital_number.lstrip('0')
if not without_hn:
continue
patients = Patient.objects.filter(demographics__hospital_number=without_hn)
cnt = patients.count()
if not cnt:
continue
if patients.count() > 1:
raise ValueError(f'hn {without_hn} is attached to multiple patients')
patient = patients.get()
if has_elcid_native_records(patient) and not has_elcid_native_records(demo.patient):
print()
old_patient = demo.patient
old_patient.delete()
print(
f'Deleting patient_id {patient.id} as it does not have subrecords and non-zero prefixed {patient.id} does'
)
# we know this patient is referenced with a non in upstream tables so reload them.
loader.load_patient(patient, run_async=False)
dicharge_summary_loader.load_dischargesummaries(demo.patient)
deleted_count += 1
print(f'deleted {deleted_count} non populated zero patients who have populated non zero counterparts')
def main():
print('Updating patients with leading zeros where there is no patient')
# e.g. mrn 01 exists but mrn 1 does not
update_patients_with_leading_zero_with_no_counter_part()
print('Removing patients who are only zeros')
# e.g. mrn 00
delete_only_zero_patients()
print('Removing patients where there is no populated nonzero counterpart')
# e.g. mrn 01 is populated, mrn 1 is not populated
delete_non_populated_non_zero_counter_parts()
delete_non_populated_zeros_where_there_is_a_populated_zero_counterpart()
calculator.calculate()
call_command('create_covid_episodes')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment