Skip to content

Commit

Permalink
seems to be fixed!
Browse files Browse the repository at this point in the history
Signed-off-by: anchit-chandran <anchit97123@gmail.com>
  • Loading branch information
anchit-chandran committed Nov 7, 2024
1 parent 603f40d commit e986368
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 40 deletions.
39 changes: 6 additions & 33 deletions project/npda/kpi_class/kpis.py
Original file line number Diff line number Diff line change
Expand Up @@ -3255,6 +3255,8 @@ def calculate_kpi_45_median_hba1c(
SINGLE NUMBER: median of HbA1c measurements (item 17) within the audit
period, excluding measurements taken within 90 days of diagnosis
i.e. valid = hba1c date > diagnosis date + 90 days
NOTE: The median for each patient is calculated. We then calculate the
median of the medians.
Expand All @@ -3273,54 +3275,25 @@ def calculate_kpi_45_median_hba1c(
# Retrieve all visits with valid HbA1c values
valid_visits = Visit.objects.filter(
visit_date__range=self.AUDIT_DATE_RANGE,
hba1c_date__gte=F("patient__diagnosis_date") + timedelta(days=90)
).values("patient__postcode", "hba1c", "patient__pk")

# debugging
from pprint import pprint, pformat
tmp_pts_data = defaultdict(list)
for v in Visit.objects.all().values('visit_date', 'hba1c_date','patient__diagnosis_date', 'hba1c', 'patient__postcode'):
tmp_pts_data[(v['patient__postcode'], v['patient__diagnosis_date'])].append({
v['visit_date']: {
'hba1c_date': v['hba1c_date'],
'hba1c': v['hba1c'],
}
})
for (pt_name, diagnosis_date), visit_data in tmp_pts_data.items():
print(f'{pt_name=}, diagnosed on {diagnosis_date}')
pprint(visit_data)
print()
breakpoint()
# diagnosis_date = v['patient__diagnosis_date']
# hba1c_date = v['hba1c_date']
# print(f'{v["patient__postcode"]}')
# print(f"{v['patient__diagnosis_date']=} {v['hba1c_date']=}")
# if diagnosis_date and hba1c_date:
# print(f"Measurement taken {v['hba1c_date'] - v['patient__diagnosis_date']} days after diagnosis: {'excluded' if (hba1c_date - diagnosis_date).days < 90 else 'included'}")
# else:
# print(f"Taken after 90 days of diabetes diagnosis: False")
# print(f"{v['visit_date']=}")
# print(f"{v['hba1c']=}")
# print()
hba1c_date__gt=F("patient__diagnosis_date") + timedelta(days=90)
).values("patient__pk", "hba1c")

# Group HbA1c values by patient ID into a list so can use
# calculate_median method
# We're doing this in Python instead of Django ORM because median
# aggregation gets complicated
hba1c_values_by_patient = defaultdict(list)
for visit in valid_visits:
hba1c_values_by_patient[visit["patient__postcode"]].append(visit["hba1c"])
hba1c_values_by_patient[visit["patient__pk"]].append(visit["hba1c"])

# For each patient, calculate the median of their HbA1c values
median_hba1cs = []
for _, hba1c_values in hba1c_values_by_patient.items():
print(f'{_}: {hba1c_values=}')
median_hba1cs.append(self.calculate_median(hba1c_values))

print(f'\n{median_hba1cs=}\n')
# Finally calculate the median of the medians
median_of_median_hba1cs = self.calculate_median(median_hba1cs)
print(f'final result: {median_of_median_hba1cs=}')

# Also set pt querysets to be returned if required
patient_querysets = self._get_pt_querysets_object(
eligible=eligible_patients,
Expand Down
17 changes: 10 additions & 7 deletions project/npda/tests/kpi_calculations/test_kpis_44_49.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ def test_kpi_calculation_45(AUDIT_START_DATE):
SINGLE NUMBER: median of HbA1c measurements (item 17) within the audit
period, excluding measurements taken within 90 days of diagnosis
i.e. valid = hba1c date > diagnosis date + 90 days
NOTE: The median for each patient is calculated. We then calculate the
median of the medians.
Expand All @@ -167,17 +169,17 @@ def test_kpi_calculation_45(AUDIT_START_DATE):
Patient.objects.all().delete()

# Create Patients and Visits that should be eligible (KPI1)
DIAGNOSIS_DATE = AUDIT_START_DATE - relativedelta(days=91)
DIAGNOSIS_DATE = AUDIT_START_DATE - relativedelta(days=89)
eligible_criteria = {
"date_of_birth": AUDIT_START_DATE - relativedelta(days=365 * 10),
"diagnosis_date": DIAGNOSIS_DATE,
}

# Create passing pts
pt_1_hba1cs = [Decimal(n) for n in [45, 46, 47]]
# Diagnosis date is 91 days before AUDIT_START_DATE so valid hba1c dates are
# Diagnosis date is 89 days before AUDIT_START_DATE so valid hba1c dates are
# all from AUDIT_START_DATE+1day onwards
# 91 so we can test for 90 days after diagnosis whilst still being in audit period
pt_1_hba1c_date_1 = AUDIT_START_DATE+relativedelta(days=1)
pt_1_hba1c_date_1 = AUDIT_START_DATE+relativedelta(days=2)
passing_pt_1_median_46 = PatientFactory(
# KPI1 eligible
**eligible_criteria,
Expand Down Expand Up @@ -206,7 +208,7 @@ def test_kpi_calculation_45(AUDIT_START_DATE):
)

pt_2_hba1cs = [Decimal(n) for n in [47, 48, 49]]
pt_2_hba1c_date_1 = AUDIT_START_DATE+relativedelta(days=1)
pt_2_hba1c_date_1 = AUDIT_START_DATE+relativedelta(days=2)
passing_pt_2_median_48 = PatientFactory(
# KPI1 eligible
**eligible_criteria,
Expand All @@ -233,8 +235,9 @@ def test_kpi_calculation_45(AUDIT_START_DATE):
hba1c_date=pt_2_hba1c_date_3,
hba1c=pt_2_hba1cs[2],
)
# This measurement should NOT be counted as it's within 90 days of diagnosis
pt_2_invalid_hba1c_date_wihtin_90_days_diagnosis = AUDIT_START_DATE
# This measurement should NOT be counted as it's exactly 90 days after diagnosis
# even though it's within the audit period
pt_2_invalid_hba1c_date_wihtin_90_days_diagnosis = AUDIT_START_DATE + relativedelta(days=1)
VisitFactory(
patient=passing_pt_2_median_48,
visit_date=pt_2_invalid_hba1c_date_wihtin_90_days_diagnosis,
Expand Down

0 comments on commit e986368

Please sign in to comment.