Example #1
0
             "M": 0.49,
             "F": 0.51
         }
     },
 }),
 stp=patients.registered_practice_as_of(
     "2020-02-29",
     returning="stp_code",
     return_expectations={
         "rate": "universal",
         "category": {
             "ratios": {
                 "STP1": 0.1,
                 "STP2": 0.1,
                 "STP3": 0.1,
                 "STP4": 0.1,
                 "STP5": 0.1,
                 "STP6": 0.1,
                 "STP7": 0.1,
                 "STP8": 0.1,
                 "STP9": 0.1,
                 "STP10": 0.1,
             }
         },
     },
 ),
 imd=patients.address_as_of(
     "2020-02-29",
     returning="index_of_multiple_deprivation",
     round_to_nearest=100,
     return_expectations={
    # https://github.com/ebmdatalab/tpp-sql-notebook/issues/46
    sex=patients.sex(),

    # https://github.com/ebmdatalab/tpp-sql-notebook/issues/52
    imd=patients.address_as_of(
        "2020-02-01", returning="index_of_multiple_deprivation", round_to_nearest=100
    ),

    # https://github.com/ebmdatalab/tpp-sql-notebook/issues/37
    rural_urban=patients.address_as_of(
        "2020-02-01", returning="rural_urban_classification"
    ),

    # https://github.com/ebmdatalab/tpp-sql-notebook/issues/54
    stp=patients.registered_practice_as_of("2020-02-01", returning="stp_code"),

    # region - one of NHS England 9 regions
    region=patients.registered_practice_as_of("2020-02-01", returning="nhse_region_name"),

    # https://github.com/ebmdatalab/tpp-sql-notebook/issues/10
    bmi=patients.most_recent_bmi(
        on_or_after="2010-02-01",
        minimum_age_at_measurement=16,
        include_measurement_date=True,
        include_month=True,
    ),

    # https://github.com/ebmdatalab/tpp-sql-notebook/issues/6
    smoking_status=patients.categorised_as(
        {
Example #3
0
         "incidence": 0.6,
         "float": {
             "distribution": "normal",
             "mean": 120,
             "stddev": 10
         },
     },
 ),
 # https://github.com/ebmdatalab/tpp-sql-notebook/issues/54
 stp=patients.registered_practice_as_of(
     "2020-02-01",
     returning="stp_code",
     return_expectations={
         "rate": "universal",
         "category": {
             "ratios": {
                 "STP1": 0.5,
                 "STP2": 0.5
             }
         },
     },
 ),
 msoa=patients.registered_practice_as_of(
     "2020-02-01",
     returning="msoa_code",
     return_expectations={
         "rate": "universal",
         "category": {
             "ratios": {
                 "MSOA1": 0.5,
                 "MSOA2": 0.5
def test_patients_registered_practice_as_of():
    session = make_session()
    org_1 = Organisation(
        STPCode="123", MSOACode="E0201", Region="East of England", Organisation_ID=1
    )
    org_2 = Organisation(
        STPCode="456", MSOACode="E0202", Region="Midlands", Organisation_ID=2
    )
    org_3 = Organisation(
        STPCode="789", MSOACode="E0203", Region="London", Organisation_ID=3
    )
    org_4 = Organisation(
        STPCode="910", MSOACode="E0204", Region="North West", Organisation_ID=4
    )
    patient = Patient()
    patient.RegistrationHistory.append(
        RegistrationHistory(
            StartDate="1990-01-01", EndDate="2018-01-01", Organisation=org_1
        )
    )
    # We deliberately create overlapping registration periods so we can check
    # that we handle these correctly
    patient.RegistrationHistory.append(
        RegistrationHistory(
            StartDate="2018-01-01", EndDate="2022-01-01", Organisation=org_2
        )
    )
    patient.RegistrationHistory.append(
        RegistrationHistory(
            StartDate="2019-09-01", EndDate="2020-05-01", Organisation=org_3
        )
    )
    patient.RegistrationHistory.append(
        RegistrationHistory(
            StartDate="2022-01-01", EndDate="9999-12-31", Organisation=org_4
        )
    )
    patient_2 = Patient()
    patient_2.RegistrationHistory.append(
        RegistrationHistory(
            StartDate="2010-01-01", EndDate="9999-12-31", Organisation=org_1
        )
    )
    patient_3 = Patient()
    patient_3.RegistrationHistory.append(
        RegistrationHistory(StartDate="2010-01-01", EndDate="9999-12-31")
    )
    session.add_all([patient, patient_2, patient_3])
    session.commit()
    study = StudyDefinition(
        population=patients.all(),
        stp=patients.registered_practice_as_of("2020-01-01", returning="stp_code"),
        msoa=patients.registered_practice_as_of("2020-01-01", returning="msoa_code"),
        region=patients.registered_practice_as_of(
            "2020-01-01", returning="nhse_region_name"
        ),
        pseudo_id=patients.registered_practice_as_of(
            "2020-01-01", returning="pseudo_id"
        ),
    )
    results = study.to_dicts()
    assert [i["stp"] for i in results] == ["789", "123", ""]
    assert [i["msoa"] for i in results] == ["E0203", "E0201", ""]
    assert [i["region"] for i in results] == ["London", "East of England", ""]
    assert [i["pseudo_id"] for i in results] == ["3", "1", "0"]