Skip to content

Commit

Permalink
move to dedicated folder and tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
wjchulme committed Dec 18, 2024
1 parent ec4392c commit d3be8fd
Show file tree
Hide file tree
Showing 8 changed files with 391 additions and 549 deletions.
81 changes: 12 additions & 69 deletions analysis/codelists.py → analysis/PRIMIS/codelists.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,13 @@
# Purpose:
# define codelist objects from codelist files imported by codelist.txt spec

# Import code building blocks from cohort extractor package
# Import code building blocks from ehrql package
from ehrql import codelist_from_csv


## --VARIABLES--
# if the variable uses a codelist then it should be added below
# after updating the codelist.txt configuration and importing the codelist

# Ethnicity

ethnicity_codelist5 = codelist_from_csv(
"codelists/opensafely-ethnicity-snomed-0removed.csv",
column="code",
category_column="Label_6", # it's 6 because there is an additional "6 - Not stated" but this is not represented in SNOMED, instead corresponding to no ethnicity code
)

ethnicity_codelist16 = codelist_from_csv(
"codelists/opensafely-ethnicity-snomed-0removed.csv",
column="code",
category_column="Label_16",
)

#######################################################
# PRIMIS
#######################################################

#Asthma

# Asthma

## Asthma Diagnosis code
ast = codelist_from_csv(
"codelists/primis-covid19-vacc-uptake-ast.csv",
Expand Down Expand Up @@ -65,6 +45,7 @@
)

# CKD

## Chronic kidney disease diagnostic codes
ckd_cov = codelist_from_csv(
"codelists/primis-covid19-vacc-uptake-ckd_cov.csv",
Expand All @@ -89,7 +70,8 @@
column="code",
)

# DB
# Diabetes

## Diabetes diagnosis codes
diab = codelist_from_csv(
"codelists/primis-covid19-vacc-uptake-diab.csv",
Expand Down Expand Up @@ -180,19 +162,21 @@
column="code",
)

# BMI
# Severe Obesity

## BMI
bmi = codelist_from_csv(
"codelists/primis-covid19-vacc-uptake-bmi.csv",
column="code",
)

# All BMI coded terms
## All BMI coded terms
bmi_stage = codelist_from_csv(
"codelists/primis-covid19-vacc-uptake-bmi_stage.csv",
column="code",
)

# Severe Obesity code recorded
## Severe Obesity code recorded
sev_obesity = codelist_from_csv(
"codelists/primis-covid19-vacc-uptake-sev_obesity.csv",
column="code",
Expand All @@ -203,44 +187,3 @@
"codelists/primis-covid19-vacc-uptake-learndis.csv",
column="code",
)


# Cancer

cancer_haem_snomed=codelist_from_csv(
"codelists/opensafely-haematological-cancer-snomed.csv",
column="id",
)

cancer_nonhaem_nonlung_snomed=codelist_from_csv(
"codelists/opensafely-cancer-excluding-lung-and-haematological-snomed.csv",
column="id",
)

cancer_lung_snomed=codelist_from_csv(
"codelists/opensafely-lung-cancer-snomed.csv",
column="id",
)

chemotherapy_radiotherapy_snomed = codelist_from_csv(
"codelists/opensafely-chemotherapy-or-radiotherapy-snomed.csv",
column = "id"
)

cancer_nonhaem_snomed = (
cancer_nonhaem_nonlung_snomed +
cancer_lung_snomed +
chemotherapy_radiotherapy_snomed
)

# solid organ transplant
solid_organ_transplant=codelist_from_csv(
"codelists/opensafely-solid-organ-transplantation-snomed.csv",
column="id",
)

# HIV/AIDS
hiv_aids=codelist_from_csv(
"codelists/nhsd-hiv-aids-snomed.csv",
column="code",
)
48 changes: 48 additions & 0 deletions analysis/PRIMIS/dataset_definition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from ehrql import create_dataset
from ehrql.tables.tpp import patients, practice_registrations

# import variable definitions
from variables_function import *

#Import codelists
from codelists import *

# initialise dataset
dataset = create_dataset()

# Choose an index date
index_date = "2020-12-08"

#Dummy data
dataset.configure_dummy_data(population_size=1000)

# define dataset population
dataset.define_population(
practice_registrations.for_patient_on(index_date).exists_for_patient() &
((patients.date_of_death> index_date) | patients.date_of_death.is_null())
)

# Example 1: Add specific PRIMIS variables

dataset.immunosuppressed = is_immunosuppressed(index_date) #immunosuppress grouped
dataset.ckd = has_ckd(index_date) #chronic kidney disease
dataset.crd = has_crd(index_date) # chronis respratory disease
dataset.diabetes = has_diabetes(index_date) #diabetes
dataset.cld = has_prior_event(cld, index_date) # chronic liver disease
dataset.chd = has_prior_event(chd_cov, index_date) #chronic heart disease
dataset.cns = has_prior_event(cns_cov, index_date) # chronic neurological disease
dataset.asplenia = has_prior_event(spln_cov, index_date) # asplenia or dysfunction of the Spleen
dataset.learndis = has_prior_event(learndis, index_date) # learning Disability
dataset.smi = has_smi(index_date) #severe mental illness
dataset.severe_obesity = has_severe_obesity(index_date) #immunosuppress grouped

# Example 2: add the single PRIMIS "at risk" variable

dataset.primis_atrisk = primis_atrisk(index_date) # at risk (at least one of the conditions above)

# EXAMPLE 3: alternatively, use the `primis_variables` function to add variables programmatically:

for i in range(0, 2):
suffix = f"_{i}"
primis_variables(dataset = dataset, index_date = index_date+years(i), var_name_suffix = suffix)

Loading

0 comments on commit d3be8fd

Please sign in to comment.