generated from opensafely/research-template
/
measures_checks.py
138 lines (107 loc) · 4.84 KB
/
measures_checks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
###########################################################
# The purpose of this script is to check the distributions
# of records over time.
# It counts the monthly number of records by RTT start date,
# RTT end date and week ending date.
# It also counts number of patients by RTT end date.
###########################################################
from ehrql import INTERVAL, Measures, months
from ehrql.tables.tpp import (
patients,
wl_clockstops)
##########
# # Monthly counts - overall, admitted, and not admitted
# count_overall = wl_clockstops.where(
# wl_clockstops.referral_to_treatment_period_end_date.is_during(INTERVAL)
# ).pseudo_referral_identifier.count_distinct_for_patient()
# count_admitted = wl_clockstops.where(
# wl_clockstops.referral_to_treatment_period_end_date.is_during(INTERVAL)
# & wl_clockstops.waiting_list_type.is_in(["IRTT","PTLI","RTTI"])
# ).pseudo_referral_identifier.count_distinct_for_patient()
# count_not_admitted = wl_clockstops.where(
# wl_clockstops.referral_to_treatment_period_end_date.is_during(INTERVAL)
# & wl_clockstops.waiting_list_type.is_in(["ORTT","PTLO","RTTO"])
# ).pseudo_referral_identifier.count_distinct_for_patient()
# Monthly counts - orthopaedic only
count_ortho = wl_clockstops.where(
wl_clockstops.referral_to_treatment_period_end_date.is_during(INTERVAL)
& wl_clockstops.activity_treatment_function_code.is_in(["110","111","108","115"])
).pseudo_referral_identifier.count_distinct_for_patient()
count_admitted_ortho = wl_clockstops.where(
wl_clockstops.referral_to_treatment_period_end_date.is_during(INTERVAL)
& wl_clockstops.waiting_list_type.is_in(["IRTT","PTLI","RTTI"])
& wl_clockstops.activity_treatment_function_code.is_in(["110","111","108","115"])
).pseudo_referral_identifier.count_distinct_for_patient()
count_not_admitted_ortho = wl_clockstops.where(
wl_clockstops.referral_to_treatment_period_end_date.is_during(INTERVAL)
& wl_clockstops.waiting_list_type.is_in(["ORTT","PTLO","RTTO"])
& wl_clockstops.activity_treatment_function_code.is_in(["110","111","108","115"])
).pseudo_referral_identifier.count_distinct_for_patient()
# By treatment specialty (only include most common groups reported in public statistics)
# trt_func = ["100","120","130","140","150","160","170","300","301","320","330","340","400","410","430","502",]
# count_var = {}
# for code in trt_func:
# count_var["count_" + code] = wl_clockstops.where(
# wl_clockstops.referral_to_treatment_period_end_date.is_during(INTERVAL)
# & wl_clockstops.activity_treatment_function_code.is_in([code])
# ).pseudo_referral_identifier.count_distinct_for_patient()
# count_var["count_admitted_" + code] = wl_clockstops.where(
# wl_clockstops.referral_to_treatment_period_end_date.is_during(INTERVAL)
# & wl_clockstops.waiting_list_type.is_in(["IRTT","PTLI","RTTI"])
# & wl_clockstops.activity_treatment_function_code.is_in([code])
# ).pseudo_referral_identifier.count_distinct_for_patient()
# count_var["count_not_admitted_" + code] = wl_clockstops.where(
# wl_clockstops.referral_to_treatment_period_end_date.is_during(INTERVAL)
# & wl_clockstops.waiting_list_type.is_in(["ORTT","PTLO","RTTO"])
# & wl_clockstops.activity_treatment_function_code.is_in([code])
# ).pseudo_referral_identifier.count_distinct_for_patient()
######
measures = Measures()
measures.configure_dummy_data(population_size=5000)
denominator = (
(patients.age_on(INTERVAL.start_date) >= 0)
& (patients.age_on(INTERVAL.start_date) < 110)
& (patients.sex.is_in(["male","female"]))
& wl_clockstops.exists_for_patient()
)
measures.define_defaults(
denominator = denominator,
intervals=months(13).starting_on("2021-05-01")
)
# measures.define_measure(
# name="closed_total",
# numerator=count_overall
# )
measures.define_measure(
name="closed_ortho",
numerator=count_ortho
)
# measures.define_measure(
# name="closed_admit_total",
# numerator=count_admitted
# )
measures.define_measure(
name="closed_admit_ortho",
numerator=count_admitted_ortho
)
# measures.define_measure(
# name="closed_not_admit_total",
# numerator=count_not_admitted
# )
measures.define_measure(
name="closed_not_admit_ortho",
numerator=count_not_admitted_ortho
)
# for code in trt_func:
# measures.define_measure(
# name=f"count_{code}",
# numerator=count_var["count_" + code]
# )
# measures.define_measure(
# name=f"count_admit_{code}",
# numerator=count_var["count_admitted_" + code]
# )
# measures.define_measure(
# name=f"count_not_admit_{code}",
# numerator=count_var["count_not_admitted_" + code]
# )