generated from opensafely/research-template
-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
23cc8e9
added age groups and facet function
viv3ckj 20e5797
added age grouping to clinical conditions
viv3ckj b92b58a
added breakdown by sex
viv3ckj 170181d
added breakdown by IMD
viv3ckj c6e1aa7
added region breakdown
viv3ckj 32322e4
Remove unknown sex from population
viv3ckj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ethnicity_from_sus | ||
|
||
measures = create_measures() | ||
measures.configure_dummy_data(population_size=1000) | ||
|
@@ -28,6 +28,29 @@ | |
|
||
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 | ||
max_imd = 32844 | ||
imd_quintile = case( | ||
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" | ||
) | ||
|
||
# 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) | ||
|
@@ -43,7 +66,7 @@ | |
numerator = condition_events.count_for_patient() | ||
|
||
# Define the denominator as the number of patients registered | ||
denominator = registration.exists_for_patient() | ||
denominator = registration.exists_for_patient() & patients.sex.is_in(["male", "female", "intersex"]) | ||
|
||
measures.define_measure( | ||
name=f"count_{pharmacy_first_event}", | ||
|
@@ -52,6 +75,61 @@ | |
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 |
||
|
||
# Measures for ethnicity code breakdown of clinical services | ||
measures.define_measure( | ||
name=f"count_{pharmacy_first_event}_by_ethnicity", | ||
numerator=numerator, | ||
denominator=denominator, | ||
group_by={ | ||
"ethnicity": ethnicity_from_sus.code, | ||
}, | ||
intervals=months(monthly_intervals).starting_on(start_date), | ||
) | ||
|
||
# Create measures for pharmacy first conditions | ||
pharmacy_first_conditions_codes = {} | ||
for codes, term in pharmacy_first_conditions_codelist.items(): | ||
|
@@ -76,3 +154,57 @@ | |
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), | ||
) | ||
|
||
# Measures for region breakdown of clinical conditions | ||
measures.define_measure( | ||
name=f"count_{condition_name}_by_ethnicity", | ||
numerator=numerator, | ||
denominator=denominator, | ||
group_by={ | ||
"ethnicity": ethnicity_from_sus.code, | ||
}, | ||
intervals=months(monthly_intervals).starting_on(start_date), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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!