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

Download this file

255 lines (224 with data), 9.7 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
# flake8: noqa: E402
import datetime
import os
import pathlib
import sys
import warnings
from typing import List
import meds
from femr_test_tools import EventsWithLabels, run_test_for_labeler
from femr.labelers import TimeHorizon, TimeHorizonEventLabeler
class DummyLabeler(TimeHorizonEventLabeler):
"""Dummy labeler that returns True if the event's `code` is in `self.outcome_codes`."""
def __init__(self, outcome_codes: List[int], time_horizon: TimeHorizon, allow_same_time: bool = True):
self.outcome_codes: List[str] = [str(a) for a in outcome_codes]
self.time_horizon: TimeHorizon = time_horizon
self.allow_same_time = allow_same_time
def allow_same_time_labels(self) -> bool:
return self.allow_same_time
def get_prediction_times(self, patient: meds.Patient) -> List[datetime.datetime]:
return sorted(list({e["time"] for e in patient["events"]}))
def get_time_horizon(self) -> TimeHorizon:
return self.time_horizon
def get_outcome_times(self, patient: meds.Patient) -> List[datetime.datetime]:
times: List[datetime.datetime] = []
for e in patient["events"]:
for m in e["measurements"]:
if m["code"] in self.outcome_codes:
times.append(e["time"])
return times
def test_no_outcomes(tmp_path: pathlib.Path):
# No outcomes occur in this patient's timeline
time_horizon = TimeHorizon(datetime.timedelta(days=0), datetime.timedelta(days=180))
labeler = DummyLabeler([100], time_horizon)
events_with_labels: EventsWithLabels = [
# fmt: off
(((2015, 1, 3), 2, 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, 3), 1, None), False),
(((2018, 5, 3), 2, None), False),
(((2018, 5, 3, 11), 1, None), False),
(((2018, 5, 4), 1, None), False),
(((2018, 12, 4), 1, None), "out of range"),
# fmt: on
]
run_test_for_labeler(labeler, events_with_labels, help_text="test_no_outcomes")
def test_horizon_0_180_days(tmp_path: pathlib.Path):
# (0, 180) days
time_horizon = TimeHorizon(datetime.timedelta(days=0), datetime.timedelta(days=180))
labeler = DummyLabeler([2], time_horizon)
events_with_labels: EventsWithLabels = [
# fmt: off
(((2015, 1, 3), 2, None), "duplicate"),
(((2015, 1, 3), 1, None), "duplicate"),
(((2015, 1, 3), 3, None), True),
(((2015, 10, 5), 1, None), False),
(((2018, 1, 3), 2, None), True),
(((2018, 3, 3), 1, None), True),
(((2018, 5, 3), 2, None), True),
(((2018, 5, 3, 11), 1, None), False),
(((2018, 5, 4), 1, None), False),
(((2018, 12, 4), 1, None), "out of range"),
# fmt: on
]
run_test_for_labeler(labeler, events_with_labels, help_text="test_horizon_0_180_days")
def test_horizon_0_180_days_no_same(tmp_path: pathlib.Path):
# (0, 180) days
time_horizon = TimeHorizon(datetime.timedelta(days=0), datetime.timedelta(days=180))
labeler = DummyLabeler([2], time_horizon, allow_same_time=False)
events_with_labels: EventsWithLabels = [
# fmt: off
(((2015, 1, 3), 2, None), "duplicate"),
(((2015, 1, 3), 1, None), "duplicate"),
(((2015, 1, 3), 3, None), "same"),
(((2015, 10, 5), 1, None), False),
(((2018, 1, 3), 2, None), "same"),
(((2018, 3, 3), 1, None), True),
(((2018, 5, 3), 2, None), "same"),
(((2018, 5, 3, 11), 1, None), False),
(((2018, 5, 4), 1, None), False),
(((2018, 12, 4), 1, None), "out of range"),
# fmt: on
]
run_test_for_labeler(labeler, events_with_labels, help_text="test_horizon_0_180_days")
def test_horizon_1_180_days(tmp_path: pathlib.Path):
# (1, 180) days
time_horizon = TimeHorizon(datetime.timedelta(days=1), datetime.timedelta(days=180))
labeler = DummyLabeler([2], time_horizon)
events_with_labels: EventsWithLabels = [
# fmt: off
(((2015, 1, 3), 2, 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), True),
(((2018, 3, 3), 1, None), True),
(((2018, 5, 3), 2, None), False),
(((2018, 5, 3, 11), 1, None), False),
(((2018, 5, 4), 1, None), False),
(((2018, 12, 4), 1, None), "out of range"),
# fmt: on
]
run_test_for_labeler(labeler, events_with_labels, help_text="test_horizon_1_180_days")
def test_horizon_180_365_days(tmp_path: pathlib.Path):
# (180, 365) days
time_horizon = TimeHorizon(datetime.timedelta(days=180), datetime.timedelta(days=365))
labeler = DummyLabeler([2], time_horizon)
events_with_labels: EventsWithLabels = [
# fmt: off
(((2000, 1, 3), 2, None), True),
(((2000, 10, 5), 2, None), False),
(((2002, 1, 5), 2, None), True),
(((2002, 3, 1), 1, None), True),
(((2002, 4, 5), 3, None), True),
(((2002, 4, 12), 1, None), True),
(((2002, 12, 5), 2, None), False),
(((2002, 12, 10), 1, None), False),
(((2004, 1, 10), 2, None), False),
(((2008, 1, 10), 2, None), "out of range"),
# fmt: on
]
run_test_for_labeler(labeler, events_with_labels, help_text="test_horizon_180_365_days")
def test_horizon_0_0_days(tmp_path: pathlib.Path):
# (0, 0) days
time_horizon = TimeHorizon(datetime.timedelta(days=0), datetime.timedelta(days=0))
labeler = DummyLabeler([2], time_horizon)
events_with_labels: EventsWithLabels = [
# fmt: off
(((2015, 1, 3), 2, None), "duplicate"),
(((2015, 1, 3), 1, None), True),
(((2015, 1, 4), 1, None), False),
(((2015, 1, 5), 2, None), True),
(((2015, 1, 5, 10), 1, None), False),
(((2015, 1, 6), 2, None), True),
# fmt: on
]
run_test_for_labeler(labeler, events_with_labels, help_text="test_horizon_0_0_days")
def test_horizon_10_10_days(tmp_path: pathlib.Path):
# (10, 10) days
time_horizon = TimeHorizon(datetime.timedelta(days=10), datetime.timedelta(days=10))
labeler = DummyLabeler([2], time_horizon)
events_with_labels: EventsWithLabels = [
# fmt: off
(((2015, 1, 3), 2, None), False),
(((2015, 1, 13), 1, None), True),
(((2015, 1, 23), 2, None), True),
(((2015, 2, 2), 2, None), False),
(((2015, 3, 10), 1, None), True),
(((2015, 3, 20), 2, None), False),
(((2015, 3, 29), 2, None), "out of range"),
(((2015, 3, 30), 1, None), "out of range"),
# fmt: on
]
run_test_for_labeler(labeler, events_with_labels, help_text="test_horizon_10_10_days")
def test_horizon_0_1000000_days(tmp_path: pathlib.Path):
# (0, 1000000) days
time_horizon = TimeHorizon(datetime.timedelta(days=0), datetime.timedelta(days=1000000))
labeler = DummyLabeler([2], time_horizon)
events_with_labels: EventsWithLabels = [
# fmt: off
(((2000, 1, 3), 2, None), True),
(((2001, 10, 5), 1, None), True),
(((2020, 10, 5), 2, None), True),
(((2021, 10, 5), 1, None), True),
(((2050, 1, 10), 2, None), True),
(((2051, 1, 10), 1, None), False),
(((5000, 1, 10), 1, None), "out of range"),
# fmt: on
]
run_test_for_labeler(labeler, events_with_labels, help_text="test_horizon_0_1000000_days")
def test_horizon_5_10_hours(tmp_path: pathlib.Path):
# (5 hours, 10.5 hours)
time_horizon = TimeHorizon(datetime.timedelta(hours=5), datetime.timedelta(hours=10, minutes=30))
labeler = DummyLabeler([2], time_horizon)
events_with_labels: EventsWithLabels = [
# fmt: off
(((2015, 1, 1, 0, 0), 1, None), True),
(((2015, 1, 1, 10, 29), 2, None), False),
(((2015, 1, 1, 10, 30), 1, None), False),
(((2015, 1, 1, 10, 31), 1, None), False),
#
(((2016, 1, 1, 0, 0), 1, None), True),
(((2016, 1, 1, 10, 29), 1, None), False),
(((2016, 1, 1, 10, 30), 2, None), False),
(((2016, 1, 1, 10, 31), 1, None), False),
#
(((2017, 1, 1, 0, 0), 1, None), False),
(((2017, 1, 1, 10, 29), 1, None), False),
(((2017, 1, 1, 10, 30), 1, None), False),
(((2017, 1, 1, 10, 31), 2, None), False),
#
(((2018, 1, 1, 0, 0), 1, None), False),
(((2018, 1, 1, 4, 59, 59), 2, None), False),
(((2018, 1, 1, 5), 1, None), False),
#
(((2019, 1, 1, 0, 0), 1, None), True),
(((2019, 1, 1, 4, 59, 59), 1, None), "out of range"),
(((2019, 1, 1, 5), 2, None), "out of range"),
# fmt: on
]
run_test_for_labeler(labeler, events_with_labels, help_text="test_horizon_5_10_hours")
def test_horizon_infinite(tmp_path: pathlib.Path):
# Infinite horizon
time_horizon = TimeHorizon(
datetime.timedelta(days=10),
None,
)
labeler = DummyLabeler([2], time_horizon)
events_with_labels: EventsWithLabels = [
# fmt: off
(((1950, 1, 3), 1, None), True),
(((2000, 1, 3), 1, None), True),
(((2001, 10, 5), 1, None), True),
(((2020, 10, 5), 1, None), True),
(((2021, 10, 5), 1, None), True),
(((2050, 1, 10), 2, None), True),
(((2050, 1, 20), 2, None), False),
(((2051, 1, 10), 1, None), False),
(((5000, 1, 10), 1, None), False),
# fmt: on
]
run_test_for_labeler(labeler, events_with_labels, help_text="test_horizon_infinite")