generated from opensafely/research-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from opensafely/viv3ckj/update-descriptive-sta…
…ts-measures Viv3ckj/update descriptive stats measures
- Loading branch information
Showing
7 changed files
with
121 additions
and
39 deletions.
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
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,66 +1,76 @@ | ||
from ehrql import INTERVAL, create_measures, months | ||
from ehrql.tables.tpp import ( | ||
practice_registrations, | ||
patients, | ||
) | ||
from ehrql.tables.raw.tpp import medications | ||
from ehrql.tables.tpp import practice_registrations, patients, clinical_events | ||
|
||
from pf_dataset import pharmacy_first_event_codes | ||
from measures_definition_pf_medications import pharmacy_first_med_codes | ||
from measures_definition_pf_breakdown import pharmacy_first_conditions_codes, selected_events | ||
from pf_variables_library import get_consultation_ids, get_consultationid_events | ||
from codelists import ( | ||
pharmacy_first_med_codelist, | ||
pharmacy_first_consultation_codelist, | ||
pharmacy_first_conditions_codelist, | ||
) | ||
|
||
measures = create_measures() | ||
measures.configure_dummy_data(population_size=1000) | ||
measures.configure_disclosure_control(enabled=True) | ||
|
||
start_date = "2024-02-01" | ||
monthly_intervals = 9 | ||
|
||
registration = practice_registrations.for_patient_on(INTERVAL.end_date) | ||
|
||
# Loop through all codes in each sublist of the dictionary to flatten the list ready for is_in commands to be used and have a list of pf_condition codes | ||
pf_condition_codelist = [code for sublist in pharmacy_first_conditions_codes.values() for code in sublist] | ||
# Function to retrieve consultation ids from clinical events that are PF consultations | ||
pharmacy_first_ids = get_consultation_ids( | ||
clinical_events, pharmacy_first_consultation_codelist | ||
) | ||
|
||
# Function to retrieve selected events using pharmacy first ids | ||
selected_clinical_events = get_consultationid_events( | ||
clinical_events, pharmacy_first_ids | ||
).where(clinical_events.date.is_on_or_between(INTERVAL.start_date, INTERVAL.end_date)) | ||
|
||
selected_med_events = get_consultationid_events(medications, pharmacy_first_ids).where( | ||
medications.date.is_on_or_between(INTERVAL.start_date, INTERVAL.end_date) | ||
) | ||
|
||
# Create variable which contains boolean values of whether pharmacy first event exists for patient | ||
has_pf_consultation = selected_events.where( | ||
selected_events.snomedct_code.is_in( | ||
pharmacy_first_event_codes["combined_pf_service"] | ||
) | ||
has_pf_consultation = selected_clinical_events.where( | ||
selected_clinical_events.snomedct_code.is_in(pharmacy_first_consultation_codelist) | ||
).exists_for_patient() | ||
|
||
# PF consultations with PF clinical condition | ||
has_pf_condition = selected_events.where( | ||
selected_events.snomedct_code.is_in( | ||
pf_condition_codelist | ||
) | ||
has_pf_condition = selected_clinical_events.where( | ||
selected_clinical_events.snomedct_code.is_in(pharmacy_first_conditions_codelist) | ||
).exists_for_patient() | ||
|
||
# PF consultations with prescribed PF medication | ||
has_pf_medication = selected_events.where( | ||
selected_events.snomedct_code.is_in( | ||
pharmacy_first_med_codes | ||
) | ||
has_pf_medication = selected_med_events.where( | ||
selected_med_events.dmd_code.is_in(pharmacy_first_med_codelist) | ||
).exists_for_patient() | ||
|
||
# Define the denominator as the number of patients registered | ||
denominator = registration.exists_for_patient() & patients.sex.is_in(["male", "female"]) & has_pf_consultation | ||
measures.define_defaults( | ||
denominator = denominator) | ||
denominator = ( | ||
registration.exists_for_patient() | ||
& patients.sex.is_in(["male", "female"]) | ||
& has_pf_consultation | ||
) | ||
measures.define_defaults(denominator=denominator) | ||
|
||
# Measures for PF consultations with PF medication | ||
measures.define_measure( | ||
name="count_pfmed_status", | ||
name="pf_with_pfmed", | ||
numerator=has_pf_medication, | ||
intervals=months(monthly_intervals).starting_on(start_date), | ||
) | ||
# Measures for PF consultations with PF condition | ||
measures.define_measure( | ||
name="count_pfcondition_status", | ||
name="pf_with_pfcondition", | ||
numerator=has_pf_condition, | ||
intervals=months(monthly_intervals).starting_on(start_date), | ||
) | ||
|
||
# Measures for PF consultations with both PF medication and condition | ||
measures.define_measure( | ||
name="count_pfmed_and_pfcondition_status", | ||
name="pf_with_pfmed_and_pfcondition", | ||
numerator=has_pf_condition & has_pf_medication, | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Function which formats the unformatted conditions codelist, and retrieves its codes | ||
# Unused in descriptive_stats.py, but will be used in later tickets (streamline breakdown.py) | ||
def get_pf_condition_codes(pharmacy_first_conditions_codelist): | ||
pharmacy_first_conditions_codes = {} | ||
for codes, term in pharmacy_first_conditions_codelist.items(): | ||
normalised_term = term.lower().replace(" ", "_") | ||
codes = [codes] | ||
pharmacy_first_conditions_codes[normalised_term] = codes | ||
|
||
return pharmacy_first_conditions_codes |
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
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,12 +1,12 @@ | ||
patient_id,consultation_id,date,snomedct_code | ||
1,1,2023-12-01,1577041000000109 | ||
1,1,2023-12-01,15805002 | ||
2,2,2023-12-01,1577041000000109 | ||
2,2,2023-12-01,100 | ||
3,3,2023-12-01,1577041000000109 | ||
3,3,2023-12-01,15805002 | ||
4,4,2023-12-01,1577041000000109 | ||
4,4,2023-12-01,15805002 | ||
4,4,2023-12-01,39692111000001101 | ||
5,5,2023-12-01,100000 | ||
5,5,2023-12-01,15805002 | ||
1,1,2024-03-01,1577041000000109 | ||
1,1,2024-03-01,15805002 | ||
2,2,2024-03-01,1577041000000109 | ||
2,2,2024-03-01,100 | ||
3,3,2024-03-01,1577041000000109 | ||
3,3,2024-03-01,15805002 | ||
4,4,2024-03-01,1577041000000109 | ||
4,4,2024-03-01,15805002 | ||
4,4,2024-03-01,39692111000001101 | ||
5,5,2024-03-01,100000 | ||
5,5,2024-03-01,15805002 |
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
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