def test_recursive_definitions_produce_errors(): with pytest.raises(ValueError): StudyDefinition( population=patients.all(), this=patients.satisfying("that = 1"), that=patients.satisfying("this = 1"), )
def test_patients_categorised_as(): session = make_session() session.add_all( [ Patient( Sex="M", CodedEvents=[ CodedEvent(CTV3Code="foo1", ConsultationDate="2000-01-01") ], ), Patient( Sex="F", CodedEvents=[ CodedEvent(CTV3Code="foo2", ConsultationDate="2000-01-01"), CodedEvent(CTV3Code="bar1", ConsultationDate="2000-01-01"), ], ), Patient( Sex="M", CodedEvents=[ CodedEvent(CTV3Code="foo2", ConsultationDate="2000-01-01") ], ), Patient( Sex="F", CodedEvents=[ CodedEvent(CTV3Code="foo3", ConsultationDate="2000-01-01") ], ), ] ) session.commit() foo_codes = codelist([("foo1", "A"), ("foo2", "B"), ("foo3", "C")], "ctv3") bar_codes = codelist(["bar1"], "ctv3") study = StudyDefinition( population=patients.all(), category=patients.categorised_as( { "W": "foo_category = 'B' AND female_with_bar", "X": "sex = 'F' AND (foo_category = 'B' OR foo_category = 'C')", "Y": "sex = 'M' AND foo_category = 'A'", "Z": "DEFAULT", }, sex=patients.sex(), foo_category=patients.with_these_clinical_events( foo_codes, returning="category", find_last_match_in_period=True ), female_with_bar=patients.satisfying( "has_bar AND sex = 'F'", has_bar=patients.with_these_clinical_events(bar_codes), ), ), ) results = study.to_dicts() assert [x["category"] for x in results] == ["Y", "W", "Z", "X"] # Assert that internal columns do not appear assert "foo_category" not in results[0].keys() assert "female_with_bar" not in results[0].keys() assert "has_bar" not in results[0].keys()
def test_column_name_clashes_produce_errors(): with pytest.raises(ValueError): StudyDefinition( population=patients.all(), age=patients.age_as_of("2020-01-01"), status=patients.satisfying( "age > 70 AND sex = 'M'", sex=patients.sex(), age=patients.age_as_of("2010-01-01"), ), )
def test_patients_satisfying_with_hidden_columns(): condition_code = "ASTHMA" condition_code2 = "COPD" session = make_session() patient_1 = Patient(DateOfBirth="1940-01-01", Sex="M") patient_2 = Patient(DateOfBirth="1940-01-01", Sex="F") patient_3 = Patient(DateOfBirth="1990-01-01", Sex="M") patient_4 = Patient(DateOfBirth="1940-01-01", Sex="F") patient_4.CodedEvents.append( CodedEvent(CTV3Code=condition_code, ConsultationDate="2010-01-01") ) patient_5 = Patient(DateOfBirth="1940-01-01", Sex="F") patient_5.CodedEvents.append( CodedEvent(CTV3Code=condition_code, ConsultationDate="2010-01-01") ) patient_5.CodedEvents.append( CodedEvent(CTV3Code=condition_code2, ConsultationDate="2010-01-01") ) session.add_all([patient_1, patient_2, patient_3, patient_4, patient_5]) session.commit() study = StudyDefinition( population=patients.all(), sex=patients.sex(), age=patients.age_as_of("2020-01-01"), at_risk=patients.satisfying( """ (age > 70 AND sex = "M") OR (has_asthma AND NOT copd) """, has_asthma=patients.with_these_clinical_events( codelist([condition_code], "ctv3") ), copd=patients.with_these_clinical_events( codelist([condition_code2], "ctv3") ), ), ) results = study.to_dicts() assert [i["at_risk"] for i in results] == ["1", "0", "0", "1", "0"] assert "has_asthma" not in results[0].keys()
def test_make_df_from_expectations_with_satisfying(): study = StudyDefinition( population=patients.all(), has_condition=patients.satisfying( "condition_a OR condition_b", condition_a=patients.with_these_clinical_events( codelist(["A", "B", "C"], system="ctv3")), condition_b=patients.with_these_clinical_events( codelist(["X", "Y", "Z"], system="ctv3")), return_expectations={ "date": { "earliest": "2001-01-01", "latest": "2020-03-01" }, "incidence": 0.95, }, ), ) population_size = 10000 result = study.make_df_from_expectations(population_size) assert result.columns == ["has_condition"]
def test_patients_satisfying(): condition_code = "ASTHMA" session = make_session() patient_1 = Patient(DateOfBirth="1940-01-01", Sex="M") patient_2 = Patient(DateOfBirth="1940-01-01", Sex="F") patient_3 = Patient(DateOfBirth="1990-01-01", Sex="M") patient_4 = Patient(DateOfBirth="1940-01-01", Sex="F") patient_4.CodedEvents.append( CodedEvent(CTV3Code=condition_code, ConsultationDate="2010-01-01") ) session.add_all([patient_1, patient_2, patient_3, patient_4]) session.commit() study = StudyDefinition( population=patients.all(), sex=patients.sex(), age=patients.age_as_of("2020-01-01"), has_asthma=patients.with_these_clinical_events( codelist([condition_code], "ctv3") ), at_risk=patients.satisfying("(age > 70 AND sex = 'M') OR has_asthma"), ) results = study.to_dicts() assert [i["at_risk"] for i in results] == ["1", "0", "0", "1"]
def test_using_expression_in_population_definition(): session = make_session() session.add_all( [ Patient( Sex="M", DateOfBirth="1970-01-01", CodedEvents=[ CodedEvent(CTV3Code="foo1", ConsultationDate="2000-01-01") ], ), Patient(Sex="M", DateOfBirth="1975-01-01"), Patient( Sex="F", DateOfBirth="1980-01-01", CodedEvents=[ CodedEvent(CTV3Code="foo1", ConsultationDate="2000-01-01") ], ), Patient(Sex="F", DateOfBirth="1985-01-01"), ] ) session.commit() study = StudyDefinition( population=patients.satisfying( "has_foo_code AND sex = 'M'", has_foo_code=patients.with_these_clinical_events( codelist(["foo1"], "ctv3") ), sex=patients.sex(), ), age=patients.age_as_of("2020-01-01"), ) results = study.to_dicts() assert results[0].keys() == {"patient_id", "age"} assert [i["age"] for i in results] == ["50"]
"rate": "uniform", "incidence": 0.2, }, ## STUDY POPULATION (required) population=patients.satisfying( """ copd AND (age >=35 AND age <= 110) AND 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,
asthma_codes, between=["2017-02-28", "2020-02-29"], return_expectations={"incidence": 0.5}, ), asthma_ever=patients.with_these_clinical_events( asthma_ever_codes, on_or_before="2020-02-29", return_expectations={"incidence": 0.8}, ), age_cat=patients.satisfying( "age >=18 AND age <= 110", return_expectations={"incidence": 0.9}, age=patients.age_as_of( "2020-02-29", return_expectations={ "rate": "universal", "int": { "distribution": "population_ages" }, }, ), ), 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(
default_expectations={ "date": {"earliest": "1970-01-01", "latest": "today"}, "rate": "uniform", "incidence": 0.05, }, ## STUDY POPULATION (required) population=patients.all(), copd=patients.with_these_clinical_events( copd_codes, on_or_before="2020-02-29", return_expectations={"incidence": 0.4}, ), age_cat=patients.satisfying( "age >=35 AND age <= 110", return_expectations={"incidence": 0.9}, age=patients.age_as_of( "2020-02-29", return_expectations={ "rate": "universal", "int": {"distribution": "population_ages"}, }, ), ), 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,
population=patients.satisfying( """ ( has_asthma OR (asthma_ever AND any_asthma_med) ) AND (age >=18 AND age <= 110) AND has_follow_up AND NOT copd AND NOT other_respiratory AND NOT nebules AND NOT ( (lama_single OR laba_lama) AND NOT ( high_dose_ics OR high_dose_ics_single_ing OR high_dose_ics_multiple_ingredient OR low_med_dose_ics_single_ingredient OR 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 high_dose_ics_single_ing OR high_dose_ics """)),