Skip to content
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

Viv3ckj/statistics #66

Merged
merged 5 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions analysis/measures_definition_pf_breakdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
when(age.is_null()).then("Missing"),
)


# IMD groupings for IMD breakdown
imd = addresses.for_patient_on(INTERVAL.start_date).imd_rounded
max_imd = 32844
Expand Down Expand Up @@ -68,7 +67,6 @@
clinical_events.date.is_on_or_between(INTERVAL.start_date, INTERVAL.end_date)
).where(clinical_events.consultation_id.is_in(pharmacy_first_ids))


# Breakdown metrics to be produced as graphs
breakdown_metrics = {
"age_band": age_band,
Expand Down Expand Up @@ -150,4 +148,4 @@
denominator=pf_condition_denominators[condition_name],
group_by={breakdown: variable},
intervals=months(monthly_intervals).starting_on(start_date),
)
)
66 changes: 66 additions & 0 deletions analysis/measures_definition_pf_descriptive_stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from ehrql import INTERVAL, create_measures, months
from ehrql.tables.tpp import (
practice_registrations,
patients,
)

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

measures = create_measures()
measures.configure_dummy_data(population_size=1000)

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]

# 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"]
)
).exists_for_patient()

# PF consultations with PF clinical condition
has_pf_condition = selected_events.where(
selected_events.snomedct_code.is_in(
pf_condition_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
)
).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)

# Measures for PF consultations with PF medication
measures.define_measure(
name="count_pfmed_status",
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",
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",
numerator=has_pf_condition & has_pf_medication,
intervals=months(monthly_intervals).starting_on(start_date),
)
35 changes: 7 additions & 28 deletions dummy_tables/clinical_events.csv
Original file line number Diff line number Diff line change
@@ -1,33 +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
5,5,2023-12-01,1577041000000109
6,6,2023-12-01,1577041000000109
7,7,2023-12-01,1577041000000109
8,8,2023-12-01,1577041000000109
9,9,2023-12-01,1577041000000109
10,10,2023-12-01,1577041000000109
11,11,2023-12-01,1577041000000109
12,12,2023-12-01,1577041000000109
13,13,2023-12-01,1577041000000109
14,14,2023-12-01,1577041000000109
15,15,2023-12-01,1577041000000109
16,16,2023-12-01,1577041000000109
17,17,2023-12-01,1577041000000109
18,18,2023-12-01,1577041000000109
19,19,2023-12-01,1577041000000109
20,20,2023-12-01,1577041000000109
21,21,2023-12-01,1577041000000109
22,22,2023-12-01,1577041000000109
23,23,2023-12-01,1577041000000109
24,24,2023-12-01,1577041000000109
25,25,2023-12-01,1577041000000109
26,26,2023-12-01,1577041000000109
27,27,2023-12-01,1577041000000109
28,28,2023-12-01,1577041000000109
29,29,2023-12-01,1577041000000109
30,30,2023-12-01,1577041000000109
31,31,2023-12-01,1577041000000109
32,32,2023-12-01,1000000000000000
4,4,2023-12-01,15805002
4,4,2023-12-01,39692111000001101
5,5,2023-12-01,100000
5,5,2023-12-01,15805002
9 changes: 9 additions & 0 deletions project.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ actions:
moderately_sensitive:
measure: output/measures/pf_medications_measures.csv

generate_pf_statistics_measures:
run: >
ehrql:v1 generate-measures analysis/measures_definition_pf_descriptive_stats.py
--dummy-tables dummy_tables
--output output/measures/pf_descriptive_stats_measures.csv
outputs:
moderately_sensitive:
measure: output/measures/pf_descriptive_stats_measures.csv

generate_pf_dataset_definition:
run: >
ehrql:v1
Expand Down