From 8d2d849cff6eaf8e97c57251b1b80c921281270f Mon Sep 17 00:00:00 2001 From: johntaz Date: Thu, 26 Nov 2020 16:34:02 +0000 Subject: [PATCH 01/21] "as-of" dates workaround --- analysis/common_variables.py | 691 +++++++++++++++++++++- analysis/study_definition_control_2019.py | 9 + analysis/study_definition_control_2020.py | 9 + analysis/study_definition_covid.py | 9 + analysis/study_definition_pneumonia.py | 9 + 5 files changed, 706 insertions(+), 21 deletions(-) diff --git a/analysis/common_variables.py b/analysis/common_variables.py index edc36ed..dbf6ff0 100644 --- a/analysis/common_variables.py +++ b/analysis/common_variables.py @@ -11,7 +11,7 @@ def days_before(s, days): def common_variable_define(start_date): common_variables = dict( - dvt_gp=patients.with_these_clinical_events( + dvt_gp_feb=patients.with_these_clinical_events( filter_codes_by_category(vte_codes_gp, include=["dvt"]), return_first_date_in_period=True, date_format="YYYY-MM-DD", @@ -20,7 +20,79 @@ def common_variable_define(start_date): "date": {"earliest": start_date}, }, ), - pe_gp=patients.with_these_clinical_events( + dvt_gp_mar=patients.with_these_clinical_events( + filter_codes_by_category(vte_codes_gp, include=["dvt"]), + return_first_date_in_period=True, + date_format="YYYY-MM-DD", + on_or_after=start_mar, + return_expectations={ + "date": {"earliest": start_mar}, + }, + ), + dvt_gp_apr=patients.with_these_clinical_events( + filter_codes_by_category(vte_codes_gp, include=["dvt"]), + return_first_date_in_period=True, + date_format="YYYY-MM-DD", + on_or_after=start_apr, + return_expectations={ + "date": {"earliest": start_apr}, + }, + ), + dvt_gp_may=patients.with_these_clinical_events( + filter_codes_by_category(vte_codes_gp, include=["dvt"]), + return_first_date_in_period=True, + date_format="YYYY-MM-DD", + on_or_after=start_may, + return_expectations={ + "date": {"earliest": start_may}, + }, + ), + dvt_gp_jun=patients.with_these_clinical_events( + filter_codes_by_category(vte_codes_gp, include=["dvt"]), + return_first_date_in_period=True, + date_format="YYYY-MM-DD", + on_or_after=start_jun, + return_expectations={ + "date": {"earliest": start_jun}, + }, + ), + dvt_gp_jul=patients.with_these_clinical_events( + filter_codes_by_category(vte_codes_gp, include=["dvt"]), + return_first_date_in_period=True, + date_format="YYYY-MM-DD", + on_or_after=start_jul, + return_expectations={ + "date": {"earliest": start_jul}, + }, + ), + dvt_gp_aug=patients.with_these_clinical_events( + filter_codes_by_category(vte_codes_gp, include=["dvt"]), + return_first_date_in_period=True, + date_format="YYYY-MM-DD", + on_or_after=start_aug, + return_expectations={ + "date": {"earliest": start_aug}, + }, + ), + dvt_gp_sep=patients.with_these_clinical_events( + filter_codes_by_category(vte_codes_gp, include=["dvt"]), + return_first_date_in_period=True, + date_format="YYYY-MM-DD", + on_or_after=start_sep, + return_expectations={ + "date": {"earliest": start_sep}, + }, + ), + dvt_gp_oct=patients.with_these_clinical_events( + filter_codes_by_category(vte_codes_gp, include=["dvt"]), + return_first_date_in_period=True, + date_format="YYYY-MM-DD", + on_or_after=start_oct, + return_expectations={ + "date": {"earliest": start_oct}, + }, + ), + pe_gp_feb=patients.with_these_clinical_events( filter_codes_by_category(vte_codes_gp, include=["pe"]), return_first_date_in_period=True, date_format="YYYY-MM-DD", @@ -29,13 +101,76 @@ def common_variable_define(start_date): "date": {"earliest": start_date}, }, ), - other_vte_gp=patients.with_these_clinical_events( - filter_codes_by_category(vte_codes_gp, include=["other"]), + pe_gp_mar=patients.with_these_clinical_events( + filter_codes_by_category(vte_codes_gp, include=["pe"]), return_first_date_in_period=True, date_format="YYYY-MM-DD", - on_or_after=start_date, + on_or_after=start_mar, return_expectations={ - "date": {"earliest": start_date}, + "date": {"earliest": start_mar}, + }, + ), + pe_gp_apr=patients.with_these_clinical_events( + filter_codes_by_category(vte_codes_gp, include=["pe"]), + return_first_date_in_period=True, + date_format="YYYY-MM-DD", + on_or_after=start_apr, + return_expectations={ + "date": {"earliest": start_apr}, + }, + ), + pe_gp_may=patients.with_these_clinical_events( + filter_codes_by_category(vte_codes_gp, include=["pe"]), + return_first_date_in_period=True, + date_format="YYYY-MM-DD", + on_or_after=start_may, + return_expectations={ + "date": {"earliest": start_may}, + }, + ), + pe_gp_jun=patients.with_these_clinical_events( + filter_codes_by_category(vte_codes_gp, include=["pe"]), + return_first_date_in_period=True, + date_format="YYYY-MM-DD", + on_or_after=start_jun, + return_expectations={ + "date": {"earliest": start_jun}, + }, + ), + pe_gp_jul=patients.with_these_clinical_events( + filter_codes_by_category(vte_codes_gp, include=["pe"]), + return_first_date_in_period=True, + date_format="YYYY-MM-DD", + on_or_after=start_jul, + return_expectations={ + "date": {"earliest": start_jul}, + }, + ), + pe_gp_aug=patients.with_these_clinical_events( + filter_codes_by_category(vte_codes_gp, include=["pe"]), + return_first_date_in_period=True, + date_format="YYYY-MM-DD", + on_or_after=start_aug, + return_expectations={ + "date": {"earliest": start_aug}, + }, + ), + pe_gp_sep=patients.with_these_clinical_events( + filter_codes_by_category(vte_codes_gp, include=["pe"]), + return_first_date_in_period=True, + date_format="YYYY-MM-DD", + on_or_after=start_sep, + return_expectations={ + "date": {"earliest": start_sep}, + }, + ), + pe_gp_oct=patients.with_these_clinical_events( + filter_codes_by_category(vte_codes_gp, include=["pe"]), + return_first_date_in_period=True, + date_format="YYYY-MM-DD", + on_or_after=start_oct, + return_expectations={ + "date": {"earliest": start_oct}, }, ), previous_vte_gp=patients.with_these_clinical_events( @@ -87,7 +222,7 @@ def common_variable_define(start_date): "incidence": 0.05, }, ), - dvt_hospital=patients.admitted_to_hospital( + dvt_hospital_feb=patients.admitted_to_hospital( returning="date_admitted", with_these_diagnoses=filter_codes_by_category( vte_codes_hospital, include=["dvt"] @@ -99,7 +234,103 @@ def common_variable_define(start_date): "date": {"earliest": start_date}, }, ), - pe_hospital=patients.admitted_to_hospital( + dvt_hospital_mar=patients.admitted_to_hospital( + returning="date_admitted", + with_these_diagnoses=filter_codes_by_category( + vte_codes_hospital, include=["dvt"] + ), # optional + on_or_after=start_mar, + date_format="YYYY-MM-DD", + find_first_match_in_period=True, + return_expectations={ + "date": {"earliest": start_mar}, + }, + ), + dvt_hospital_apr=patients.admitted_to_hospital( + returning="date_admitted", + with_these_diagnoses=filter_codes_by_category( + vte_codes_hospital, include=["dvt"] + ), # optional + on_or_after=start_apr, + date_format="YYYY-MM-DD", + find_first_match_in_period=True, + return_expectations={ + "date": {"earliest": start_apr}, + }, + ), + dvt_hospital_may=patients.admitted_to_hospital( + returning="date_admitted", + with_these_diagnoses=filter_codes_by_category( + vte_codes_hospital, include=["dvt"] + ), # optional + on_or_after=start_may, + date_format="YYYY-MM-DD", + find_first_match_in_period=True, + return_expectations={ + "date": {"earliest": start_may}, + }, + ), + dvt_hospital_jun=patients.admitted_to_hospital( + returning="date_admitted", + with_these_diagnoses=filter_codes_by_category( + vte_codes_hospital, include=["dvt"] + ), # optional + on_or_after=start_jun, + date_format="YYYY-MM-DD", + find_first_match_in_period=True, + return_expectations={ + "date": {"earliest": start_jun}, + }, + ), + dvt_hospital_jul=patients.admitted_to_hospital( + returning="date_admitted", + with_these_diagnoses=filter_codes_by_category( + vte_codes_hospital, include=["dvt"] + ), # optional + on_or_after=start_jul, + date_format="YYYY-MM-DD", + find_first_match_in_period=True, + return_expectations={ + "date": {"earliest": start_jul}, + }, + ), + dvt_hospital_aug=patients.admitted_to_hospital( + returning="date_admitted", + with_these_diagnoses=filter_codes_by_category( + vte_codes_hospital, include=["dvt"] + ), # optional + on_or_after=start_aug, + date_format="YYYY-MM-DD", + find_first_match_in_period=True, + return_expectations={ + "date": {"earliest": start_aug}, + }, + ), + dvt_hospital_sep=patients.admitted_to_hospital( + returning="date_admitted", + with_these_diagnoses=filter_codes_by_category( + vte_codes_hospital, include=["dvt"] + ), # optional + on_or_after=start_sep, + date_format="YYYY-MM-DD", + find_first_match_in_period=True, + return_expectations={ + "date": {"earliest": start_sep}, + }, + ), + dvt_hospital_oct=patients.admitted_to_hospital( + returning="date_admitted", + with_these_diagnoses=filter_codes_by_category( + vte_codes_hospital, include=["dvt"] + ), # optional + on_or_after=start_oct, + date_format="YYYY-MM-DD", + find_first_match_in_period=True, + return_expectations={ + "date": {"earliest": start_oct}, + }, + ), + pe_hospital_feb=patients.admitted_to_hospital( returning="date_admitted", with_these_diagnoses=filter_codes_by_category( vte_codes_hospital, include=["pe"] @@ -111,6 +342,102 @@ def common_variable_define(start_date): "date": {"earliest": start_date}, }, ), + pe_hospital_mar=patients.admitted_to_hospital( + returning="date_admitted", + with_these_diagnoses=filter_codes_by_category( + vte_codes_hospital, include=["pe"] + ), # optional + on_or_after=start_mar, + date_format="YYYY-MM-DD", + find_first_match_in_period=True, + return_expectations={ + "date": {"earliest": start_mar}, + }, + ), + pe_hospital_apr=patients.admitted_to_hospital( + returning="date_admitted", + with_these_diagnoses=filter_codes_by_category( + vte_codes_hospital, include=["pe"] + ), # optional + on_or_after=start_apr, + date_format="YYYY-MM-DD", + find_first_match_in_period=True, + return_expectations={ + "date": {"earliest": start_apr}, + }, + ), + pe_hospital_may=patients.admitted_to_hospital( + returning="date_admitted", + with_these_diagnoses=filter_codes_by_category( + vte_codes_hospital, include=["pe"] + ), # optional + on_or_after=start_may, + date_format="YYYY-MM-DD", + find_first_match_in_period=True, + return_expectations={ + "date": {"earliest": start_may}, + }, + ), + pe_hospital_jun=patients.admitted_to_hospital( + returning="date_admitted", + with_these_diagnoses=filter_codes_by_category( + vte_codes_hospital, include=["pe"] + ), # optional + on_or_after=start_jun, + date_format="YYYY-MM-DD", + find_first_match_in_period=True, + return_expectations={ + "date": {"earliest": start_jun}, + }, + ), + pe_hospital_jul=patients.admitted_to_hospital( + returning="date_admitted", + with_these_diagnoses=filter_codes_by_category( + vte_codes_hospital, include=["pe"] + ), # optional + on_or_after=start_jul, + date_format="YYYY-MM-DD", + find_first_match_in_period=True, + return_expectations={ + "date": {"earliest": start_jul}, + }, + ), + pe_hospital_aug=patients.admitted_to_hospital( + returning="date_admitted", + with_these_diagnoses=filter_codes_by_category( + vte_codes_hospital, include=["pe"] + ), # optional + on_or_after=start_aug, + date_format="YYYY-MM-DD", + find_first_match_in_period=True, + return_expectations={ + "date": {"earliest": start_aug}, + }, + ), + pe_hospital_sep=patients.admitted_to_hospital( + returning="date_admitted", + with_these_diagnoses=filter_codes_by_category( + vte_codes_hospital, include=["pe"] + ), # optional + on_or_after=start_sep, + date_format="YYYY-MM-DD", + find_first_match_in_period=True, + return_expectations={ + "date": {"earliest": start_sep}, + }, + ), + pe_hospital_oct=patients.admitted_to_hospital( + returning="date_admitted", + with_these_diagnoses=filter_codes_by_category( + vte_codes_hospital, include=["pe"] + ), # optional + on_or_after=start_oct, + date_format="YYYY-MM-DD", + find_first_match_in_period=True, + return_expectations={ + "date": {"earliest": start_oct}, + }, + ), other_vte_hospital=patients.admitted_to_hospital( returning="date_admitted", with_these_diagnoses=filter_codes_by_category( @@ -123,20 +450,134 @@ def common_variable_define(start_date): "date": {"earliest": start_date}, }, ), - dvt_ons=patients.with_these_codes_on_death_certificate( + dvt_ons_feb=patients.with_these_codes_on_death_certificate( filter_codes_by_category(vte_codes_hospital, include=["dvt"]), returning="date_of_death", match_only_underlying_cause=False, on_or_after=start_date, return_expectations={"date": {"earliest": start_date}}, ), - pe_ons=patients.with_these_codes_on_death_certificate( + dvt_ons_mar=patients.with_these_codes_on_death_certificate( + filter_codes_by_category(vte_codes_hospital, include=["dvt"]), + returning="date_of_death", + match_only_underlying_cause=False, + on_or_after=start_mar, + return_expectations={"date": {"earliest": start_mar}}, + ), + dvt_ons_apr=patients.with_these_codes_on_death_certificate( + filter_codes_by_category(vte_codes_hospital, include=["dvt"]), + returning="date_of_death", + match_only_underlying_cause=False, + on_or_after=start_apr, + return_expectations={"date": {"earliest": start_apr}}, + ), + dvt_ons_may=patients.with_these_codes_on_death_certificate( + filter_codes_by_category(vte_codes_hospital, include=["dvt"]), + returning="date_of_death", + match_only_underlying_cause=False, + on_or_after=start_may, + return_expectations={"date": {"earliest": start_may}}, + ), + dvt_ons_jun=patients.with_these_codes_on_death_certificate( + filter_codes_by_category(vte_codes_hospital, include=["dvt"]), + returning="date_of_death", + match_only_underlying_cause=False, + on_or_after=start_jun, + return_expectations={"date": {"earliest": start_jun}}, + ), + dvt_ons_jul=patients.with_these_codes_on_death_certificate( + filter_codes_by_category(vte_codes_hospital, include=["dvt"]), + returning="date_of_death", + match_only_underlying_cause=False, + on_or_after=start_jul, + return_expectations={"date": {"earliest": start_jul}}, + ), + dvt_ons_aug=patients.with_these_codes_on_death_certificate( + filter_codes_by_category(vte_codes_hospital, include=["dvt"]), + returning="date_of_death", + match_only_underlying_cause=False, + on_or_after=start_aug, + return_expectations={"date": {"earliest": start_aug}}, + ), + dvt_ons_sep=patients.with_these_codes_on_death_certificate( + filter_codes_by_category(vte_codes_hospital, include=["dvt"]), + returning="date_of_death", + match_only_underlying_cause=False, + on_or_after=start_sep, + return_expectations={"date": {"earliest": start_sep}}, + ), + dvt_ons_oct=patients.with_these_codes_on_death_certificate( + filter_codes_by_category(vte_codes_hospital, include=["dvt"]), + returning="date_of_death", + match_only_underlying_cause=False, + on_or_after=start_oct, + return_expectations={"date": {"earliest": start_oct}}, + ), + + pe_ons_feb=patients.with_these_codes_on_death_certificate( filter_codes_by_category(vte_codes_hospital, include=["pe"]), returning="date_of_death", match_only_underlying_cause=False, - on_or_after=start_date, - return_expectations={"date": {"earliest": start_date}}, + on_or_after=start_feb, + return_expectations={"date": {"earliest": start_feb}}, ), + pe_ons_mar=patients.with_these_codes_on_death_certificate( + filter_codes_by_category(vte_codes_hospital, include=["pe"]), + returning="date_of_death", + match_only_underlying_cause=False, + on_or_after=start_mar, + return_expectations={"date": {"earliest": start_mar}}, + ), + pe_ons_apr=patients.with_these_codes_on_death_certificate( + filter_codes_by_category(vte_codes_hospital, include=["pe"]), + returning="date_of_death", + match_only_underlying_cause=False, + on_or_after=start_apr, + return_expectations={"date": {"earliest": start_apr}}, + ), + pe_ons_may=patients.with_these_codes_on_death_certificate( + filter_codes_by_category(vte_codes_hospital, include=["pe"]), + returning="date_of_death", + match_only_underlying_cause=False, + on_or_after=start_may, + return_expectations={"date": {"earliest": start_may}}, + ), + pe_ons_jun=patients.with_these_codes_on_death_certificate( + filter_codes_by_category(vte_codes_hospital, include=["pe"]), + returning="date_of_death", + match_only_underlying_cause=False, + on_or_after=start_jun, + return_expectations={"date": {"earliest": start_jun}}, + ), + pe_ons_jul=patients.with_these_codes_on_death_certificate( + filter_codes_by_category(vte_codes_hospital, include=["pe"]), + returning="date_of_death", + match_only_underlying_cause=False, + on_or_after=start_jul, + return_expectations={"date": {"earliest": start_jul}}, + ), + pe_ons_aug=patients.with_these_codes_on_death_certificate( + filter_codes_by_category(vte_codes_hospital, include=["pe"]), + returning="date_of_death", + match_only_underlying_cause=False, + on_or_after=start_aug, + return_expectations={"date": {"earliest": start_aug}}, + ), + pe_ons_sep=patients.with_these_codes_on_death_certificate( + filter_codes_by_category(vte_codes_hospital, include=["pe"]), + returning="date_of_death", + match_only_underlying_cause=False, + on_or_after=start_sep, + return_expectations={"date": {"earliest": start_sep}}, + ), + pe_ons_oct=patients.with_these_codes_on_death_certificate( + filter_codes_by_category(vte_codes_hospital, include=["pe"]), + returning="date_of_death", + match_only_underlying_cause=False, + on_or_after=start_oct, + return_expectations={"date": {"earliest": start_oct}}, + ), + other_vte_ons=patients.with_these_codes_on_death_certificate( filter_codes_by_category(vte_codes_hospital, include=["other"]), returning="date_of_death", @@ -153,32 +594,240 @@ def common_variable_define(start_date): "incidence": 0.05, }, ), - stroke_gp=patients.with_these_clinical_events( - stroke, - return_first_date_in_period=True, - on_or_after=start_date, + stroke_gp_feb=patients.with_these_clinical_events( + stroke, + return_first_date_in_strokeriod=True, date_format="YYYY-MM-DD", + on_or_after=start_date, return_expectations={ "date": {"earliest": start_date}, }, ), - stroke_hospital=patients.admitted_to_hospital( + stroke_gp_mar=patients.with_these_clinical_events( + stroke, + return_first_date_in_strokeriod=True, + date_format="YYYY-MM-DD", + on_or_after=start_mar, + return_expectations={ + "date": {"earliest": start_mar}, + }, + ), + stroke_gp_apr=patients.with_these_clinical_events( + stroke, + return_first_date_in_strokeriod=True, + date_format="YYYY-MM-DD", + on_or_after=start_apr, + return_expectations={ + "date": {"earliest": start_apr}, + }, + ), + stroke_gp_may=patients.with_these_clinical_events( + stroke, + return_first_date_in_strokeriod=True, + date_format="YYYY-MM-DD", + on_or_after=start_may, + return_expectations={ + "date": {"earliest": start_may}, + }, + ), + stroke_gp_jun=patients.with_these_clinical_events( + stroke, + return_first_date_in_strokeriod=True, + date_format="YYYY-MM-DD", + on_or_after=start_jun, + return_expectations={ + "date": {"earliest": start_jun}, + }, + ), + stroke_gp_jul=patients.with_these_clinical_events( + stroke, + return_first_date_in_strokeriod=True, + date_format="YYYY-MM-DD", + on_or_after=start_jul, + return_expectations={ + "date": {"earliest": start_jul}, + }, + ), + stroke_gp_aug=patients.with_these_clinical_events( + stroke, + return_first_date_in_strokeriod=True, + date_format="YYYY-MM-DD", + on_or_after=start_aug, + return_expectations={ + "date": {"earliest": start_aug}, + }, + ), + stroke_gp_sep=patients.with_these_clinical_events( + stroke, + return_first_date_in_strokeriod=True, + date_format="YYYY-MM-DD", + on_or_after=start_sep, + return_expectations={ + "date": {"earliest": start_sep}, + }, + ), + stroke_gp_oct=patients.with_these_clinical_events( + stroke, + return_first_date_in_strokeriod=True, + date_format="YYYY-MM-DD", + on_or_after=start_oct, + return_expectations={ + "date": {"earliest": start_oct}, + }, + ), + stroke_hospital_feb=patients.admitted_to_hospital( returning="date_admitted", with_these_diagnoses=stroke_hospital, on_or_after=start_date, date_format="YYYY-MM-DD", - find_first_match_in_period=True, + find_first_match_in_strokeriod=True, return_expectations={ "date": {"earliest": start_date}, }, ), - stroke_ons=patients.with_these_codes_on_death_certificate( + stroke_hospital_mar=patients.admitted_to_hospital( + returning="date_admitted", + with_these_diagnoses=stroke_hospital, + on_or_after=start_mar, + date_format="YYYY-MM-DD", + find_first_match_in_strokeriod=True, + return_expectations={ + "date": {"earliest": start_mar}, + }, + ), + stroke_hospital_apr=patients.admitted_to_hospital( + returning="date_admitted", + with_these_diagnoses=stroke_hospital, + on_or_after=start_apr, + date_format="YYYY-MM-DD", + find_first_match_in_strokeriod=True, + return_expectations={ + "date": {"earliest": start_apr}, + }, + ), + stroke_hospital_may=patients.admitted_to_hospital( + returning="date_admitted", + with_these_diagnoses=stroke_hospital, + on_or_after=start_may, + date_format="YYYY-MM-DD", + find_first_match_in_strokeriod=True, + return_expectations={ + "date": {"earliest": start_may}, + }, + ), + stroke_hospital_jun=patients.admitted_to_hospital( + returning="date_admitted", + with_these_diagnoses=stroke_hospital, + on_or_after=start_jun, + date_format="YYYY-MM-DD", + find_first_match_in_strokeriod=True, + return_expectations={ + "date": {"earliest": start_jun}, + }, + ), + stroke_hospital_jul=patients.admitted_to_hospital( + returning="date_admitted", + with_these_diagnoses=stroke_hospital, + on_or_after=start_jul, + date_format="YYYY-MM-DD", + find_first_match_in_strokeriod=True, + return_expectations={ + "date": {"earliest": start_jul}, + }, + ), + stroke_hospital_aug=patients.admitted_to_hospital( + returning="date_admitted", + with_these_diagnoses=stroke_hospital, + on_or_after=start_aug, + date_format="YYYY-MM-DD", + find_first_match_in_strokeriod=True, + return_expectations={ + "date": {"earliest": start_aug}, + }, + ), + stroke_hospital_sep=patients.admitted_to_hospital( + returning="date_admitted", + with_these_diagnoses=stroke_hospital, + on_or_after=start_sep, + date_format="YYYY-MM-DD", + find_first_match_in_strokeriod=True, + return_expectations={ + "date": {"earliest": start_sep}, + }, + ), + stroke_hospital_oct=patients.admitted_to_hospital( + returning="date_admitted", + with_these_diagnoses=stroke_hospital, + on_or_after=start_oct, + date_format="YYYY-MM-DD", + find_first_match_in_strokeriod=True, + return_expectations={ + "date": {"earliest": start_oct}, + }, + ), + stroke_ons_feb=patients.with_these_codes_on_death_certificate( + stroke_hospital, + returning="date_of_death", + match_only_underlying_cause=False, + on_or_after=start_feb, + return_expectations={"date": {"earliest": start_feb}}, + ), + stroke_ons_mar=patients.with_these_codes_on_death_certificate( stroke_hospital, returning="date_of_death", match_only_underlying_cause=False, - on_or_after=start_date, - return_expectations={"date": {"earliest": start_date}}, + on_or_after=start_mar, + return_expectations={"date": {"earliest": start_mar}}, ), + stroke_ons_apr=patients.with_these_codes_on_death_certificate( + stroke_hospital, + returning="date_of_death", + match_only_underlying_cause=False, + on_or_after=start_apr, + return_expectations={"date": {"earliest": start_apr}}, + ), + stroke_ons_may=patients.with_these_codes_on_death_certificate( + stroke_hospital, + returning="date_of_death", + match_only_underlying_cause=False, + on_or_after=start_may, + return_expectations={"date": {"earliest": start_may}}, + ), + stroke_ons_jun=patients.with_these_codes_on_death_certificate( + stroke_hospital, + returning="date_of_death", + match_only_underlying_cause=False, + on_or_after=start_jun, + return_expectations={"date": {"earliest": start_jun}}, + ), + stroke_ons_jul=patients.with_these_codes_on_death_certificate( + stroke_hospital, + returning="date_of_death", + match_only_underlying_cause=False, + on_or_after=start_jul, + return_expectations={"date": {"earliest": start_jul}}, + ), + stroke_ons_aug=patients.with_these_codes_on_death_certificate( + stroke_hospital, + returning="date_of_death", + match_only_underlying_cause=False, + on_or_after=start_aug, + return_expectations={"date": {"earliest": start_aug}}, + ), + stroke_ons_sep=patients.with_these_codes_on_death_certificate( + stroke_hospital, + returning="date_of_death", + match_only_underlying_cause=False, + on_or_after=start_sep, + return_expectations={"date": {"earliest": start_sep}}, + ), + stroke_ons_oct=patients.with_these_codes_on_death_certificate( + stroke_hospital, + returning="date_of_death", + match_only_underlying_cause=False, + on_or_after=start_oct, + return_expectations={"date": {"earliest": start_oct}}, + ), previous_stroke_gp=patients.with_these_clinical_events( stroke, returning="date", diff --git a/analysis/study_definition_control_2019.py b/analysis/study_definition_control_2019.py index c7d518f..69b2d3a 100644 --- a/analysis/study_definition_control_2019.py +++ b/analysis/study_definition_control_2019.py @@ -3,6 +3,15 @@ from codelists import * start_date = "2019-02-01" +start_mar = "2019-03-01" +start_apr = "2019-04-01" +start_may = "2019-05-01" +start_jun = "2019-06-01" +start_jul = "2019-07-01" +start_aug = "2019-08-01" +start_sep = "2019-09-01" +start_oct = "2019-10-01" + common_variables = common_variable_define(start_date) study = StudyDefinition( diff --git a/analysis/study_definition_control_2020.py b/analysis/study_definition_control_2020.py index 57bdb95..a932d87 100644 --- a/analysis/study_definition_control_2020.py +++ b/analysis/study_definition_control_2020.py @@ -3,6 +3,15 @@ from codelists import * start_date = "2020-02-01" +start_mar = "2020-03-01" +start_apr = "2020-04-01" +start_may = "2020-05-01" +start_jun = "2020-06-01" +start_jul = "2020-07-01" +start_aug = "2020-08-01" +start_sep = "2020-09-01" +start_oct = "2020-10-01" + common_variables = common_variable_define(start_date) study = StudyDefinition( diff --git a/analysis/study_definition_covid.py b/analysis/study_definition_covid.py index 36b3601..b02cfca 100644 --- a/analysis/study_definition_covid.py +++ b/analysis/study_definition_covid.py @@ -3,6 +3,15 @@ from codelists import * start_date = "2020-02-01" +start_mar = "2020-03-01" +start_apr = "2020-04-01" +start_may = "2020-05-01" +start_jun = "2020-06-01" +start_jul = "2020-07-01" +start_aug = "2020-08-01" +start_sep = "2020-09-01" +start_oct = "2020-10-01" + common_variables = common_variable_define(start_date) study = StudyDefinition( diff --git a/analysis/study_definition_pneumonia.py b/analysis/study_definition_pneumonia.py index 62eca91..04919de 100644 --- a/analysis/study_definition_pneumonia.py +++ b/analysis/study_definition_pneumonia.py @@ -3,6 +3,15 @@ from codelists import * start_date = "2019-02-01" +start_mar = "2019-03-01" +start_apr = "2019-04-01" +start_may = "2019-05-01" +start_jun = "2019-06-01" +start_jul = "2019-07-01" +start_aug = "2019-08-01" +start_sep = "2019-09-01" +start_oct = "2019-10-01" + common_variables = common_variable_define(start_date) study = StudyDefinition( From 649cfc222093fb2d33dbb1bcfadbb0165a8fcf0e Mon Sep 17 00:00:00 2001 From: johntaz Date: Thu, 26 Nov 2020 16:58:15 +0000 Subject: [PATCH 02/21] hist of af code list --- analysis/codelists.py | 6 ++++++ analysis/common_variables.py | 5 +++++ codelists/codelists.txt | 11 ++++++----- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/analysis/codelists.py b/analysis/codelists.py index 58dffdd..ff53ff1 100644 --- a/analysis/codelists.py +++ b/analysis/codelists.py @@ -1,5 +1,11 @@ from cohortextractor import codelist, codelist_from_csv +af_codes = codelist_from_csv( + "codelists/atrial-fibrillation-clinical-finding.csv", + system="ctv3", + column="CTV3Code", +) + ethnicity_codes = codelist_from_csv( "codelists/opensafely-ethnicity.csv", system="ctv3", diff --git a/analysis/common_variables.py b/analysis/common_variables.py index dbf6ff0..59b1c9d 100644 --- a/analysis/common_variables.py +++ b/analysis/common_variables.py @@ -11,6 +11,11 @@ def days_before(s, days): def common_variable_define(start_date): common_variables = dict( + af=patients.with_these_clinical_events( + af_codes, + return_first_date_in_period=True, + date_format="YYYY-MM-DD", + ), dvt_gp_feb=patients.with_these_clinical_events( filter_codes_by_category(vte_codes_gp, include=["dvt"]), return_first_date_in_period=True, diff --git a/codelists/codelists.txt b/codelists/codelists.txt index 5337851..0bea381 100644 --- a/codelists/codelists.txt +++ b/codelists/codelists.txt @@ -2,9 +2,10 @@ opensafely/covid-identification/2020-06-03 opensafely/diabetes/2020-04-15 opensafely/ethnicity/2020-04-27 opensafely/hypertension/2020-04-28 -opensafely/incident-venous-thromboembolic-disease/2020-09-30-draft -opensafely/pneumonia-secondary-care/2020-10-05-draft +opensafely/incident-venous-thromboembolic-disease/2020-09-30 +opensafely/pneumonia-secondary-care/2020-10-05 opensafely/smoking-clear/2020-04-29 -opensafely/incident-stroke/2020-10-05-draft -opensafely/stroke-secondary-care/2020-10-05-draft -opensafely/venous-thromboembolic-disease-hospital/2020-10-01-draft \ No newline at end of file +opensafely/incident-stroke/2020-10-05 +opensafely/stroke-secondary-care/2020-10-05 +opensafely/venous-thromboembolic-disease-hospital/2020-10-01 +opensafely/atrial-fibrillation-clinical-finding/2020-07-09/ From 01535bde9c03be99135ee1e01b7d7d1a7aa271c6 Mon Sep 17 00:00:00 2001 From: johntaz Date: Thu, 26 Nov 2020 19:05:13 +0000 Subject: [PATCH 03/21] Update codelists.py --- analysis/codelists.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/analysis/codelists.py b/analysis/codelists.py index ff53ff1..7c26aee 100644 --- a/analysis/codelists.py +++ b/analysis/codelists.py @@ -1,7 +1,7 @@ from cohortextractor import codelist, codelist_from_csv af_codes = codelist_from_csv( - "codelists/atrial-fibrillation-clinical-finding.csv", + "codelists/opensafely-atrial-fibrillation-clinical-finding.csv", system="ctv3", column="CTV3Code", ) From 57b0d6afb9db5ef0d323606f202ddda0765481c5 Mon Sep 17 00:00:00 2001 From: johntaz Date: Fri, 27 Nov 2020 10:04:25 +0000 Subject: [PATCH 04/21] typo "/" --- codelists/codelists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codelists/codelists.txt b/codelists/codelists.txt index 0bea381..8e87251 100644 --- a/codelists/codelists.txt +++ b/codelists/codelists.txt @@ -8,4 +8,4 @@ opensafely/smoking-clear/2020-04-29 opensafely/incident-stroke/2020-10-05 opensafely/stroke-secondary-care/2020-10-05 opensafely/venous-thromboembolic-disease-hospital/2020-10-01 -opensafely/atrial-fibrillation-clinical-finding/2020-07-09/ +opensafely/atrial-fibrillation-clinical-finding/2020-07-09 From ea5dcb2e85b5deeea95a68def1422880fdd0c52d Mon Sep 17 00:00:00 2001 From: johntaz Date: Fri, 27 Nov 2020 10:26:49 +0000 Subject: [PATCH 05/21] update af clinical finding --- analysis/study_definition_covid.py | 8 ++++++++ ...fely-atrial-fibrillation-clinical-finding.csv | 16 ++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 codelists/opensafely-atrial-fibrillation-clinical-finding.csv diff --git a/analysis/study_definition_covid.py b/analysis/study_definition_covid.py index b02cfca..65a2ae6 100644 --- a/analysis/study_definition_covid.py +++ b/analysis/study_definition_covid.py @@ -40,6 +40,14 @@ date_format="YYYY-MM-DD", find_first_match_in_period=True, return_expectations={"date": {"earliest": start_date},}, + ), + exposure_hosp_primary_dx=patients.admitted_to_hospital( + returning="date_admitted", + with_these_primary_diagnoses=covid_codelist, + on_or_after=start_date, + date_format="YYYY-MM-DD", + find_first_match_in_period=True, + return_expectations={"date": {"earliest": start_date},}, ), exposure_discharge=patients.admitted_to_hospital( returning="date_discharged", diff --git a/codelists/opensafely-atrial-fibrillation-clinical-finding.csv b/codelists/opensafely-atrial-fibrillation-clinical-finding.csv new file mode 100644 index 0000000..c4e3105 --- /dev/null +++ b/codelists/opensafely-atrial-fibrillation-clinical-finding.csv @@ -0,0 +1,16 @@ +CTV3Code,Description +G573.,Atrial fibrillation and flutter +G5730,Atrial fibrillation +G5731,Atrial flutter +G573z,Atrial fibrillation and flutter NOS +X202R,Lone atrial fibrillation +X202S,Non-rheumatic atrial fibrillation +Xa2E8,Paroxysmal atrial fibrillation +Xa7nI,Controlled atrial fibrillation +XaEga,Rapid atrial fibrillation +XaOfa,Persistent atrial fibrillation +XaOft,Permanent atrial fibrillation +XaaUH,Paroxysmal atrial flutter +XaeUP,Chronic atrial fibrillation +XaeUQ,Typical atrial flutter +XaeUR,Atypical atrial flutter From bab9813eb49bdf57bb73f6557b074652b219efca Mon Sep 17 00:00:00 2001 From: johntaz Date: Fri, 27 Nov 2020 10:45:09 +0000 Subject: [PATCH 06/21] update common_variable_define --- analysis/common_variables.py | 2 +- analysis/study_definition_control_2019.py | 2 +- analysis/study_definition_control_2020.py | 2 +- analysis/study_definition_covid.py | 2 +- analysis/study_definition_pneumonia.py | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/analysis/common_variables.py b/analysis/common_variables.py index 59b1c9d..7db6a58 100644 --- a/analysis/common_variables.py +++ b/analysis/common_variables.py @@ -9,7 +9,7 @@ def days_before(s, days): return datetime.strftime(modified_date, "%Y-%m-%d") -def common_variable_define(start_date): +def common_variable_define(start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct): common_variables = dict( af=patients.with_these_clinical_events( af_codes, diff --git a/analysis/study_definition_control_2019.py b/analysis/study_definition_control_2019.py index 69b2d3a..1f4e911 100644 --- a/analysis/study_definition_control_2019.py +++ b/analysis/study_definition_control_2019.py @@ -12,7 +12,7 @@ start_sep = "2019-09-01" start_oct = "2019-10-01" -common_variables = common_variable_define(start_date) +common_variables = common_variable_define(start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct) study = StudyDefinition( default_expectations={ diff --git a/analysis/study_definition_control_2020.py b/analysis/study_definition_control_2020.py index a932d87..755de48 100644 --- a/analysis/study_definition_control_2020.py +++ b/analysis/study_definition_control_2020.py @@ -12,7 +12,7 @@ start_sep = "2020-09-01" start_oct = "2020-10-01" -common_variables = common_variable_define(start_date) +common_variables = common_variable_define(start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct) study = StudyDefinition( default_expectations={ diff --git a/analysis/study_definition_covid.py b/analysis/study_definition_covid.py index 65a2ae6..329ce47 100644 --- a/analysis/study_definition_covid.py +++ b/analysis/study_definition_covid.py @@ -12,7 +12,7 @@ start_sep = "2020-09-01" start_oct = "2020-10-01" -common_variables = common_variable_define(start_date) +common_variables = common_variable_define(start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct) study = StudyDefinition( default_expectations={ diff --git a/analysis/study_definition_pneumonia.py b/analysis/study_definition_pneumonia.py index 04919de..2552e0d 100644 --- a/analysis/study_definition_pneumonia.py +++ b/analysis/study_definition_pneumonia.py @@ -12,7 +12,7 @@ start_sep = "2019-09-01" start_oct = "2019-10-01" -common_variables = common_variable_define(start_date) +common_variables = common_variable_define(start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct) study = StudyDefinition( default_expectations={ From 1c5345f09cd06b30c318c118794cfb61c88f7f28 Mon Sep 17 00:00:00 2001 From: johntaz Date: Fri, 27 Nov 2020 11:37:17 +0000 Subject: [PATCH 07/21] Study definitions/codelists updated Added to study definition: 1. Primary hosp dx 2. common_variables fix 3. anticoag use Codelists added: 1. doacs 2. warfarin --- analysis/codelists.py | 14 ++ analysis/common_variables.py | 73 +++++++++- analysis/study_definition_control_2019.py | 5 +- analysis/study_definition_control_2020.py | 5 +- analysis/study_definition_covid.py | 12 +- analysis/study_definition_pneumonia.py | 22 ++- codelists/codelists.txt | 2 + ...direct-acting-oral-anticoagulants-doac.csv | 27 ++++ codelists/opensafely-warfarin.csv | 133 ++++++++++++++++++ 9 files changed, 288 insertions(+), 5 deletions(-) create mode 100644 codelists/opensafely-direct-acting-oral-anticoagulants-doac.csv create mode 100644 codelists/opensafely-warfarin.csv diff --git a/analysis/codelists.py b/analysis/codelists.py index 7c26aee..c4e9a4c 100644 --- a/analysis/codelists.py +++ b/analysis/codelists.py @@ -13,6 +13,20 @@ category_column="Grouping_6", ) + +# MEDICATIONS +warfarin_codes = codelist_from_csv( + "codelists/opensafely-warfarin.csv", + system="snomed", + column="id", +) + +doac_codes = codelist_from_csv( + "codelists/opensafely-direct-acting-oral-anticoagulants-doac.csv", + system="snomed", + column="id", +) + diabetes_codes = codelist_from_csv( "codelists/opensafely-diabetes.csv", system="ctv3", column="CTV3ID" ) diff --git a/analysis/common_variables.py b/analysis/common_variables.py index 7db6a58..ae49331 100644 --- a/analysis/common_variables.py +++ b/analysis/common_variables.py @@ -9,13 +9,84 @@ def days_before(s, days): return datetime.strftime(modified_date, "%Y-%m-%d") -def common_variable_define(start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct): +def common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct, end_date): common_variables = dict( af=patients.with_these_clinical_events( af_codes, return_first_date_in_period=True, date_format="YYYY-MM-DD", ), + # 3mth hist of doac/warfarin use + anticoag_rx_3mths_before_start=patients.with_these_clinical_events( + combine_codelists(doac_codes, + warfarin_codes), + return_first_date_in_period=True, + between=[prev_3mths_start, start_date], + return_expectations={"incidence": 0.3} + ), + anticoag_rx_feb=patients.with_these_clinical_events( + combine_codelists(doac_codes, + warfarin_codes), + return_first_date_in_period=True, + between=[start_date, start_mar], + return_expectations={"incidence": 0.3} + ), + anticoag_rx_mar=patients.with_these_clinical_events( + combine_codelists(doac_codes, + warfarin_codes), + return_first_date_in_period=True, + between=[start_mar, start_apr], + return_expectations={"incidence": 0.3} + ), + anticoag_rx_apr=patients.with_these_clinical_events( + combine_codelists(doac_codes, + warfarin_codes), + return_first_date_in_period=True, + between=[start_apr, start_may], + return_expectations={"incidence": 0.3} + ), + anticoag_rx_may=patients.with_these_clinical_events( + combine_codelists(doac_codes, + warfarin_codes), + return_first_date_in_period=True, + between=[start_may, start_jun], + return_expectations={"incidence": 0.3} + ), + anticoag_rx_jun=patients.with_these_clinical_events( + combine_codelists(doac_codes, + warfarin_codes), + return_first_date_in_period=True, + between=[start_jun, start_jul], + return_expectations={"incidence": 0.3} + ), + anticoag_rx_jul=patients.with_these_clinical_events( + combine_codelists(doac_codes, + warfarin_codes), + return_first_date_in_period=True, + between=[start_jul, start_aug], + return_expectations={"incidence": 0.3} + ), + anticoag_rx_aug=patients.with_these_clinical_events( + combine_codelists(doac_codes, + warfarin_codes), + return_first_date_in_period=True, + between=[start_aug, start_sep], + return_expectations={"incidence": 0.3} + ), + anticoag_rx_sep=patients.with_these_clinical_events( + combine_codelists(doac_codes, + warfarin_codes), + return_first_date_in_period=True, + between=[start_sep, start_oct], + return_expectations={"incidence": 0.3} + ), + anticoag_rx_oct=patients.with_these_clinical_events( + combine_codelists(doac_codes, + warfarin_codes), + return_first_date_in_period=True, + between=[start_oct, end_date], + return_expectations={"incidence": 0.3} + ), dvt_gp_feb=patients.with_these_clinical_events( filter_codes_by_category(vte_codes_gp, include=["dvt"]), return_first_date_in_period=True, diff --git a/analysis/study_definition_control_2019.py b/analysis/study_definition_control_2019.py index 1f4e911..6e533cd 100644 --- a/analysis/study_definition_control_2019.py +++ b/analysis/study_definition_control_2019.py @@ -2,6 +2,8 @@ from common_variables import common_variable_define from codelists import * + +prev_3mths_start = "2018-11-01" start_date = "2019-02-01" start_mar = "2019-03-01" start_apr = "2019-04-01" @@ -11,8 +13,9 @@ start_aug = "2019-08-01" start_sep = "2019-09-01" start_oct = "2019-10-01" +end_date = "2019-11-01" -common_variables = common_variable_define(start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct) +common_variables = common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct, end_date) study = StudyDefinition( default_expectations={ diff --git a/analysis/study_definition_control_2020.py b/analysis/study_definition_control_2020.py index 755de48..b49fd90 100644 --- a/analysis/study_definition_control_2020.py +++ b/analysis/study_definition_control_2020.py @@ -2,6 +2,8 @@ from common_variables import common_variable_define from codelists import * + +prev_3mths_start = "2019-11-01" start_date = "2020-02-01" start_mar = "2020-03-01" start_apr = "2020-04-01" @@ -11,8 +13,9 @@ start_aug = "2020-08-01" start_sep = "2020-09-01" start_oct = "2020-10-01" +end_date = "2020-11-01" -common_variables = common_variable_define(start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct) +common_variables = common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct, end_date) study = StudyDefinition( default_expectations={ diff --git a/analysis/study_definition_covid.py b/analysis/study_definition_covid.py index 329ce47..bdd2ff1 100644 --- a/analysis/study_definition_covid.py +++ b/analysis/study_definition_covid.py @@ -11,8 +11,9 @@ start_aug = "2020-08-01" start_sep = "2020-09-01" start_oct = "2020-10-01" +end_date = "2020-11-01" -common_variables = common_variable_define(start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct) +common_variables = common_variable_define(start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct, end_date) study = StudyDefinition( default_expectations={ @@ -48,6 +49,15 @@ date_format="YYYY-MM-DD", find_first_match_in_period=True, return_expectations={"date": {"earliest": start_date},}, + ), + # ICU admission from ICNARC-CMP + date_icu_admission=patients.admitted_to_icu( + find_first_match_in_period=True, + between=[start_date, start_oct], + returning="date_admitted", + #include_day = True, + date_format="YYYY-MM-DD", #not yet working for admitted_to_icu? + return_expectations={"date": {"earliest": start_date}}, ), exposure_discharge=patients.admitted_to_hospital( returning="date_discharged", diff --git a/analysis/study_definition_pneumonia.py b/analysis/study_definition_pneumonia.py index 2552e0d..765d322 100644 --- a/analysis/study_definition_pneumonia.py +++ b/analysis/study_definition_pneumonia.py @@ -2,6 +2,7 @@ from common_variables import common_variable_define from codelists import * +prev_3mths_start = "2018-11-01" start_date = "2019-02-01" start_mar = "2019-03-01" start_apr = "2019-04-01" @@ -11,8 +12,9 @@ start_aug = "2019-08-01" start_sep = "2019-09-01" start_oct = "2019-10-01" +end_date = "2019-11-01" -common_variables = common_variable_define(start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct) +common_variables = common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct, end_date) study = StudyDefinition( default_expectations={ @@ -41,6 +43,24 @@ find_first_match_in_period=True, return_expectations={"date": {"earliest": start_date},}, ), + exposure_hosp_primary_dx=patients.admitted_to_hospital( + returning="date_admitted", + with_these_primary_diagnoses=pneumonia_codelist, + on_or_after=start_date, + date_format="YYYY-MM-DD", + find_first_match_in_period=True, + return_expectations={"date": {"earliest": start_date},}, + ), + # ICU admission from ICNARC-CMP + date_icu_admission=patients.admitted_to_icu( + find_first_match_in_period=True, + between=[start_date, start_oct], + returning="date_admitted", + #include_day = True, + date_format="YYYY-MM-DD", #not yet working for admitted_to_icu? + return_expectations={"date": {"earliest": start_date}}, + ), + exposure_discharge=patients.admitted_to_hospital( returning="date_discharged", with_these_diagnoses=pneumonia_codelist, diff --git a/codelists/codelists.txt b/codelists/codelists.txt index 8e87251..9c661bb 100644 --- a/codelists/codelists.txt +++ b/codelists/codelists.txt @@ -9,3 +9,5 @@ opensafely/incident-stroke/2020-10-05 opensafely/stroke-secondary-care/2020-10-05 opensafely/venous-thromboembolic-disease-hospital/2020-10-01 opensafely/atrial-fibrillation-clinical-finding/2020-07-09 +opensafely/direct-acting-oral-anticoagulants-doac/2020-10-05 +opensafely/warfarin/2020-10-05 \ No newline at end of file diff --git a/codelists/opensafely-direct-acting-oral-anticoagulants-doac.csv b/codelists/opensafely-direct-acting-oral-anticoagulants-doac.csv new file mode 100644 index 0000000..f1e5c2b --- /dev/null +++ b/codelists/opensafely-direct-acting-oral-anticoagulants-doac.csv @@ -0,0 +1,27 @@ +,type,id,bnf_code,nm,chemical +34,amp,19506911000001105,0208020Z0BBAAAA,Eliquis 2.5mg tablets (Bristol-Myers Squibb Pharmaceuticals Ltd),Apixaban +35,amp,21677511000001105,0208020Z0BBABAB,Eliquis 5mg tablets (Bristol-Myers Squibb Pharmaceuticals Ltd),Apixaban +142,amp,29902111000001100,0208020AABBAAAA,Lixiana 15mg tablets (Daiichi Sankyo UK Ltd),Edoxaban +143,amp,29902411000001105,0208020AABBABAB,Lixiana 30mg tablets (Daiichi Sankyo UK Ltd),Edoxaban +144,amp,29902711000001104,0208020AABBACAC,Lixiana 60mg tablets (Daiichi Sankyo UK Ltd),Edoxaban +173,amp,13505411000001109,0208020X0BBABAB,Pradaxa 110mg capsules (Boehringer Ingelheim Ltd),Dabigatran etexilate +174,amp,19465811000001104,0208020X0BBACAC,Pradaxa 150mg capsules (Boehringer Ingelheim Ltd),Dabigatran etexilate +175,amp,13504711000001102,0208020X0BBAAAA,Pradaxa 75mg capsules (Boehringer Ingelheim Ltd),Dabigatran etexilate +301,amp,14237311000001106,0208020Y0BBAAAA,Xarelto 10mg tablets (Bayer Plc),Rivaroxaban +302,amp,34793111000001102,0208020Y0BBAEAE,Xarelto 15mg / 20mg treatment initiation pack (Bayer Plc),Rivaroxaban +303,amp,19840811000001107,0208020Y0BBABAB,Xarelto 15mg tablets (Bayer Plc),Rivaroxaban +304,amp,27160311000001106,0208020Y0BBADAD,Xarelto 2.5mg tablets (Bayer Plc),Rivaroxaban +305,amp,19841411000001101,0208020Y0BBACAC,Xarelto 20mg tablets (Bayer Plc),Rivaroxaban +312,vmp,703907006,0208020Z0AAAAAA,Apixaban 2.5mg tablets,Apixaban +313,vmp,703908001,0208020Z0AAABAB,Apixaban 5mg tablets,Apixaban +321,vmp,13532811000001109,0208020X0AAABAB,Dabigatran etexilate 110mg capsules,Dabigatran etexilate +322,vmp,19469811000001101,0208020X0AAACAC,Dabigatran etexilate 150mg capsules,Dabigatran etexilate +323,vmp,13532911000001104,0208020X0AAAAAA,Dabigatran etexilate 75mg capsules,Dabigatran etexilate +336,vmp,29903211000001100,0208020AAAAAAAA,Edoxaban 15mg tablets,Edoxaban +337,vmp,29903311000001108,0208020AAAAABAB,Edoxaban 30mg tablets,Edoxaban +338,vmp,29903411000001101,0208020AAAAACAC,Edoxaban 60mg tablets,Edoxaban +386,vmp,14254711000001104,0208020Y0AAAAAA,Rivaroxaban 10mg tablets,Rivaroxaban +387,vmp,19842111000001101,0208020Y0AAABAB,Rivaroxaban 15mg tablets,Rivaroxaban +388,vmp,34819111000001102,0208020Y0AAAEAE,Rivaroxaban 15mg tablets and Rivaroxaban 20mg tablets,Rivaroxaban +389,vmp,27810711000001104,0208020Y0AAADAD,Rivaroxaban 2.5mg tablets,Rivaroxaban +390,vmp,19842211000001107,0208020Y0AAACAC,Rivaroxaban 20mg tablets,Rivaroxaban diff --git a/codelists/opensafely-warfarin.csv b/codelists/opensafely-warfarin.csv new file mode 100644 index 0000000..3450969 --- /dev/null +++ b/codelists/opensafely-warfarin.csv @@ -0,0 +1,133 @@ +,type,id,bnf_code,nm,chemical +30,amp,18509011000001102,0208020V0BDAAAC,Coumadin 4mg tablets (Imported (Canada)),Warfarin sodium +146,amp,933311000001105,0208020V0BBAAAA,Marevan 1mg tablets (Advanz Pharma),Warfarin sodium +147,amp,284011000001104,0208020V0BBABAB,Marevan 3mg tablets (Advanz Pharma),Warfarin sodium +148,amp,204811000001109,0208020V0BBAFAS,Marevan 500microgram tablets (Advanz Pharma),Warfarin sodium +149,amp,461911000001100,0208020V0BBACAD,Marevan 5mg tablets (Advanz Pharma),Warfarin sodium +200,amp,12957311000001102,0208020V0AAARAR,Warfarin 1.5mg/5ml oral solution (Special Order),Warfarin sodium +201,amp,12958011000001104,0208020V0AAATAT,Warfarin 100micrograms/5ml oral solution (Special Order),Warfarin sodium +202,amp,8728011000001102,0208020V0AAAVAV,Warfarin 10mg/5ml oral solution (Special Order),Warfarin sodium +203,amp,8728311000001104,0208020V0AAAVAV,Warfarin 10mg/5ml oral suspension (Special Order),Warfarin sodium +204,amp,32742511000001102,0208020V0AABABA,Warfarin 1mg capsules (Special Order),Warfarin sodium +205,amp,421411000001103,0208020V0AAAAAA,Warfarin 1mg tablets (A A H Pharmaceuticals Ltd),Warfarin sodium +206,amp,19835411000001102,0208020V0AAAAAA,Warfarin 1mg tablets (APC Pharmaceuticals & Chemicals (Europe) Ltd),Warfarin sodium +207,amp,303111000001107,0208020V0AAAAAA,Warfarin 1mg tablets (Actavis UK Ltd),Warfarin sodium +208,amp,739511000001109,0208020V0AAAAAA,Warfarin 1mg tablets (Alliance Healthcare (Distribution) Ltd),Warfarin sodium +209,amp,9798311000001105,0208020V0AAAAAA,Warfarin 1mg tablets (Almus Pharmaceuticals Ltd),Warfarin sodium +210,amp,10418211000001107,0208020V0AAAAAA,Warfarin 1mg tablets (Arrow Generics Ltd),Warfarin sodium +211,amp,16071211000001106,0208020V0AAAAAA,Warfarin 1mg tablets (Bristol Laboratories Ltd),Warfarin sodium +212,amp,28988911000001100,0208020V0AAAAAA,Warfarin 1mg tablets (Crescent Pharma Ltd),Warfarin sodium +213,amp,22735311000001104,0208020V0AAAAAA,Warfarin 1mg tablets (DE Pharmaceuticals),Warfarin sodium +214,amp,272711000001102,0208020V0AAAAAA,Warfarin 1mg tablets (IVAX Pharmaceuticals UK Ltd),Warfarin sodium +215,amp,524811000001109,0208020V0AAAAAA,Warfarin 1mg tablets (Kent Pharmaceuticals Ltd),Warfarin sodium +216,amp,30945211000001105,0208020V0AAAAAA,Warfarin 1mg tablets (Mawdsley-Brooks & Company Ltd),Warfarin sodium +217,amp,746411000001102,0208020V0AAAAAA,Warfarin 1mg tablets (Mylan),Warfarin sodium +218,amp,17876711000001107,0208020V0AAAAAA,Warfarin 1mg tablets (Phoenix Healthcare Distribution Ltd),Warfarin sodium +219,amp,321511000001106,0208020V0AAAAAA,Warfarin 1mg tablets (Sandoz Ltd),Warfarin sodium +220,amp,34760111000001106,0208020V0AAAAAA,Warfarin 1mg tablets (Sun Pharmaceutical Industries Europe B.V.),Warfarin sodium +221,amp,437711000001102,0208020V0AAAAAA,Warfarin 1mg tablets (Teva UK Ltd),Warfarin sodium +222,amp,590411000001106,0208020V0AAAAAA,Warfarin 1mg tablets (The Boots Company Plc),Warfarin sodium +223,amp,36843011000001100,0208020V0AAAAAA,Warfarin 1mg tablets (Torrent Pharma (UK) Ltd),Warfarin sodium +224,amp,21919511000001107,0208020V0AAAAAA,Warfarin 1mg tablets (Waymade Healthcare Plc),Warfarin sodium +225,amp,10138911000001108,0208020V0AAAAAA,Warfarin 1mg tablets (Zentiva),Warfarin sodium +226,amp,8729811000001101,0208020V0AAANAN,Warfarin 1mg/5ml oral solution (Special Order),Warfarin sodium +227,amp,8730111000001100,0208020V0AAANAN,Warfarin 1mg/5ml oral suspension (Special Order),Warfarin sodium +228,amp,20367411000001100,0208020V0AABBBB,Warfarin 1mg/ml oral suspension sugar free (A A H Pharmaceuticals Ltd),Warfarin sodium +229,amp,18723611000001101,0208020V0AABBBB,Warfarin 1mg/ml oral suspension sugar free (Alliance Healthcare (Distribution) Ltd),Warfarin sodium +230,amp,22736911000001105,0208020V0AABBBB,Warfarin 1mg/ml oral suspension sugar free (DE Pharmaceuticals),Warfarin sodium +231,amp,18288311000001103,0208020V0AABBBB,Warfarin 1mg/ml oral suspension sugar free (Rosemont Pharmaceuticals Ltd),Warfarin sodium +232,amp,21919111000001103,0208020V0AABBBB,Warfarin 1mg/ml oral suspension sugar free (Waymade Healthcare Plc),Warfarin sodium +233,amp,12958511000001107,0208020V0AAAUAU,Warfarin 2.5mg/5ml oral solution (Special Order),Warfarin sodium +234,amp,12959011000001109,0208020V0AAAKAK,Warfarin 25mg/5ml oral solution (Special Order),Warfarin sodium +235,amp,8731411000001101,0208020V0AAAJAJ,Warfarin 2mg/5ml oral solution (Special Order),Warfarin sodium +236,amp,8733211000001101,0208020V0AAAJAJ,Warfarin 2mg/5ml oral suspension (Special Order),Warfarin sodium +237,amp,32742211000001100,0208020V0AABCBC,Warfarin 3mg capsules (Special Order),Warfarin sodium +238,amp,456711000001104,0208020V0AAABAB,Warfarin 3mg tablets (A A H Pharmaceuticals Ltd),Warfarin sodium +239,amp,19835611000001104,0208020V0AAABAB,Warfarin 3mg tablets (APC Pharmaceuticals & Chemicals (Europe) Ltd),Warfarin sodium +240,amp,555911000001104,0208020V0AAABAB,Warfarin 3mg tablets (Actavis UK Ltd),Warfarin sodium +241,amp,584211000001100,0208020V0AAABAB,Warfarin 3mg tablets (Alliance Healthcare (Distribution) Ltd),Warfarin sodium +242,amp,9798011000001107,0208020V0AAABAB,Warfarin 3mg tablets (Almus Pharmaceuticals Ltd),Warfarin sodium +243,amp,10418411000001106,0208020V0AAABAB,Warfarin 3mg tablets (Arrow Generics Ltd),Warfarin sodium +244,amp,16071411000001105,0208020V0AAABAB,Warfarin 3mg tablets (Bristol Laboratories Ltd),Warfarin sodium +245,amp,28989111000001105,0208020V0AAABAB,Warfarin 3mg tablets (Crescent Pharma Ltd),Warfarin sodium +246,amp,22735611000001109,0208020V0AAABAB,Warfarin 3mg tablets (DE Pharmaceuticals),Warfarin sodium +247,amp,927211000001109,0208020V0AAABAB,Warfarin 3mg tablets (IVAX Pharmaceuticals UK Ltd),Warfarin sodium +248,amp,772211000001101,0208020V0AAABAB,Warfarin 3mg tablets (Kent Pharmaceuticals Ltd),Warfarin sodium +249,amp,30945411000001109,0208020V0AAABAB,Warfarin 3mg tablets (Mawdsley-Brooks & Company Ltd),Warfarin sodium +250,amp,347911000001109,0208020V0AAABAB,Warfarin 3mg tablets (Mylan),Warfarin sodium +251,amp,17876911000001109,0208020V0AAABAB,Warfarin 3mg tablets (Phoenix Healthcare Distribution Ltd),Warfarin sodium +252,amp,550411000001100,0208020V0AAABAB,Warfarin 3mg tablets (Sandoz Ltd),Warfarin sodium +253,amp,34760411000001101,0208020V0AAABAB,Warfarin 3mg tablets (Sun Pharmaceutical Industries Europe B.V.),Warfarin sodium +254,amp,508311000001104,0208020V0AAABAB,Warfarin 3mg tablets (Teva UK Ltd),Warfarin sodium +255,amp,473111000001108,0208020V0AAABAB,Warfarin 3mg tablets (The Boots Company Plc),Warfarin sodium +256,amp,36844011000001103,0208020V0AAABAB,Warfarin 3mg tablets (Torrent Pharma (UK) Ltd),Warfarin sodium +257,amp,21919811000001105,0208020V0AAABAB,Warfarin 3mg tablets (Waymade Healthcare Plc),Warfarin sodium +258,amp,10139111000001103,0208020V0AAABAB,Warfarin 3mg tablets (Zentiva),Warfarin sodium +259,amp,8728611000001109,0208020V0AAAMAM,Warfarin 3mg/5ml oral solution (Special Order),Warfarin sodium +260,amp,8729111000001108,0208020V0AAAMAM,Warfarin 3mg/5ml oral suspension (Special Order),Warfarin sodium +261,amp,12959411000001100,0208020V0AAAPAP,Warfarin 4.16mg/5ml oral solution (Special Order),Warfarin sodium +262,amp,12960011000001105,0208020V0AAAWAW,Warfarin 4mg/5ml oral solution (Special Order),Warfarin sodium +263,amp,3659711000001104,0208020V0AAASAS,Warfarin 500microgram tablets (A A H Pharmaceuticals Ltd),Warfarin sodium +264,amp,10305211000001109,0208020V0AAASAS,Warfarin 500microgram tablets (Actavis UK Ltd),Warfarin sodium +265,amp,20538311000001102,0208020V0AAASAS,Warfarin 500microgram tablets (Advanz Pharma),Warfarin sodium +266,amp,395111000001106,0208020V0AAASAS,Warfarin 500microgram tablets (Alliance Healthcare (Distribution) Ltd),Warfarin sodium +267,amp,10417011000001103,0208020V0AAASAS,Warfarin 500microgram tablets (Arrow Generics Ltd),Warfarin sodium +268,amp,22736711000001108,0208020V0AAASAS,Warfarin 500microgram tablets (DE Pharmaceuticals),Warfarin sodium +269,amp,622211000001107,0208020V0AAASAS,Warfarin 500microgram tablets (Kent Pharmaceuticals Ltd),Warfarin sodium +270,amp,37272811000001105,0208020V0AAASAS,Warfarin 500microgram tablets (Mawdsley-Brooks & Company Ltd),Warfarin sodium +271,amp,17876511000001102,0208020V0AAASAS,Warfarin 500microgram tablets (Phoenix Healthcare Distribution Ltd),Warfarin sodium +272,amp,15202611000001100,0208020V0AAASAS,Warfarin 500microgram tablets (Sigma Pharmaceuticals Plc),Warfarin sodium +273,amp,34759911000001108,0208020V0AAASAS,Warfarin 500microgram tablets (Sun Pharmaceutical Industries Europe B.V.),Warfarin sodium +274,amp,21919311000001101,0208020V0AAASAS,Warfarin 500microgram tablets (Waymade Healthcare Plc),Warfarin sodium +275,amp,10733911000001106,0208020V0AAASAS,Warfarin 500microgram tablets (Zentiva),Warfarin sodium +276,amp,12961311000001103,0208020V0AAAXAX,Warfarin 500micrograms/5ml oral solution (Special Order),Warfarin sodium +277,amp,32741911000001103,0208020V0AABDBD,Warfarin 5mg capsules (Special Order),Warfarin sodium +278,amp,751611000001108,0208020V0AAADAD,Warfarin 5mg tablets (A A H Pharmaceuticals Ltd),Warfarin sodium +279,amp,19835811000001100,0208020V0AAADAD,Warfarin 5mg tablets (APC Pharmaceuticals & Chemicals (Europe) Ltd),Warfarin sodium +280,amp,642911000001101,0208020V0AAADAD,Warfarin 5mg tablets (Actavis UK Ltd),Warfarin sodium +281,amp,883011000001107,0208020V0AAADAD,Warfarin 5mg tablets (Alliance Healthcare (Distribution) Ltd),Warfarin sodium +282,amp,9797411000001102,0208020V0AAADAD,Warfarin 5mg tablets (Almus Pharmaceuticals Ltd),Warfarin sodium +283,amp,10418611000001109,0208020V0AAADAD,Warfarin 5mg tablets (Arrow Generics Ltd),Warfarin sodium +284,amp,16071611000001108,0208020V0AAADAD,Warfarin 5mg tablets (Bristol Laboratories Ltd),Warfarin sodium +285,amp,28989311000001107,0208020V0AAADAD,Warfarin 5mg tablets (Crescent Pharma Ltd),Warfarin sodium +286,amp,22736411000001102,0208020V0AAADAD,Warfarin 5mg tablets (DE Pharmaceuticals),Warfarin sodium +287,amp,729811000001109,0208020V0AAADAD,Warfarin 5mg tablets (IVAX Pharmaceuticals UK Ltd),Warfarin sodium +288,amp,36911000001105,0208020V0AAADAD,Warfarin 5mg tablets (Kent Pharmaceuticals Ltd),Warfarin sodium +289,amp,30945711000001103,0208020V0AAADAD,Warfarin 5mg tablets (Mawdsley-Brooks & Company Ltd),Warfarin sodium +290,amp,727111000001108,0208020V0AAADAD,Warfarin 5mg tablets (Mylan),Warfarin sodium +291,amp,17877111000001109,0208020V0AAADAD,Warfarin 5mg tablets (Phoenix Healthcare Distribution Ltd),Warfarin sodium +292,amp,919811000001107,0208020V0AAADAD,Warfarin 5mg tablets (Sandoz Ltd),Warfarin sodium +293,amp,34760611000001103,0208020V0AAADAD,Warfarin 5mg tablets (Sun Pharmaceutical Industries Europe B.V.),Warfarin sodium +294,amp,617811000001100,0208020V0AAADAD,Warfarin 5mg tablets (Teva UK Ltd),Warfarin sodium +295,amp,504911000001105,0208020V0AAADAD,Warfarin 5mg tablets (The Boots Company Plc),Warfarin sodium +296,amp,36844311000001100,0208020V0AAADAD,Warfarin 5mg tablets (Torrent Pharma (UK) Ltd),Warfarin sodium +297,amp,21920111000001102,0208020V0AAADAD,Warfarin 5mg tablets (Waymade Healthcare Plc),Warfarin sodium +298,amp,10139311000001101,0208020V0AAADAD,Warfarin 5mg tablets (Zentiva),Warfarin sodium +299,amp,8730911000001102,0208020V0AAAIAI,Warfarin 5mg/5ml oral solution (Special Order),Warfarin sodium +300,amp,8732411000001106,0208020V0AAAIAI,Warfarin 5mg/5ml oral suspension (Special Order),Warfarin sodium +402,vmp,13016611000001102,0208020V0AAARAR,Warfarin 1.5mg/5ml oral solution,Warfarin sodium +403,vmp,13016711000001106,0208020V0AAATAT,Warfarin 100micrograms/5ml oral solution,Warfarin sodium +404,vmp,8797911000001107,0208020V0AAAVAV,Warfarin 10mg/5ml oral solution,Warfarin sodium +405,vmp,8798011000001109,0208020V0AAAVAV,Warfarin 10mg/5ml oral suspension,Warfarin sodium +406,vmp,32751111000001103,0208020V0AABABA,Warfarin 1mg capsules,Warfarin sodium +407,vmp,319733000,0208020V0AAAAAA,Warfarin 1mg tablets,Warfarin sodium +408,vmp,8798111000001105,0208020V0AAANAN,Warfarin 1mg/5ml oral solution,Warfarin sodium +409,vmp,8798211000001104,0208020V0AAANAN,Warfarin 1mg/5ml oral suspension,Warfarin sodium +410,vmp,18290011000001102,0208020V0AABBBB,Warfarin 1mg/ml oral suspension sugar free,Warfarin sodium +411,vmp,13016811000001103,0208020V0AAAUAU,Warfarin 2.5mg/5ml oral solution,Warfarin sodium +412,vmp,13016911000001108,0208020V0AAAKAK,Warfarin 25mg/5ml oral solution,Warfarin sodium +413,vmp,8798311000001107,0208020V0AAAJAJ,Warfarin 2mg/5ml oral solution,Warfarin sodium +414,vmp,8798411000001100,0208020V0AAAJAJ,Warfarin 2mg/5ml oral suspension,Warfarin sodium +415,vmp,32751211000001109,0208020V0AABCBC,Warfarin 3mg capsules,Warfarin sodium +416,vmp,319734006,0208020V0AAABAB,Warfarin 3mg tablets,Warfarin sodium +417,vmp,8798511000001101,0208020V0AAAMAM,Warfarin 3mg/5ml oral solution,Warfarin sodium +418,vmp,8798611000001102,0208020V0AAAMAM,Warfarin 3mg/5ml oral suspension,Warfarin sodium +419,vmp,13017011000001107,0208020V0AAAPAP,Warfarin 4.16mg/5ml oral solution,Warfarin sodium +420,vmp,375374009,0208020V0AAACAC,Warfarin 4mg tablets,Warfarin sodium +421,vmp,13017111000001108,0208020V0AAAWAW,Warfarin 4mg/5ml oral solution,Warfarin sodium +422,vmp,319736008,0208020V0AAASAS,Warfarin 500microgram tablets,Warfarin sodium +423,vmp,13017211000001102,0208020V0AAAXAX,Warfarin 500micrograms/5ml oral solution,Warfarin sodium +424,vmp,32751311000001101,0208020V0AABDBD,Warfarin 5mg capsules,Warfarin sodium +425,vmp,319735007,0208020V0AAADAD,Warfarin 5mg tablets,Warfarin sodium +426,vmp,8798711000001106,0208020V0AAAIAI,Warfarin 5mg/5ml oral solution,Warfarin sodium +427,vmp,8798811000001103,0208020V0AAAIAI,Warfarin 5mg/5ml oral suspension,Warfarin sodium From 58eabb740cade5685ee668c39978476a690dc25f Mon Sep 17 00:00:00 2001 From: johntaz Date: Fri, 27 Nov 2020 11:42:45 +0000 Subject: [PATCH 08/21] import "combine_codelists" from cohort extractor --- analysis/common_variables.py | 2 +- analysis/study_definition_control_2019.py | 2 +- analysis/study_definition_control_2020.py | 2 +- analysis/study_definition_covid.py | 5 +++-- analysis/study_definition_pneumonia.py | 2 +- 5 files changed, 7 insertions(+), 6 deletions(-) diff --git a/analysis/common_variables.py b/analysis/common_variables.py index ae49331..7ae41e3 100644 --- a/analysis/common_variables.py +++ b/analysis/common_variables.py @@ -1,4 +1,4 @@ -from cohortextractor import filter_codes_by_category, patients +from cohortextractor import filter_codes_by_category, patients, combine_codelists from codelists import * from datetime import datetime, timedelta diff --git a/analysis/study_definition_control_2019.py b/analysis/study_definition_control_2019.py index 6e533cd..12603fd 100644 --- a/analysis/study_definition_control_2019.py +++ b/analysis/study_definition_control_2019.py @@ -1,4 +1,4 @@ -from cohortextractor import StudyDefinition, patients, codelist, codelist_from_csv +from cohortextractor import StudyDefinition, patients, codelist, codelist_from_csv, combine_codelists from common_variables import common_variable_define from codelists import * diff --git a/analysis/study_definition_control_2020.py b/analysis/study_definition_control_2020.py index b49fd90..43d9b3d 100644 --- a/analysis/study_definition_control_2020.py +++ b/analysis/study_definition_control_2020.py @@ -1,4 +1,4 @@ -from cohortextractor import StudyDefinition, patients, codelist, codelist_from_csv +from cohortextractor import StudyDefinition, patients, codelist, codelist_from_csv, combine_codelists from common_variables import common_variable_define from codelists import * diff --git a/analysis/study_definition_covid.py b/analysis/study_definition_covid.py index bdd2ff1..bb9d768 100644 --- a/analysis/study_definition_covid.py +++ b/analysis/study_definition_covid.py @@ -1,7 +1,8 @@ -from cohortextractor import StudyDefinition, patients, codelist, codelist_from_csv +from cohortextractor import StudyDefinition, patients, codelist, codelist_from_csv, combine_codelists from common_variables import common_variable_define from codelists import * +prev_3mths_start = "2019-11-01" start_date = "2020-02-01" start_mar = "2020-03-01" start_apr = "2020-04-01" @@ -13,7 +14,7 @@ start_oct = "2020-10-01" end_date = "2020-11-01" -common_variables = common_variable_define(start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct, end_date) +common_variables = common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct, end_date) study = StudyDefinition( default_expectations={ diff --git a/analysis/study_definition_pneumonia.py b/analysis/study_definition_pneumonia.py index 765d322..ddb2e39 100644 --- a/analysis/study_definition_pneumonia.py +++ b/analysis/study_definition_pneumonia.py @@ -1,4 +1,4 @@ -from cohortextractor import StudyDefinition, patients, codelist, codelist_from_csv +from cohortextractor import StudyDefinition, patients, codelist, codelist_from_csv, combine_codelists from common_variables import common_variable_define from codelists import * From 36fc420c132e78fab11635e0f72adbb680614da5 Mon Sep 17 00:00:00 2001 From: johntaz Date: Fri, 27 Nov 2020 11:47:46 +0000 Subject: [PATCH 09/21] remove start_feb and replace with start_date (same month) --- analysis/common_variables.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/analysis/common_variables.py b/analysis/common_variables.py index 7ae41e3..dfb414a 100644 --- a/analysis/common_variables.py +++ b/analysis/common_variables.py @@ -594,8 +594,8 @@ def common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, s filter_codes_by_category(vte_codes_hospital, include=["pe"]), returning="date_of_death", match_only_underlying_cause=False, - on_or_after=start_feb, - return_expectations={"date": {"earliest": start_feb}}, + on_or_after=start_date, + return_expectations={"date": {"earliest": start_date}}, ), pe_ons_mar=patients.with_these_codes_on_death_certificate( filter_codes_by_category(vte_codes_hospital, include=["pe"]), @@ -845,8 +845,8 @@ def common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, s stroke_hospital, returning="date_of_death", match_only_underlying_cause=False, - on_or_after=start_feb, - return_expectations={"date": {"earliest": start_feb}}, + on_or_after=start_date, + return_expectations={"date": {"earliest": start_date}}, ), stroke_ons_mar=patients.with_these_codes_on_death_certificate( stroke_hospital, From 5b674d08fe46d602137bb8b8eeb3d2a185dc1f48 Mon Sep 17 00:00:00 2001 From: johntaz Date: Fri, 27 Nov 2020 11:55:06 +0000 Subject: [PATCH 10/21] fix stroke typo from text replacing --- analysis/common_variables.py | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/analysis/common_variables.py b/analysis/common_variables.py index dfb414a..96f3263 100644 --- a/analysis/common_variables.py +++ b/analysis/common_variables.py @@ -672,7 +672,7 @@ def common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, s ), stroke_gp_feb=patients.with_these_clinical_events( stroke, - return_first_date_in_strokeriod=True, + return_first_date_in_period=True, date_format="YYYY-MM-DD", on_or_after=start_date, return_expectations={ @@ -681,7 +681,7 @@ def common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, s ), stroke_gp_mar=patients.with_these_clinical_events( stroke, - return_first_date_in_strokeriod=True, + return_first_date_in_period=True, date_format="YYYY-MM-DD", on_or_after=start_mar, return_expectations={ @@ -690,7 +690,7 @@ def common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, s ), stroke_gp_apr=patients.with_these_clinical_events( stroke, - return_first_date_in_strokeriod=True, + return_first_date_in_period=True, date_format="YYYY-MM-DD", on_or_after=start_apr, return_expectations={ @@ -699,7 +699,7 @@ def common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, s ), stroke_gp_may=patients.with_these_clinical_events( stroke, - return_first_date_in_strokeriod=True, + return_first_date_in_period=True, date_format="YYYY-MM-DD", on_or_after=start_may, return_expectations={ @@ -708,7 +708,7 @@ def common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, s ), stroke_gp_jun=patients.with_these_clinical_events( stroke, - return_first_date_in_strokeriod=True, + return_first_date_in_period=True, date_format="YYYY-MM-DD", on_or_after=start_jun, return_expectations={ @@ -717,7 +717,7 @@ def common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, s ), stroke_gp_jul=patients.with_these_clinical_events( stroke, - return_first_date_in_strokeriod=True, + return_first_date_in_period=True, date_format="YYYY-MM-DD", on_or_after=start_jul, return_expectations={ @@ -726,7 +726,7 @@ def common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, s ), stroke_gp_aug=patients.with_these_clinical_events( stroke, - return_first_date_in_strokeriod=True, + return_first_date_in_period=True, date_format="YYYY-MM-DD", on_or_after=start_aug, return_expectations={ @@ -735,7 +735,7 @@ def common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, s ), stroke_gp_sep=patients.with_these_clinical_events( stroke, - return_first_date_in_strokeriod=True, + return_first_date_in_period=True, date_format="YYYY-MM-DD", on_or_after=start_sep, return_expectations={ @@ -744,7 +744,7 @@ def common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, s ), stroke_gp_oct=patients.with_these_clinical_events( stroke, - return_first_date_in_strokeriod=True, + return_first_date_in_period=True, date_format="YYYY-MM-DD", on_or_after=start_oct, return_expectations={ @@ -756,7 +756,7 @@ def common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, s with_these_diagnoses=stroke_hospital, on_or_after=start_date, date_format="YYYY-MM-DD", - find_first_match_in_strokeriod=True, + find_first_match_in_period=True, return_expectations={ "date": {"earliest": start_date}, }, @@ -766,7 +766,7 @@ def common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, s with_these_diagnoses=stroke_hospital, on_or_after=start_mar, date_format="YYYY-MM-DD", - find_first_match_in_strokeriod=True, + find_first_match_in_period=True, return_expectations={ "date": {"earliest": start_mar}, }, @@ -776,7 +776,7 @@ def common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, s with_these_diagnoses=stroke_hospital, on_or_after=start_apr, date_format="YYYY-MM-DD", - find_first_match_in_strokeriod=True, + find_first_match_in_period=True, return_expectations={ "date": {"earliest": start_apr}, }, @@ -786,7 +786,7 @@ def common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, s with_these_diagnoses=stroke_hospital, on_or_after=start_may, date_format="YYYY-MM-DD", - find_first_match_in_strokeriod=True, + find_first_match_in_period=True, return_expectations={ "date": {"earliest": start_may}, }, @@ -796,7 +796,7 @@ def common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, s with_these_diagnoses=stroke_hospital, on_or_after=start_jun, date_format="YYYY-MM-DD", - find_first_match_in_strokeriod=True, + find_first_match_in_period=True, return_expectations={ "date": {"earliest": start_jun}, }, @@ -806,7 +806,7 @@ def common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, s with_these_diagnoses=stroke_hospital, on_or_after=start_jul, date_format="YYYY-MM-DD", - find_first_match_in_strokeriod=True, + find_first_match_in_period=True, return_expectations={ "date": {"earliest": start_jul}, }, @@ -816,7 +816,7 @@ def common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, s with_these_diagnoses=stroke_hospital, on_or_after=start_aug, date_format="YYYY-MM-DD", - find_first_match_in_strokeriod=True, + find_first_match_in_period=True, return_expectations={ "date": {"earliest": start_aug}, }, @@ -826,7 +826,7 @@ def common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, s with_these_diagnoses=stroke_hospital, on_or_after=start_sep, date_format="YYYY-MM-DD", - find_first_match_in_strokeriod=True, + find_first_match_in_period=True, return_expectations={ "date": {"earliest": start_sep}, }, @@ -836,7 +836,7 @@ def common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, s with_these_diagnoses=stroke_hospital, on_or_after=start_oct, date_format="YYYY-MM-DD", - find_first_match_in_strokeriod=True, + find_first_match_in_period=True, return_expectations={ "date": {"earliest": start_oct}, }, From e9ccf25dcbc8f73d30c66dc27d5d986388979c64 Mon Sep 17 00:00:00 2001 From: johntaz Date: Fri, 27 Nov 2020 12:12:02 +0000 Subject: [PATCH 11/21] update "with_these_medications" typo --- analysis/common_variables.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/analysis/common_variables.py b/analysis/common_variables.py index 96f3263..18755be 100644 --- a/analysis/common_variables.py +++ b/analysis/common_variables.py @@ -17,76 +17,76 @@ def common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, s date_format="YYYY-MM-DD", ), # 3mth hist of doac/warfarin use - anticoag_rx_3mths_before_start=patients.with_these_clinical_events( + anticoag_rx_3mths_before_start=patients.with_these_medications( combine_codelists(doac_codes, warfarin_codes), return_first_date_in_period=True, between=[prev_3mths_start, start_date], return_expectations={"incidence": 0.3} ), - anticoag_rx_feb=patients.with_these_clinical_events( + anticoag_rx_feb=patients.with_these_medications( combine_codelists(doac_codes, warfarin_codes), return_first_date_in_period=True, between=[start_date, start_mar], return_expectations={"incidence": 0.3} ), - anticoag_rx_mar=patients.with_these_clinical_events( + anticoag_rx_mar=patients.with_these_medications( combine_codelists(doac_codes, warfarin_codes), return_first_date_in_period=True, between=[start_mar, start_apr], return_expectations={"incidence": 0.3} ), - anticoag_rx_apr=patients.with_these_clinical_events( + anticoag_rx_apr=patients.with_these_medications( combine_codelists(doac_codes, warfarin_codes), return_first_date_in_period=True, between=[start_apr, start_may], return_expectations={"incidence": 0.3} ), - anticoag_rx_may=patients.with_these_clinical_events( + anticoag_rx_may=patients.with_these_medications( combine_codelists(doac_codes, warfarin_codes), return_first_date_in_period=True, between=[start_may, start_jun], return_expectations={"incidence": 0.3} ), - anticoag_rx_jun=patients.with_these_clinical_events( + anticoag_rx_jun=patients.with_these_medications( combine_codelists(doac_codes, warfarin_codes), return_first_date_in_period=True, between=[start_jun, start_jul], return_expectations={"incidence": 0.3} ), - anticoag_rx_jul=patients.with_these_clinical_events( + anticoag_rx_jul=patients.with_these_medications( combine_codelists(doac_codes, warfarin_codes), return_first_date_in_period=True, between=[start_jul, start_aug], return_expectations={"incidence": 0.3} ), - anticoag_rx_aug=patients.with_these_clinical_events( + anticoag_rx_aug=patients.with_these_medications( combine_codelists(doac_codes, warfarin_codes), return_first_date_in_period=True, between=[start_aug, start_sep], return_expectations={"incidence": 0.3} ), - anticoag_rx_sep=patients.with_these_clinical_events( + anticoag_rx_sep=patients.with_these_medications( combine_codelists(doac_codes, warfarin_codes), return_first_date_in_period=True, between=[start_sep, start_oct], return_expectations={"incidence": 0.3} ), - anticoag_rx_oct=patients.with_these_clinical_events( + anticoag_rx_oct=patients.with_these_medications( combine_codelists(doac_codes, warfarin_codes), return_first_date_in_period=True, between=[start_oct, end_date], return_expectations={"incidence": 0.3} - ), + ), dvt_gp_feb=patients.with_these_clinical_events( filter_codes_by_category(vte_codes_gp, include=["dvt"]), return_first_date_in_period=True, From d0c2f93c4aaae2222c32320056f18bbcb17d3d5c Mon Sep 17 00:00:00 2001 From: johntaz Date: Fri, 27 Nov 2020 14:41:25 +0000 Subject: [PATCH 12/21] update anticaog format --- analysis/common_variables.py | 40 +++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/analysis/common_variables.py b/analysis/common_variables.py index 18755be..484b07c 100644 --- a/analysis/common_variables.py +++ b/analysis/common_variables.py @@ -22,70 +22,90 @@ def common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, s warfarin_codes), return_first_date_in_period=True, between=[prev_3mths_start, start_date], - return_expectations={"incidence": 0.3} + return_expectations={ + "date": {"earliest": prev_3mths}, + } ), anticoag_rx_feb=patients.with_these_medications( combine_codelists(doac_codes, warfarin_codes), return_first_date_in_period=True, between=[start_date, start_mar], - return_expectations={"incidence": 0.3} + return_expectations={ + "date": {"earliest": prev_3mths}, + } ), anticoag_rx_mar=patients.with_these_medications( combine_codelists(doac_codes, warfarin_codes), return_first_date_in_period=True, between=[start_mar, start_apr], - return_expectations={"incidence": 0.3} + return_expectations={ + "date": {"earliest": prev_3mths}, + } ), anticoag_rx_apr=patients.with_these_medications( combine_codelists(doac_codes, warfarin_codes), return_first_date_in_period=True, between=[start_apr, start_may], - return_expectations={"incidence": 0.3} + return_expectations={ + "date": {"earliest": prev_3mths}, + } ), anticoag_rx_may=patients.with_these_medications( combine_codelists(doac_codes, warfarin_codes), return_first_date_in_period=True, between=[start_may, start_jun], - return_expectations={"incidence": 0.3} + return_expectations={ + "date": {"earliest": prev_3mths}, + } ), anticoag_rx_jun=patients.with_these_medications( combine_codelists(doac_codes, warfarin_codes), return_first_date_in_period=True, between=[start_jun, start_jul], - return_expectations={"incidence": 0.3} + return_expectations={ + "date": {"earliest": prev_3mths}, + } ), anticoag_rx_jul=patients.with_these_medications( combine_codelists(doac_codes, warfarin_codes), return_first_date_in_period=True, between=[start_jul, start_aug], - return_expectations={"incidence": 0.3} + return_expectations={ + "date": {"earliest": prev_3mths}, + } ), anticoag_rx_aug=patients.with_these_medications( combine_codelists(doac_codes, warfarin_codes), return_first_date_in_period=True, between=[start_aug, start_sep], - return_expectations={"incidence": 0.3} + return_expectations={ + "date": {"earliest": prev_3mths}, + } ), anticoag_rx_sep=patients.with_these_medications( combine_codelists(doac_codes, warfarin_codes), return_first_date_in_period=True, between=[start_sep, start_oct], - return_expectations={"incidence": 0.3} + return_expectations={ + "date": {"earliest": prev_3mths}, + } ), anticoag_rx_oct=patients.with_these_medications( combine_codelists(doac_codes, warfarin_codes), return_first_date_in_period=True, between=[start_oct, end_date], - return_expectations={"incidence": 0.3} + return_expectations={ + "date": {"earliest": prev_3mths}, + } ), dvt_gp_feb=patients.with_these_clinical_events( filter_codes_by_category(vte_codes_gp, include=["dvt"]), From 1b2d790679b1d27cc62eb7d2e24dce4a31cc1edd Mon Sep 17 00:00:00 2001 From: johntaz Date: Fri, 27 Nov 2020 14:42:59 +0000 Subject: [PATCH 13/21] update anticaog dates --- analysis/common_variables.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/analysis/common_variables.py b/analysis/common_variables.py index 484b07c..fba82d7 100644 --- a/analysis/common_variables.py +++ b/analysis/common_variables.py @@ -32,7 +32,7 @@ def common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, s return_first_date_in_period=True, between=[start_date, start_mar], return_expectations={ - "date": {"earliest": prev_3mths}, + "date": {"earliest": start_date}, } ), anticoag_rx_mar=patients.with_these_medications( @@ -41,7 +41,7 @@ def common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, s return_first_date_in_period=True, between=[start_mar, start_apr], return_expectations={ - "date": {"earliest": prev_3mths}, + "date": {"earliest": start_mar}, } ), anticoag_rx_apr=patients.with_these_medications( @@ -50,7 +50,7 @@ def common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, s return_first_date_in_period=True, between=[start_apr, start_may], return_expectations={ - "date": {"earliest": prev_3mths}, + "date": {"earliest": start_apr}, } ), anticoag_rx_may=patients.with_these_medications( @@ -59,7 +59,7 @@ def common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, s return_first_date_in_period=True, between=[start_may, start_jun], return_expectations={ - "date": {"earliest": prev_3mths}, + "date": {"earliest": start_may}, } ), anticoag_rx_jun=patients.with_these_medications( @@ -68,7 +68,7 @@ def common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, s return_first_date_in_period=True, between=[start_jun, start_jul], return_expectations={ - "date": {"earliest": prev_3mths}, + "date": {"earliest": start_jun}, } ), anticoag_rx_jul=patients.with_these_medications( @@ -77,7 +77,7 @@ def common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, s return_first_date_in_period=True, between=[start_jul, start_aug], return_expectations={ - "date": {"earliest": prev_3mths}, + "date": {"earliest": start_jul}, } ), anticoag_rx_aug=patients.with_these_medications( @@ -86,7 +86,7 @@ def common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, s return_first_date_in_period=True, between=[start_aug, start_sep], return_expectations={ - "date": {"earliest": prev_3mths}, + "date": {"earliest": start_aug}, } ), anticoag_rx_sep=patients.with_these_medications( @@ -95,7 +95,7 @@ def common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, s return_first_date_in_period=True, between=[start_sep, start_oct], return_expectations={ - "date": {"earliest": prev_3mths}, + "date": {"earliest": start_sep}, } ), anticoag_rx_oct=patients.with_these_medications( @@ -104,7 +104,7 @@ def common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, s return_first_date_in_period=True, between=[start_oct, end_date], return_expectations={ - "date": {"earliest": prev_3mths}, + "date": {"earliest": start_oct}, } ), dvt_gp_feb=patients.with_these_clinical_events( From b61101d00aa636abf9fc7f14d280a37ecfdb8662 Mon Sep 17 00:00:00 2001 From: johntaz Date: Fri, 27 Nov 2020 14:45:52 +0000 Subject: [PATCH 14/21] typo --- analysis/common_variables.py | 4 ++-- analysis/study_definition_control_2019.py | 4 ++-- analysis/study_definition_control_2020.py | 4 ++-- analysis/study_definition_covid.py | 4 ++-- analysis/study_definition_pneumonia.py | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/analysis/common_variables.py b/analysis/common_variables.py index fba82d7..17789e0 100644 --- a/analysis/common_variables.py +++ b/analysis/common_variables.py @@ -9,7 +9,7 @@ def days_before(s, days): return datetime.strftime(modified_date, "%Y-%m-%d") -def common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct, end_date): +def common_variable_define(prev_3mths, start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct, end_date): common_variables = dict( af=patients.with_these_clinical_events( af_codes, @@ -21,7 +21,7 @@ def common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, s combine_codelists(doac_codes, warfarin_codes), return_first_date_in_period=True, - between=[prev_3mths_start, start_date], + between=[prev_3mths, start_date], return_expectations={ "date": {"earliest": prev_3mths}, } diff --git a/analysis/study_definition_control_2019.py b/analysis/study_definition_control_2019.py index 12603fd..6381421 100644 --- a/analysis/study_definition_control_2019.py +++ b/analysis/study_definition_control_2019.py @@ -3,7 +3,7 @@ from codelists import * -prev_3mths_start = "2018-11-01" +prev_3mths = "2018-11-01" start_date = "2019-02-01" start_mar = "2019-03-01" start_apr = "2019-04-01" @@ -15,7 +15,7 @@ start_oct = "2019-10-01" end_date = "2019-11-01" -common_variables = common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct, end_date) +common_variables = common_variable_define(prev_3mths, start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct, end_date) study = StudyDefinition( default_expectations={ diff --git a/analysis/study_definition_control_2020.py b/analysis/study_definition_control_2020.py index 43d9b3d..17584ea 100644 --- a/analysis/study_definition_control_2020.py +++ b/analysis/study_definition_control_2020.py @@ -3,7 +3,7 @@ from codelists import * -prev_3mths_start = "2019-11-01" +prev_3mths = "2019-11-01" start_date = "2020-02-01" start_mar = "2020-03-01" start_apr = "2020-04-01" @@ -15,7 +15,7 @@ start_oct = "2020-10-01" end_date = "2020-11-01" -common_variables = common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct, end_date) +common_variables = common_variable_define(prev_3mths, start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct, end_date) study = StudyDefinition( default_expectations={ diff --git a/analysis/study_definition_covid.py b/analysis/study_definition_covid.py index bb9d768..e633210 100644 --- a/analysis/study_definition_covid.py +++ b/analysis/study_definition_covid.py @@ -2,7 +2,7 @@ from common_variables import common_variable_define from codelists import * -prev_3mths_start = "2019-11-01" +prev_3mths = "2019-11-01" start_date = "2020-02-01" start_mar = "2020-03-01" start_apr = "2020-04-01" @@ -14,7 +14,7 @@ start_oct = "2020-10-01" end_date = "2020-11-01" -common_variables = common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct, end_date) +common_variables = common_variable_define(prev_3mths, start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct, end_date) study = StudyDefinition( default_expectations={ diff --git a/analysis/study_definition_pneumonia.py b/analysis/study_definition_pneumonia.py index ddb2e39..4db6938 100644 --- a/analysis/study_definition_pneumonia.py +++ b/analysis/study_definition_pneumonia.py @@ -2,7 +2,7 @@ from common_variables import common_variable_define from codelists import * -prev_3mths_start = "2018-11-01" +prev_3mths = "2018-11-01" start_date = "2019-02-01" start_mar = "2019-03-01" start_apr = "2019-04-01" @@ -14,7 +14,7 @@ start_oct = "2019-10-01" end_date = "2019-11-01" -common_variables = common_variable_define(prev_3mths_start, start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct, end_date) +common_variables = common_variable_define(prev_3mths, start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct, end_date) study = StudyDefinition( default_expectations={ From fed56ac13dd948e6fe8fd9bc897318f927decd66 Mon Sep 17 00:00:00 2001 From: johntaz Date: Fri, 27 Nov 2020 15:06:48 +0000 Subject: [PATCH 15/21] updates to common_variable_define In order to get more granular anticoag prescription recording --- analysis/common_variables.py | 26 +++++++++++++++++++---- analysis/study_definition_control_2019.py | 7 ++++-- analysis/study_definition_control_2020.py | 6 ++++-- analysis/study_definition_covid.py | 6 ++++-- analysis/study_definition_pneumonia.py | 6 ++++-- 5 files changed, 39 insertions(+), 12 deletions(-) diff --git a/analysis/common_variables.py b/analysis/common_variables.py index 17789e0..ed0e2ee 100644 --- a/analysis/common_variables.py +++ b/analysis/common_variables.py @@ -9,7 +9,7 @@ def days_before(s, days): return datetime.strftime(modified_date, "%Y-%m-%d") -def common_variable_define(prev_3mths, start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct, end_date): +def common_variable_define(start_jan, prev_nov, prev_dec, start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct, end_date): common_variables = dict( af=patients.with_these_clinical_events( af_codes, @@ -17,13 +17,31 @@ def common_variable_define(prev_3mths, start_date, start_mar, start_apr, start_m date_format="YYYY-MM-DD", ), # 3mth hist of doac/warfarin use - anticoag_rx_3mths_before_start=patients.with_these_medications( + anticoag_rx_prev_nov=patients.with_these_medications( combine_codelists(doac_codes, warfarin_codes), return_first_date_in_period=True, - between=[prev_3mths, start_date], + between=[prev_nov, prev_dec], return_expectations={ - "date": {"earliest": prev_3mths}, + "date": {"earliest": prev_nov}, + } + ), + anticoag_rx_prev_dec=patients.with_these_medications( + combine_codelists(doac_codes, + warfarin_codes), + return_first_date_in_period=True, + between=[prev_dec, start_jan], + return_expectations={ + "date": {"earliest": prev_dec}, + } + ), + anticoag_rx_jan=patients.with_these_medications( + combine_codelists(doac_codes, + warfarin_codes), + return_first_date_in_period=True, + between=[start_jan, start_date], + return_expectations={ + "date": {"earliest": start_jan}, } ), anticoag_rx_feb=patients.with_these_medications( diff --git a/analysis/study_definition_control_2019.py b/analysis/study_definition_control_2019.py index 6381421..9dac3d8 100644 --- a/analysis/study_definition_control_2019.py +++ b/analysis/study_definition_control_2019.py @@ -3,7 +3,10 @@ from codelists import * -prev_3mths = "2018-11-01" + +prev_nov = "2017-11-01" +prev_dec = "2018-12-01" +start_jan = "2019-01-01" start_date = "2019-02-01" start_mar = "2019-03-01" start_apr = "2019-04-01" @@ -15,7 +18,7 @@ start_oct = "2019-10-01" end_date = "2019-11-01" -common_variables = common_variable_define(prev_3mths, start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct, end_date) +common_variables = common_variable_define(start_jan, prev_nov, prev_dec, start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct, end_date) study = StudyDefinition( default_expectations={ diff --git a/analysis/study_definition_control_2020.py b/analysis/study_definition_control_2020.py index 17584ea..142a074 100644 --- a/analysis/study_definition_control_2020.py +++ b/analysis/study_definition_control_2020.py @@ -3,7 +3,9 @@ from codelists import * -prev_3mths = "2019-11-01" +prev_nov = "2019-11-01" +prev_dec = "2019-12-01" +start_jan = "2020-01-01" start_date = "2020-02-01" start_mar = "2020-03-01" start_apr = "2020-04-01" @@ -15,7 +17,7 @@ start_oct = "2020-10-01" end_date = "2020-11-01" -common_variables = common_variable_define(prev_3mths, start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct, end_date) +common_variables = common_variable_define(start_jan, prev_nov, prev_dec, start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct, end_date) study = StudyDefinition( default_expectations={ diff --git a/analysis/study_definition_covid.py b/analysis/study_definition_covid.py index e633210..af56759 100644 --- a/analysis/study_definition_covid.py +++ b/analysis/study_definition_covid.py @@ -2,7 +2,9 @@ from common_variables import common_variable_define from codelists import * -prev_3mths = "2019-11-01" +prev_nov = "2019-11-01" +prev_dec = "2019-12-01" +start_jan = "2020-01-01" start_date = "2020-02-01" start_mar = "2020-03-01" start_apr = "2020-04-01" @@ -14,7 +16,7 @@ start_oct = "2020-10-01" end_date = "2020-11-01" -common_variables = common_variable_define(prev_3mths, start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct, end_date) +common_variables = common_variable_define(start_jan, prev_nov, prev_dec, start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct, end_date) study = StudyDefinition( default_expectations={ diff --git a/analysis/study_definition_pneumonia.py b/analysis/study_definition_pneumonia.py index 4db6938..7db3c2a 100644 --- a/analysis/study_definition_pneumonia.py +++ b/analysis/study_definition_pneumonia.py @@ -2,7 +2,9 @@ from common_variables import common_variable_define from codelists import * -prev_3mths = "2018-11-01" +prev_nov = "2018-11-01" +prev_dec = "2018-12-01" +start_jan = "2019-01-01" start_date = "2019-02-01" start_mar = "2019-03-01" start_apr = "2019-04-01" @@ -14,7 +16,7 @@ start_oct = "2019-10-01" end_date = "2019-11-01" -common_variables = common_variable_define(prev_3mths, start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct, end_date) +common_variables = common_variable_define(start_jan, prev_nov, prev_dec, start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct, end_date) study = StudyDefinition( default_expectations={ From d8066d2b72178a694d318e2295192cc6a81c3cdc Mon Sep 17 00:00:00 2001 From: johntaz Date: Mon, 30 Nov 2020 09:54:04 +0000 Subject: [PATCH 16/21] stratified and update outcome definitions added --- .../000_cr_define_covariates_simple_rates.do | 201 ++++++++++++++++-- analysis/201_cr_simple_rates.do | 71 ++++++- 2 files changed, 248 insertions(+), 24 deletions(-) diff --git a/analysis/000_cr_define_covariates_simple_rates.do b/analysis/000_cr_define_covariates_simple_rates.do index 0f5fcb7..b3c18ad 100644 --- a/analysis/000_cr_define_covariates_simple_rates.do +++ b/analysis/000_cr_define_covariates_simple_rates.do @@ -36,11 +36,28 @@ drop if discharged_expo_date ==. drop if discharged_expo_date > $dataEndDate drop if discharged_expo_date < hospitalised_expo_date + +* Hospitalised covid/pneumonia is primary dx +gen hospitalised_expo_primary_dx = date(exposure_hosp_primary_dx, "YMD") +format hospitalised_expo_primary_dx %td + +gen community_exp = cond(hospitalised_expo_primary_dx !=. , 1, 0) + +* Length of stay +gen length_of_stay = discharged_expo_date - hospitalised_expo_date + 1 +label var length_of_stay "Length of stay in hospital (days)" +hist length , name(length_of_stay_$group, replace) graphregion(color(white)) col(navy%50) ylab(,angle(h)) lcol(navy%20) +graph export "output/length_of_stay_$group.png" , replace + +* Create flag for patients staying in hospital longer than the median length +summ length, detail +gen long_hosp_stay = cond(length_of_stay >= `r(p50)' , 1, 0) + * for matching gen exposed = 1 -gen indexdate= discharged_expo_date +gen indexdate= hospitalised_expo_date format indexdate %td -gen indexMonth = month(discharged_expo_date) +gen indexMonth = month(hospitalised_expo_date) gen flag = "$group" @@ -50,14 +67,15 @@ gen flag = "$group" ****************************** * To be added: dates related to outcomes -foreach var of varlist dvt_gp /// - pe_gp /// - other_vte_gp /// - dvt_hospital /// - pe_hospital /// +foreach var of varlist af /// + date_icu_admission /// + dvt_gp_* /// + pe_gp_* /// + dvt_hospital_* /// + pe_hospital_* /// other_vte_hospital /// - stroke_gp /// - stroke_hospital /// + stroke_gp_* /// + stroke_hospital_* /// died_date_ons /// bmi_date_measured /// hypertension /// @@ -85,7 +103,7 @@ capture confirm string variable `var' format `var'_date %td } - +rename date_icu_admission_date icu_admission_date /* BMI */ * Set implausible BMIs to missing: @@ -189,7 +207,8 @@ drop region_string ************************** /* Age variables */ - +drop if age < 18 +drop if age > 110 * Create categorised age recode age 18/39.9999=1 /// 40/49.9999=2 /// @@ -197,7 +216,7 @@ recode age 18/39.9999=1 /// 60/69.9999=4 /// 70/79.9999=5 /// 80/max=6, /// - gen(agegroup) + gen(agegroup) label define agegroup 1 "18-<40" /// 2 "40-<50" /// @@ -304,9 +323,153 @@ label define hba1ccat 0 "<6.5%" /// * Delete unneeded variables drop hba1c_pct hba1c_percentage_1 hba1c_mmol_per_mol_1 +***************** +* History of AF * +***************** + +gen hist_of_af = cond(af < hospitalised_expo_date, 1, 0) + +*************************** +* Hist of anticoagulation * +*************************** + +gen hist_of_anticoag = . + +levelsof indexMonth, local(months) +foreach m of local months { + +* Feb +if `m' == 2 { +replace hist_of_anticoag = 1 if anticoag_rx_jan !=. | /// + anticoag_rx_prev_nov !=. | /// + anticoag_rx_prev_dec !=. & /// + indexMonth == 2 +} +* Mar +if `m' == 3 { +replace hist_of_anticoag = 1 if anticoag_rx_jan !=. | /// + anticoag_rx_feb !=. | /// + anticoag_rx_prev_dec !=. & /// + indexMonth == 3 +} +* Apr +if `m' == 4 { +replace hist_of_anticoag = 1 if anticoag_rx_jan !=. | /// + anticoag_rx_feb !=. | /// + anticoag_rx_mar !=. & /// + indexMonth == 4 +} + +* May +if `m' == 5 { +replace hist_of_anticoag = 1 if anticoag_rx_feb !=. | /// + anticoag_rx_mar !=. | /// + anticoag_rx_apr !=. & /// + indexMonth == 5 +} + +* Jun +if `m' == 6 { +replace hist_of_anticoag = 1 if anticoag_rx_mar !=. | /// + anticoag_rx_apr !=. | /// + anticoag_rx_may !=. & /// + indexMonth == 6 +} + +* Jul +if `m' == 7 { +replace hist_of_anticoag = 1 if anticoag_rx_apr !=. | /// + anticoag_rx_may !=. | /// + anticoag_rx_jun !=. & /// + indexMonth == 7 +} + +* Aug +if `m' == 8 { +replace hist_of_anticoag = 1 if anticoag_rx_may !=. | /// + anticoag_rx_jun !=. | /// + anticoag_rx_jul !=. & /// + indexMonth == 8 +} + +* Sep +if `m' == 9 { +replace hist_of_anticoag = 1 if anticoag_rx_jun !=. | /// + anticoag_rx_jul !=. | /// + anticoag_rx_aug !=. & /// + indexMonth == 9 +} + +* Oct +if `m' == 10 { +replace hist_of_anticoag = 1 if anticoag_rx_jul !=. | /// + anticoag_rx_aug !=. | /// + anticoag_rx_sep !=. & /// + indexMonth == 10 +} + +} + +replace hist_of_anticoag = 0 if hist_of_anticoag == . + + + +****************************************** +* Admitted to ICU during hospitalisation * +****************************************** + +gen icu_admission = cond( icu_admission_date >= hospitalised_expo_date & icu_admission_date <= discharged_expo_date , 1, 0) + + ************** * Outcomes * -************** +************** +foreach o in stroke dvt pe { + + * Set all dates which are less than the hospitalised date to missing + foreach v in feb mar apr may jun jul aug sep oct { + replace `o'_gp_`v'_date =. if `o'_gp_`v'_date < hospitalised_expo_date + replace `o'_hospital_`v'_date =. if `o'_gp_`v'_date < hospitalised_expo_date + } + * Select the minimum date of the dates as the outcome + gen `o'_gp = min(`o'_gp_feb_date, /// + `o'_gp_mar_date, /// + `o'_gp_apr_date, /// + `o'_gp_may_date, /// + `o'_gp_jun_date, /// + `o'_gp_jul_date, /// + `o'_gp_aug_date, /// + `o'_gp_sep_date, /// + `o'_gp_oct_date) + format `o'_gp %td + + gen `o'_hospital = min(`o'_hospital_feb_date, /// + `o'_hospital_mar_date, /// + `o'_hospital_apr_date, /// + `o'_hospital_may_date, /// + `o'_hospital_jun_date, /// + `o'_hospital_jul_date, /// + `o'_hospital_aug_date, /// + `o'_hospital_sep_date, /// + `o'_hospital_oct_date) + format `o'_hospital %td + + * For ONS they will be dropped below if they have died before hospitalisation + * This just picks up the min value as this doesn't represent an exact YYYY-MM-DD date + gen `o'_ons = min(`o'_hospital_feb_date, /// + `o'_hospital_mar_date, /// + `o'_hospital_apr_date, /// + `o'_hospital_may_date, /// + `o'_hospital_jun_date, /// + `o'_hospital_jul_date, /// + `o'_hospital_aug_date, /// + `o'_hospital_sep_date, /// + `o'_hospital_oct_date) + + + +} + * Note: There may be deaths recorded after end of our study (08 Oct) * Set these to missing replace died_date_ons_date = . if died_date_ons_date>td(01oct2020) @@ -368,7 +531,17 @@ foreach out in stroke dvt pe { } - +**** Tidy dataset +keep patient_id died_date_ons_date age ethnicity hospitalised_expo_date /// + discharged_expo_date community_exp long_hosp_stay gender agegroup hist_of_af /// + hist_of_anticoag icu_admission stroke_gp stroke_hospital stroke_ons dvt_gp /// + dvt_hospital dvt_ons pe_gp pe_hospital pe_ons hist_stroke hist_dvt hist_pe /// + stroke_in_hosp stroke_in_hosp_end_date stroke_post_hosp stroke_post_hosp_end_date /// + stroke_post_hosp_gp stroke_post_hosp_gp_end_date dvt_in_hosp dvt_in_hosp_end_date /// + dvt_post_hosp dvt_post_hosp_end_date dvt_post_hosp_gp dvt_post_hosp_gp_end_date /// + pe_in_hosp pe_in_hosp_end_date pe_post_hosp pe_post_hosp_end_date pe_post_hosp_gp /// + pe_post_hosp_gp_end_date + save "data/cohort_rates_$group", replace diff --git a/analysis/201_cr_simple_rates.do b/analysis/201_cr_simple_rates.do index 65056f1..9d6fbc6 100644 --- a/analysis/201_cr_simple_rates.do +++ b/analysis/201_cr_simple_rates.do @@ -21,7 +21,7 @@ use "data/cohort_rates_$group", replace tempname measures - postfile `measures' str13(group) str12(outcome) str12(analysis) str20(variable) category personTime numEvents rate lc uc using "data/rates_summary_$group", replace + postfile `measures' str16(group) str20(outcome) str12(analysis) str20(history) level str20(variable) category personTime numEvents rate lc uc using "data/rates_summary_$group", replace foreach v in stroke dvt pe { @@ -35,11 +35,11 @@ foreach v in stroke dvt pe { * Overall rate stptime * Save measure - post `measures' ("$group") ("Overall") ("in_hosp") ("") (0) (`r(ptime)') /// + post `measures' ("$group") ("`v'") ("in_hosp") ("") (0) ("Overall") (0) (`r(ptime)') /// (`r(failures)') (`r(rate)') /// (`r(lb)') (`r(ub))') - * Stratified + * Stratified by history of... foreach c in hist_`v' { qui levelsof `c' , local(cats) di `cats' @@ -49,9 +49,37 @@ foreach v in stroke dvt pe { stptime if `c'==`l' * Save measures - post `measures' ("$group") ("`v'") ("in_hosp") ("`c'") (`l') (`r(ptime)') /// + post `measures' ("$group") ("`v'") ("in_hosp") ("`c'") (`l') ("History of") (`l') (`r(ptime)') /// (`r(failures)') (`r(rate)') /// (`r(lb)') (`r(ub))') + + + * Further stratified by... + + foreach s in agegroup gender ethnicity hist_of_af hist_of_anticoag long_hosp_stay icu_admission community_exp { + qui levelsof `s' , local(cats) + di `cats' + foreach t of local cats { + noi di "$group, `c' level `l' : Calculate rate for variable `s' and level `t'" + + qui count if `c' ==`l' & `s' ==`t' + if `r(N)' > 0 { + stptime if `c'==`l' & `s' == `t' + * Save measures + post `measures' ("$group") ("`v'") ("in_hosp") ("`c'") (`l') ("`s'") (`t') (`r(ptime)') /// + (`r(failures)') (`r(rate)') /// + (`r(lb)') (`r(ub))') + } + else { + post `measures' ("$group") ("`v'") ("in_hosp") ("`c'") (`l') ("`s'") (`t') (.) /// + (.) (.) /// + (.) (.) + } + + + } + } + } } @@ -64,7 +92,7 @@ foreach v in stroke dvt pe { * Overall rate stptime * Save measure - post `measures' ("$group") ("Overall") ("`a'") ("") (0) (`r(ptime)') /// + post `measures' ("$group") ("`v'") ("`a'") ("") (0) ("Overall") (0) (`r(ptime)') /// (`r(failures)') (`r(rate)') /// (`r(lb)') (`r(ub))') @@ -78,9 +106,35 @@ foreach v in stroke dvt pe { stptime if `c'==`l' * Save measures - post `measures' ("$group") ("`v'") ("`a'") ("`c'") (`l') (`r(ptime)') /// + post `measures' ("$group") ("`v'") ("`a'") ("`c'") (`l') ("History of") (`l') (`r(ptime)') /// (`r(failures)') (`r(rate)') /// (`r(lb)') (`r(ub))') + + * Further stratified by... + + foreach s in agegroup gender ethnicity hist_of_af hist_of_anticoag long_hosp_stay icu_admission community_exp { + qui levelsof `s' , local(cats) + di `cats' + foreach t of local cats { + noi di "$group, `c' level `l' : Calculate rate for variable `s' and level `t'" + + qui count if `c' ==`l' & `s' ==`t' + if `r(N)' > 0 { + stptime if `c'==`l' & `s' == `t' + * Save measures + post `measures' ("$group") ("`v'") ("`a'") ("`c'") (`l') ("`s'") (`t') (`r(ptime)') /// + (`r(failures)') (`r(rate)') /// + (`r(lb)') (`r(ub))') + } + else { + post `measures' ("$group") ("`v'") ("`a'") ("`c'") (`l') ("`s'") (`t') (.) /// + (.) (.) /// + (.) (.) + } + + } + } + } } } @@ -96,8 +150,5 @@ gen rate_ppm = 100*(rate * 365.25 / 12) gen lc_ppm = 100*(lc * 365.25 /12) gen uc_ppm = 100*(uc * 365.25 /12) - - -* per perso export delimited using "data/rates_summary_$group.csv", replace -erase "data/rates_summary_$group.dta" + From 03edd8185d17ff2b43c63be36853849890b70bf3 Mon Sep 17 00:00:00 2001 From: johntaz Date: Mon, 30 Nov 2020 17:26:08 +0000 Subject: [PATCH 17/21] Removed additional stratification by history of outcome. Removed community acquired stratfier --- analysis/201_cr_simple_rates.do | 76 +++------ ...cr_simple_rates_old_full_stratification.do | 154 ++++++++++++++++++ 2 files changed, 174 insertions(+), 56 deletions(-) create mode 100644 analysis/201_cr_simple_rates_old_full_stratification.do diff --git a/analysis/201_cr_simple_rates.do b/analysis/201_cr_simple_rates.do index 9d6fbc6..76cc108 100644 --- a/analysis/201_cr_simple_rates.do +++ b/analysis/201_cr_simple_rates.do @@ -21,7 +21,7 @@ use "data/cohort_rates_$group", replace tempname measures - postfile `measures' str16(group) str20(outcome) str12(analysis) str20(history) level str20(variable) category personTime numEvents rate lc uc using "data/rates_summary_$group", replace + postfile `measures' str16(group) str20(outcome) str12(analysis) str20(variable) category personTime numEvents rate lc uc using "data/rates_summary_$group", replace foreach v in stroke dvt pe { @@ -35,50 +35,31 @@ foreach v in stroke dvt pe { * Overall rate stptime * Save measure - post `measures' ("$group") ("`v'") ("in_hosp") ("") (0) ("Overall") (0) (`r(ptime)') /// + post `measures' ("$group") ("`v'") ("in_hosp") ("Overall") (0) (`r(ptime)') /// (`r(failures)') (`r(rate)') /// (`r(lb)') (`r(ub))') * Stratified by history of... - foreach c in hist_`v' { + foreach c in hist_`v' agegroup gender ethnicity hist_of_af hist_of_anticoag long_hosp_stay icu_admission { qui levelsof `c' , local(cats) di `cats' foreach l of local cats { noi di "$group: Calculate rate for variable `c' and level `l'" - + qui count if `c' ==`l' + if `r(N)' > 0 { stptime if `c'==`l' * Save measures - post `measures' ("$group") ("`v'") ("in_hosp") ("`c'") (`l') ("History of") (`l') (`r(ptime)') /// + post `measures' ("$group") ("`v'") ("in_hosp") ("`c'") (`l') (`r(ptime)') /// (`r(failures)') (`r(rate)') /// - (`r(lb)') (`r(ub))') - - - * Further stratified by... - - foreach s in agegroup gender ethnicity hist_of_af hist_of_anticoag long_hosp_stay icu_admission community_exp { - qui levelsof `s' , local(cats) - di `cats' - foreach t of local cats { - noi di "$group, `c' level `l' : Calculate rate for variable `s' and level `t'" - - qui count if `c' ==`l' & `s' ==`t' - if `r(N)' > 0 { - stptime if `c'==`l' & `s' == `t' - * Save measures - post `measures' ("$group") ("`v'") ("in_hosp") ("`c'") (`l') ("`s'") (`t') (`r(ptime)') /// - (`r(failures)') (`r(rate)') /// - (`r(lb)') (`r(ub))') - } - else { - post `measures' ("$group") ("`v'") ("in_hosp") ("`c'") (`l') ("`s'") (`t') (.) /// + (`r(lb)') (`r(ub))') + } + else { + post `measures' ("$group") ("`v'") ("`a'") ("`c'") (`l') (.) /// (.) (.) /// (.) (.) - } - + } - } - } } } @@ -92,47 +73,30 @@ foreach v in stroke dvt pe { * Overall rate stptime * Save measure - post `measures' ("$group") ("`v'") ("`a'") ("") (0) ("Overall") (0) (`r(ptime)') /// + post `measures' ("$group") ("`v'") ("`a'") ("Overall") (0) (`r(ptime)') /// (`r(failures)') (`r(rate)') /// (`r(lb)') (`r(ub))') * Stratified - foreach c in hist_`v' { + foreach c in hist_`v' agegroup gender ethnicity hist_of_af hist_of_anticoag long_hosp_stay icu_admission { qui levelsof `c' , local(cats) di `cats' foreach l of local cats { noi di "$group: Calculate rate for variable `c' and level `l'" - + qui count if `c' ==`l' + if `r(N)' > 0 { stptime if `c'==`l' * Save measures - post `measures' ("$group") ("`v'") ("`a'") ("`c'") (`l') ("History of") (`l') (`r(ptime)') /// + post `measures' ("$group") ("`v'") ("`a'") ("`c'") (`l') (`r(ptime)') /// (`r(failures)') (`r(rate)') /// (`r(lb)') (`r(ub))') - - * Further stratified by... - - foreach s in agegroup gender ethnicity hist_of_af hist_of_anticoag long_hosp_stay icu_admission community_exp { - qui levelsof `s' , local(cats) - di `cats' - foreach t of local cats { - noi di "$group, `c' level `l' : Calculate rate for variable `s' and level `t'" - - qui count if `c' ==`l' & `s' ==`t' - if `r(N)' > 0 { - stptime if `c'==`l' & `s' == `t' - * Save measures - post `measures' ("$group") ("`v'") ("`a'") ("`c'") (`l') ("`s'") (`t') (`r(ptime)') /// - (`r(failures)') (`r(rate)') /// - (`r(lb)') (`r(ub))') - } - else { - post `measures' ("$group") ("`v'") ("`a'") ("`c'") (`l') ("`s'") (`t') (.) /// + } + + else { + post `measures' ("$group") ("`v'") ("`a'") ("`c'") (`l') (.) /// (.) (.) /// (.) (.) - } - - } } } diff --git a/analysis/201_cr_simple_rates_old_full_stratification.do b/analysis/201_cr_simple_rates_old_full_stratification.do new file mode 100644 index 0000000..9d6fbc6 --- /dev/null +++ b/analysis/201_cr_simple_rates_old_full_stratification.do @@ -0,0 +1,154 @@ +******************************************************************************** +* +* Do-file: 201_cr_simple_rates.do +* +* Programmed by: John & Alex +* +* Data used: None +* +* Data created: None +* +* Other output: None +* +******************************************************************************** +* +* Purpose: +* +* Note: +******************************************************************************** + + +use "data/cohort_rates_$group", replace + +tempname measures + postfile `measures' str16(group) str20(outcome) str12(analysis) str20(history) level str20(variable) category personTime numEvents rate lc uc using "data/rates_summary_$group", replace + + +foreach v in stroke dvt pe { + + noi di "Starting analysis for $group: `v' Outcome ..." + noi di "$group: stset in hospital" + + stset `v'_in_hosp_end_date , id(patient_id) failure(`v'_in_hosp) enter(hospitalised_expo_date) + + + * Overall rate + stptime + * Save measure + post `measures' ("$group") ("`v'") ("in_hosp") ("") (0) ("Overall") (0) (`r(ptime)') /// + (`r(failures)') (`r(rate)') /// + (`r(lb)') (`r(ub))') + + * Stratified by history of... + foreach c in hist_`v' { + qui levelsof `c' , local(cats) + di `cats' + foreach l of local cats { + noi di "$group: Calculate rate for variable `c' and level `l'" + + + stptime if `c'==`l' + * Save measures + post `measures' ("$group") ("`v'") ("in_hosp") ("`c'") (`l') ("History of") (`l') (`r(ptime)') /// + (`r(failures)') (`r(rate)') /// + (`r(lb)') (`r(ub))') + + + * Further stratified by... + + foreach s in agegroup gender ethnicity hist_of_af hist_of_anticoag long_hosp_stay icu_admission community_exp { + qui levelsof `s' , local(cats) + di `cats' + foreach t of local cats { + noi di "$group, `c' level `l' : Calculate rate for variable `s' and level `t'" + + qui count if `c' ==`l' & `s' ==`t' + if `r(N)' > 0 { + stptime if `c'==`l' & `s' == `t' + * Save measures + post `measures' ("$group") ("`v'") ("in_hosp") ("`c'") (`l') ("`s'") (`t') (`r(ptime)') /// + (`r(failures)') (`r(rate)') /// + (`r(lb)') (`r(ub))') + } + else { + post `measures' ("$group") ("`v'") ("in_hosp") ("`c'") (`l') ("`s'") (`t') (.) /// + (.) (.) /// + (.) (.) + } + + + } + } + + } + } + + foreach a in post_hosp post_hosp_gp { + + noi di "$group: stset in `a'" + + stset `v'_`a'_end_date , id(patient_id) failure(`v'_`a') enter(discharged_expo_date) + + * Overall rate + stptime + * Save measure + post `measures' ("$group") ("`v'") ("`a'") ("") (0) ("Overall") (0) (`r(ptime)') /// + (`r(failures)') (`r(rate)') /// + (`r(lb)') (`r(ub))') + + * Stratified + foreach c in hist_`v' { + qui levelsof `c' , local(cats) + di `cats' + foreach l of local cats { + noi di "$group: Calculate rate for variable `c' and level `l'" + + stptime if `c'==`l' + + * Save measures + post `measures' ("$group") ("`v'") ("`a'") ("`c'") (`l') ("History of") (`l') (`r(ptime)') /// + (`r(failures)') (`r(rate)') /// + (`r(lb)') (`r(ub))') + + * Further stratified by... + + foreach s in agegroup gender ethnicity hist_of_af hist_of_anticoag long_hosp_stay icu_admission community_exp { + qui levelsof `s' , local(cats) + di `cats' + foreach t of local cats { + noi di "$group, `c' level `l' : Calculate rate for variable `s' and level `t'" + + qui count if `c' ==`l' & `s' ==`t' + if `r(N)' > 0 { + stptime if `c'==`l' & `s' == `t' + * Save measures + post `measures' ("$group") ("`v'") ("`a'") ("`c'") (`l') ("`s'") (`t') (`r(ptime)') /// + (`r(failures)') (`r(rate)') /// + (`r(lb)') (`r(ub))') + } + else { + post `measures' ("$group") ("`v'") ("`a'") ("`c'") (`l') ("`s'") (`t') (.) /// + (.) (.) /// + (.) (.) + } + + } + } + + } + } + } +} + +postclose `measures' + +* Change postfiles to csv +use "data/rates_summary_$group", replace + +* Change from per person-day to per 100 person-months +gen rate_ppm = 100*(rate * 365.25 / 12) +gen lc_ppm = 100*(lc * 365.25 /12) +gen uc_ppm = 100*(uc * 365.25 /12) + +export delimited using "data/rates_summary_$group.csv", replace + From 2cfb5e41f816b487bde33fbcd60b8c2dec08b844 Mon Sep 17 00:00:00 2001 From: johntaz Date: Mon, 30 Nov 2020 17:33:31 +0000 Subject: [PATCH 18/21] update graphs to .svg --- analysis/000_cr_define_covariates_simple_rates.do | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/analysis/000_cr_define_covariates_simple_rates.do b/analysis/000_cr_define_covariates_simple_rates.do index b3c18ad..cbc3348 100644 --- a/analysis/000_cr_define_covariates_simple_rates.do +++ b/analysis/000_cr_define_covariates_simple_rates.do @@ -47,7 +47,7 @@ gen community_exp = cond(hospitalised_expo_primary_dx !=. , 1, 0) gen length_of_stay = discharged_expo_date - hospitalised_expo_date + 1 label var length_of_stay "Length of stay in hospital (days)" hist length , name(length_of_stay_$group, replace) graphregion(color(white)) col(navy%50) ylab(,angle(h)) lcol(navy%20) -graph export "output/length_of_stay_$group.png" , replace +graph export "output/length_of_stay_$group.svg" , as(svg) replace * Create flag for patients staying in hospital longer than the median length summ length, detail From 55fe9a534ceed9e7e2039666ce7943fdd4ac71db Mon Sep 17 00:00:00 2001 From: Alex Walker Date: Tue, 1 Dec 2020 23:23:43 +0000 Subject: [PATCH 19/21] format Python code with black --- analysis/codelists.py | 8 +- analysis/common_variables.py | 608 ++++++++-------------- analysis/study_definition_control_2019.py | 43 +- analysis/study_definition_control_2020.py | 42 +- analysis/study_definition_covid.py | 58 ++- analysis/study_definition_pneumonia.py | 61 ++- 6 files changed, 375 insertions(+), 445 deletions(-) diff --git a/analysis/codelists.py b/analysis/codelists.py index c4e9a4c..808a037 100644 --- a/analysis/codelists.py +++ b/analysis/codelists.py @@ -1,9 +1,9 @@ from cohortextractor import codelist, codelist_from_csv af_codes = codelist_from_csv( - "codelists/opensafely-atrial-fibrillation-clinical-finding.csv", - system="ctv3", - column="CTV3Code", + "codelists/opensafely-atrial-fibrillation-clinical-finding.csv", + system="ctv3", + column="CTV3Code", ) ethnicity_codes = codelist_from_csv( @@ -13,7 +13,7 @@ category_column="Grouping_6", ) - + # MEDICATIONS warfarin_codes = codelist_from_csv( "codelists/opensafely-warfarin.csv", diff --git a/analysis/common_variables.py b/analysis/common_variables.py index ed0e2ee..24ee955 100644 --- a/analysis/common_variables.py +++ b/analysis/common_variables.py @@ -9,301 +9,239 @@ def days_before(s, days): return datetime.strftime(modified_date, "%Y-%m-%d") -def common_variable_define(start_jan, prev_nov, prev_dec, start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct, end_date): +def common_variable_define( + start_jan, + prev_nov, + prev_dec, + start_date, + start_mar, + start_apr, + start_may, + start_jun, + start_jul, + start_aug, + start_sep, + start_oct, + end_date, +): common_variables = dict( - af=patients.with_these_clinical_events( + af=patients.with_these_clinical_events( af_codes, return_first_date_in_period=True, date_format="YYYY-MM-DD", ), - # 3mth hist of doac/warfarin use - anticoag_rx_prev_nov=patients.with_these_medications( - combine_codelists(doac_codes, - warfarin_codes), + # 3mth hist of doac/warfarin use + anticoag_rx_prev_nov=patients.with_these_medications( + combine_codelists(doac_codes, warfarin_codes), return_first_date_in_period=True, between=[prev_nov, prev_dec], - return_expectations={ - "date": {"earliest": prev_nov}, - } - ), - anticoag_rx_prev_dec=patients.with_these_medications( - combine_codelists(doac_codes, - warfarin_codes), + return_expectations={"date": {"earliest": prev_nov}}, + ), + anticoag_rx_prev_dec=patients.with_these_medications( + combine_codelists(doac_codes, warfarin_codes), return_first_date_in_period=True, between=[prev_dec, start_jan], - return_expectations={ - "date": {"earliest": prev_dec}, - } - ), - anticoag_rx_jan=patients.with_these_medications( - combine_codelists(doac_codes, - warfarin_codes), + return_expectations={"date": {"earliest": prev_dec}}, + ), + anticoag_rx_jan=patients.with_these_medications( + combine_codelists(doac_codes, warfarin_codes), return_first_date_in_period=True, between=[start_jan, start_date], - return_expectations={ - "date": {"earliest": start_jan}, - } + return_expectations={"date": {"earliest": start_jan}}, ), - anticoag_rx_feb=patients.with_these_medications( - combine_codelists(doac_codes, - warfarin_codes), + anticoag_rx_feb=patients.with_these_medications( + combine_codelists(doac_codes, warfarin_codes), return_first_date_in_period=True, between=[start_date, start_mar], - return_expectations={ - "date": {"earliest": start_date}, - } + return_expectations={"date": {"earliest": start_date}}, ), - anticoag_rx_mar=patients.with_these_medications( - combine_codelists(doac_codes, - warfarin_codes), + anticoag_rx_mar=patients.with_these_medications( + combine_codelists(doac_codes, warfarin_codes), return_first_date_in_period=True, between=[start_mar, start_apr], - return_expectations={ - "date": {"earliest": start_mar}, - } + return_expectations={"date": {"earliest": start_mar}}, ), - anticoag_rx_apr=patients.with_these_medications( - combine_codelists(doac_codes, - warfarin_codes), + anticoag_rx_apr=patients.with_these_medications( + combine_codelists(doac_codes, warfarin_codes), return_first_date_in_period=True, between=[start_apr, start_may], - return_expectations={ - "date": {"earliest": start_apr}, - } + return_expectations={"date": {"earliest": start_apr}}, ), - anticoag_rx_may=patients.with_these_medications( - combine_codelists(doac_codes, - warfarin_codes), + anticoag_rx_may=patients.with_these_medications( + combine_codelists(doac_codes, warfarin_codes), return_first_date_in_period=True, between=[start_may, start_jun], - return_expectations={ - "date": {"earliest": start_may}, - } - ), - anticoag_rx_jun=patients.with_these_medications( - combine_codelists(doac_codes, - warfarin_codes), + return_expectations={"date": {"earliest": start_may}}, + ), + anticoag_rx_jun=patients.with_these_medications( + combine_codelists(doac_codes, warfarin_codes), return_first_date_in_period=True, between=[start_jun, start_jul], - return_expectations={ - "date": {"earliest": start_jun}, - } - ), - anticoag_rx_jul=patients.with_these_medications( - combine_codelists(doac_codes, - warfarin_codes), + return_expectations={"date": {"earliest": start_jun}}, + ), + anticoag_rx_jul=patients.with_these_medications( + combine_codelists(doac_codes, warfarin_codes), return_first_date_in_period=True, between=[start_jul, start_aug], - return_expectations={ - "date": {"earliest": start_jul}, - } - ), - anticoag_rx_aug=patients.with_these_medications( - combine_codelists(doac_codes, - warfarin_codes), + return_expectations={"date": {"earliest": start_jul}}, + ), + anticoag_rx_aug=patients.with_these_medications( + combine_codelists(doac_codes, warfarin_codes), return_first_date_in_period=True, between=[start_aug, start_sep], - return_expectations={ - "date": {"earliest": start_aug}, - } - ), - anticoag_rx_sep=patients.with_these_medications( - combine_codelists(doac_codes, - warfarin_codes), + return_expectations={"date": {"earliest": start_aug}}, + ), + anticoag_rx_sep=patients.with_these_medications( + combine_codelists(doac_codes, warfarin_codes), return_first_date_in_period=True, between=[start_sep, start_oct], - return_expectations={ - "date": {"earliest": start_sep}, - } - ), - anticoag_rx_oct=patients.with_these_medications( - combine_codelists(doac_codes, - warfarin_codes), + return_expectations={"date": {"earliest": start_sep}}, + ), + anticoag_rx_oct=patients.with_these_medications( + combine_codelists(doac_codes, warfarin_codes), return_first_date_in_period=True, between=[start_oct, end_date], - return_expectations={ - "date": {"earliest": start_oct}, - } + return_expectations={"date": {"earliest": start_oct}}, ), dvt_gp_feb=patients.with_these_clinical_events( filter_codes_by_category(vte_codes_gp, include=["dvt"]), return_first_date_in_period=True, date_format="YYYY-MM-DD", on_or_after=start_date, - return_expectations={ - "date": {"earliest": start_date}, - }, + return_expectations={"date": {"earliest": start_date}}, ), - dvt_gp_mar=patients.with_these_clinical_events( + dvt_gp_mar=patients.with_these_clinical_events( filter_codes_by_category(vte_codes_gp, include=["dvt"]), return_first_date_in_period=True, date_format="YYYY-MM-DD", on_or_after=start_mar, - return_expectations={ - "date": {"earliest": start_mar}, - }, + return_expectations={"date": {"earliest": start_mar}}, ), - dvt_gp_apr=patients.with_these_clinical_events( + dvt_gp_apr=patients.with_these_clinical_events( filter_codes_by_category(vte_codes_gp, include=["dvt"]), return_first_date_in_period=True, date_format="YYYY-MM-DD", on_or_after=start_apr, - return_expectations={ - "date": {"earliest": start_apr}, - }, + return_expectations={"date": {"earliest": start_apr}}, ), - dvt_gp_may=patients.with_these_clinical_events( + dvt_gp_may=patients.with_these_clinical_events( filter_codes_by_category(vte_codes_gp, include=["dvt"]), return_first_date_in_period=True, date_format="YYYY-MM-DD", on_or_after=start_may, - return_expectations={ - "date": {"earliest": start_may}, - }, - ), - dvt_gp_jun=patients.with_these_clinical_events( + return_expectations={"date": {"earliest": start_may}}, + ), + dvt_gp_jun=patients.with_these_clinical_events( filter_codes_by_category(vte_codes_gp, include=["dvt"]), return_first_date_in_period=True, date_format="YYYY-MM-DD", on_or_after=start_jun, - return_expectations={ - "date": {"earliest": start_jun}, - }, + return_expectations={"date": {"earliest": start_jun}}, ), - dvt_gp_jul=patients.with_these_clinical_events( + dvt_gp_jul=patients.with_these_clinical_events( filter_codes_by_category(vte_codes_gp, include=["dvt"]), return_first_date_in_period=True, date_format="YYYY-MM-DD", on_or_after=start_jul, - return_expectations={ - "date": {"earliest": start_jul}, - }, + return_expectations={"date": {"earliest": start_jul}}, ), - dvt_gp_aug=patients.with_these_clinical_events( + dvt_gp_aug=patients.with_these_clinical_events( filter_codes_by_category(vte_codes_gp, include=["dvt"]), return_first_date_in_period=True, date_format="YYYY-MM-DD", on_or_after=start_aug, - return_expectations={ - "date": {"earliest": start_aug}, - }, + return_expectations={"date": {"earliest": start_aug}}, ), - dvt_gp_sep=patients.with_these_clinical_events( + dvt_gp_sep=patients.with_these_clinical_events( filter_codes_by_category(vte_codes_gp, include=["dvt"]), return_first_date_in_period=True, date_format="YYYY-MM-DD", on_or_after=start_sep, - return_expectations={ - "date": {"earliest": start_sep}, - }, + return_expectations={"date": {"earliest": start_sep}}, ), - dvt_gp_oct=patients.with_these_clinical_events( + dvt_gp_oct=patients.with_these_clinical_events( filter_codes_by_category(vte_codes_gp, include=["dvt"]), return_first_date_in_period=True, date_format="YYYY-MM-DD", on_or_after=start_oct, - return_expectations={ - "date": {"earliest": start_oct}, - }, + return_expectations={"date": {"earliest": start_oct}}, ), pe_gp_feb=patients.with_these_clinical_events( filter_codes_by_category(vte_codes_gp, include=["pe"]), return_first_date_in_period=True, date_format="YYYY-MM-DD", on_or_after=start_date, - return_expectations={ - "date": {"earliest": start_date}, - }, + return_expectations={"date": {"earliest": start_date}}, ), - pe_gp_mar=patients.with_these_clinical_events( + pe_gp_mar=patients.with_these_clinical_events( filter_codes_by_category(vte_codes_gp, include=["pe"]), return_first_date_in_period=True, date_format="YYYY-MM-DD", on_or_after=start_mar, - return_expectations={ - "date": {"earliest": start_mar}, - }, + return_expectations={"date": {"earliest": start_mar}}, ), - pe_gp_apr=patients.with_these_clinical_events( + pe_gp_apr=patients.with_these_clinical_events( filter_codes_by_category(vte_codes_gp, include=["pe"]), return_first_date_in_period=True, date_format="YYYY-MM-DD", on_or_after=start_apr, - return_expectations={ - "date": {"earliest": start_apr}, - }, + return_expectations={"date": {"earliest": start_apr}}, ), - pe_gp_may=patients.with_these_clinical_events( + pe_gp_may=patients.with_these_clinical_events( filter_codes_by_category(vte_codes_gp, include=["pe"]), return_first_date_in_period=True, date_format="YYYY-MM-DD", on_or_after=start_may, - return_expectations={ - "date": {"earliest": start_may}, - }, - ), - pe_gp_jun=patients.with_these_clinical_events( + return_expectations={"date": {"earliest": start_may}}, + ), + pe_gp_jun=patients.with_these_clinical_events( filter_codes_by_category(vte_codes_gp, include=["pe"]), return_first_date_in_period=True, date_format="YYYY-MM-DD", on_or_after=start_jun, - return_expectations={ - "date": {"earliest": start_jun}, - }, + return_expectations={"date": {"earliest": start_jun}}, ), - pe_gp_jul=patients.with_these_clinical_events( + pe_gp_jul=patients.with_these_clinical_events( filter_codes_by_category(vte_codes_gp, include=["pe"]), return_first_date_in_period=True, date_format="YYYY-MM-DD", on_or_after=start_jul, - return_expectations={ - "date": {"earliest": start_jul}, - }, + return_expectations={"date": {"earliest": start_jul}}, ), - pe_gp_aug=patients.with_these_clinical_events( + pe_gp_aug=patients.with_these_clinical_events( filter_codes_by_category(vte_codes_gp, include=["pe"]), return_first_date_in_period=True, date_format="YYYY-MM-DD", on_or_after=start_aug, - return_expectations={ - "date": {"earliest": start_aug}, - }, + return_expectations={"date": {"earliest": start_aug}}, ), - pe_gp_sep=patients.with_these_clinical_events( + pe_gp_sep=patients.with_these_clinical_events( filter_codes_by_category(vte_codes_gp, include=["pe"]), return_first_date_in_period=True, date_format="YYYY-MM-DD", on_or_after=start_sep, - return_expectations={ - "date": {"earliest": start_sep}, - }, + return_expectations={"date": {"earliest": start_sep}}, ), - pe_gp_oct=patients.with_these_clinical_events( + pe_gp_oct=patients.with_these_clinical_events( filter_codes_by_category(vte_codes_gp, include=["pe"]), return_first_date_in_period=True, date_format="YYYY-MM-DD", on_or_after=start_oct, - return_expectations={ - "date": {"earliest": start_oct}, - }, + return_expectations={"date": {"earliest": start_oct}}, ), previous_vte_gp=patients.with_these_clinical_events( vte_codes_gp, returning="date", date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={ - "incidence": 0.05, - }, + return_expectations={"incidence": 0.05}, ), previous_pe_gp=patients.with_these_clinical_events( filter_codes_by_category(vte_codes_gp, include=["pe"]), returning="date", date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={ - "incidence": 0.05, - }, + return_expectations={"incidence": 0.05}, ), previous_pe_hospital=patients.admitted_to_hospital( with_these_diagnoses=filter_codes_by_category( @@ -312,18 +250,14 @@ def common_variable_define(start_jan, prev_nov, prev_dec, start_date, start_mar, returning="date_admitted", date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={ - "incidence": 0.05, - }, + return_expectations={"incidence": 0.05}, ), previous_dvt_gp=patients.with_these_clinical_events( filter_codes_by_category(vte_codes_gp, include=["dvt"]), returning="date", date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={ - "incidence": 0.05, - }, + return_expectations={"incidence": 0.05}, ), previous_dvt_hospital=patients.admitted_to_hospital( with_these_diagnoses=filter_codes_by_category( @@ -332,9 +266,7 @@ def common_variable_define(start_jan, prev_nov, prev_dec, start_date, start_mar, returning="date_admitted", date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={ - "incidence": 0.05, - }, + return_expectations={"incidence": 0.05}, ), dvt_hospital_feb=patients.admitted_to_hospital( returning="date_admitted", @@ -344,9 +276,7 @@ def common_variable_define(start_jan, prev_nov, prev_dec, start_date, start_mar, on_or_after=start_date, date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={ - "date": {"earliest": start_date}, - }, + return_expectations={"date": {"earliest": start_date}}, ), dvt_hospital_mar=patients.admitted_to_hospital( returning="date_admitted", @@ -356,11 +286,9 @@ def common_variable_define(start_jan, prev_nov, prev_dec, start_date, start_mar, on_or_after=start_mar, date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={ - "date": {"earliest": start_mar}, - }, - ), - dvt_hospital_apr=patients.admitted_to_hospital( + return_expectations={"date": {"earliest": start_mar}}, + ), + dvt_hospital_apr=patients.admitted_to_hospital( returning="date_admitted", with_these_diagnoses=filter_codes_by_category( vte_codes_hospital, include=["dvt"] @@ -368,11 +296,9 @@ def common_variable_define(start_jan, prev_nov, prev_dec, start_date, start_mar, on_or_after=start_apr, date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={ - "date": {"earliest": start_apr}, - }, - ), - dvt_hospital_may=patients.admitted_to_hospital( + return_expectations={"date": {"earliest": start_apr}}, + ), + dvt_hospital_may=patients.admitted_to_hospital( returning="date_admitted", with_these_diagnoses=filter_codes_by_category( vte_codes_hospital, include=["dvt"] @@ -380,11 +306,9 @@ def common_variable_define(start_jan, prev_nov, prev_dec, start_date, start_mar, on_or_after=start_may, date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={ - "date": {"earliest": start_may}, - }, - ), - dvt_hospital_jun=patients.admitted_to_hospital( + return_expectations={"date": {"earliest": start_may}}, + ), + dvt_hospital_jun=patients.admitted_to_hospital( returning="date_admitted", with_these_diagnoses=filter_codes_by_category( vte_codes_hospital, include=["dvt"] @@ -392,11 +316,9 @@ def common_variable_define(start_jan, prev_nov, prev_dec, start_date, start_mar, on_or_after=start_jun, date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={ - "date": {"earliest": start_jun}, - }, - ), - dvt_hospital_jul=patients.admitted_to_hospital( + return_expectations={"date": {"earliest": start_jun}}, + ), + dvt_hospital_jul=patients.admitted_to_hospital( returning="date_admitted", with_these_diagnoses=filter_codes_by_category( vte_codes_hospital, include=["dvt"] @@ -404,11 +326,9 @@ def common_variable_define(start_jan, prev_nov, prev_dec, start_date, start_mar, on_or_after=start_jul, date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={ - "date": {"earliest": start_jul}, - }, - ), - dvt_hospital_aug=patients.admitted_to_hospital( + return_expectations={"date": {"earliest": start_jul}}, + ), + dvt_hospital_aug=patients.admitted_to_hospital( returning="date_admitted", with_these_diagnoses=filter_codes_by_category( vte_codes_hospital, include=["dvt"] @@ -416,11 +336,9 @@ def common_variable_define(start_jan, prev_nov, prev_dec, start_date, start_mar, on_or_after=start_aug, date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={ - "date": {"earliest": start_aug}, - }, - ), - dvt_hospital_sep=patients.admitted_to_hospital( + return_expectations={"date": {"earliest": start_aug}}, + ), + dvt_hospital_sep=patients.admitted_to_hospital( returning="date_admitted", with_these_diagnoses=filter_codes_by_category( vte_codes_hospital, include=["dvt"] @@ -428,11 +346,9 @@ def common_variable_define(start_jan, prev_nov, prev_dec, start_date, start_mar, on_or_after=start_sep, date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={ - "date": {"earliest": start_sep}, - }, - ), - dvt_hospital_oct=patients.admitted_to_hospital( + return_expectations={"date": {"earliest": start_sep}}, + ), + dvt_hospital_oct=patients.admitted_to_hospital( returning="date_admitted", with_these_diagnoses=filter_codes_by_category( vte_codes_hospital, include=["dvt"] @@ -440,10 +356,8 @@ def common_variable_define(start_jan, prev_nov, prev_dec, start_date, start_mar, on_or_after=start_oct, date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={ - "date": {"earliest": start_oct}, - }, - ), + return_expectations={"date": {"earliest": start_oct}}, + ), pe_hospital_feb=patients.admitted_to_hospital( returning="date_admitted", with_these_diagnoses=filter_codes_by_category( @@ -452,9 +366,7 @@ def common_variable_define(start_jan, prev_nov, prev_dec, start_date, start_mar, on_or_after=start_date, date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={ - "date": {"earliest": start_date}, - }, + return_expectations={"date": {"earliest": start_date}}, ), pe_hospital_mar=patients.admitted_to_hospital( returning="date_admitted", @@ -464,11 +376,9 @@ def common_variable_define(start_jan, prev_nov, prev_dec, start_date, start_mar, on_or_after=start_mar, date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={ - "date": {"earliest": start_mar}, - }, - ), - pe_hospital_apr=patients.admitted_to_hospital( + return_expectations={"date": {"earliest": start_mar}}, + ), + pe_hospital_apr=patients.admitted_to_hospital( returning="date_admitted", with_these_diagnoses=filter_codes_by_category( vte_codes_hospital, include=["pe"] @@ -476,11 +386,9 @@ def common_variable_define(start_jan, prev_nov, prev_dec, start_date, start_mar, on_or_after=start_apr, date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={ - "date": {"earliest": start_apr}, - }, - ), - pe_hospital_may=patients.admitted_to_hospital( + return_expectations={"date": {"earliest": start_apr}}, + ), + pe_hospital_may=patients.admitted_to_hospital( returning="date_admitted", with_these_diagnoses=filter_codes_by_category( vte_codes_hospital, include=["pe"] @@ -488,11 +396,9 @@ def common_variable_define(start_jan, prev_nov, prev_dec, start_date, start_mar, on_or_after=start_may, date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={ - "date": {"earliest": start_may}, - }, - ), - pe_hospital_jun=patients.admitted_to_hospital( + return_expectations={"date": {"earliest": start_may}}, + ), + pe_hospital_jun=patients.admitted_to_hospital( returning="date_admitted", with_these_diagnoses=filter_codes_by_category( vte_codes_hospital, include=["pe"] @@ -500,11 +406,9 @@ def common_variable_define(start_jan, prev_nov, prev_dec, start_date, start_mar, on_or_after=start_jun, date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={ - "date": {"earliest": start_jun}, - }, - ), - pe_hospital_jul=patients.admitted_to_hospital( + return_expectations={"date": {"earliest": start_jun}}, + ), + pe_hospital_jul=patients.admitted_to_hospital( returning="date_admitted", with_these_diagnoses=filter_codes_by_category( vte_codes_hospital, include=["pe"] @@ -512,11 +416,9 @@ def common_variable_define(start_jan, prev_nov, prev_dec, start_date, start_mar, on_or_after=start_jul, date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={ - "date": {"earliest": start_jul}, - }, - ), - pe_hospital_aug=patients.admitted_to_hospital( + return_expectations={"date": {"earliest": start_jul}}, + ), + pe_hospital_aug=patients.admitted_to_hospital( returning="date_admitted", with_these_diagnoses=filter_codes_by_category( vte_codes_hospital, include=["pe"] @@ -524,11 +426,9 @@ def common_variable_define(start_jan, prev_nov, prev_dec, start_date, start_mar, on_or_after=start_aug, date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={ - "date": {"earliest": start_aug}, - }, - ), - pe_hospital_sep=patients.admitted_to_hospital( + return_expectations={"date": {"earliest": start_aug}}, + ), + pe_hospital_sep=patients.admitted_to_hospital( returning="date_admitted", with_these_diagnoses=filter_codes_by_category( vte_codes_hospital, include=["pe"] @@ -536,11 +436,9 @@ def common_variable_define(start_jan, prev_nov, prev_dec, start_date, start_mar, on_or_after=start_sep, date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={ - "date": {"earliest": start_sep}, - }, - ), - pe_hospital_oct=patients.admitted_to_hospital( + return_expectations={"date": {"earliest": start_sep}}, + ), + pe_hospital_oct=patients.admitted_to_hospital( returning="date_admitted", with_these_diagnoses=filter_codes_by_category( vte_codes_hospital, include=["pe"] @@ -548,10 +446,8 @@ def common_variable_define(start_jan, prev_nov, prev_dec, start_date, start_mar, on_or_after=start_oct, date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={ - "date": {"earliest": start_oct}, - }, - ), + return_expectations={"date": {"earliest": start_oct}}, + ), other_vte_hospital=patients.admitted_to_hospital( returning="date_admitted", with_these_diagnoses=filter_codes_by_category( @@ -560,9 +456,7 @@ def common_variable_define(start_jan, prev_nov, prev_dec, start_date, start_mar, on_or_after=start_date, date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={ - "date": {"earliest": start_date}, - }, + return_expectations={"date": {"earliest": start_date}}, ), dvt_ons_feb=patients.with_these_codes_on_death_certificate( filter_codes_by_category(vte_codes_hospital, include=["dvt"]), @@ -571,63 +465,62 @@ def common_variable_define(start_jan, prev_nov, prev_dec, start_date, start_mar, on_or_after=start_date, return_expectations={"date": {"earliest": start_date}}, ), - dvt_ons_mar=patients.with_these_codes_on_death_certificate( + dvt_ons_mar=patients.with_these_codes_on_death_certificate( filter_codes_by_category(vte_codes_hospital, include=["dvt"]), returning="date_of_death", match_only_underlying_cause=False, on_or_after=start_mar, return_expectations={"date": {"earliest": start_mar}}, ), - dvt_ons_apr=patients.with_these_codes_on_death_certificate( + dvt_ons_apr=patients.with_these_codes_on_death_certificate( filter_codes_by_category(vte_codes_hospital, include=["dvt"]), returning="date_of_death", match_only_underlying_cause=False, on_or_after=start_apr, return_expectations={"date": {"earliest": start_apr}}, ), - dvt_ons_may=patients.with_these_codes_on_death_certificate( + dvt_ons_may=patients.with_these_codes_on_death_certificate( filter_codes_by_category(vte_codes_hospital, include=["dvt"]), returning="date_of_death", match_only_underlying_cause=False, on_or_after=start_may, return_expectations={"date": {"earliest": start_may}}, ), - dvt_ons_jun=patients.with_these_codes_on_death_certificate( + dvt_ons_jun=patients.with_these_codes_on_death_certificate( filter_codes_by_category(vte_codes_hospital, include=["dvt"]), returning="date_of_death", match_only_underlying_cause=False, on_or_after=start_jun, return_expectations={"date": {"earliest": start_jun}}, ), - dvt_ons_jul=patients.with_these_codes_on_death_certificate( + dvt_ons_jul=patients.with_these_codes_on_death_certificate( filter_codes_by_category(vte_codes_hospital, include=["dvt"]), returning="date_of_death", match_only_underlying_cause=False, on_or_after=start_jul, return_expectations={"date": {"earliest": start_jul}}, ), - dvt_ons_aug=patients.with_these_codes_on_death_certificate( + dvt_ons_aug=patients.with_these_codes_on_death_certificate( filter_codes_by_category(vte_codes_hospital, include=["dvt"]), returning="date_of_death", match_only_underlying_cause=False, on_or_after=start_aug, return_expectations={"date": {"earliest": start_aug}}, ), - dvt_ons_sep=patients.with_these_codes_on_death_certificate( + dvt_ons_sep=patients.with_these_codes_on_death_certificate( filter_codes_by_category(vte_codes_hospital, include=["dvt"]), returning="date_of_death", match_only_underlying_cause=False, on_or_after=start_sep, return_expectations={"date": {"earliest": start_sep}}, ), - dvt_ons_oct=patients.with_these_codes_on_death_certificate( + dvt_ons_oct=patients.with_these_codes_on_death_certificate( filter_codes_by_category(vte_codes_hospital, include=["dvt"]), returning="date_of_death", match_only_underlying_cause=False, on_or_after=start_oct, return_expectations={"date": {"earliest": start_oct}}, ), - pe_ons_feb=patients.with_these_codes_on_death_certificate( filter_codes_by_category(vte_codes_hospital, include=["pe"]), returning="date_of_death", @@ -635,63 +528,62 @@ def common_variable_define(start_jan, prev_nov, prev_dec, start_date, start_mar, on_or_after=start_date, return_expectations={"date": {"earliest": start_date}}, ), - pe_ons_mar=patients.with_these_codes_on_death_certificate( + pe_ons_mar=patients.with_these_codes_on_death_certificate( filter_codes_by_category(vte_codes_hospital, include=["pe"]), returning="date_of_death", match_only_underlying_cause=False, on_or_after=start_mar, return_expectations={"date": {"earliest": start_mar}}, ), - pe_ons_apr=patients.with_these_codes_on_death_certificate( + pe_ons_apr=patients.with_these_codes_on_death_certificate( filter_codes_by_category(vte_codes_hospital, include=["pe"]), returning="date_of_death", match_only_underlying_cause=False, on_or_after=start_apr, return_expectations={"date": {"earliest": start_apr}}, ), - pe_ons_may=patients.with_these_codes_on_death_certificate( + pe_ons_may=patients.with_these_codes_on_death_certificate( filter_codes_by_category(vte_codes_hospital, include=["pe"]), returning="date_of_death", match_only_underlying_cause=False, on_or_after=start_may, return_expectations={"date": {"earliest": start_may}}, ), - pe_ons_jun=patients.with_these_codes_on_death_certificate( + pe_ons_jun=patients.with_these_codes_on_death_certificate( filter_codes_by_category(vte_codes_hospital, include=["pe"]), returning="date_of_death", match_only_underlying_cause=False, on_or_after=start_jun, return_expectations={"date": {"earliest": start_jun}}, ), - pe_ons_jul=patients.with_these_codes_on_death_certificate( + pe_ons_jul=patients.with_these_codes_on_death_certificate( filter_codes_by_category(vte_codes_hospital, include=["pe"]), returning="date_of_death", match_only_underlying_cause=False, on_or_after=start_jul, return_expectations={"date": {"earliest": start_jul}}, ), - pe_ons_aug=patients.with_these_codes_on_death_certificate( + pe_ons_aug=patients.with_these_codes_on_death_certificate( filter_codes_by_category(vte_codes_hospital, include=["pe"]), returning="date_of_death", match_only_underlying_cause=False, on_or_after=start_aug, return_expectations={"date": {"earliest": start_aug}}, ), - pe_ons_sep=patients.with_these_codes_on_death_certificate( + pe_ons_sep=patients.with_these_codes_on_death_certificate( filter_codes_by_category(vte_codes_hospital, include=["pe"]), returning="date_of_death", match_only_underlying_cause=False, on_or_after=start_sep, return_expectations={"date": {"earliest": start_sep}}, ), - pe_ons_oct=patients.with_these_codes_on_death_certificate( + pe_ons_oct=patients.with_these_codes_on_death_certificate( filter_codes_by_category(vte_codes_hospital, include=["pe"]), returning="date_of_death", match_only_underlying_cause=False, on_or_after=start_oct, return_expectations={"date": {"earliest": start_oct}}, ), - other_vte_ons=patients.with_these_codes_on_death_certificate( filter_codes_by_category(vte_codes_hospital, include=["other"]), returning="date_of_death", @@ -704,100 +596,78 @@ def common_variable_define(start_jan, prev_nov, prev_dec, start_date, start_mar, returning="date_admitted", date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={ - "incidence": 0.05, - }, + return_expectations={"incidence": 0.05}, ), stroke_gp_feb=patients.with_these_clinical_events( - stroke, + stroke, return_first_date_in_period=True, date_format="YYYY-MM-DD", on_or_after=start_date, - return_expectations={ - "date": {"earliest": start_date}, - }, + return_expectations={"date": {"earliest": start_date}}, ), - stroke_gp_mar=patients.with_these_clinical_events( - stroke, + stroke_gp_mar=patients.with_these_clinical_events( + stroke, return_first_date_in_period=True, date_format="YYYY-MM-DD", on_or_after=start_mar, - return_expectations={ - "date": {"earliest": start_mar}, - }, + return_expectations={"date": {"earliest": start_mar}}, ), - stroke_gp_apr=patients.with_these_clinical_events( - stroke, + stroke_gp_apr=patients.with_these_clinical_events( + stroke, return_first_date_in_period=True, date_format="YYYY-MM-DD", on_or_after=start_apr, - return_expectations={ - "date": {"earliest": start_apr}, - }, + return_expectations={"date": {"earliest": start_apr}}, ), - stroke_gp_may=patients.with_these_clinical_events( - stroke, + stroke_gp_may=patients.with_these_clinical_events( + stroke, return_first_date_in_period=True, date_format="YYYY-MM-DD", on_or_after=start_may, - return_expectations={ - "date": {"earliest": start_may}, - }, - ), - stroke_gp_jun=patients.with_these_clinical_events( - stroke, + return_expectations={"date": {"earliest": start_may}}, + ), + stroke_gp_jun=patients.with_these_clinical_events( + stroke, return_first_date_in_period=True, date_format="YYYY-MM-DD", on_or_after=start_jun, - return_expectations={ - "date": {"earliest": start_jun}, - }, + return_expectations={"date": {"earliest": start_jun}}, ), - stroke_gp_jul=patients.with_these_clinical_events( - stroke, + stroke_gp_jul=patients.with_these_clinical_events( + stroke, return_first_date_in_period=True, date_format="YYYY-MM-DD", on_or_after=start_jul, - return_expectations={ - "date": {"earliest": start_jul}, - }, + return_expectations={"date": {"earliest": start_jul}}, ), - stroke_gp_aug=patients.with_these_clinical_events( - stroke, + stroke_gp_aug=patients.with_these_clinical_events( + stroke, return_first_date_in_period=True, date_format="YYYY-MM-DD", on_or_after=start_aug, - return_expectations={ - "date": {"earliest": start_aug}, - }, + return_expectations={"date": {"earliest": start_aug}}, ), - stroke_gp_sep=patients.with_these_clinical_events( - stroke, + stroke_gp_sep=patients.with_these_clinical_events( + stroke, return_first_date_in_period=True, date_format="YYYY-MM-DD", on_or_after=start_sep, - return_expectations={ - "date": {"earliest": start_sep}, - }, + return_expectations={"date": {"earliest": start_sep}}, ), - stroke_gp_oct=patients.with_these_clinical_events( - stroke, + stroke_gp_oct=patients.with_these_clinical_events( + stroke, return_first_date_in_period=True, date_format="YYYY-MM-DD", on_or_after=start_oct, - return_expectations={ - "date": {"earliest": start_oct}, - }, + return_expectations={"date": {"earliest": start_oct}}, ), - stroke_hospital_feb=patients.admitted_to_hospital( + stroke_hospital_feb=patients.admitted_to_hospital( returning="date_admitted", with_these_diagnoses=stroke_hospital, on_or_after=start_date, date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={ - "date": {"earliest": start_date}, - }, + return_expectations={"date": {"earliest": start_date}}, ), stroke_hospital_mar=patients.admitted_to_hospital( returning="date_admitted", @@ -805,31 +675,25 @@ def common_variable_define(start_jan, prev_nov, prev_dec, start_date, start_mar, on_or_after=start_mar, date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={ - "date": {"earliest": start_mar}, - }, - ), - stroke_hospital_apr=patients.admitted_to_hospital( + return_expectations={"date": {"earliest": start_mar}}, + ), + stroke_hospital_apr=patients.admitted_to_hospital( returning="date_admitted", with_these_diagnoses=stroke_hospital, on_or_after=start_apr, date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={ - "date": {"earliest": start_apr}, - }, - ), - stroke_hospital_may=patients.admitted_to_hospital( + return_expectations={"date": {"earliest": start_apr}}, + ), + stroke_hospital_may=patients.admitted_to_hospital( returning="date_admitted", with_these_diagnoses=stroke_hospital, on_or_after=start_may, date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={ - "date": {"earliest": start_may}, - }, - ), - stroke_hospital_jun=patients.admitted_to_hospital( + return_expectations={"date": {"earliest": start_may}}, + ), + stroke_hospital_jun=patients.admitted_to_hospital( returning="date_admitted", with_these_diagnoses=stroke_hospital, on_or_after=start_jun, @@ -838,127 +702,115 @@ def common_variable_define(start_jan, prev_nov, prev_dec, start_date, start_mar, return_expectations={ "date": {"earliest": start_jun}, }, - ), - stroke_hospital_jul=patients.admitted_to_hospital( + ), + stroke_hospital_jul=patients.admitted_to_hospital( returning="date_admitted", with_these_diagnoses=stroke_hospital, on_or_after=start_jul, date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={ - "date": {"earliest": start_jul}, - }, - ), - stroke_hospital_aug=patients.admitted_to_hospital( + return_expectations={"date": {"earliest": start_jul}}, + ), + stroke_hospital_aug=patients.admitted_to_hospital( returning="date_admitted", with_these_diagnoses=stroke_hospital, on_or_after=start_aug, date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={ - "date": {"earliest": start_aug}, - }, - ), - stroke_hospital_sep=patients.admitted_to_hospital( + return_expectations={"date": {"earliest": start_aug}}, + ), + stroke_hospital_sep=patients.admitted_to_hospital( returning="date_admitted", with_these_diagnoses=stroke_hospital, on_or_after=start_sep, date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={ - "date": {"earliest": start_sep}, - }, - ), - stroke_hospital_oct=patients.admitted_to_hospital( + return_expectations={"date": {"earliest": start_sep}}, + ), + stroke_hospital_oct=patients.admitted_to_hospital( returning="date_admitted", with_these_diagnoses=stroke_hospital, on_or_after=start_oct, date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={ - "date": {"earliest": start_oct}, - }, - ), - stroke_ons_feb=patients.with_these_codes_on_death_certificate( + return_expectations={"date": {"earliest": start_oct}}, + ), + stroke_ons_feb=patients.with_these_codes_on_death_certificate( stroke_hospital, returning="date_of_death", match_only_underlying_cause=False, on_or_after=start_date, return_expectations={"date": {"earliest": start_date}}, ), - stroke_ons_mar=patients.with_these_codes_on_death_certificate( + stroke_ons_mar=patients.with_these_codes_on_death_certificate( stroke_hospital, returning="date_of_death", match_only_underlying_cause=False, on_or_after=start_mar, return_expectations={"date": {"earliest": start_mar}}, ), - stroke_ons_apr=patients.with_these_codes_on_death_certificate( + stroke_ons_apr=patients.with_these_codes_on_death_certificate( stroke_hospital, returning="date_of_death", match_only_underlying_cause=False, on_or_after=start_apr, return_expectations={"date": {"earliest": start_apr}}, ), - stroke_ons_may=patients.with_these_codes_on_death_certificate( + stroke_ons_may=patients.with_these_codes_on_death_certificate( stroke_hospital, returning="date_of_death", match_only_underlying_cause=False, on_or_after=start_may, return_expectations={"date": {"earliest": start_may}}, ), - stroke_ons_jun=patients.with_these_codes_on_death_certificate( + stroke_ons_jun=patients.with_these_codes_on_death_certificate( stroke_hospital, returning="date_of_death", match_only_underlying_cause=False, on_or_after=start_jun, return_expectations={"date": {"earliest": start_jun}}, ), - stroke_ons_jul=patients.with_these_codes_on_death_certificate( + stroke_ons_jul=patients.with_these_codes_on_death_certificate( stroke_hospital, returning="date_of_death", match_only_underlying_cause=False, on_or_after=start_jul, return_expectations={"date": {"earliest": start_jul}}, ), - stroke_ons_aug=patients.with_these_codes_on_death_certificate( + stroke_ons_aug=patients.with_these_codes_on_death_certificate( stroke_hospital, returning="date_of_death", match_only_underlying_cause=False, on_or_after=start_aug, return_expectations={"date": {"earliest": start_aug}}, ), - stroke_ons_sep=patients.with_these_codes_on_death_certificate( + stroke_ons_sep=patients.with_these_codes_on_death_certificate( stroke_hospital, returning="date_of_death", match_only_underlying_cause=False, on_or_after=start_sep, return_expectations={"date": {"earliest": start_sep}}, ), - stroke_ons_oct=patients.with_these_codes_on_death_certificate( + stroke_ons_oct=patients.with_these_codes_on_death_certificate( stroke_hospital, returning="date_of_death", match_only_underlying_cause=False, on_or_after=start_oct, return_expectations={"date": {"earliest": start_oct}}, - ), + ), previous_stroke_gp=patients.with_these_clinical_events( stroke, returning="date", date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={ - "incidence": 0.05, - }, + return_expectations={"incidence": 0.05}, ), previous_stroke_hospital=patients.admitted_to_hospital( with_these_diagnoses=stroke_hospital, returning="date_admitted", date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={ - "incidence": 0.05, - }, + return_expectations={"incidence": 0.05}, ), died_date_ons=patients.died_from_any_cause( on_or_after=start_date, diff --git a/analysis/study_definition_control_2019.py b/analysis/study_definition_control_2019.py index 9dac3d8..0a65685 100644 --- a/analysis/study_definition_control_2019.py +++ b/analysis/study_definition_control_2019.py @@ -1,24 +1,43 @@ -from cohortextractor import StudyDefinition, patients, codelist, codelist_from_csv, combine_codelists +from cohortextractor import ( + StudyDefinition, + patients, + codelist, + codelist_from_csv, + combine_codelists, +) from common_variables import common_variable_define from codelists import * - prev_nov = "2017-11-01" prev_dec = "2018-12-01" start_jan = "2019-01-01" start_date = "2019-02-01" -start_mar = "2019-03-01" -start_apr = "2019-04-01" -start_may = "2019-05-01" -start_jun = "2019-06-01" -start_jul = "2019-07-01" -start_aug = "2019-08-01" -start_sep = "2019-09-01" -start_oct = "2019-10-01" -end_date = "2019-11-01" +start_mar = "2019-03-01" +start_apr = "2019-04-01" +start_may = "2019-05-01" +start_jun = "2019-06-01" +start_jul = "2019-07-01" +start_aug = "2019-08-01" +start_sep = "2019-09-01" +start_oct = "2019-10-01" +end_date = "2019-11-01" -common_variables = common_variable_define(start_jan, prev_nov, prev_dec, start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct, end_date) +common_variables = common_variable_define( + start_jan, + prev_nov, + prev_dec, + start_date, + start_mar, + start_apr, + start_may, + start_jun, + start_jul, + start_aug, + start_sep, + start_oct, + end_date, +) study = StudyDefinition( default_expectations={ diff --git a/analysis/study_definition_control_2020.py b/analysis/study_definition_control_2020.py index 142a074..de308d9 100644 --- a/analysis/study_definition_control_2020.py +++ b/analysis/study_definition_control_2020.py @@ -1,4 +1,10 @@ -from cohortextractor import StudyDefinition, patients, codelist, codelist_from_csv, combine_codelists +from cohortextractor import ( + StudyDefinition, + patients, + codelist, + codelist_from_csv, + combine_codelists, +) from common_variables import common_variable_define from codelists import * @@ -7,17 +13,31 @@ prev_dec = "2019-12-01" start_jan = "2020-01-01" start_date = "2020-02-01" -start_mar = "2020-03-01" -start_apr = "2020-04-01" -start_may = "2020-05-01" -start_jun = "2020-06-01" -start_jul = "2020-07-01" -start_aug = "2020-08-01" -start_sep = "2020-09-01" -start_oct = "2020-10-01" -end_date = "2020-11-01" +start_mar = "2020-03-01" +start_apr = "2020-04-01" +start_may = "2020-05-01" +start_jun = "2020-06-01" +start_jul = "2020-07-01" +start_aug = "2020-08-01" +start_sep = "2020-09-01" +start_oct = "2020-10-01" +end_date = "2020-11-01" -common_variables = common_variable_define(start_jan, prev_nov, prev_dec, start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct, end_date) +common_variables = common_variable_define( + start_jan, + prev_nov, + prev_dec, + start_date, + start_mar, + start_apr, + start_may, + start_jun, + start_jul, + start_aug, + start_sep, + start_oct, + end_date, +) study = StudyDefinition( default_expectations={ diff --git a/analysis/study_definition_covid.py b/analysis/study_definition_covid.py index af56759..ff3aa31 100644 --- a/analysis/study_definition_covid.py +++ b/analysis/study_definition_covid.py @@ -1,4 +1,10 @@ -from cohortextractor import StudyDefinition, patients, codelist, codelist_from_csv, combine_codelists +from cohortextractor import ( + StudyDefinition, + patients, + codelist, + codelist_from_csv, + combine_codelists, +) from common_variables import common_variable_define from codelists import * @@ -6,17 +12,31 @@ prev_dec = "2019-12-01" start_jan = "2020-01-01" start_date = "2020-02-01" -start_mar = "2020-03-01" -start_apr = "2020-04-01" -start_may = "2020-05-01" -start_jun = "2020-06-01" -start_jul = "2020-07-01" -start_aug = "2020-08-01" -start_sep = "2020-09-01" -start_oct = "2020-10-01" -end_date = "2020-11-01" +start_mar = "2020-03-01" +start_apr = "2020-04-01" +start_may = "2020-05-01" +start_jun = "2020-06-01" +start_jul = "2020-07-01" +start_aug = "2020-08-01" +start_sep = "2020-09-01" +start_oct = "2020-10-01" +end_date = "2020-11-01" -common_variables = common_variable_define(start_jan, prev_nov, prev_dec, start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct, end_date) +common_variables = common_variable_define( + start_jan, + prev_nov, + prev_dec, + start_date, + start_mar, + start_apr, + start_may, + start_jun, + start_jul, + start_aug, + start_sep, + start_oct, + end_date, +) study = StudyDefinition( default_expectations={ @@ -31,7 +51,7 @@ AND (sex = "M" OR sex = "F") AND imd > 0 AND exposure_hospitalisation - AND NOT stp = "" + AND NOT stp = "" """, has_follow_up=patients.registered_with_one_practice_between( "2019-02-01", "2020-02-01" @@ -43,23 +63,23 @@ on_or_after=start_date, date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={"date": {"earliest": start_date},}, + return_expectations={"date": {"earliest": start_date}}, ), - exposure_hosp_primary_dx=patients.admitted_to_hospital( + exposure_hosp_primary_dx=patients.admitted_to_hospital( returning="date_admitted", with_these_primary_diagnoses=covid_codelist, on_or_after=start_date, date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={"date": {"earliest": start_date},}, + return_expectations={"date": {"earliest": start_date}}, ), - # ICU admission from ICNARC-CMP + # ICU admission from ICNARC-CMP date_icu_admission=patients.admitted_to_icu( find_first_match_in_period=True, between=[start_date, start_oct], returning="date_admitted", - #include_day = True, - date_format="YYYY-MM-DD", #not yet working for admitted_to_icu? + # include_day = True, + date_format="YYYY-MM-DD", # not yet working for admitted_to_icu? return_expectations={"date": {"earliest": start_date}}, ), exposure_discharge=patients.admitted_to_hospital( @@ -68,7 +88,7 @@ on_or_after=start_date, date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={"date": {"earliest": start_date},}, + return_expectations={"date": {"earliest": start_date}}, ), **common_variables ) diff --git a/analysis/study_definition_pneumonia.py b/analysis/study_definition_pneumonia.py index 7db3c2a..bd54b4d 100644 --- a/analysis/study_definition_pneumonia.py +++ b/analysis/study_definition_pneumonia.py @@ -1,4 +1,10 @@ -from cohortextractor import StudyDefinition, patients, codelist, codelist_from_csv, combine_codelists +from cohortextractor import ( + StudyDefinition, + patients, + codelist, + codelist_from_csv, + combine_codelists, +) from common_variables import common_variable_define from codelists import * @@ -6,17 +12,31 @@ prev_dec = "2018-12-01" start_jan = "2019-01-01" start_date = "2019-02-01" -start_mar = "2019-03-01" -start_apr = "2019-04-01" -start_may = "2019-05-01" -start_jun = "2019-06-01" -start_jul = "2019-07-01" -start_aug = "2019-08-01" -start_sep = "2019-09-01" -start_oct = "2019-10-01" -end_date = "2019-11-01" +start_mar = "2019-03-01" +start_apr = "2019-04-01" +start_may = "2019-05-01" +start_jun = "2019-06-01" +start_jul = "2019-07-01" +start_aug = "2019-08-01" +start_sep = "2019-09-01" +start_oct = "2019-10-01" +end_date = "2019-11-01" -common_variables = common_variable_define(start_jan, prev_nov, prev_dec, start_date, start_mar, start_apr, start_may, start_jun, start_jul, start_aug, start_sep, start_oct, end_date) +common_variables = common_variable_define( + start_jan, + prev_nov, + prev_dec, + start_date, + start_mar, + start_apr, + start_may, + start_jun, + start_jul, + start_aug, + start_sep, + start_oct, + end_date, +) study = StudyDefinition( default_expectations={ @@ -31,7 +51,7 @@ AND (sex = "M" OR sex = "F") AND imd > 0 AND exposure_hospitalisation - AND NOT stp = "" + AND NOT stp = "" """, has_follow_up=patients.registered_with_one_practice_between( "2018-02-01", "2019-02-01" @@ -43,33 +63,32 @@ on_or_after=start_date, date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={"date": {"earliest": start_date},}, + return_expectations={"date": {"earliest": start_date}}, ), - exposure_hosp_primary_dx=patients.admitted_to_hospital( + exposure_hosp_primary_dx=patients.admitted_to_hospital( returning="date_admitted", with_these_primary_diagnoses=pneumonia_codelist, on_or_after=start_date, date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={"date": {"earliest": start_date},}, + return_expectations={"date": {"earliest": start_date}}, ), - # ICU admission from ICNARC-CMP + # ICU admission from ICNARC-CMP date_icu_admission=patients.admitted_to_icu( find_first_match_in_period=True, between=[start_date, start_oct], returning="date_admitted", - #include_day = True, - date_format="YYYY-MM-DD", #not yet working for admitted_to_icu? + # include_day = True, + date_format="YYYY-MM-DD", # not yet working for admitted_to_icu? return_expectations={"date": {"earliest": start_date}}, - ), - + ), exposure_discharge=patients.admitted_to_hospital( returning="date_discharged", with_these_diagnoses=pneumonia_codelist, on_or_after=start_date, date_format="YYYY-MM-DD", find_first_match_in_period=True, - return_expectations={"date": {"earliest": start_date},}, + return_expectations={"date": {"earliest": start_date}}, ), **common_variables ) From a6e8ea3663f0ae633677e58059c0c4db5ed12550 Mon Sep 17 00:00:00 2001 From: johntaz Date: Wed, 2 Dec 2020 08:42:33 +0000 Subject: [PATCH 20/21] changes requested from PR --- .../000_cr_define_covariates_simple_rates.do | 40 ++--- ...cr_simple_rates_old_full_stratification.do | 154 ------------------ 2 files changed, 20 insertions(+), 174 deletions(-) delete mode 100644 analysis/201_cr_simple_rates_old_full_stratification.do diff --git a/analysis/000_cr_define_covariates_simple_rates.do b/analysis/000_cr_define_covariates_simple_rates.do index cbc3348..acbdd4c 100644 --- a/analysis/000_cr_define_covariates_simple_rates.do +++ b/analysis/000_cr_define_covariates_simple_rates.do @@ -207,8 +207,8 @@ drop region_string ************************** /* Age variables */ -drop if age < 18 -drop if age > 110 +assert age >= 18 & age <=110 + * Create categorised age recode age 18/39.9999=1 /// 40/49.9999=2 /// @@ -340,71 +340,71 @@ foreach m of local months { * Feb if `m' == 2 { -replace hist_of_anticoag = 1 if anticoag_rx_jan !=. | /// +replace hist_of_anticoag = 1 if (anticoag_rx_jan !=. | /// anticoag_rx_prev_nov !=. | /// - anticoag_rx_prev_dec !=. & /// + anticoag_rx_prev_dec !=.) & /// indexMonth == 2 } * Mar if `m' == 3 { -replace hist_of_anticoag = 1 if anticoag_rx_jan !=. | /// +replace hist_of_anticoag = 1 if (anticoag_rx_jan !=. | /// anticoag_rx_feb !=. | /// - anticoag_rx_prev_dec !=. & /// + anticoag_rx_prev_dec !=.) & /// indexMonth == 3 } * Apr if `m' == 4 { -replace hist_of_anticoag = 1 if anticoag_rx_jan !=. | /// +replace hist_of_anticoag = 1 if (anticoag_rx_jan !=. | /// anticoag_rx_feb !=. | /// - anticoag_rx_mar !=. & /// + anticoag_rx_mar !=.) & /// indexMonth == 4 } * May if `m' == 5 { -replace hist_of_anticoag = 1 if anticoag_rx_feb !=. | /// +replace hist_of_anticoag = 1 if (anticoag_rx_feb !=. | /// anticoag_rx_mar !=. | /// - anticoag_rx_apr !=. & /// + anticoag_rx_apr !=.) & /// indexMonth == 5 } * Jun if `m' == 6 { -replace hist_of_anticoag = 1 if anticoag_rx_mar !=. | /// +replace hist_of_anticoag = 1 if (anticoag_rx_mar !=. | /// anticoag_rx_apr !=. | /// - anticoag_rx_may !=. & /// + anticoag_rx_may !=.) & /// indexMonth == 6 } * Jul if `m' == 7 { -replace hist_of_anticoag = 1 if anticoag_rx_apr !=. | /// +replace hist_of_anticoag = 1 if (anticoag_rx_apr !=. | /// anticoag_rx_may !=. | /// - anticoag_rx_jun !=. & /// + anticoag_rx_jun !=.) & /// indexMonth == 7 } * Aug if `m' == 8 { -replace hist_of_anticoag = 1 if anticoag_rx_may !=. | /// +replace hist_of_anticoag = 1 if (anticoag_rx_may !=. | /// anticoag_rx_jun !=. | /// - anticoag_rx_jul !=. & /// + anticoag_rx_jul !=.) & /// indexMonth == 8 } * Sep if `m' == 9 { -replace hist_of_anticoag = 1 if anticoag_rx_jun !=. | /// +replace hist_of_anticoag = 1 if (anticoag_rx_jun !=. | /// anticoag_rx_jul !=. | /// - anticoag_rx_aug !=. & /// + anticoag_rx_aug !=.) & /// indexMonth == 9 } * Oct if `m' == 10 { -replace hist_of_anticoag = 1 if anticoag_rx_jul !=. | /// +replace hist_of_anticoag = 1 if (anticoag_rx_jul !=. | /// anticoag_rx_aug !=. | /// - anticoag_rx_sep !=. & /// + anticoag_rx_sep !=.) & /// indexMonth == 10 } diff --git a/analysis/201_cr_simple_rates_old_full_stratification.do b/analysis/201_cr_simple_rates_old_full_stratification.do deleted file mode 100644 index 9d6fbc6..0000000 --- a/analysis/201_cr_simple_rates_old_full_stratification.do +++ /dev/null @@ -1,154 +0,0 @@ -******************************************************************************** -* -* Do-file: 201_cr_simple_rates.do -* -* Programmed by: John & Alex -* -* Data used: None -* -* Data created: None -* -* Other output: None -* -******************************************************************************** -* -* Purpose: -* -* Note: -******************************************************************************** - - -use "data/cohort_rates_$group", replace - -tempname measures - postfile `measures' str16(group) str20(outcome) str12(analysis) str20(history) level str20(variable) category personTime numEvents rate lc uc using "data/rates_summary_$group", replace - - -foreach v in stroke dvt pe { - - noi di "Starting analysis for $group: `v' Outcome ..." - noi di "$group: stset in hospital" - - stset `v'_in_hosp_end_date , id(patient_id) failure(`v'_in_hosp) enter(hospitalised_expo_date) - - - * Overall rate - stptime - * Save measure - post `measures' ("$group") ("`v'") ("in_hosp") ("") (0) ("Overall") (0) (`r(ptime)') /// - (`r(failures)') (`r(rate)') /// - (`r(lb)') (`r(ub))') - - * Stratified by history of... - foreach c in hist_`v' { - qui levelsof `c' , local(cats) - di `cats' - foreach l of local cats { - noi di "$group: Calculate rate for variable `c' and level `l'" - - - stptime if `c'==`l' - * Save measures - post `measures' ("$group") ("`v'") ("in_hosp") ("`c'") (`l') ("History of") (`l') (`r(ptime)') /// - (`r(failures)') (`r(rate)') /// - (`r(lb)') (`r(ub))') - - - * Further stratified by... - - foreach s in agegroup gender ethnicity hist_of_af hist_of_anticoag long_hosp_stay icu_admission community_exp { - qui levelsof `s' , local(cats) - di `cats' - foreach t of local cats { - noi di "$group, `c' level `l' : Calculate rate for variable `s' and level `t'" - - qui count if `c' ==`l' & `s' ==`t' - if `r(N)' > 0 { - stptime if `c'==`l' & `s' == `t' - * Save measures - post `measures' ("$group") ("`v'") ("in_hosp") ("`c'") (`l') ("`s'") (`t') (`r(ptime)') /// - (`r(failures)') (`r(rate)') /// - (`r(lb)') (`r(ub))') - } - else { - post `measures' ("$group") ("`v'") ("in_hosp") ("`c'") (`l') ("`s'") (`t') (.) /// - (.) (.) /// - (.) (.) - } - - - } - } - - } - } - - foreach a in post_hosp post_hosp_gp { - - noi di "$group: stset in `a'" - - stset `v'_`a'_end_date , id(patient_id) failure(`v'_`a') enter(discharged_expo_date) - - * Overall rate - stptime - * Save measure - post `measures' ("$group") ("`v'") ("`a'") ("") (0) ("Overall") (0) (`r(ptime)') /// - (`r(failures)') (`r(rate)') /// - (`r(lb)') (`r(ub))') - - * Stratified - foreach c in hist_`v' { - qui levelsof `c' , local(cats) - di `cats' - foreach l of local cats { - noi di "$group: Calculate rate for variable `c' and level `l'" - - stptime if `c'==`l' - - * Save measures - post `measures' ("$group") ("`v'") ("`a'") ("`c'") (`l') ("History of") (`l') (`r(ptime)') /// - (`r(failures)') (`r(rate)') /// - (`r(lb)') (`r(ub))') - - * Further stratified by... - - foreach s in agegroup gender ethnicity hist_of_af hist_of_anticoag long_hosp_stay icu_admission community_exp { - qui levelsof `s' , local(cats) - di `cats' - foreach t of local cats { - noi di "$group, `c' level `l' : Calculate rate for variable `s' and level `t'" - - qui count if `c' ==`l' & `s' ==`t' - if `r(N)' > 0 { - stptime if `c'==`l' & `s' == `t' - * Save measures - post `measures' ("$group") ("`v'") ("`a'") ("`c'") (`l') ("`s'") (`t') (`r(ptime)') /// - (`r(failures)') (`r(rate)') /// - (`r(lb)') (`r(ub))') - } - else { - post `measures' ("$group") ("`v'") ("`a'") ("`c'") (`l') ("`s'") (`t') (.) /// - (.) (.) /// - (.) (.) - } - - } - } - - } - } - } -} - -postclose `measures' - -* Change postfiles to csv -use "data/rates_summary_$group", replace - -* Change from per person-day to per 100 person-months -gen rate_ppm = 100*(rate * 365.25 / 12) -gen lc_ppm = 100*(lc * 365.25 /12) -gen uc_ppm = 100*(uc * 365.25 /12) - -export delimited using "data/rates_summary_$group.csv", replace - From 033c01f3c0ed8c3ac6fafd81f6936ad169c00f3d Mon Sep 17 00:00:00 2001 From: Alex Walker Date: Wed, 2 Dec 2020 09:07:02 +0000 Subject: [PATCH 21/21] possible age range workaround --- .github/workflows/test_runner.yaml | 2 +- analysis/common_variables.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test_runner.yaml b/.github/workflows/test_runner.yaml index 49ac181..edc12a0 100644 --- a/.github/workflows/test_runner.yaml +++ b/.github/workflows/test_runner.yaml @@ -39,7 +39,7 @@ jobs: run: '[[ -z $(git status --porcelain codelists) ]] || (echo "Uncommitted changes to codelists detected, aborting" && exit 1)' - name: Generate dummy data - run: cohortextractor generate_cohort --expectations-population=100000 + run: cohortextractor generate_cohort --expectations-population=10000 - name: Run model run: docker run --mount source=${{ github.workspace }},dst=/workspace,type=bind docker.opensafely.org/stata-mp analysis/model.do diff --git a/analysis/common_variables.py b/analysis/common_variables.py index 24ee955..842a197 100644 --- a/analysis/common_variables.py +++ b/analysis/common_variables.py @@ -822,7 +822,7 @@ def common_variable_define( start_date, return_expectations={ "rate": "universal", - "int": {"distribution": "population_ages"}, + "int": {"distribution": "normal", "mean": 64, "stddev": 5}, }, ), sex=patients.sex(