|
2 | 2 | from ehrql.tables.raw.tpp import medications
|
3 | 3 | from ehrql.tables.tpp import practice_registrations, patients, clinical_events
|
4 | 4 |
|
5 |
| -from pf_variables_library import select_events_from_codelist, select_events_by_consultation_id |
| 5 | +from pf_variables_library import select_events |
6 | 6 | from codelists import (
|
7 | 7 | pharmacy_first_med_codelist,
|
8 | 8 | pharmacy_first_consultation_codelist,
|
9 | 9 | pharmacy_first_conditions_codelist,
|
10 | 10 | )
|
11 |
| -from config import start_date_measure_descriptive_stats, monthly_intervals_measure_descriptive_stats |
| 11 | +from config import ( |
| 12 | + start_date_measure_descriptive_stats, |
| 13 | + monthly_intervals_measure_descriptive_stats, |
| 14 | +) |
12 | 15 |
|
13 | 16 | measures = create_measures()
|
14 |
| -measures.configure_dummy_data(population_size=1000) |
| 17 | +measures.configure_dummy_data(population_size=100) |
15 | 18 | measures.configure_disclosure_control(enabled=True)
|
16 | 19 |
|
17 | 20 | start_date = start_date_measure_descriptive_stats
|
18 | 21 | monthly_intervals = monthly_intervals_measure_descriptive_stats
|
19 | 22 |
|
20 | 23 | registration = practice_registrations.for_patient_on(INTERVAL.end_date)
|
21 | 24 |
|
22 |
| -# Function to retrieve consultation ids from clinical events that are PF consultations |
23 |
| -pharmacy_first_ids = select_events_from_codelist( |
24 |
| - clinical_events, pharmacy_first_consultation_codelist |
25 |
| -).consultation_id |
| 25 | +# Select clinical events and medications for measures INTERVAL |
| 26 | +selected_events = clinical_events.where( |
| 27 | + clinical_events.date.is_on_or_between( |
| 28 | + INTERVAL.start_date, |
| 29 | + INTERVAL.end_date, |
| 30 | + ) |
| 31 | +) |
| 32 | +selected_medications = medications.where( |
| 33 | + medications.date.is_on_or_between( |
| 34 | + INTERVAL.start_date, |
| 35 | + INTERVAL.end_date, |
| 36 | + ) |
| 37 | +) |
| 38 | + |
| 39 | +# Select all Pharmacy First consultation events |
| 40 | +pf_consultation_events = select_events( |
| 41 | + selected_events, |
| 42 | + codelist=pharmacy_first_consultation_codelist, |
| 43 | +) |
| 44 | + |
| 45 | +# Extract Pharmacy First consultation IDs and dates |
| 46 | +pf_ids = pf_consultation_events.consultation_id |
| 47 | +pf_dates = pf_consultation_events.date |
26 | 48 |
|
27 |
| -# Function to retrieve selected events using pharmacy first ids |
28 |
| -selected_clinical_events = select_events_by_consultation_id( |
29 |
| - clinical_events, pharmacy_first_ids |
30 |
| -).where(clinical_events.date.is_on_or_between(INTERVAL.start_date, INTERVAL.end_date)) |
| 49 | +has_pf_consultation = pf_consultation_events.exists_for_patient() |
31 | 50 |
|
32 |
| -selected_med_events = select_events_by_consultation_id(medications, pharmacy_first_ids).where( |
33 |
| - medications.date.is_on_or_between(INTERVAL.start_date, INTERVAL.end_date) |
| 51 | +# Select Pharmacy First conditions by ID and date |
| 52 | +selected_pf_id_conditions = selected_events.where( |
| 53 | + selected_events.consultation_id.is_in(pf_ids) |
| 54 | +).where(selected_events.snomedct_code.is_in(pharmacy_first_conditions_codelist)) |
| 55 | + |
| 56 | +selected_pf_date_conditions = ( |
| 57 | + selected_events.where(selected_events.consultation_id.is_not_in(pf_ids)) |
| 58 | + .where(selected_events.date.is_in(pf_dates)) |
| 59 | + .where(selected_events.snomedct_code.is_in(pharmacy_first_conditions_codelist)) |
34 | 60 | )
|
35 | 61 |
|
36 |
| -# Create variable which contains boolean values of whether pharmacy first event exists for patient |
37 |
| -has_pf_consultation = select_events_from_codelist(selected_clinical_events, pharmacy_first_consultation_codelist).exists_for_patient() |
| 62 | +has_pf_id_condition = selected_pf_id_conditions.exists_for_patient() |
| 63 | +has_pf_date_condition = selected_pf_date_conditions.exists_for_patient() |
| 64 | + |
| 65 | +# Select Pharmacy First Medications by ID and date |
| 66 | +selected_pf_id_medications = selected_medications.where( |
| 67 | + selected_medications.consultation_id.is_in(pf_ids) |
| 68 | +).where(selected_medications.dmd_code.is_in(pharmacy_first_med_codelist)) |
38 | 69 |
|
39 |
| -# PF consultations with PF clinical condition |
40 |
| -has_pf_condition = select_events_from_codelist(selected_clinical_events, pharmacy_first_conditions_codelist).exists_for_patient() |
| 70 | +selected_pf_date_medications = ( |
| 71 | + selected_medications.where(selected_medications.consultation_id.is_not_in(pf_ids)) |
| 72 | + .where(selected_medications.date.is_in(pf_dates)) |
| 73 | + .where(selected_medications.dmd_code.is_in(pharmacy_first_med_codelist)) |
| 74 | +) |
41 | 75 |
|
42 |
| -# PF consultations with prescribed PF medication |
43 |
| -has_pf_medication = selected_med_events.where( |
44 |
| - selected_med_events.dmd_code.is_in(pharmacy_first_med_codelist) |
45 |
| -).exists_for_patient() |
| 76 | +has_pf_id_medication = selected_pf_id_medications.exists_for_patient() |
| 77 | +has_pf_date_medication = selected_pf_date_medications.exists_for_patient() |
46 | 78 |
|
47 |
| -# Define the denominator as the number of patients registered |
48 |
| -denominator = ( |
49 |
| - registration.exists_for_patient() |
50 |
| - & patients.sex.is_in(["male", "female"]) |
51 |
| - & has_pf_consultation |
| 79 | +# Define measures |
| 80 | +measures.define_defaults( |
| 81 | + denominator=( |
| 82 | + registration.exists_for_patient() |
| 83 | + & patients.sex.is_in(["male", "female"]) |
| 84 | + & has_pf_consultation |
| 85 | + ), |
| 86 | + intervals=months(monthly_intervals).starting_on(start_date), |
52 | 87 | )
|
53 |
| -measures.define_defaults(denominator=denominator) |
54 | 88 |
|
55 |
| -# Measures for PF consultations with PF medication |
| 89 | +# Measures linked by Pharmacy First consultation ID |
56 | 90 | measures.define_measure(
|
57 |
| - name="pf_with_pfmed", |
58 |
| - numerator=has_pf_medication, |
59 |
| - intervals=months(monthly_intervals).starting_on(start_date), |
| 91 | + name="pfmed_with_pfid", |
| 92 | + numerator=has_pf_id_medication, |
60 | 93 | )
|
61 |
| -# Measures for PF consultations with PF condition |
| 94 | + |
62 | 95 | measures.define_measure(
|
63 |
| - name="pf_with_pfcondition", |
64 |
| - numerator=has_pf_condition, |
65 |
| - intervals=months(monthly_intervals).starting_on(start_date), |
| 96 | + name="pfcondition_with_pfid", |
| 97 | + numerator=has_pf_id_condition, |
66 | 98 | )
|
67 | 99 |
|
68 |
| -# Measures for PF consultations with both PF medication and condition |
69 | 100 | measures.define_measure(
|
70 |
| - name="pf_with_pfmed_and_pfcondition", |
71 |
| - numerator=has_pf_condition & has_pf_medication, |
72 |
| - intervals=months(monthly_intervals).starting_on(start_date), |
| 101 | + name="pfmed_and_pfcondition_with_pfid", |
| 102 | + numerator=has_pf_id_medication & has_pf_id_condition, |
| 103 | +) |
| 104 | + |
| 105 | +# Measures linked by Pharmacy First consultation date |
| 106 | +measures.define_measure( |
| 107 | + name="pfmed_on_pfdate", |
| 108 | + numerator=has_pf_date_medication, |
| 109 | +) |
| 110 | + |
| 111 | +measures.define_measure( |
| 112 | + name="pfcondition_on_pfdate", |
| 113 | + numerator=has_pf_date_condition, |
| 114 | +) |
| 115 | + |
| 116 | +measures.define_measure( |
| 117 | + name="pfmed_and_pfcondition_on_pfdate", |
| 118 | + numerator=has_pf_date_medication & has_pf_date_condition, |
73 | 119 | )
|
0 commit comments