Skip to content

Commit

Permalink
Clean code and test with stepwise process
Browse files Browse the repository at this point in the history
  • Loading branch information
viv3ckj committed Nov 4, 2024
1 parent a9c5b1a commit ccea2a3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 195 deletions.
54 changes: 13 additions & 41 deletions analysis/pf_dataset.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,7 @@
from ehrql.tables.tpp import patients, case, when, clinical_events
from ehrql.tables.tpp import patients, case, when

from pf_variables_library import check_pregnancy_status, count_past_events

# Create dictionary of pharmacy first codes
pharmacy_first_event_codes = {
# # Community Pharmacy (CP) Blood Pressure (BP) Check Service (procedure)
# "blood_pressure_service": ["1659111000000107"],
# # Community Pharmacy (CP) Contraception Service (procedure)
# "contraception_service": ["1659121000000101"],
# Community Pharmacist (CP) Consultation Service for minor illness (procedure)
"consultation_service": ["1577041000000109"],
# Pharmacy First service (qualifier value)
"pharmacy_first_service": ["983341000000102"],
"combined_pf_service": ["1577041000000109", "983341000000102"],
}


def get_pf_consultation_ids_from_events(events, codelist):
pharmacy_first_ids = events.where(
events.snomedct_code.is_in(codelist)
).consultation_id
return pharmacy_first_ids


def get_pf_clinical_events(pharmacy_first_ids):
selected_events = clinical_events.where(
clinical_events.consultation_id.is_in(pharmacy_first_ids)
)
return selected_events


# Create denominator variables for each clinical condition
# These are based on NHS England rules using sex, age, pregnancy status and repeated diagnoses
# NOTE: The following exclusions have not been added:
Expand All @@ -38,12 +10,12 @@ def get_pf_clinical_events(pharmacy_first_ids):
# - chronic sinusitis and immunosuppressed individuals for acute sinusitis
def get_uncomplicated_uti_denominator(index_date, selected_events, pregnancy_codelist):
urt_code = ["1090711000000102"]
count_urt_6m = count_past_events(index_date, clinical_events, urt_code, 6)
count_urt_12m = count_past_events(index_date, clinical_events, urt_code, 12)
count_urt_6m = count_past_events(index_date, selected_events, urt_code, 6)
count_urt_12m = count_past_events(index_date, selected_events, urt_code, 12)

age = patients.age_on(index_date)
pregnancy_status = check_pregnancy_status(
index_date, clinical_events, pregnancy_codelist
index_date, selected_events, pregnancy_codelist
)

