Skip to content

Commit

Permalink
Merge pull request #21 from opensafely/milanwiedemann/update-report-b…
Browse files Browse the repository at this point in the history
…reakdown

Update report breakdown
  • Loading branch information
milanwiedemann authored Oct 4, 2024
2 parents b4c294d + 40a7663 commit e87c20e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 55 deletions.
15 changes: 15 additions & 0 deletions analysis/codelists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from ehrql import codelist_from_csv

# Import pharmacy first conditions codelist
pharmacy_first_conditions_codelist = codelist_from_csv(
"codelists/user-chriswood-pharmacy-first-clinical-pathway-conditions.csv",
column="code",
category_column="term",
)

# Import ethnicity codelist
ethnicity_codelist = codelist_from_csv(
"codelists/opensafely-ethnicity-snomed-0removed.csv",
column="snomedcode",
category_column="Grouping_6",
)
83 changes: 28 additions & 55 deletions analysis/measures_definition_pf_codes_conditions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from ehrql import INTERVAL, create_measures, months, codelist_from_csv, case, when
from ehrql.tables.tpp import clinical_events, practice_registrations, patients, addresses, ethnicity_from_sus
from ehrql import INTERVAL, create_measures, months, case, when
from ehrql.tables.tpp import (
clinical_events,
practice_registrations,
patients,
addresses,
)
from codelists import pharmacy_first_conditions_codelist, ethnicity_codelist

measures = create_measures()
measures.configure_dummy_data(population_size=1000)
Expand All @@ -19,55 +25,25 @@
"pharmacy_first_service": ["983341000000102"],
}

# Import pharmacy first conditions codelist
pharmacy_first_conditions_codelist = codelist_from_csv(
"codelists/user-chriswood-pharmacy-first-clinical-pathway-conditions.csv",
column="code",
category_column="term",
)
registration = practice_registrations.for_patient_on(INTERVAL.end_date)

# Import ethnicity codelist
ethnicity_codelist = codelist_from_csv(
"codelists/opensafely-ethnicity-snomed-0removed.csv",
column="snomedcode",
category_column="Grouping_6",
latest_ethnicity_category_num = (
clinical_events.where(clinical_events.snomedct_code.is_in(ethnicity_codelist))
.where(clinical_events.date.is_on_or_before(INTERVAL.start_date))
.sort_by(clinical_events.date)
.last_for_patient()
.snomedct_code.to_category(ethnicity_codelist)
)


# # Get the latest ethnicity data for each patient
# ethnicity = (
# clinical_events.where(
# clinical_events.snomedct_code.is_in(ethnicity_codelist)
# )
# .sort_by(clinical_events.date)
# .last_for_patient()
# .snomedct_code.to_category(ethnicity_codelist)
# )

# # Get the latest ethnicity data for each patient
# latest_ethnicity_code = (
# clinical_events.where(clinical_events.snomedct_code.is_in(ethnicity_codelist))
# .where(clinical_events.date.is_on_or_before(INTERVAL.start_date))
# .sort_by(clinical_events.date)
# .last_for_patient()
# .snomedct_code
# )

# ethnicity = latest_ethnicity_code.to_category(ethnicity_codelist)

ethnicity = (
clinical_events.where(clinical_events
.snomedct_code.is_in(ethnicity_codelist))
.where(clinical_events.date.is_on_or_before(INTERVAL.start_date))
.sort_by(clinical_events.date)
.last_for_patient()
.snomedct_code
latest_ethnicity_category_desc = case(
when(latest_ethnicity_category_num == "1").then("White"),
when(latest_ethnicity_category_num == "2").then("Mixed"),
when(latest_ethnicity_category_num == "3").then("Asian or Asian British"),
when(latest_ethnicity_category_num == "4").then("Black or Black British"),
when(latest_ethnicity_category_num == "5").then("Chinese or Other Ethnic Groups"),
when(latest_ethnicity_category_num.is_null()).then("Missing"),
)

ethnicity = ethnicity.to_category(ethnicity_codelist)

registration = practice_registrations.for_patient_on(INTERVAL.end_date)

# Age bands for age breakdown
age = patients.age_on(INTERVAL.start_date)
age_band = case(
Expand All @@ -83,15 +59,14 @@
imd = addresses.for_patient_on(INTERVAL.start_date).imd_rounded
max_imd = 32844
imd_quintile = case(
when((imd >=0) & (imd < int(max_imd * 1 / 5))).then("1"),
when((imd >= 0) & (imd < int(max_imd * 1 / 5))).then("1"),
when(imd < int(max_imd * 2 / 5)).then("2"),
when(imd < int(max_imd * 3 / 5)).then("3"),
when(imd < int(max_imd * 4 / 5)).then("4"),
when(imd <= max_imd).then("5"),
otherwise="Missing"
otherwise="Missing",
)


# Select clinical events in interval date range
selected_events = clinical_events.where(
clinical_events.date.is_on_or_between(INTERVAL.start_date, INTERVAL.end_date)
Expand All @@ -103,13 +78,12 @@
"sex": patients.sex,
"imd": imd_quintile,
"region": registration.practice_nuts1_region_name,
"ethnicity": ethnicity,
"ethnicity": latest_ethnicity_category_desc,
}

# Define the denominator as the number of patients registered
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():
condition_events = selected_events.where(
Expand All @@ -135,7 +109,7 @@
denominator=denominator,
group_by={breakdown: variable},
intervals=months(monthly_intervals).starting_on(start_date),
)
)

# Create measures for pharmacy first conditions
pharmacy_first_conditions_codes = {}
Expand All @@ -158,7 +132,7 @@
numerator=numerator,
denominator=denominator,
intervals=months(monthly_intervals).starting_on(start_date),
)
)

# Nested loop for each breakdown measure in clinical conditions
for breakdown, variable in breakdown_metrics.items():
Expand All @@ -168,5 +142,4 @@
denominator=denominator,
group_by={breakdown: variable},
intervals=months(monthly_intervals).starting_on(start_date),
)

)

0 comments on commit e87c20e

Please sign in to comment.