[f54d94]: / tests / labelers / test_CodeLabelers.py

Download this file

445 lines (380 with data), 16.0 kB

  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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
import datetime
import pathlib
from typing import List, Set
# Needed to import `tools` for local testing
from femr_test_tools import EventsWithLabels, run_test_for_labeler
from femr.labelers import TimeHorizon
from femr.labelers.omop import (
AKICodeLabeler,
AnemiaCodeLabeler,
CodeLabeler,
HyperkalemiaCodeLabeler,
HypoglycemiaCodeLabeler,
HyponatremiaCodeLabeler,
LupusCodeLabeler,
MortalityCodeLabeler,
NeutropeniaCodeLabeler,
OMOPConceptCodeLabeler,
ThrombocytopeniaCodeLabeler,
)
#############################################
#############################################
#
# Generic CodeLabeler
#
#############################################
#############################################
def test_outcome_codes(tmp_path: pathlib.Path):
time_horizon = TimeHorizon(datetime.timedelta(days=0), datetime.timedelta(days=10))
# One outcome
labeler = CodeLabeler(["2"], time_horizon)
events_with_labels: EventsWithLabels = [
(((2015, 1, 3), 2, None), "duplicate"),
(((2015, 1, 3), 4, None), "duplicate"),
(((2015, 1, 3), 1, None), "duplicate"),
(((2015, 1, 3), 3, None), "skip"),
(((2015, 10, 5), 1, None), False),
(((2018, 1, 3), 2, None), "skip"),
(((2018, 3, 1), 4, None), False),
(((2018, 3, 3), 1, None), False),
(((2018, 5, 2), 5, None), True),
(((2018, 5, 3), 2, None), "skip"),
(((2018, 5, 3, 11), 1, None), False),
(((2018, 5, 4), 1, None), "duplicate"),
(((2018, 5, 4), 4, None), False),
(((2018, 11, 1), 5, None), False),
(((2018, 12, 4), 1, None), False),
(((2018, 12, 30), 4, None), "out of range"),
]
run_test_for_labeler(labeler, events_with_labels, help_text="test_outcome_codes_one")
# Zero outcome
labeler = CodeLabeler([], time_horizon)
events_with_labels = [
(((2015, 1, 3), 2, None), "duplicate"),
(((2015, 1, 3), 4, None), "duplicate"),
(((2015, 1, 3), 1, None), "duplicate"),
(((2015, 1, 3), 3, None), False),
(((2015, 10, 5), 1, None), False),
(((2018, 1, 3), 2, None), False),
(((2018, 3, 1), 4, None), False),
(((2018, 3, 3), 1, None), False),
(((2018, 5, 2), 5, None), False),
(((2018, 5, 3), 2, None), False),
(((2018, 5, 3, 11), 1, None), False),
(((2018, 5, 4), 1, None), "duplicate"),
(((2018, 5, 4), 4, None), False),
(((2018, 11, 1), 5, None), False),
(((2018, 12, 4), 1, None), False),
(((2018, 12, 30), 4, None), "out of range"),
]
run_test_for_labeler(labeler, events_with_labels, help_text="test_outcome_codes_zero")
# Multiple outcomes
labeler = CodeLabeler(["1", "4"], time_horizon)
events_with_labels = [
(((2015, 1, 3), 2, None), "duplicate"),
(((2015, 1, 3), 4, None), "duplicate"),
(((2015, 1, 3), 1, None), "duplicate"),
(((2015, 1, 3), 3, None), "skip"),
(((2015, 10, 5), 1, None), "skip"),
(((2018, 1, 3), 2, None), False),
(((2018, 3, 1), 4, None), "skip"),
(((2018, 3, 3), 1, None), "skip"),
(((2018, 5, 2), 5, None), False),
(((2018, 5, 3), 2, None), False),
(((2018, 6, 2), 0, None), True),
(((2018, 6, 3, 11), 1, None), "skip"),
(((2018, 6, 3, 23), 3, None), False),
(((2018, 9, 1), 3, None), True),
(((2018, 9, 4), 4, None), "skip"),
(((2018, 11, 1), 5, None), False),
(((2018, 12, 3), 0, None), True),
(((2018, 12, 4), 4, None), "skip"),
(((2018, 12, 30), 0, None), "out of range"),
]
run_test_for_labeler(labeler, events_with_labels, help_text="test_outcome_codes_multiple")
def test_prediction_codes(tmp_path: pathlib.Path):
# One outcome + multiple predictions
time_horizon = TimeHorizon(datetime.timedelta(days=0), datetime.timedelta(days=10))
labeler = CodeLabeler(["2"], time_horizon, prediction_codes=["4", "5"])
events_with_labels: EventsWithLabels = [
(((2015, 1, 3), 2, None), "skip"),
(((2015, 1, 3), 4, None), "skip"),
(((2015, 1, 3), 1, None), "skip"),
(((2015, 1, 3), 3, None), "skip"),
(((2015, 10, 5), 1, None), "skip"),
(((2018, 1, 3), 2, None), "skip"),
(((2018, 3, 1), 4, None), False),
(((2018, 3, 3), 1, None), "skip"),
(((2018, 5, 2), 5, None), True),
(((2018, 5, 3), 2, None), "skip"),
(((2018, 5, 3, 11), 1, None), "skip"),
(((2018, 5, 4), 4, None), False),
(((2018, 5, 4), 1, None), "skip"),
(((2018, 11, 1), 5, None), False),
(((2018, 12, 4), 1, None), "skip"),
(((2018, 12, 30), 4, None), "out of range"),
]
run_test_for_labeler(labeler, events_with_labels, help_text="prediction_codes_one_outcomes")
# Multiple outcomes + multiple predictions
labeler = CodeLabeler(["2", "6", "7"], time_horizon, prediction_codes=["4", "5"])
events_with_labels = [
(((2010, 1, 1), 2, None), "skip"),
(((2010, 1, 3), 4, None), True),
(((2010, 1, 8), 6, None), "skip"),
(((2010, 2, 1), 5, None), True),
(((2010, 2, 9), 7, None), "skip"),
(((2010, 2, 11), 4, None), False),
(((2015, 1, 3), 2, None), "skip"),
(((2015, 1, 3), 4, None), "skip"),
(((2015, 1, 3), 1, None), "skip"),
(((2015, 1, 3), 3, None), "skip"),
(((2015, 10, 5), 1, None), "skip"),
(((2018, 1, 3), 2, None), "skip"),
(((2018, 3, 1), 4, None), True),
(((2018, 3, 2), 7, None), "skip"),
(((2018, 3, 3), 1, None), "skip"),
(((2018, 5, 2), 5, None), True),
(((2018, 5, 3), 2, None), "skip"),
(((2018, 5, 3, 11), 1, None), "skip"),
(((2018, 5, 4), 4, None), False),
(((2018, 5, 4), 1, None), "skip"),
(((2018, 11, 1), 5, None), False),
(((2018, 12, 4), 1, None), "skip"),
(((2018, 12, 30), 4, None), "out of range"),
]
run_test_for_labeler(
labeler,
events_with_labels,
help_text="prediction_codes_multiple_outcomes",
)
# Multiple outcomes + no predictions
labeler = CodeLabeler(["2", "6", "7"], time_horizon, prediction_codes=[])
events_with_labels = [
(((2010, 1, 1), 2, None), "skip"),
(((2010, 1, 3), 4, None), "skip"),
(((2010, 1, 8), 6, None), "skip"),
(((2010, 2, 1), 5, None), "skip"),
(((2010, 2, 9), 7, None), "skip"),
(((2010, 2, 11), 4, None), "skip"),
(((2015, 1, 3), 2, None), "skip"),
(((2015, 1, 3), 4, None), "skip"),
(((2015, 1, 3), 1, None), "skip"),
(((2015, 1, 3), 3, None), "skip"),
(((2015, 10, 5), 1, None), "skip"),
(((2018, 1, 3), 2, None), "skip"),
(((2018, 3, 1), 4, None), "skip"),
(((2018, 3, 2), 7, None), "skip"),
(((2018, 3, 3), 1, None), "skip"),
(((2018, 5, 2), 5, None), "skip"),
(((2018, 5, 3), 2, None), "skip"),
(((2018, 5, 3, 11), 1, None), "skip"),
(((2018, 5, 4), 4, None), "skip"),
(((2018, 5, 4), 1, None), "skip"),
(((2018, 11, 1), 5, None), "skip"),
(((2018, 12, 4), 1, None), "skip"),
(((2018, 12, 30), 4, None), "skip"),
]
run_test_for_labeler(
labeler,
events_with_labels,
help_text="prediction_codes_zero_predictions",
)
#############################################
#############################################
#
# Generic OMOPConceptCodeLabeler
#
#############################################
#############################################
class DummyOntology_Base:
def get_children(self, code: str) -> Set[str]:
return set()
def get_all_children(self, code: str) -> Set[str]:
val = {code}
for child in self.get_children(code):
val |= self.get_all_children(child)
return val
class DummyOntology_OMOPConcept(DummyOntology_Base):
def get_children(self, parent_code: str) -> Set[str]:
if parent_code == "OMOP_CONCEPT_A":
return {"OMOP_CONCEPT_A_CHILD", "OMOP_CONCEPT_A_CHILD2"}
elif parent_code == "OMOP_CONCEPT_B":
return {"OMOP_CONCEPT_B_CHILD"}
elif parent_code == "OMOP_CONCEPT_A_CHILD":
return {"OMOP_CONCEPT_A_CHILD_CHILD"}
else:
return set()
class DummyLabeler_OMOPConcept(OMOPConceptCodeLabeler):
original_omop_concept_codes = [
"OMOP_CONCEPT_A",
"OMOP_CONCEPT_B",
]
def test_omop_concept_code_labeler(tmp_path: pathlib.Path):
time_horizon = TimeHorizon(datetime.timedelta(days=0), datetime.timedelta(days=10))
ontology = DummyOntology_OMOPConcept()
labeler = DummyLabeler_OMOPConcept(ontology, time_horizon, prediction_codes=["1", "2"]) # type: ignore
assert set(labeler.outcome_codes) == {
"OMOP_CONCEPT_A_CHILD_CHILD",
"OMOP_CONCEPT_B",
"OMOP_CONCEPT_B_CHILD",
"OMOP_CONCEPT_A_CHILD2",
"OMOP_CONCEPT_A",
"OMOP_CONCEPT_A_CHILD",
}
assert labeler.prediction_codes == ["1", "2"]
assert labeler.get_time_horizon() == time_horizon
#############################################
#############################################
#
# Specific instances of CodeLabeler
#
#############################################
#############################################
#############################################
# MortalityCodeLabeler
#############################################
class DummyOntology_Mortality(DummyOntology_Base):
def get_children(self, parent_code: str) -> Set[str]:
return set()
def test_MortalityCodeLabeler() -> None:
"""Create a MortalityCodeLabeler for codes 3 and 6"""
time_horizon = TimeHorizon(datetime.timedelta(days=0), datetime.timedelta(days=180))
events_with_labels: EventsWithLabels = [
(((1995, 1, 3), 0, 34.5), False),
(((2000, 1, 1), 1, "test_value"), True),
(((2000, 1, 5), 2, 1), True),
(((2000, 6, 5), "SNOMED/419620001", True), "skip"),
(((2005, 2, 5), 2, None), False),
(((2005, 7, 5), 2, None), False),
(((2010, 10, 5), 1, None), False),
(((2015, 2, 5, 0), 2, None), False),
(((2015, 7, 5, 0), 0, None), True),
(((2015, 11, 5, 10, 10), 2, None), True),
(((2015, 11, 15, 11), "SNOMED/419620001", None), "skip"),
(((2020, 1, 1), 2, None), "out of range"),
(((2020, 3, 1, 10, 10, 10), 2, None), "out of range"),
]
ontology = DummyOntology_Mortality()
# Run labeler
labeler = MortalityCodeLabeler(ontology, time_horizon) # type: ignore
run_test_for_labeler(labeler, events_with_labels, help_text="MortalityLabeler")
#############################################
# LupusCodeLabeler
#############################################
class DummyOntology_Lupus(DummyOntology_Base):
def get_children(self, parent_code: str) -> Set[str]:
if parent_code == "SNOMED/55464009":
return {"SNOMED_55464009", "Lupus_child_seven", "Lupus_child_nine", "Lupus_child_ten"}
else:
return set()
def test_LupusCodeLabeler() -> None:
"""Create a LupusCodeLabeler for codes 3 and 6"""
time_horizon = TimeHorizon(datetime.timedelta(days=0), datetime.timedelta(days=180))
events_with_labels: EventsWithLabels = [
(((1995, 1, 3), 0, 34.5), False),
(((2000, 1, 1), 1, "test_value"), True),
(((2000, 1, 5), 2, 1), True),
(((2000, 5, 5), "SNOMED/201436003", None), "skip"),
(((2005, 2, 5), 2, None), False),
(((2005, 7, 5), 2, None), False),
(((2010, 10, 5), 1, None), True),
(((2010, 10, 8), "Lupus_child_seven", None), "skip"),
(((2015, 2, 5, 0), 2, None), False),
(((2015, 7, 5, 0), 0, None), True),
(((2015, 11, 5, 10, 10), 2, None), True),
(((2015, 11, 15, 11), "SNOMED/55464009", None), "skip"),
(((2020, 1, 1), "Lupus_child_ten", None), "skip"),
(((2020, 3, 1, 10, 10, 10), 2, None), "out of range"),
]
ontology = DummyOntology_Lupus()
labeler = LupusCodeLabeler(ontology, time_horizon) # type: ignore
# Check that we selected the right codes
assert set(labeler.outcome_codes) == {
"SNOMED_55464009",
"SNOMED/201436003",
"Lupus_child_nine",
"SNOMED/55464009",
"Lupus_child_ten",
"Lupus_child_seven",
}
run_test_for_labeler(labeler, events_with_labels, help_text="LupusCodeLabeler")
#############################################
#############################################
#
# Specific instances of OMOPConceptCodeLabeler
#
#############################################
#############################################
class DummyOntology_OMOPConcept_Specific(DummyOntology_Base):
def __init__(self, new_codes: List[str]):
self.new_codes = new_codes + ["", ""]
def get_children(self, parent_code: str) -> Set[str]:
if parent_code == "child_1":
return {"child_1_1"}
elif parent_code == self.new_codes[0]:
return {"child_1"}
elif parent_code == self.new_codes[1]:
return {"child_2"}
return set()
def _assert_labvalue_constructor_correct(
labeler: OMOPConceptCodeLabeler,
time_horizon: TimeHorizon,
outcome_codes: set,
):
assert set(labeler.outcome_codes) == outcome_codes
assert labeler.prediction_codes == ["1", "2"]
assert labeler.get_time_horizon() == time_horizon
def _create_specific_labvalue_labeler(LabelerClass, outcome_codes: set):
time_horizon = TimeHorizon(datetime.timedelta(days=0), datetime.timedelta(days=10))
ontology = DummyOntology_OMOPConcept_Specific(LabelerClass.original_omop_concept_codes)
labeler = LabelerClass(ontology, time_horizon, prediction_codes=["1", "2"]) # type: ignore
_assert_labvalue_constructor_correct(labeler, time_horizon, outcome_codes)
return labeler
def test_thrombocytopenia(tmp_path: pathlib.Path):
outcome_codes = {"child_1_1", "child_2", "SNOMED/89627008", "child_1", "SNOMED/267447008"}
_create_specific_labvalue_labeler(ThrombocytopeniaCodeLabeler, outcome_codes)
def test_hyperkalemia(tmp_path: pathlib.Path):
outcome_codes = {"child_1", "SNOMED/14140009", "child_1_1"}
_create_specific_labvalue_labeler(HyperkalemiaCodeLabeler, outcome_codes)
def test_hypoglycemia(tmp_path: pathlib.Path):
outcome_codes = {
"SNOMED/120731000119103",
"child_2",
"child_1",
"SNOMED/52767006",
"SNOMED/719216001",
"SNOMED/302866003",
"SNOMED/267384006",
"SNOMED/421725003",
"SNOMED/237637005",
"SNOMED/237633009",
"SNOMED/190448007",
"child_1_1",
"SNOMED/421437000",
"SNOMED/230796005",
"SNOMED/84371000119108",
}
_create_specific_labvalue_labeler(HypoglycemiaCodeLabeler, outcome_codes)
def test_hyponatremia(tmp_path: pathlib.Path):
outcome_codes = {"child_2", "child_1", "SNOMED/89627008", "SNOMED/267447008", "child_1_1"}
_create_specific_labvalue_labeler(HyponatremiaCodeLabeler, outcome_codes)
def test_anemia(tmp_path: pathlib.Path):
outcome_codes = {
"child_1",
"SNOMED/713496008",
"SNOMED/691411000119101",
"SNOMED/691401000119104",
"SNOMED/767657005",
"child_2",
"SNOMED/111570005",
"SNOMED/271737000",
"SNOMED/713349004",
"child_1_1",
}
_create_specific_labvalue_labeler(AnemiaCodeLabeler, outcome_codes)
def test_neutropenia(tmp_path: pathlib.Path):
outcome_codes = {"child_1", "SNOMED/165517008", "child_1_1"}
_create_specific_labvalue_labeler(NeutropeniaCodeLabeler, outcome_codes)
def test_aki(tmp_path: pathlib.Path):
outcome_codes = {"child_2", "child_1_1", "child_1", "SNOMED/298015003", "SNOMED/14669001", "SNOMED/35455006"}
_create_specific_labvalue_labeler(AKICodeLabeler, outcome_codes)