return (
Expand All @@ -58,7 +30,7 @@ def get_uncomplicated_uti_denominator(index_date, selected_events, pregnancy_cod
def get_shingles_denominator(index_date, selected_events, pregnancy_codelist):
age = patients.age_on(index_date)
pregnancy_status = check_pregnancy_status(
index_date, clinical_events, pregnancy_codelist
index_date, selected_events, pregnancy_codelist
)

return (age >= 18) & (pregnancy_status == False)
Expand All @@ -67,12 +39,12 @@ def get_shingles_denominator(index_date, selected_events, pregnancy_codelist):
def get_impetigo_denominator(index_date, selected_events, pregnancy_codelist):
impetigo_code = ["48277006"]
count_impetigo_12m = count_past_events(
index_date, clinical_events, impetigo_code, 12
index_date, selected_events, impetigo_code, 12
)

age = patients.age_on(index_date)
pregnancy_status = check_pregnancy_status(
index_date, clinical_events, pregnancy_codelist
index_date, selected_events, pregnancy_codelist
)

return ((age >= 1) & (count_impetigo_12m < 2)) & (
Expand All @@ -93,7 +65,7 @@ def get_infected_insect_bites_denominator(
):
age = patients.age_on(index_date)
pregnancy_status = check_pregnancy_status(
index_date, clinical_events, pregnancy_codelist
index_date, selected_events, pregnancy_codelist
)

return (age >= 1) & (
Expand All @@ -104,7 +76,7 @@ def get_infected_insect_bites_denominator(
def get_acute_sore_throat_denominator(index_date, selected_events, pregnancy_codelist):
age = patients.age_on(index_date)
pregnancy_status = check_pregnancy_status(
index_date, clinical_events, pregnancy_codelist
index_date, selected_events, pregnancy_codelist
)

return (age >= 5) & (
Expand All @@ -115,7 +87,7 @@ def get_acute_sore_throat_denominator(index_date, selected_events, pregnancy_cod
def get_acute_sinusitis_denominator(index_date, selected_events, pregnancy_codelist):
age = patients.age_on(index_date)
pregnancy_status = check_pregnancy_status(
index_date, clinical_events, pregnancy_codelist
index_date, selected_events, pregnancy_codelist
)

return (age >= 12) & (
Expand All @@ -126,15 +98,15 @@ def get_acute_sinusitis_denominator(index_date, selected_events, pregnancy_codel
def get_acute_otitis_media_denominator(index_date, selected_events, pregnancy_codelist):
acute_otitis_code = ["3110003"]
count_acute_otitis_6m = count_past_events(
index_date, clinical_events, acute_otitis_code, 6
index_date, selected_events, acute_otitis_code, 6
)
count_acute_otitis_12m = count_past_events(
index_date, clinical_events, acute_otitis_code, 12
index_date, selected_events, acute_otitis_code, 12
)

age = patients.age_on(index_date)
pregnancy_status = check_pregnancy_status(
index_date, clinical_events, pregnancy_codelist
index_date, selected_events, pregnancy_codelist
)

return (
Expand Down
48 changes: 21 additions & 27 deletions analysis/pf_dataset_definition.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from ehrql import INTERVAL, Measures, months, create_dataset
from ehrql import create_dataset
from ehrql.tables.tpp import (
patients,
clinical_events,
practice_registrations,
ethnicity_from_sus,
)

from pf_variables_library import check_pregnancy_status
from pf_dataset import (
get_uncomplicated_uti_denominator,
get_shingles_denominator,
Expand All @@ -14,53 +13,48 @@
get_acute_sore_throat_denominator,
get_acute_sinusitis_denominator,
get_acute_otitis_media_denominator,
get_pf_clinical_events,
get_pf_consultation_ids_from_events,
get_latest_ethnicity,
check_pregnancy_status_for_debug,
pharmacy_first_event_codes,
)

import codelists

index_date = "2024-10-10"
dataset = create_dataset()

pharmacy_first_ids = get_pf_consultation_ids_from_events(
clinical_events, pharmacy_first_event_codes["combined_pf_service"]
)
selected_events = get_pf_clinical_events(pharmacy_first_ids)

registration = practice_registrations.for_patient_on(index_date)

# Create new columns for each denominator rule
dataset.uti_denominator = get_uncomplicated_uti_denominator(
index_date, selected_events, codelists.pregnancy_codelist
index_date, clinical_events, codelists.pregnancy_codelist
)

dataset.shingles_denominator = get_shingles_denominator(
index_date, selected_events, codelists.pregnancy_codelist
index_date, clinical_events, codelists.pregnancy_codelist
)

dataset.impetigo_denominator = get_impetigo_denominator(
index_date, selected_events, codelists.pregnancy_codelist
index_date, clinical_events, codelists.pregnancy_codelist
)
dataset.insect_bite_denominator = get_infected_insect_bites_denominator(
index_date, selected_events, codelists.pregnancy_codelist

dataset.insectbite_denominator = get_infected_insect_bites_denominator(
index_date, clinical_events, codelists.pregnancy_codelist
)
dataset.sore_throat_denominator = get_acute_sore_throat_denominator(
index_date, selected_events, codelists.pregnancy_codelist

dataset.sorethroat_denominator = get_acute_sore_throat_denominator(
index_date, clinical_events, codelists.pregnancy_codelist
)

dataset.sinusitis_denominator = get_acute_sinusitis_denominator(
index_date, selected_events, codelists.pregnancy_codelist
)
dataset.otitis_media_denominator = get_acute_otitis_media_denominator(
index_date, selected_events, codelists.pregnancy_codelist
index_date, clinical_events, codelists.pregnancy_codelist
)
dataset.latest_ethnicity = get_latest_ethnicity(
index_date, clinical_events, codelists.ethnicity_codelist, ethnicity_from_sus

dataset.otitismedia_denominator = get_acute_otitis_media_denominator(
index_date, clinical_events, codelists.pregnancy_codelist
)
dataset.pregnancy_status_check = check_pregnancy_status_for_debug(

dataset.pregnancy_status = check_pregnancy_status(
index_date, clinical_events, codelists.pregnancy_codelist
)

dataset.define_population(
registration.exists_for_patient() & patients.sex.is_in(["male", "female"])
)
Loading

0 comments on commit ccea2a3

Please sign in to comment.