Skip to content

Commit

Permalink
Merge pull request #96 from opensafely/viv3ckj/refactor-codelists
Browse files Browse the repository at this point in the history
Make codelist names consistent, and remove repeated codelists.
  • Loading branch information
viv3ckj authored Jan 10, 2025
2 parents 4e15124 + 303f0a4 commit 4a321cf
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 53 deletions.
48 changes: 25 additions & 23 deletions analysis/codelists.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from ehrql import codelist_from_csv

# Import pharmacy first conditions codelist
pharmacy_first_conditions_codelist = codelist_from_csv(
pf_conditions_codelist = codelist_from_csv(
"codelists/user-chriswood-pharmacy-first-clinical-pathway-conditions.csv",
column="code",
category_column="term",
Expand All @@ -21,62 +21,64 @@
category_column="term",
)

acute_otitis_media_tx_cod = codelist_from_csv(
acute_otitis_media_tx_codelist = codelist_from_csv(
"codelists/opensafely-pharmacy-first-acute-otitis-media-treatment-dmd.csv",
column="code",
)

impetigo_treatment_tx_cod = codelist_from_csv(
impetigo_treatment_tx_codelist = codelist_from_csv(
"codelists/opensafely-pharmacy-first-impetigo-treatment-dmd.csv",
column="code",
)

infected_insect_bites_tx_cod = codelist_from_csv(
infected_insect_bites_tx_codelist = codelist_from_csv(
"codelists/opensafely-pharmacy-first-infected-insect-bites-treatment-dmd.csv",
column="code",
)

shingles_treatment_tx_cod = codelist_from_csv(
shingles_treatment_tx_codelist = codelist_from_csv(
"codelists/opensafely-pharmacy-first-shingles-treatment-dmd.csv",
column="code",
)

sinusitis_tx_cod = codelist_from_csv(
sinusitis_tx_codelist = codelist_from_csv(
"codelists/opensafely-pharmacy-first-sinusitis-treatment-dmd.csv",
column="code",
)

sore_throat_tx_cod = codelist_from_csv(
sore_throat_tx_codelist = codelist_from_csv(
"codelists/opensafely-pharmacy-first-sore-throat-treatment-dmd.csv",
column="code",
)

urinary_tract_infection_tx_cod = codelist_from_csv(
urinary_tract_infection_tx_codelist = codelist_from_csv(
"codelists/opensafely-pharmacy-first-urinary-tract-infection-treatment-dmd.csv",
column="code",
)

pharmacy_first_med_codelist = (
acute_otitis_media_tx_cod
+ impetigo_treatment_tx_cod
+ infected_insect_bites_tx_cod
+ shingles_treatment_tx_cod
+ sinusitis_tx_cod
+ sore_throat_tx_cod
+ urinary_tract_infection_tx_cod
pf_med_codelist = (
acute_otitis_media_tx_codelist
+ impetigo_treatment_tx_codelist
+ infected_insect_bites_tx_codelist
+ shingles_treatment_tx_codelist
+ sinusitis_tx_codelist
+ sore_throat_tx_codelist
+ urinary_tract_infection_tx_codelist
)
# Community Pharmacist Consultation Service for minor illness - 1577041000000109
pf_consultation_cp_minorillness = ["1577041000000109"]
# Pharmacy First service - 983341000000102
pf_consultation_service = ["983341000000102"]
# Community Pharmacy First Service - 2129921000000100
pharmacy_first_consultation_codelist = ["1577041000000109", "983341000000102", "2129921000000100"]
pf_consultation_cp_service = ["2129921000000100"]

# PF codes separated for individual analysis
pharmacy_first_event_codes = {
pf_consultation_events_dict = {
# Community Pharmacist (CP) Consultation Service for minor illness (procedure)
"consultation_service": ["1577041000000109"],
"pf_consultation_cp_minorillness": pf_consultation_cp_minorillness,
# Pharmacy First service (qualifier value)
"pharmacy_first_service": ["983341000000102"],
"pf_consultation_service": pf_consultation_service,
# Community Pharmacy Pharmacy First Service
"community_pharmacy_first_service": ["2129921000000100"],
"combined_pf_service": ["1577041000000109", "983341000000102", "2129921000000100"],
"pf_consultation_cp_service": pf_consultation_cp_service,
"pf_consultation_services_combined": pf_consultation_cp_minorillness
+ pf_consultation_service + pf_consultation_cp_service,
}
10 changes: 5 additions & 5 deletions analysis/measures_definition_pf_breakdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
ethnicity_from_sus,
)
from codelists import (
pharmacy_first_conditions_codelist,
pf_conditions_codelist,
ethnicity_codelist,
)

from pf_dataset import get_latest_ethnicity
from codelists import pharmacy_first_event_codes, pharmacy_first_consultation_codelist
from codelists import pf_consultation_events_dict
from config import start_date_measure_pf_breakdown, monthly_intervals_measure_pf_breakdown
from pf_variables_library import select_events

Expand Down Expand Up @@ -59,7 +59,7 @@
otherwise="Missing",
)

pharmacy_first_ids = select_events(clinical_events, codelist=pharmacy_first_consultation_codelist).consultation_id
pharmacy_first_ids = select_events(clinical_events, codelist=pf_consultation_events_dict["pf_consultation_services_combined"]).consultation_id

# # Select clinical events in interval date range
selected_events = select_events(clinical_events, start_date=INTERVAL.start_date, end_date=INTERVAL.end_date).where(
Expand All @@ -79,7 +79,7 @@
denominator = registration.exists_for_patient() & patients.sex.is_in(["male", "female"])

# Create measures for pharmacy first services
for pharmacy_first_event, codelist in pharmacy_first_event_codes.items():
for pharmacy_first_event, codelist in pf_consultation_events_dict.items():
condition_events = selected_events.where(
selected_events.snomedct_code.is_in(codelist)
)
Expand Down Expand Up @@ -118,7 +118,7 @@

# Create measures for pharmacy first conditions
pharmacy_first_conditions_codes = {}
for codes, term in pharmacy_first_conditions_codelist.items():
for codes, term in pf_conditions_codelist.items():
normalised_term = term.lower().replace(" ", "_")
codes = [codes]
pharmacy_first_conditions_codes[normalised_term] = codes
Expand Down
4 changes: 2 additions & 2 deletions analysis/measures_definition_pf_condition_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
pharmacy_first_conditions_codes,
imd_quintile,
)
from codelists import pharmacy_first_consultation_codelist
from codelists import pf_consultation_events_dict
from config import start_date_measure_condition_provider, monthly_intervals_measure_condition_provider
from pf_variables_library import select_events

Expand All @@ -23,7 +23,7 @@
selected_events = select_events(clinical_events, start_date=INTERVAL.start_date, end_date=INTERVAL.end_date)

# Create variable which contains boolean values of whether pharmacy first event exists for patient
has_pharmacy_first = select_events(selected_events, codelist=pharmacy_first_consultation_codelist).exists_for_patient()
has_pharmacy_first = select_events(selected_events, codelist=pf_consultation_events_dict["pf_consultation_services_combined"]).exists_for_patient()

for condition_name, condition_code in pharmacy_first_conditions_codes.items():
condition_events = selected_events.where(
Expand Down
8 changes: 4 additions & 4 deletions analysis/measures_definition_pf_consultation_med_counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

from config import start_date_measure_med_counts, monthly_intervals_measure_med_counts
from codelists import (
pharmacy_first_consultation_codelist,
pharmacy_first_med_codelist,
pf_consultation_events_dict,
pf_med_codelist,
)
from pf_variables_library import select_events

Expand All @@ -29,7 +29,7 @@
start_date=INTERVAL.start_date,
end_date=INTERVAL.end_date).where(
clinical_events.snomedct_code.is_in(
pharmacy_first_consultation_codelist
pf_consultation_events_dict["pf_consultation_services_combined"]
)
)

Expand All @@ -47,7 +47,7 @@
selected_medications.sort_by(selected_medications.date).first_for_patient().dmd_code
)
# Boolean variable that selected medication is part of pharmacy first med codelists
has_pharmacy_first_medication = first_selected_medication.is_in(pharmacy_first_med_codelist)
has_pharmacy_first_medication = first_selected_medication.is_in(pf_med_codelist)

# Numerator, patients with a PF medication
# This allows me to count all (first) medications linked to a PF consultation
Expand Down
16 changes: 8 additions & 8 deletions analysis/measures_definition_pf_descriptive_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

from pf_variables_library import select_events
from codelists import (
pharmacy_first_med_codelist,
pharmacy_first_consultation_codelist,
pharmacy_first_conditions_codelist,
pf_med_codelist,
pf_consultation_events_dict,
pf_conditions_codelist,
)
from config import (
start_date_measure_descriptive_stats,
Expand Down Expand Up @@ -39,7 +39,7 @@
# Select all Pharmacy First consultation events
pf_consultation_events = select_events(
selected_events,
codelist=pharmacy_first_consultation_codelist,
codelist=pf_consultation_events_dict["pf_consultation_services_combined"],
)

# Extract Pharmacy First consultation IDs and dates
Expand All @@ -51,12 +51,12 @@
# Select Pharmacy First conditions by ID and date
selected_pf_id_conditions = selected_events.where(
selected_events.consultation_id.is_in(pf_ids)
).where(selected_events.snomedct_code.is_in(pharmacy_first_conditions_codelist))
).where(selected_events.snomedct_code.is_in(pf_conditions_codelist))

selected_pf_date_conditions = (
selected_events.where(selected_events.consultation_id.is_not_in(pf_ids))
.where(selected_events.date.is_in(pf_dates))
.where(selected_events.snomedct_code.is_in(pharmacy_first_conditions_codelist))
.where(selected_events.snomedct_code.is_in(pf_conditions_codelist))
)

has_pf_id_condition = selected_pf_id_conditions.exists_for_patient()
Expand All @@ -65,12 +65,12 @@
# Select Pharmacy First Medications by ID and date
selected_pf_id_medications = selected_medications.where(
selected_medications.consultation_id.is_in(pf_ids)
).where(selected_medications.dmd_code.is_in(pharmacy_first_med_codelist))
).where(selected_medications.dmd_code.is_in(pf_med_codelist))

selected_pf_date_medications = (
selected_medications.where(selected_medications.consultation_id.is_not_in(pf_ids))
.where(selected_medications.date.is_in(pf_dates))
.where(selected_medications.dmd_code.is_in(pharmacy_first_med_codelist))
.where(selected_medications.dmd_code.is_in(pf_med_codelist))
)

has_pf_id_medication = selected_pf_id_medications.exists_for_patient()
Expand Down
6 changes: 3 additions & 3 deletions analysis/measures_definition_pf_medications.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
practice_registrations,
)
from ehrql.tables.raw.tpp import medications
from codelists import pharmacy_first_consultation_codelist, pharmacy_first_med_codelist
from codelists import pf_consultation_events_dict, pf_med_codelist
from config import start_date_measure_medications, monthly_intervals_measure_medications
from pf_variables_library import select_events

Expand All @@ -21,7 +21,7 @@

# Select Pharmacy First consultations during interval date range
pharmacy_first_events = select_events(clinical_events, start_date=INTERVAL.start_date, end_date=INTERVAL.end_date).where(
clinical_events.snomedct_code.is_in(pharmacy_first_consultation_codelist)
clinical_events.snomedct_code.is_in(pf_consultation_events_dict["pf_consultation_services_combined"])
)

pharmacy_first_ids = pharmacy_first_events.consultation_id
Expand All @@ -39,7 +39,7 @@

# Check if a medication is from our PF codelists
has_pharmacy_first_medication = first_selected_medication.is_in(
pharmacy_first_med_codelist
pf_med_codelist
)

# Numerator, patients with a PF medication
Expand Down
16 changes: 8 additions & 8 deletions lib/functions/tidy_measures.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Define dictionaries with tidy names and mappings for measures
pf_measures_name_dict <- list(
consultation_service = "Consultation Service",
pharmacy_first_service = "Pharmacy First Consultation",
community_pharmacy_first_service = "Community Pharmacy First Service",
combined_pf_service = "Pharmacy First Consultations (Combined)",
pf_consultation_cp_minorillness = "Consultation Service",
pf_consultation_service = "Pharmacy First Consultation",
pf_consultation_cp_service = "Community Pharmacy First Service",
pf_consultation_services_combined = "Pharmacy First Consultations (Combined)",
acute_otitis_media = "Acute Otitis Media",
herpes_zoster = "Herpes Zoster",
acute_sinusitis = "Acute Sinusitis",
Expand All @@ -14,10 +14,10 @@ pf_measures_name_dict <- list(
)

pf_measures_name_mapping <- list(
consultation_service = "clinical_service",
pharmacy_first_service = "clinical_service",
community_pharmacy_first_service = "clinical_service",
combined_pf_service = "pharmacy_first_services",
pf_consultation_cp_minorillness = "clinical_service",
pf_consultation_service = "clinical_service",
pf_consultation_cp_service = "clinical_service",
pf_consultation_services_combined = "pharmacy_first_services",
acute_otitis_media = "clinical_condition",
herpes_zoster = "clinical_condition",
acute_sinusitis = "clinical_condition",
Expand Down

0 comments on commit 4a321cf

Please sign in to comment.