diff --git a/project/npda/kpi_class/kpis.py b/project/npda/kpi_class/kpis.py index 25680015..21aefe9b 100644 --- a/project/npda/kpi_class/kpis.py +++ b/project/npda/kpi_class/kpis.py @@ -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. @@ -3273,35 +3275,8 @@ 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 @@ -3309,18 +3284,16 @@ def calculate_kpi_45_median_hba1c( # 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, diff --git a/project/npda/tests/kpi_calculations/test_kpis_44_49.py b/project/npda/tests/kpi_calculations/test_kpis_44_49.py index f4b9dbc1..d4625164 100644 --- a/project/npda/tests/kpi_calculations/test_kpis_44_49.py +++ b/project/npda/tests/kpi_calculations/test_kpis_44_49.py @@ -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. @@ -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, @@ -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, @@ -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,