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

Reworking code, file names, and folder structure #7

Merged
merged 4 commits into from
Sep 25, 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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ venv/
.DS_Store
.Rhistory
.Rproj.user/
reports/pf_report.html
reports/pharmacy_first_report.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from ehrql import INTERVAL, create_measures, months, codelist_from_csv
from ehrql.tables.tpp import clinical_events, patients, practice_registrations
from ehrql.tables.tpp import clinical_events, practice_registrations

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

# Dictionary of pharmacy first codes
start_date = "2023-11-01"
monthly_intervals = 8

# Create dictionary of pharmacy first codes
pharmacy_first_event_codes = {
# Community Pharmacy (CP) Blood Pressure (BP) Check Service (procedure)
"blood_pressure_service": ["1659111000000107"],
Expand All @@ -16,27 +19,21 @@
"pharmacy_first_service": ["983341000000102"],
}

# Import the codelist from CSV
pharmacy_first_codelist = codelist_from_csv(
# Import pharmacy first conditions codelist
pharmacy_first_conditions_codelist = codelist_from_csv(
"codelists/user-chriswood-pharmacy-first-clinical-pathway-conditions.csv",
column="code", category_column = "term"
column="code",
category_column="term",
)

pharmacy_first_conditions_codes = {}
# Iterate through codelist, forming a dictionary
for codes, term in pharmacy_first_codelist.items():
normalised_term = term.lower().replace(" ", "_")
codes = [codes]
pharmacy_first_conditions_codes[normalised_term] = codes

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 CLINICAL SERVICE to create a measure
# Create measures for pharmacy first services
for pharmacy_first_event, codelist in pharmacy_first_event_codes.items():
condition_events = selected_events.where(
clinical_events.snomedct_code.is_in(codelist)
Expand All @@ -52,10 +49,16 @@
name=f"count_{pharmacy_first_event}",
numerator=numerator,
denominator=denominator,
intervals=months(8).starting_on("2023-11-01")
intervals=months(monthly_intervals).starting_on(start_date),
)

# Loop through each CLINICAL CONDITION to create a measure
# Create measures for pharmacy first conditions
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

for condition_name, condition_code in pharmacy_first_conditions_codes.items():
condition_events = selected_events.where(
clinical_events.snomedct_code.is_in(condition_code)
Expand All @@ -71,5 +74,5 @@
name=f"count_{condition_name}",
numerator=numerator,
denominator=denominator,
intervals=months(8).starting_on("2023-11-01")
)
intervals=months(monthly_intervals).starting_on(start_date),
)
40 changes: 20 additions & 20 deletions lib/functions/function_plot_measures.R
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
#' Plot Measures Over Time
#'
#'
#' Creates a line plot of measures over time, with customisable labels and colours.
#'
#'
#' @param data A dataframe containing the data to plot.
#' @param measure_names Strings specifiying the names of measure columns to be plotted.
#' @param custom_labels Strings specifying the names of legend labels.
#' @param title A string specifying the title of the plot. Default is NULL.
#' @param custom_labels Strings specifying the names of legend labels.
#' @param title A string specifying the title of the plot. Default is NULL.
#' @param x_label A string specifying the label for the x-axis. Default is NULL.
#' @param y_label A string specifying the label for the y-axis. Default is NULL.
#' @param y_label A string specifying the label for the y-axis. Default is NULL.
#' @param color_label A string specifying the label for the color legend. Default is NULL.
#' @param value_col The name of the dataframe column which contains the y-axis values. Default is "numerator".
#' @param measure_col The name of the dataframe column which contains the categorical variable. Default is "measure".
#'
#'
#'
#' @return A ggplot object.

# Define the function
plot_measures <- function(
data,
measure_names,
Expand All @@ -28,7 +26,6 @@ plot_measures <- function(
y_label = NULL,
color_label = NULL,
axis_x_text_size = 7) {

# Check if the necessary columns exist in the data
if (date_col %in% names(data) == FALSE) {
stop("Data does not have a column with the name '", date_col, "'")
Expand Down Expand Up @@ -80,24 +77,27 @@ plot_measures <- function(
limits = c(0, NA),
) +
theme_minimal() +
theme(axis.text.x = element_text(size = axis_x_text_size),
legend.position="bottom",
legend.key.size = unit(0.5, "cm"),
legend.text = element_text(size = 8),
legend.title = element_text(size = 8)) +
theme(
axis.text.x = element_text(size = axis_x_text_size),
legend.position = "bottom",
legend.key.size = unit(0.5, "cm"),
legend.text = element_text(size = 8),
legend.title = element_text(size = 8)
) +
# Adjust number of rows in the legend
guides(
color = guide_legend(nrow = 2) # Adjust number of rows in the legend
) +
color = guide_legend(nrow = 2)
) +
geom_vline(
xintercept = lubridate::as_date("2024-02-01"),
linetype = "dotted",
colour = "orange",
linewidth = .7) +
linewidth = .7
) +
scale_x_date(
date_breaks = "1 month",
date_breaks = "1 month",
date_labels = "%b %Y"
)


plot1
}
}
6 changes: 3 additions & 3 deletions project.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ expectations:
actions:
generate_pf_measures:
run: >
ehrql:v1 generate-measures analysis/report_measures.py
--output output/report/conditions_measures.csv
ehrql:v1 generate-measures analysis/measures_definition_pf_codes_conditions.py
--output output/measures/pf_codes_conditions_measures.csv
outputs:
moderately_sensitive:
measure: output/report/conditions_measures.csv
measure: output/measures/pf_codes_conditions_measures.csv
206 changes: 0 additions & 206 deletions reports/pf_report.Rmd

This file was deleted.

Loading