Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions analysis/dataset_definition_med_status_data_development.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

# Time interval for selecting medications pre and post Pharmacy First launch date
time_interval = months(6)
pf_period = months(25)

pharmacy_first_ids = clinical_events.where(
clinical_events.snomedct_code.is_in(pharmacy_first_event_codes)
Expand Down Expand Up @@ -96,7 +97,7 @@
selected_medications_pfmed_post = (
medications.where(
medications.date.is_on_or_between(
pharmacy_first_launch_date, pharmacy_first_launch_date + time_interval
pharmacy_first_launch_date, pharmacy_first_launch_date + pf_period
)
)
.where(medications.dmd_code.is_in(pharmacy_first_med_codes))
Expand All @@ -108,7 +109,7 @@
selected_medications_pfmed_pfid_post = (
medications.where(
medications.date.is_on_or_between(
pharmacy_first_launch_date, pharmacy_first_launch_date + time_interval
pharmacy_first_launch_date, pharmacy_first_launch_date + pf_period
)
)
.where(medications.dmd_code.is_in(pharmacy_first_med_codes))
Expand All @@ -120,7 +121,7 @@
selected_medications_anymed_pfid_post = (
medications.where(
medications.date.is_on_or_between(
pharmacy_first_launch_date, pharmacy_first_launch_date + time_interval
pharmacy_first_launch_date, pharmacy_first_launch_date + pf_period
)
)
.where(medications.consultation_id.is_in(pharmacy_first_ids))
Expand All @@ -131,7 +132,7 @@
selected_medications_pfdate_pfid_post = (
medications.where(
medications.date.is_on_or_between(
pharmacy_first_launch_date, pharmacy_first_launch_date + time_interval
pharmacy_first_launch_date, pharmacy_first_launch_date + pf_period
)
)
.where(medications.consultation_id.is_in(pharmacy_first_ids))
Expand All @@ -156,7 +157,7 @@
selected_medications_pfmed_pfdate_post = (
medications.where(
medications.date.is_on_or_between(
pharmacy_first_launch_date, pharmacy_first_launch_date + time_interval
pharmacy_first_launch_date, pharmacy_first_launch_date + pf_period
)
)
.where(medications.dmd_code.is_in(pharmacy_first_med_codes))
Expand All @@ -180,7 +181,7 @@
selected_medications_anymed_pfdate_post = (
medications.where(
medications.date.is_on_or_between(
pharmacy_first_launch_date, pharmacy_first_launch_date + time_interval
pharmacy_first_launch_date, pharmacy_first_launch_date + pf_period
)
)
.where(medications.date.is_in(pharmacy_first_dates))
Expand Down
6 changes: 5 additions & 1 deletion analysis/measures_definition_clinical_codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

# Dictionary of pharmacy first codes
pharmacy_first_event_codes = {
# Blood Pressure Check Service (1659111000000107)
"blood_pressure_service": ["1659111000000107"],
# Contraception service (1659121000000101)
"contraception_service": ["1659121000000101"],
# Community Pharmacist (CP) Consultation Service for minor illness (procedure)
"consultation_service": ["1577041000000109"],
# Pharmacy First service (qualifier value)
Expand All @@ -29,7 +33,7 @@
pharmacy_first_code_counts[f"count_{code_desc}"] = count_codes_query

# Define intervals and measures
intervals = months(18).starting_on("2023-02-01")
intervals = months(37).starting_on("2023-02-01")

for measures_name, code_counts in pharmacy_first_code_counts.items():
measures.define_measure(
Expand Down
21 changes: 21 additions & 0 deletions analysis/measures_med_status_counts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from ehrql import create_dataset, months, days, INTERVAL, create_measures
from ehrql.tables.tpp import patients, clinical_events
from ehrql.tables.raw.tpp import medications

from codelists import pharmacy_first_event_codes
measures = create_measures()
start_date = "2023-08-01"
monthly_intervals = 32

pharmacy_first_ids = clinical_events.where(clinical_events.snomedct_code.is_in(pharmacy_first_event_codes)).consultation_id

pf_medications = medications.where(medications.consultation_id.is_in(pharmacy_first_ids)).where(medications.date.is_on_or_between(INTERVAL.start_date, INTERVAL.end_date)).sort_by(medications.date).first_for_patient()
medication_status = pf_medications.medication_status

measures.define_measure(
name="pf_medications_by_med_status",
numerator=pf_medications.count_for_patient(),
denominator=pf_medications.count_for_patient(),
group_by={"medication_status": medication_status},
intervals=months(monthly_intervals).starting_on(start_date),
)
4 changes: 2 additions & 2 deletions analysis/test_dataset_definition_pf_data_development.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@
"post_all_count_pf_events": 2,
"post_pfid_count_pf_events": 2,
"post_pfdate_count_pf_events": 2,
"post_all_count_pf_pathways": 2,
"post_all_count_pf_pathways": 3,
"post_pfid_count_pf_pathways": 1,
"post_pfdate_count_pf_pathways": 3,
"post_pfdate_count_pf_pathways": 2,
},
},
}
10 changes: 9 additions & 1 deletion project.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,12 @@ actions:
--output output/measures/consultation_med_counts_measures.csv
outputs:
moderately_sensitive:
measure: output/measures/consultation_med_counts_measures.csv
measure: output/measures/consultation_med_counts_measures.csv

generate_med_status_counts_measures:
run: >
ehrql:v1 generate-measures analysis/measures_med_status_counts.py
--output output/measures/measures_med_status_counts.csv
outputs:
moderately_sensitive:
measure: output/measures/measures_med_status_counts.csv
Loading
Loading