-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added breakdown graphs and facet function #11
Changes from 5 commits
23cc8e9
20e5797
b92b58a
170181d
c6e1aa7
32322e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,5 +1,5 @@ | ||||||
from ehrql import INTERVAL, create_measures, months, codelist_from_csv | ||||||
from ehrql.tables.tpp import clinical_events, practice_registrations | ||||||
from ehrql import INTERVAL, create_measures, months, codelist_from_csv, case, when | ||||||
from ehrql.tables.tpp import clinical_events, practice_registrations, patients, addresses | ||||||
|
||||||
measures = create_measures() | ||||||
measures.configure_dummy_data(population_size=1000) | ||||||
|
@@ -28,6 +28,28 @@ | |||||
|
||||||
registration = practice_registrations.for_patient_on(INTERVAL.end_date) | ||||||
|
||||||
# Age bands for age breakdown | ||||||
age = patients.age_on(INTERVAL.start_date) | ||||||
age_band = case( | ||||||
when((age >= 0) & (age < 20)).then("0-19"), | ||||||
when((age >= 20) & (age < 40)).then("20-39"), | ||||||
when((age >= 40) & (age < 60)).then("40-59"), | ||||||
when((age >= 60) & (age < 80)).then("60-79"), | ||||||
when(age >= 80).then("80+"), | ||||||
when(age.is_null()).then("Missing"), | ||||||
) | ||||||
|
||||||
# IMD groupings for IMD breakdown | ||||||
imd = addresses.for_patient_on(INTERVAL.start_date).imd_rounded | ||||||
imd_quintile = case( | ||||||
when((imd >=0) & (imd < int(32844 * 1 / 5))).then("1 (most deprived)"), | ||||||
when(imd < int(32844 * 2 / 5)).then("2"), | ||||||
when(imd < int(32844 * 3 / 5)).then("3"), | ||||||
when(imd < int(32844 * 4 / 5)).then("4"), | ||||||
when(imd < int(32844 * 5 / 5)).then("5 (least deprived)"), | ||||||
otherwise="unknown" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. although the code does exactly the same as this suggestions in the tutorial, I'd use something like this because it's a bit easier to review and harder to make a mistake: imd_rounded = addresses.for_patient_on(
INTERVAL.start_date
).imd_rounded
max_imd = 32844
dataset.imd_quintile = case(
when(imd_rounded < int(max_imd * 1 / 5)).then(1),
when(imd_rounded < int(max_imd * 2 / 5)).then(2),
when(imd_rounded < int(max_imd * 3 / 5)).then(3),
when(imd_rounded < int(max_imd * 4 / 5)).then(4),
when(imd_rounded <= max_imd).then(5),
) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to be consistent with the value for "Missing" data, I'd use the same as for age bands here as well
Suggested change
|
||||||
) | ||||||
|
||||||
# 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) | ||||||
|
@@ -51,6 +73,49 @@ | |||||
denominator=denominator, | ||||||
intervals=months(monthly_intervals).starting_on(start_date), | ||||||
) | ||||||
# Measures for age breakdown of clinical services | ||||||
measures.define_measure( | ||||||
name=f"count_{pharmacy_first_event}_by_age", | ||||||
numerator=numerator, | ||||||
denominator=denominator, | ||||||
group_by={ | ||||||
"age_band": age_band, | ||||||
}, | ||||||
intervals=months(monthly_intervals).starting_on(start_date), | ||||||
) | ||||||
|
||||||
# Measures for sex breakdown of clinical services | ||||||
measures.define_measure( | ||||||
name=f"count_{pharmacy_first_event}_by_sex", | ||||||
numerator=numerator, | ||||||
denominator=denominator, | ||||||
group_by={ | ||||||
"sex": patients.sex, | ||||||
}, | ||||||
intervals=months(monthly_intervals).starting_on(start_date), | ||||||
) | ||||||
|
||||||
# Measures for IMD breakdown of clinical services | ||||||
measures.define_measure( | ||||||
name=f"count_{pharmacy_first_event}_by_imd", | ||||||
numerator=numerator, | ||||||
denominator=denominator, | ||||||
group_by={ | ||||||
"imd": imd_quintile, | ||||||
}, | ||||||
intervals=months(monthly_intervals).starting_on(start_date), | ||||||
) | ||||||
|
||||||
# Measures for region breakdown of clinical services | ||||||
measures.define_measure( | ||||||
name=f"count_{pharmacy_first_event}_by_region", | ||||||
numerator=numerator, | ||||||
denominator=denominator, | ||||||
group_by={ | ||||||
"region": registration.practice_nuts1_region_name, | ||||||
}, | ||||||
intervals=months(monthly_intervals).starting_on(start_date), | ||||||
) | ||||||
Comment on lines
+78
to
+120
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this looks correct, but I think we can make this much shorter if we group by multiple features, see https://docs.opensafely.org/ehrql/explanation/measures/#grouping-by-multiple-features |
||||||
|
||||||
# Create measures for pharmacy first conditions | ||||||
pharmacy_first_conditions_codes = {} | ||||||
|
@@ -76,3 +141,46 @@ | |||||
denominator=denominator, | ||||||
intervals=months(monthly_intervals).starting_on(start_date), | ||||||
) | ||||||
# Measures for age breakdown of clinical conditions | ||||||
measures.define_measure( | ||||||
name=f"count_{condition_name}_by_age", | ||||||
numerator=numerator, | ||||||
denominator=denominator, | ||||||
group_by={ | ||||||
"age_band": age_band, | ||||||
}, | ||||||
intervals=months(monthly_intervals).starting_on(start_date), | ||||||
) | ||||||
|
||||||
# Measures for age breakdown of clinical conditions | ||||||
measures.define_measure( | ||||||
name=f"count_{condition_name}_by_sex", | ||||||
numerator=numerator, | ||||||
denominator=denominator, | ||||||
group_by={ | ||||||
"sex": patients.sex, | ||||||
}, | ||||||
intervals=months(monthly_intervals).starting_on(start_date), | ||||||
) | ||||||
|
||||||
# Measures for imd breakdown of clinical conditions | ||||||
measures.define_measure( | ||||||
name=f"count_{condition_name}_by_imd", | ||||||
numerator=numerator, | ||||||
denominator=denominator, | ||||||
group_by={ | ||||||
"imd": imd_quintile, | ||||||
}, | ||||||
intervals=months(monthly_intervals).starting_on(start_date), | ||||||
) | ||||||
|
||||||
# Measures for region breakdown of clinical conditions | ||||||
measures.define_measure( | ||||||
name=f"count_{condition_name}_by_region", | ||||||
numerator=numerator, | ||||||
denominator=denominator, | ||||||
group_by={ | ||||||
"region": registration.practice_nuts1_region_name, | ||||||
}, | ||||||
intervals=months(monthly_intervals).starting_on(start_date), | ||||||
) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see comment above, I think we can make this consierably shorter |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks great!