/
referral_test.py
150 lines (125 loc) · 4.93 KB
/
referral_test.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
from ehrql import Dataset, years, days, months, minimum_of, maximum_of, case, when
from ehrql.tables.core import patients, clinical_events
from ehrql.tables.tpp import practice_registrations, ons_deaths, opa, opa_proc, apcs
import codelists
index_date = "2018-03-23"
end_date = "2023-10-22"
dataset = Dataset()
lowerGI_2ww_ref = clinical_events.where(clinical_events.snomedct_code.is_in(codelists.colorectal_referral_codes)
).where(
clinical_events.date.is_on_or_between(index_date, end_date)
).sort_by(
clinical_events.date
).first_for_patient()
has_lowerGI_2ww = lowerGI_2ww_ref.exists_for_patient()
dataset.define_population(
has_lowerGI_2ww
)
dataset.lowerGI_2ww_date = lowerGI_2ww_ref.date
first_attendance_code = ["1", "3"]
colorectal_surg_clinic_code = ["104"]
gastro_clinic_code = ["301"]
#colonoscopy_code = ["H22", "H18", "H25", "H28"]
"""
colorectal_surg_clinic_21days = (
opa.where(opa.treatment_function_code.is_in(colorectal_surg_clinic_code)
).where(
opa.appointment_date.is_on_or_between(dataset.lowerGI_2ww_date, dataset.lowerGI_2ww_date + days(21))
).where(
opa.first_attendance.is_in(first_attendance_code)
).sort_by(
opa.appointment_date
).first_for_patient()
)
"""
colorectal_surg_clinic_6weeks = (
opa.where(opa.treatment_function_code.is_in(colorectal_surg_clinic_code)
).where(
opa.appointment_date.is_on_or_between(dataset.lowerGI_2ww_date, dataset.lowerGI_2ww_date + days(42))
).where(
opa.first_attendance.is_in(first_attendance_code)
).sort_by(
opa.appointment_date
).first_for_patient()
)
#dataset.colorectal_surg_clinic_21d = colorectal_surg_clinic_21days.exists_for_patient()
dataset.colorectal_surg_clinic_6w = colorectal_surg_clinic_6weeks.exists_for_patient()
"""
gastro_clinic_21days = (
opa.where(opa.treatment_function_code.is_in(gastro_clinic_code)
).where(
opa.appointment_date.is_on_or_between(dataset.lowerGI_2ww_date, dataset.lowerGI_2ww_date + days(21))
).where(
opa.first_attendance.is_in(first_attendance_code)
).sort_by(
opa.appointment_date
).first_for_patient()
)
"""
gastro_clinic_6weeks = (
opa.where(opa.treatment_function_code.is_in(gastro_clinic_code)
).where(
opa.appointment_date.is_on_or_between(dataset.lowerGI_2ww_date, dataset.lowerGI_2ww_date + days(42))
).where(
opa.first_attendance.is_in(first_attendance_code)
).sort_by(
opa.appointment_date
).first_for_patient()
)
#dataset.gastro_clinic_21d = gastro_clinic_21days.exists_for_patient()
dataset.gastro_clinic_6w = gastro_clinic_6weeks.exists_for_patient()
"""
colonoscopy_21days = (
opa_proc.where(opa_proc.primary_procedure_code.is_in(colonoscopy_code)
).where(
opa_proc.appointment_date.is_on_or_between(dataset.lowerGI_2ww_date, dataset.lowerGI_2ww_date + days(21))
).sort_by(
opa_proc.appointment_date
).first_for_patient()
)
colonoscopy_1month = (
opa_proc.where(opa_proc.primary_procedure_code.is_in(colonoscopy_code)
).where(
opa_proc.appointment_date.is_on_or_between(dataset.lowerGI_2ww_date, dataset.lowerGI_2ww_date + months(1))
).sort_by(
opa_proc.appointment_date
).first_for_patient()
)
dataset.colonoscopy_21d = colonoscopy_21days.exists_for_patient()
dataset.colonoscopy_1m = colonoscopy_1month.exists_for_patient()
"""
lowergi_diagnostic_6weeks = (
apcs.where(apcs.spell_core_hrg_sus.is_in(codelists.lowerGI_diagnostic_codes)
).where(
apcs.admission_date.is_on_or_between(dataset.lowerGI_2ww_date, dataset.lowerGI_2ww_date + days(42))
).sort_by(
apcs.admission_date
).first_for_patient()
)
dataset.lowergi_diagnostic_6w = lowergi_diagnostic_6weeks.exists_for_patient()
opa_1month = (
opa.where(opa.appointment_date.is_on_or_between(dataset.lowerGI_2ww_date, dataset.lowerGI_2ww_date + months(1))
).where(
opa.first_attendance.is_in(first_attendance_code)
).sort_by(
opa.appointment_date
).first_for_patient()
)
dataset.opa_1m_tfc = opa_1month.treatment_function_code
"""
proc_1month = (
opa_proc.where(opa_proc.appointment_date.is_on_or_between(dataset.lowerGI_2ww_date, dataset.lowerGI_2ww_date + months(1))
).sort_by(
opa_proc.appointment_date
).first_for_patient()
)
dataset.proc_1m_opcs = proc_1month.primary_procedure_code
"""
apcs_6weeks = (
apcs.where(apcs.admission_date.is_on_or_between(dataset.lowerGI_2ww_date, dataset.lowerGI_2ww_date + days(42))
).sort_by(
apcs.admission_date
).first_for_patient()
)
dataset.apcs_6w_icd10 = apcs_6weeks.primary_diagnosis
dataset.apcs_6w_hrg = apcs_6weeks.spell_core_hrg_sus