|
| 1 | +from ehrql import INTERVAL, create_measures, months, case, when |
| 2 | +from ehrql.tables.tpp import ( |
| 3 | + practice_registrations, |
| 4 | + patients, |
| 5 | + clinical_events, |
| 6 | + addresses, |
| 7 | +) |
| 8 | +from dm_reg_dataset import dm_cod |
| 9 | + |
| 10 | +measures = create_measures() |
| 11 | +measures.configure_dummy_data(population_size=100) |
| 12 | +measures.configure_disclosure_control(enabled=False) |
| 13 | + |
| 14 | +start_date = "2017-03-31" |
| 15 | + |
| 16 | +# Is patient registered to a GP practice? (Boolean variable) |
| 17 | +has_registration = practice_registrations.for_patient_on( |
| 18 | + INTERVAL.end_date |
| 19 | +).exists_for_patient() |
| 20 | + |
| 21 | +# Select clinical events for measures interval |
| 22 | +selected_events = clinical_events.where( |
| 23 | + clinical_events.date.is_on_or_between( |
| 24 | + INTERVAL.start_date, |
| 25 | + INTERVAL.end_date, |
| 26 | + ) |
| 27 | +) |
| 28 | + |
| 29 | +# Select clinical events with a diabetes diagnosis code (occurence - how many patients had a diabetes-coded clinical event during the interval) |
| 30 | +dm_occurence = selected_events.where( |
| 31 | + selected_events.snomedct_code.is_in(dm_cod) |
| 32 | +).exists_for_patient() |
| 33 | + |
| 34 | +# Age band groupings |
| 35 | +age = patients.age_on(INTERVAL.start_date) |
| 36 | +age_band = case( |
| 37 | + when((age >= 0) & (age < 20)).then("0-19"), |
| 38 | + when((age >= 20) & (age < 40)).then("20-39"), |
| 39 | + when((age >= 40) & (age < 60)).then("40-59"), |
| 40 | + when((age >= 60) & (age < 80)).then("60-79"), |
| 41 | + when(age >= 80).then("80+"), |
| 42 | + when(age.is_null()).then("Missing"), |
| 43 | +) |
| 44 | + |
| 45 | +# IMD groupings |
| 46 | +imd = addresses.for_patient_on(INTERVAL.start_date).imd_rounded |
| 47 | +max_imd = 32844 |
| 48 | +imd_quintile = case( |
| 49 | + when((imd >= 0) & (imd < int(max_imd * 1 / 5))).then("1 (Most Deprived)"), |
| 50 | + when(imd < int(max_imd * 2 / 5)).then("2"), |
| 51 | + when(imd < int(max_imd * 3 / 5)).then("3"), |
| 52 | + when(imd < int(max_imd * 4 / 5)).then("4"), |
| 53 | + when(imd <= max_imd).then("5 (Least Deprived)"), |
| 54 | + otherwise="Missing", |
| 55 | +) |
| 56 | + |
| 57 | +# Defining default denominators (as we are using the same denominator and interval for each measure here) |
| 58 | +measures.define_defaults( |
| 59 | + denominator=(has_registration & (patients.age_on(INTERVAL.start_date) > 17)), |
| 60 | + intervals=months(12).starting_on(start_date), |
| 61 | +) |
| 62 | + |
| 63 | +# This measure looks at the number of patients with a diabetes diagnosis code per interval |
| 64 | +measures.define_measure(name="dm_occurence", numerator=dm_occurence) |
| 65 | + |
| 66 | +# This measure looks at the number of patients with a diabetes diagnosis code per interval by age band |
| 67 | +measures.define_measure( |
| 68 | + name="dm_occurence_by_age", numerator=dm_occurence, group_by={"age_band": age_band} |
| 69 | +) |
| 70 | + |
| 71 | +# This measure looks at the number of patients with a diabetes diagnosis code per interval by IMD |
| 72 | +measures.define_measure( |
| 73 | + name="dm_occurence_by_imd", numerator=dm_occurence, group_by={"imd": imd_quintile} |
| 74 | +) |
0 commit comments