generated from opensafely/research-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
variable_helper_functions.py
182 lines (163 loc) · 6.66 KB
/
variable_helper_functions.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import operator
from functools import reduce # for function building, e.g. any_of
from ehrql.tables.tpp import (
patients,
practice_registrations,
addresses,
appointments,
occupation_on_covid_vaccine_record,
vaccinations,
sgss_covid_all_tests,
apcs,
ec,
opa,
opa_diag,
clinical_events,
medications,
ons_deaths,
emergency_care_attendances,
)
def ever_matching_event_clinical_ctv3_before(codelist, start_date, where=True):
return(
clinical_events.where(where)
.where(clinical_events.ctv3_code.is_in(codelist))
.where(clinical_events.date.is_before(start_date))
)
def last_matching_event_clinical_ctv3_before(codelist, start_date, where=True):
return(
clinical_events.where(where)
.where(clinical_events.ctv3_code.is_in(codelist))
.where(clinical_events.date.is_before(start_date))
.sort_by(clinical_events.date)
.last_for_patient()
)
def last_matching_event_clinical_snomed_before(codelist, start_date, where=True):
return(
clinical_events.where(where)
.where(clinical_events.snomedct_code.is_in(codelist))
.where(clinical_events.date.is_before(start_date))
.sort_by(clinical_events.date)
.last_for_patient()
)
def last_matching_med_dmd_before(codelist, start_date, where=True):
return(
medications.where(where)
.where(medications.dmd_code.is_in(codelist))
.where(medications.date.is_before(start_date))
.sort_by(medications.date)
.last_for_patient()
)
def last_matching_event_apc_before(codelist, start_date, where=True):
return(
apcs.where(where)
.where(apcs.primary_diagnosis.is_in(codelist) | apcs.secondary_diagnosis.is_in(codelist))
.where(apcs.admission_date.is_before(start_date))
.sort_by(apcs.admission_date)
.last_for_patient()
)
def last_matching_event_opa_before(codelist, start_date, where=True):
return(
opa_diag.where(where)
.where(opa_diag.primary_diagnosis_code.is_in(codelist) | opa_diag.secondary_diagnosis_code_1.is_in(codelist))
.where(opa_diag.appointment_date.is_before(start_date))
.sort_by(opa_diag.appointment_date)
.last_for_patient()
)
# helper function
def any_of(conditions):
return reduce(operator.or_, conditions)
def last_matching_event_ec_snomed_before(codelist, start_date, where=True):
conditions = [
getattr(emergency_care_attendances, column_name).is_in(codelist)
for column_name in ([f"diagnosis_{i:02d}" for i in range(1, 25)])
]
return(
emergency_care_attendances.where(where)
.where(any_of(conditions))
.where(emergency_care_attendances.arrival_date.is_before(start_date))
.sort_by(emergency_care_attendances.arrival_date)
.last_for_patient()
)
def matching_death_before(codelist, start_date, where=True):
conditions = [
getattr(ons_deaths, column_name).is_in(codelist)
for column_name in (["underlying_cause_of_death"] + [f"cause_of_death_{i:02d}" for i in range(1, 16)])
]
return any_of(conditions) & ons_deaths.date.is_before(start_date)
def last_matching_event_clinical_snomed_between(codelist, start_date, end_date, where=True):
return(
clinical_events.where(where)
.where(clinical_events.snomedct_code.is_in(codelist))
.where(clinical_events.date.is_on_or_between(start_date, end_date))
.sort_by(clinical_events.date)
.last_for_patient()
)
def last_matching_med_dmd_between(codelist, start_date, end_date, where=True):
return(
medications.where(where)
.where(medications.dmd_code.is_in(codelist))
.where(medications.date.is_on_or_between(start_date, end_date))
.sort_by(medications.date)
.last_for_patient()
)
def first_matching_event_clinical_ctv3_between(codelist, start_date, end_date, where=True):
return(
clinical_events.where(where)
.where(clinical_events.ctv3_code.is_in(codelist))
.where(clinical_events.date.is_on_or_between(start_date, end_date))
.sort_by(clinical_events.date)
.first_for_patient()
)
def first_matching_event_clinical_snomed_between(codelist, start_date, end_date, where=True):
return(
clinical_events.where(where)
.where(clinical_events.snomedct_code.is_in(codelist))
.where(clinical_events.date.is_on_or_between(start_date, end_date))
.sort_by(clinical_events.date)
.first_for_patient()
)
def first_matching_med_dmd_between(codelist, start_date, end_date, where=True):
return(
medications.where(where)
.where(medications.dmd_code.is_in(codelist))
.where(medications.date.is_on_or_between(start_date, end_date))
.sort_by(medications.date)
.first_for_patient()
)
def first_matching_event_apc_between(codelist, start_date, end_date, where=True):
return(
apcs.where(where)
.where(apcs.primary_diagnosis.is_in(codelist) | apcs.secondary_diagnosis.is_in(codelist))
.where(apcs.admission_date.is_on_or_between(start_date, end_date))
.sort_by(apcs.admission_date)
.first_for_patient()
)
def first_matching_event_opa_between(codelist, start_date, end_date, where=True):
return(
opa_diag.where(where)
.where(opa_diag.primary_diagnosis_code.is_in(codelist) | opa_diag.secondary_diagnosis_code_1.is_in(codelist))
.where(opa_diag.appointment_date.is_on_or_between(start_date, end_date))
.sort_by(opa_diag.appointment_date)
.first_for_patient()
)
def first_matching_event_ec_snomed_between(codelist, start_date, end_date, where=True):
conditions = [
getattr(emergency_care_attendances, column_name).is_in(codelist)
for column_name in ([f"diagnosis_{i:02d}" for i in range(1, 25)])
]
return(
emergency_care_attendances.where(where)
.where(any_of(conditions))
.where(emergency_care_attendances.arrival_date.is_on_or_between(start_date, end_date))
.sort_by(emergency_care_attendances.arrival_date)
.first_for_patient()
)
def matching_death_between(codelist, start_date, end_date, where=True):
conditions = [
getattr(ons_deaths, column_name).is_in(codelist)
for column_name in (["underlying_cause_of_death"] + [f"cause_of_death_{i:02d}" for i in range(1, 16)])
]
return any_of(conditions) & ons_deaths.date.is_on_or_between(start_date, end_date)
# filter a codelist based on whether its values included a specified set of allowed values (include)
def filter_codes_by_category(codelist, include):
return {k:v for k,v in codelist.items() if v in include}