|
a |
|
b/docs/reference/cheatsheet.md |
|
|
1 |
# ehrQL cheatsheet |
|
|
2 |
|
|
|
3 |
## Frames |
|
|
4 |
|
|
|
5 |
Patient frames contain one row per patient |
|
|
6 |
|
|
|
7 |
``` |
|
|
8 |
patient id date_of_birth sex |
|
|
9 |
123 1980-01-01 m |
|
|
10 |
456 1990-06-06 f |
|
|
11 |
789 2020-01-01 i |
|
|
12 |
``` |
|
|
13 |
|
|
|
14 |
Event frames contain many rows per patient |
|
|
15 |
|
|
|
16 |
``` |
|
|
17 |
patient_id event_date event_code |
|
|
18 |
123 2020-04-01 abc |
|
|
19 |
123 2020-04-02 def |
|
|
20 |
123 2021-01-01 ghi |
|
|
21 |
``` |
|
|
22 |
|
|
|
23 |
## Simple dataset |
|
|
24 |
|
|
|
25 |
```python |
|
|
26 |
from ehrql import create_dataset |
|
|
27 |
from ehrql.tables.core import patients |
|
|
28 |
from ehrql.tables.tpp import addresses |
|
|
29 |
|
|
|
30 |
dataset = create_dataset() |
|
|
31 |
... |
|
|
32 |
dataset.define_population( |
|
|
33 |
patients.exists_for_patient() |
|
|
34 |
) |
|
|
35 |
``` |
|
|
36 |
|
|
|
37 |
## Codelists |
|
|
38 |
|
|
|
39 |
```python |
|
|
40 |
statin_medications = codelist_from_csv("codelists/dm_cod.csv", column="code") |
|
|
41 |
``` |
|
|
42 |
|
|
|
43 |
## Show |
|
|
44 |
|
|
|
45 |
```python |
|
|
46 |
from ehrql import show |
|
|
47 |
show(patients) |
|
|
48 |
``` |
|
|
49 |
|
|
|
50 |
## Tables |
|
|
51 |
|
|
|
52 |
### core |
|
|
53 |
|
|
|
54 |
```python |
|
|
55 |
clinical_events |
|
|
56 |
medications |
|
|
57 |
ons_deaths |
|
|
58 |
patients |
|
|
59 |
practice_registrations |
|
|
60 |
``` |
|
|
61 |
|
|
|
62 |
#### selected values |
|
|
63 |
|
|
|
64 |
```python |
|
|
65 |
clinical_events.date |
|
|
66 |
clinical_events.snomedct_code |
|
|
67 |
clinical_events.numeric_value |
|
|
68 |
patients.date_of_birth |
|
|
69 |
patients.sex |
|
|
70 |
patients.date_of_death |
|
|
71 |
``` |
|
|
72 |
|
|
|
73 |
### tpp |
|
|
74 |
|
|
|
75 |
```python |
|
|
76 |
addresses |
|
|
77 |
apcs |
|
|
78 |
apcs_cost |
|
|
79 |
appointments |
|
|
80 |
clinical_events |
|
|
81 |
clinical_events_ranges |
|
|
82 |
covid_therapeutics |
|
|
83 |
ec |
|
|
84 |
ec_cost |
|
|
85 |
emergency_care_attendances |
|
|
86 |
ethnicity_from_sus |
|
|
87 |
household_memberships_2020 |
|
|
88 |
medications |
|
|
89 |
occupation_on_covid_vaccine_record |
|
|
90 |
ons_deaths |
|
|
91 |
opa |
|
|
92 |
opa_cost |
|
|
93 |
opa_diag |
|
|
94 |
opa_proc |
|
|
95 |
open_prompt |
|
|
96 |
parents |
|
|
97 |
patients |
|
|
98 |
practice_registrations |
|
|
99 |
sgss_covid_all_tests |
|
|
100 |
ukrr |
|
|
101 |
vaccinations |
|
|
102 |
wl_clockstops |
|
|
103 |
wl_openpathways |
|
|
104 |
``` |
|
|
105 |
|
|
|
106 |
#### selected values |
|
|
107 |
|
|
|
108 |
```python |
|
|
109 |
addresses.address_id |
|
|
110 |
addresses.start_date |
|
|
111 |
addresses.end_date |
|
|
112 |
addresses.imd_rounded |
|
|
113 |
apcs.admission_date |
|
|
114 |
medications.date |
|
|
115 |
medications.dmd_code |
|
|
116 |
practice_registration.start_date |
|
|
117 |
practice_registration.end_date |
|
|
118 |
practice_registration.practice_pseudo_id |
|
|
119 |
practice_registration.practice_stp |
|
|
120 |
ukrr.renal_centre |
|
|
121 |
vaccinations.date |
|
|
122 |
vaccinations.product_name |
|
|
123 |
``` |
|
|
124 |
|
|
|
125 |
## adding data to a dataset |
|
|
126 |
|
|
|
127 |
### from a patient frame |
|
|
128 |
|
|
|
129 |
value series |
|
|
130 |
|
|
|
131 |
```python |
|
|
132 |
dataset.sex = patients.sex |
|
|
133 |
dataset.date_of_birth = patients.date_of_birth |
|
|
134 |
dataset.birth_year = patients.date_of_birth.year |
|
|
135 |
dataset.age = patients.age_on("2024-01-01") |
|
|
136 |
``` |
|
|
137 |
|
|
|
138 |
boolean series |
|
|
139 |
|
|
|
140 |
```python |
|
|
141 |
dataset.died_with_X = ons_deaths.cause_of_death_is_in(cause_of_death_X_codelist) |
|
|
142 |
``` |
|
|
143 |
|
|
|
144 |
### from an event frame |
|
|
145 |
|
|
|
146 |
value series |
|
|
147 |
|
|
|
148 |
```python |
|
|
149 |
dataset.imd = addresses.for_patient_on("2023-01-01").imd_rounded |
|
|
150 |
``` |
|
|
151 |
|
|
|
152 |
aggregated value series |
|
|
153 |
|
|
|
154 |
```python |
|
|
155 |
dataset.mean_hba1c = clinical_events.where( |
|
|
156 |
clinical_events.snomedct_code.is_in(hba1c_codelist) |
|
|
157 |
).where( |
|
|
158 |
clinical_events.date.is_on_or_after("2022-07-01") |
|
|
159 |
).numeric_value.mean_for_patient() |
|
|
160 |
``` |
|
|
161 |
|
|
|
162 |
sorted value series |
|
|
163 |
|
|
|
164 |
```python |
|
|
165 |
dataset.first_statin_prescription_date = medications.where( |
|
|
166 |
medications.dmd_code.is_in(statin_medications) |
|
|
167 |
).sort_by( |
|
|
168 |
medications.date |
|
|
169 |
).first_for_patient().date |
|
|
170 |
``` |
|
|
171 |
|
|
|
172 |
boolean series |
|
|
173 |
|
|
|
174 |
```python |
|
|
175 |
dataset.has_had_asthma_diagnosis = clinical_events.where( |
|
|
176 |
clinical_events.snomedct_code.is_in(asthma_codelist) |
|
|
177 |
).exists_for_patient() |
|
|
178 |
``` |
|
|
179 |
|
|
|
180 |
boolean series with a date range |
|
|
181 |
|
|
|
182 |
```python |
|
|
183 |
dataset.has_recent_cardiac_admission = apcs.where( |
|
|
184 |
apcs.primary_diagnosis.is_in(cardiac_diagnosis_codes) |
|
|
185 |
).where( |
|
|
186 |
apcs.admission_date.is_on_or_between("2022-07-01", "2023-01-01") |
|
|
187 |
).exists_for_patient() |
|
|
188 |
``` |
|
|
189 |
|
|
|
190 |
## logic operators |
|
|
191 |
|
|
|
192 |
* `==` equals |
|
|
193 |
* `!=` not equals |
|
|
194 |
* `&` and |
|
|
195 |
* `|` or |
|
|
196 |
* `~` not |
|
|
197 |
* `>` greater than |
|
|
198 |
* `>=` greater than or equals |
|
|
199 |
* `<=` less than or equals |
|
|
200 |
* `<` less than |
|
|
201 |
|
|
|
202 |
## selected functions |
|
|
203 |
|
|
|
204 |
### common |
|
|
205 |
|
|
|
206 |
```python |
|
|
207 |
.is_null() |
|
|
208 |
.is_not_null() |
|
|
209 |
.is_in() |
|
|
210 |
.contains() |
|
|
211 |
.map_values() |
|
|
212 |
``` |
|
|
213 |
|
|
|
214 |
### aggregation |
|
|
215 |
|
|
|
216 |
```python |
|
|
217 |
.minimum_for_patient() |
|
|
218 |
.maximum_for_patient() |
|
|
219 |
.sum_for_patient() |
|
|
220 |
.mean_for_patient() |
|
|
221 |
.count_for_patient() |
|
|
222 |
``` |
|
|
223 |
|
|
|
224 |
### date |
|
|
225 |
|
|
|
226 |
```python |
|
|
227 |
.is_before(other) |
|
|
228 |
.is_on_or_before(other) |
|
|
229 |
.is_after(other) |
|
|
230 |
.is_on_or_after(other) |
|
|
231 |
.is_between_but_not_on(start, end) |
|
|
232 |
.is_on_or_between(start, end) |
|
|
233 |
.is_during(interval) |
|
|
234 |
``` |
|
|
235 |
|
|
|
236 |
### sorted event frames |
|
|
237 |
|
|
|
238 |
```python |
|
|
239 |
.first_for_patient() |
|
|
240 |
.last_for_patient() |
|
|
241 |
``` |