From 2b2acd9212aa8c478064b567029b0ef80052c850 Mon Sep 17 00:00:00 2001 From: milanwiedemann Date: Thu, 14 Sep 2023 16:23:13 +0100 Subject: [PATCH 1/2] Add ehrql dataset definition and action This is not a direct translation of the cohort-extractor example in study_definition.py. I thought it would be useful to show how to add a column to the dataset using an ehrql method (age_on) and show how to use two conditions to define the pupulation using &. --- analysis/dataset_definition.py | 14 ++++++++++++++ project.yaml | 6 ++++++ 2 files changed, 20 insertions(+) create mode 100644 analysis/dataset_definition.py diff --git a/analysis/dataset_definition.py b/analysis/dataset_definition.py new file mode 100644 index 0000000..8ca2273 --- /dev/null +++ b/analysis/dataset_definition.py @@ -0,0 +1,14 @@ +from ehrql import Dataset +from ehrql.tables.beta.tpp import patients, practice_registrations + +dataset = Dataset() + +index_date = "2020-03-31" + +has_registration = practice_registrations.for_patient_on( + index_date +).exists_for_patient() + +dataset.age = patients.age_on(index_date) + +dataset.define_population(has_registration & (dataset.age > 17)) diff --git a/project.yaml b/project.yaml index fbdb81b..a369a7d 100644 --- a/project.yaml +++ b/project.yaml @@ -10,3 +10,9 @@ actions: outputs: highly_sensitive: cohort: output/input.csv.gz + + # generate_dataset: + # run: ehrql:v0 generate-dataset analysis/dataset_definition.py --output output/dataset.csv.gz + # outputs: + # highly_sensitive: + # cohort: output/dataset.csv.gz From 84206c8327ca4f7d0aa9b78de734e9a924e73b51 Mon Sep 17 00:00:00 2001 From: milanwiedemann Date: Thu, 14 Sep 2023 16:33:59 +0100 Subject: [PATCH 2/2] Add instructions for cohortextractor / ehrql users This adds instruction to the study_population.py and dataset_definition.py files for cohort-extractor vs ehrql users. --- analysis/dataset_definition.py | 5 +++++ analysis/study_definition.py | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/analysis/dataset_definition.py b/analysis/dataset_definition.py index 8ca2273..6202e23 100644 --- a/analysis/dataset_definition.py +++ b/analysis/dataset_definition.py @@ -1,3 +1,8 @@ +# Note: If you are using ehrQL to define your study population you need to: +# (1) uncomment the ehrQL action in the project.yaml file (lines 14-18), +# (2) delete the cohort-extractor action from the project.yaml file (lines 8-12), and +# (3) delete the study_definition.py file. + from ehrql import Dataset from ehrql.tables.beta.tpp import patients, practice_registrations diff --git a/analysis/study_definition.py b/analysis/study_definition.py index 3b111d1..c9bb2d9 100644 --- a/analysis/study_definition.py +++ b/analysis/study_definition.py @@ -1,3 +1,7 @@ +# Note: If you are using cohortextrator to define your study population you need to +# (1) delete the ehrQL action in the project.yaml file (lines 14-18) and +# (2) delete the dataset_definition.py file. + from cohortextractor import StudyDefinition, patients, codelist, codelist_from_csv # NOQA