Last active
October 25, 2017 09:34
-
-
Save fredkingham/a52744d85f2dd42d9962320a8871289a to your computer and use it in GitHub Desktop.
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
from lab.models import LabTest | |
from opal.models import Patient | |
from collections import defaultdict | |
from django.db import transaction | |
def get_problematic_lab_tests(): | |
all_lab_tests = LabTest.objects.all() | |
by_patient_bloodtest = defaultdict(list) | |
lab_tests = [i for i in all_lab_tests if not i.extras["isolate"]] | |
for i in lab_tests: | |
key = ( | |
i.patient_id, | |
i.extras.get("lab_number"), | |
i.extras.get("aerobic") | |
) | |
by_patient_bloodtest[key].append(i) | |
return by_patient_bloodtest | |
def fix_single(patient_id, lab_number, aerobic): | |
all_lab_tests = LabTest.objects.filter(patient_id=patient_id) | |
lab_tests = [ | |
i for i in lab_tests if i.extras["lab_number"] and i.extras["aerobic"] == aerobic | |
] | |
lab_tests_with_isolates = [ | |
i for i in lab_tests if i.extras["isolate"] | |
] | |
lab_tests_without_isolates = [ | |
i for i in lab_tests if not i.extras["isolate"] | |
] | |
if not len(lab_tests_without_isolates) == 1: | |
raise ValueError('unable to fix {}'.format(patient_id)) | |
else: | |
ltwi = lab_tests_without_isolates[0] | |
ltwi.extras["islate"] = len(lab_tests_with_isolates) + 1 | |
ltwi.save() | |
@transaction.atomic | |
def deal_with_singles(problematic_lab_tests): | |
for i, v in problematic_lab_tests.items(): | |
if len(v) == 1: | |
print "{} is ok fixing".format(i) | |
fix_single(i) | |
else: | |
p = Patient.objects.get(i[0]) | |
lab_tests = p.labtest_set.all().values_list( | |
'lab_test_type', flat=True | |
) | |
if len(set(lab_tests)) == 1 and lab_tests[0] == "Organism": | |
print "{} only has organisms ".format(i) | |
print "problem with {} with {}".format(i, lab_tests) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment