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

Counts by Pharmacy First Service #3

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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
14 changes: 0 additions & 14 deletions analysis/dataset_definition.py

This file was deleted.

69 changes: 69 additions & 0 deletions analysis/report_measures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from ehrql import INTERVAL, create_measures, months, codelist_from_csv
from ehrql.tables.tpp import clinical_events, patients, practice_registrations

measures = create_measures()

# 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"],
}

registration = practice_registrations.for_patient_on(INTERVAL.end_date)

# 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)
)

# Loop through each condition to create a measure
for condition_name, codelist in pharmacy_first_event_codes.items():
viv3ckj marked this conversation as resolved.
Show resolved Hide resolved
condition_events = selected_events.where(
clinical_events.snomedct_code.is_in(codelist)
)

# Define the numerator as the count of events for the condition
numerator = condition_events.count_for_patient()


# Define the denominator as the number of patients registered
denominator = registration.exists_for_patient()

# Define the measure
measures.define_measure(
name=f"count_{condition_name}",
numerator=numerator,
denominator=denominator,
intervals=months(8).starting_on("2023-11-01")
)

measures.configure_dummy_data(population_size=1000)
viv3ckj marked this conversation as resolved.
Show resolved Hide resolved

# # Count pharmacy first codes
# pharmacy_first_code_counts = {}

# for code_desc, code in pharmacy_first_event_codes.items():
# count_codes_query = selected_events.where(
# selected_events.snomedct_code.is_in(code)
# ).count_for_patient()
# pharmacy_first_code_counts[f"count_{code_desc}"] = count_codes_query


# for measures_name, code_counts in pharmacy_first_code_counts.items():
# measures.define_measure(
# name=measures_name,
# numerator=code_counts,
# group_by={
# "practice_region": registration.practice_nuts1_region_name
# },
# denominator=patients.exists_for_patient(),
# intervals=intervals,
# )


1 change: 0 additions & 1 deletion analysis/stata.do

This file was deleted.

31 changes: 31 additions & 0 deletions lib/functions/function_plot_measures.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
plot_measures <- function(
viv3ckj marked this conversation as resolved.
Show resolved Hide resolved
data,
title = NULL,
x_label = NULL,
y_label = NULL) {

# Create plot
plot <- ggplot(
data,
aes(
x = interval_end,
y = numerator,
color = measure,
group = measure
viv3ckj marked this conversation as resolved.
Show resolved Hide resolved
)
) +
geom_line() +
labs(
title = title,
x = x_label,
y = y_label,
color = "Condition"
) +
# Setting the minimum y-value
ylim(y_min, NA) +
viv3ckj marked this conversation as resolved.
Show resolved Hide resolved
# Applying the minimal theme
theme_minimal()
viv3ckj marked this conversation as resolved.
Show resolved Hide resolved

# Return plot
plot
}
23 changes: 23 additions & 0 deletions lib/functions/sketch.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# In this file we're trying out some code
library(tidyverse)
library(ggplot2)
library(gridExtra) # For arranging multiple plots

# Load data
df_measures <- readr::read_csv(
here::here("output", "report", "conditions_measures.csv")
)

# Develop plotting function
p <- ggplot(df_measures, aes(x = interval_end, y = numerator, color = measure, group = measure)) +
geom_line() +
labs(
title = paste("Number of Consultations for each Pharmacy First Clinical Condition per month"),
x = "Date",
y = "Number of Consultations",
color = "Condition"
) +
ylim(0, NA)

source(here::here("lib", "functions", "graph_func.R"))
viv3ckj marked this conversation as resolved.
Show resolved Hide resolved
plot1_conditions <- plot_pharmacy_first_conditions(df_measures)
10 changes: 6 additions & 4 deletions project.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ expectations:
population_size: 1000

actions:
generate_dataset:
run: ehrql:v1 generate-dataset analysis/dataset_definition.py --output output/dataset.csv.gz
generate_pf_measures:
viv3ckj marked this conversation as resolved.
Show resolved Hide resolved
run: >
ehrql:v1 generate-measures analysis/report_measures.py
--output output/report/conditions_measures.csv
outputs:
highly_sensitive:
dataset: output/dataset.csv.gz
moderately_sensitive:
measure: output/report/conditions_measures.csv