def medication_count_12m_6m(med_codelist): return patients.with_these_medications( med_codelist, between=["2019-03-01", "2020-08-31"], returning="number_of_matches_in_period", return_expectations={ "int": { "distribution": "normal", "mean": 3, "stddev": 2 }, "incidence": 0.1, }, )
def medication_latest(med_codelist): return patients.with_these_medications( med_codelist, between=["2010-01-01", "2020-02-29"], returning="date", find_last_match_in_period=True, include_month=True, return_expectations={ "incidence": 0.2, "date": { "earliest": "2010-01-01", "latest": "2020-02-29" }, }, )
def test_column_refs_in_date_expressions_do_not_trigger_errors(): # Further down the road we want to actually interpret these expressions and # generate appopriate dates, but for now we just need to not blow up when # we encounter them study = StudyDefinition( population=patients.all(), copd_exacerbation=patients.with_these_clinical_events( codelist(["X"], system="ctv3"), between=["2001-12-01", "2002-06-01"], returning="date", return_expectations={ "rate": "exponential_increase", "incidence": 0.2, "date": { "earliest": "1990-01-01", "latest": "today" }, }, find_last_match_in_period=True, date_format="YYYY-MM-DD", ), drug_after_exacerbation=patients.with_these_medications( codelist(["Y"], system="snomed"), between=["copd_exacerbation", "copd_exacerbation + 3 months"], returning="date", return_expectations={ "rate": "exponential_increase", "incidence": 0.2, "date": { "earliest": "1990-01-01", "latest": "today" }, }, find_first_match_in_period=True, date_format="YYYY-MM-DD", ), ) population_size = 10000 # Just ensure no exception is raised study.make_df_from_expectations(population_size)
"2020-02-01", returning="index_of_multiple_deprivation", round_to_nearest=100, return_expectations={ "rate": "universal", "category": {"ratios": {"100": 0.1, "200": 0.2, "300": 0.7}}, }, ), # https://github.com/opensafely/risk-factors-research/issues/47 rural_urban=patients.address_as_of( "2020-02-01", returning="rural_urban_classification", return_expectations={ "rate": "universal", "category": {"ratios": {"rural": 0.1, "urban": 0.9}}, }, ), # https://codelists.opensafely.org/codelist/opensafely/asthma-inhaler-salbutamol-medication/2020-04-15/ recent_salbutamol_count=patients.with_these_medications( salbutamol_codes, between=["2018-02-01", "2020-02-01"], returning="number_of_matches_in_period", return_expectations={ "incidence": 0.6, "int": {"distribution": "normal", "mean": 8, "stddev": 2}, }, ), )
"NO_DM": "DEFAULT", }, return_expectations={ "category": { "ratios": { "T1DM": 0.03, "T2DM": 0.2, "UNKNOWN_DM": 0.02, "NO_DM": 0.75 } }, "rate": "universal" }, oad_lastyear_meds=patients.with_these_medications( oad_med_codes, between=["2019-02-01", "2020-02-01"], returning="number_of_matches_in_period", ), insulin_lastyear_meds=patients.with_these_medications( insulin_med_codes, between=["2019-02-01", "2020-02-01"], returning="number_of_matches_in_period", ), ), #EXETER ALGORITHM USING OPENSAFELY CODELISTS diabetes_exeter_os=patients.categorised_as( { "T1DM_EX_OS": """ insulin_last6mo >= 2 AND t1dm_count >= t2dm_count * 2 """,
has_follow_up=patients.registered_with_one_practice_between( "2019-02-28", "2020-02-29", return_expectations={"incidence": 0.9}), copd=patients.with_these_clinical_events( copd_codes, on_or_before="2020-02-29", return_expectations={"incidence": 0.05}, ), ### OTHER RESPIRATORY other_respiratory=patients.with_these_clinical_events( other_respiratory_codes, on_or_before="2020-02-29", return_expectations={"incidence": 0.05}, ), nebules=patients.with_these_medications( nebulised_med_codes, between=["2019-02-28", "2020-02-29"], return_expectations={"incidence": 0.05}, ), #### HIGH DOSE ICS - all preparation high_dose_ics=patients.with_these_medications( high_dose_ics_med_codes, between=["2019-11-01", "2020-02-29"], return_expectations={"incidence": 0.05}, ), #### HIGH DOSE ICS - single ingredient preparations high_dose_ics_single_ing=patients.with_these_medications( high_dose_ics_single_ingredient_med_codes, between=["2019-11-01", "2020-02-29"], return_expectations={"incidence": 0.05}, ), #### HIGH DOSE ICS - multiple ingredient preparation
bp_sys=patients.mean_recorded_value(systolic_blood_pressure_codes, on_most_recent_day_of_measurement=True, between=["2017-02-01", "2020-01-31"], include_measurement_date=True, date_format="YYYY-MM", return_expectations={"float": {"distribution": "normal", "mean": 80, "stddev": 10}, "date": {"earliest": "2019-02-01", "latest": "2020-01-31"}, "incidence": 0.95,}), ## ace inhibitor ace_inhibitor = patients.with_these_medications(ace_inhibitor_codes, between=["1900-01-01", "2020-02-01"], include_date_of_match = True, date_format="YYYY-MM-DD", returning="binary_flag", return_expectations = {"incidence": 0.05, "date": {"earliest": "1980-02-01", "latest": "2020-01-31"}}), ## arb arb_inhibitor = patients.with_these_medications(arb_inhibitor_codes, between=["1900-01-01", "2020-02-01"], include_date_of_match = True, date_format="YYYY-MM-DD", returning="binary_flag", return_expectations = {"incidence": 0.05, "date": {"earliest": "1980-02-01", "latest": "2020-01-31"}}),
"0": 0.8, "1": 0.1, "2": 0.1 } }, }, recent_asthma_code=patients.with_these_clinical_events( asthma_codes, between=["2017-02-01", "2020-01-31"], ), asthma_code_ever=patients.with_these_clinical_events(asthma_codes), copd_code_ever=patients.with_these_clinical_events( chronic_respiratory_disease_codes), prednisolone_last_year=patients.with_these_medications( pred_codes, between=["2019-02-01", "2020-01-31"], returning="number_of_matches_in_period", ), ), chronic_cardiac_disease=patients.with_these_clinical_events( chronic_cardiac_disease_codes, return_first_date_in_period=True, include_month=True, return_expectations={"date": { "latest": "2020-01-31" }}, ), type1_diabetes=patients.with_these_clinical_events( diabetes_t1_codes, on_or_before="2020-01-31", return_first_date_in_period=True,
"2020-02-29", return_expectations={"incidence": 0.9}, ), has_asthma=patients.with_these_clinical_events( current_asthma_codes, between=["2017-02-28", "2020-02-29"], return_expectations={"incidence": 0.9}, ), # MEDICATIONS nsaid_last_three_years=patients.with_these_medications( nsaid_codes, between=["2017-02-28", "2020-02-29"], returning="date", find_last_match_in_period=True, include_month=True, include_day=False, return_expectations={ "date": { "earliest": "2017-02-28", "latest": "2020-02-29" } }, ), # ASPIRIN aspirin_ten_years=patients.with_these_medications( aspirin_med_codes, between=["2010-02-01", "2020-02-29"], returning="date", find_last_match_in_period=True, include_month=True, include_day=False, return_expectations={
ever_smoked=patients.with_these_clinical_events( filter_codes_by_category(clear_smoking_codes, include=["S", "E"]), on_or_before="2020-02-29", return_expectations={"incidence": 0.9}, ), has_follow_up=patients.registered_with_one_practice_between( "2019-02-28", "2020-02-29", return_expectations={"incidence": 0.9}, ), recent_asthma=patients.with_these_clinical_events( asthma_codes, between=["2017-02-28", "2020-02-29"], return_expectations={"incidence": 0.05}, ), other_respiratory=patients.with_these_clinical_events( other_respiratory_codes, on_or_before="2020-02-29", return_expectations={"incidence": 0.05}, ), #### NEBULES nebules=patients.with_these_medications( nebulised_med_codes, between=["2019-02-28", "2020-02-29"], return_expectations={"incidence": 0.05}, ), ltra_single=patients.with_these_medications( leukotriene_med_codes, between=["2019-11-01", "2020-02-29"], return_expectations={"incidence": 0.05}, ), )
date_format="YYYY-MM-DD", with_these_diagnoses= ics_codes, # placeholder https://github.com/opensafely/cohort-extractor/issues/182#issuecomment-651782064 return_expectations={"date": { "earliest": "2020-03-01" }}, ), # MEDICATIONS # NSAID nsaid_last_three_years=patients.with_these_medications( nsaid_codes, between=["2017-02-28", "2020-02-29"], returning="date", find_last_match_in_period=True, include_month=True, include_day=False, return_expectations={ "date": { "earliest": "2017-02-28", "latest": "2020-02-29" } }, ), nsaid_after_march=patients.with_these_medications( nsaid_codes, on_or_after="2020-03-01", returning="date", find_first_match_in_period=True, include_month=True, include_day=True, return_expectations={ "date": {
low_med_dose_ics_multiple_ingredient OR low_med_dose_ics OR ics_single OR laba_ics OR laba_lama_ics ) ) """, has_asthma=patients.with_these_clinical_events( asthma_codes, between=["2017-02-28", "2020-02-29"], ), has_follow_up=patients.registered_with_one_practice_between( "2019-02-28", "2020-02-29"), nebules=patients.with_these_medications( nebulised_med_codes, between=["2019-02-28", "2020-02-29"], ), any_asthma_med=patients.satisfying(""" ltra_single OR laba_lama_ics OR laba_lama OR laba_ics OR lama_single OR laba_single OR sama_single OR saba_single OR ics_single OR low_med_dose_ics OR low_med_dose_ics_multiple_ingredient OR low_med_dose_ics_single_ingredient OR high_dose_ics_multiple_ingredient OR
"incidence": 0.01, }, ), current_copd=patients.with_these_clinical_events( current_copd_codes, on_or_before=campaign_start, returning="binary_flag", return_expectations={ "incidence": 0.01, }, ), # on a dmard - indicative of immunosuppression dmards=patients.with_these_medications( dmards_codes, on_or_before=campaign_start, returning="binary_flag", return_expectations={ "incidence": 0.01, }, ), # dementia dementia=patients.with_these_clinical_events( dementia_codes, on_or_before=campaign_start, returning="binary_flag", return_expectations={ "incidence": 0.01, }, ), dialysis=patients.with_these_clinical_events( dialysis_codes, on_or_before=campaign_start,
ever_smoked AND has_follow_up AND NOT recent_asthma AND NOT other_respiratory AND NOT nebules AND NOT ltra_single """, has_follow_up=patients.registered_with_one_practice_between( "2019-02-28", "2020-02-29" ), recent_asthma=patients.with_these_clinical_events( asthma_codes, between=["2017-02-28", "2020-02-29"], ), #### NEBULES nebules=patients.with_these_medications( nebulised_med_codes, between=["2019-02-28", "2020-02-29"], ), ), ## OUTCOMES (at least one outcome or covariate is required) icu_date_admitted=patients.admitted_to_icu( on_or_after="2020-03-01", include_day=True, returning="date_admitted", return_expectations={"date": {"earliest": "2020-03-01"}}, ), died_date_cpns=patients.with_death_recorded_in_cpns( on_or_after="2020-03-01", returning="date_of_death", include_month=True, include_day=True, return_expectations={"date": {"earliest": "2020-03-01"}},
"0": 0.8, "1": 0.1, "2": 0.1 } }, }, recent_asthma_code=patients.with_these_clinical_events( asthma_codes, between=["2017-02-01", "2020-11-16"], ), asthma_code_ever=patients.with_these_clinical_events(asthma_codes), copd_code_ever=patients.with_these_clinical_events( chronic_respiratory_disease_codes), prednisolone_last_year=patients.with_these_medications( pred_codes, between=["2019-11-16", "2020-11-16"], returning="number_of_matches_in_period", ), ), # https://github.com/ebmdatalab/tpp-sql-notebook/issues/7 chronic_cardiac_disease=patients.with_these_clinical_events( chronic_cardiac_disease_codes, return_first_date_in_period=True, include_month=True, ), # https://github.com/ebmdatalab/tpp-sql-notebook/issues/30 diabetes=patients.with_these_clinical_events( diabetes_codes, return_first_date_in_period=True,