-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathtest_label_generators.py
199 lines (168 loc) · 7.22 KB
/
test_label_generators.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
from datetime import date, timedelta
import testing.postgresql
import pytest
from sqlalchemy import create_engine
from triage.component.architect.label_generators import LabelGenerator
from .utils import create_binary_outcome_events
# Sample events data to use for all tests
events_data = [
# entity id, event_date, outcome
[1, date(2014, 1, 1), True],
[1, date(2014, 11, 10), False],
[1, date(2015, 1, 1), False],
[1, date(2015, 11, 10), True],
[2, date(2014, 6, 8), True],
[2, date(2015, 6, 8), False],
[3, date(2014, 3, 3), False],
[3, date(2014, 7, 24), False],
[3, date(2015, 3, 3), True],
[3, date(2015, 7, 24), False],
[4, date(2014, 12, 13), False],
[4, date(2015, 12, 13), False],
]
# An example label generation query to use for all tests.
# Since this is expected to be passed in properly by the user
# we don't need to test variations. Just reuse this one.
LABEL_GENERATE_QUERY = """select
events.entity_id,
bool_or(outcome::bool)::integer as outcome
from events
where
'{as_of_date}' <= outcome_date
and outcome_date < '{as_of_date}'::timestamp + interval '{label_timespan}'
group by entity_id
"""
LABELS_TABLE_NAME = "labels"
def test_label_generation():
# Generate labels for one as-of-date/label timespan combo
with testing.postgresql.Postgresql() as postgresql:
engine = create_engine(postgresql.url())
create_binary_outcome_events(engine, "events", events_data)
label_generator = LabelGenerator(db_engine=engine, query=LABEL_GENERATE_QUERY)
label_generator._create_labels_table(LABELS_TABLE_NAME)
label_generator.generate(
start_date="2014-09-30", label_timespan="6months", labels_table="labels"
)
expected = [
# entity_id, as_of_date, label_timespan, name, type, label
(1, date(2014, 9, 30), timedelta(180), "outcome", "binary", False),
(3, date(2014, 9, 30), timedelta(180), "outcome", "binary", True),
(4, date(2014, 9, 30), timedelta(180), "outcome", "binary", False),
]
result = engine.execute(
"select * from {} order by entity_id, as_of_date".format(LABELS_TABLE_NAME)
)
records = [row for row in result]
assert records == expected
def test_generate_all_labels_replace():
# Generate labels for combinations of as-of-date and label timespan
# use replace=True
with testing.postgresql.Postgresql() as postgresql:
engine = create_engine(postgresql.url())
create_binary_outcome_events(engine, "events", events_data)
label_generator = LabelGenerator(db_engine=engine, query=LABEL_GENERATE_QUERY, replace=True)
label_generator.generate_all_labels(
labels_table=LABELS_TABLE_NAME,
as_of_dates=["2014-09-30", "2015-03-30"],
label_timespans=["6month", "3month"],
)
result = engine.execute(
"""
select * from {}
order by entity_id, as_of_date, label_timespan desc
""".format(
LABELS_TABLE_NAME
)
)
records = [row for row in result]
expected = [
# entity_id, as_of_date, label_timespan, name, type, label
(1, date(2014, 9, 30), timedelta(180), "outcome", "binary", False),
(1, date(2014, 9, 30), timedelta(90), "outcome", "binary", False),
(2, date(2015, 3, 30), timedelta(180), "outcome", "binary", False),
(2, date(2015, 3, 30), timedelta(90), "outcome", "binary", False),
(3, date(2014, 9, 30), timedelta(180), "outcome", "binary", True),
(3, date(2015, 3, 30), timedelta(180), "outcome", "binary", False),
(4, date(2014, 9, 30), timedelta(180), "outcome", "binary", False),
(4, date(2014, 9, 30), timedelta(90), "outcome", "binary", False),
]
assert records == expected
def test_generate_all_labels_noreplace():
# test the 'replace=False' functionality
with testing.postgresql.Postgresql() as postgresql:
engine = create_engine(postgresql.url())
create_binary_outcome_events(engine, "events", events_data)
label_generator = LabelGenerator(
db_engine=engine,
query=LABEL_GENERATE_QUERY,
replace=False
)
label_generator.generate_all_labels(
labels_table=LABELS_TABLE_NAME,
as_of_dates=["2014-09-30"],
label_timespans=["6month"],
)
result = engine.execute(
"""
select * from {}
order by entity_id, as_of_date, label_timespan desc
""".format(
LABELS_TABLE_NAME
)
)
records = [row for row in result]
expected = [
# entity_id, as_of_date, label_timespan, name, type, label
(1, date(2014, 9, 30), timedelta(180), "outcome", "binary", False),
(3, date(2014, 9, 30), timedelta(180), "outcome", "binary", True),
(4, date(2014, 9, 30), timedelta(180), "outcome", "binary", False),
]
assert records == expected
# round 2
label_generator.generate_all_labels(
labels_table=LABELS_TABLE_NAME,
as_of_dates=["2014-09-30", "2015-03-30"],
label_timespans=["6month", "3month"],
)
result = engine.execute(
"""
select * from {}
order by entity_id, as_of_date, label_timespan desc
""".format(
LABELS_TABLE_NAME
)
)
records = [row for row in result]
expected = [
# entity_id, as_of_date, label_timespan, name, type, label
(1, date(2014, 9, 30), timedelta(180), "outcome", "binary", False),
(1, date(2014, 9, 30), timedelta(90), "outcome", "binary", False),
(2, date(2015, 3, 30), timedelta(180), "outcome", "binary", False),
(2, date(2015, 3, 30), timedelta(90), "outcome", "binary", False),
(3, date(2014, 9, 30), timedelta(180), "outcome", "binary", True),
(3, date(2015, 3, 30), timedelta(180), "outcome", "binary", False),
(4, date(2014, 9, 30), timedelta(180), "outcome", "binary", False),
(4, date(2014, 9, 30), timedelta(90), "outcome", "binary", False),
]
assert records == expected
def test_generate_all_labels_errors_on_duplicates():
# label query that will yield duplicates (one row for each event in the timespan)
BAD_LABEL_GENERATE_QUERY = """
select
events.entity_id,
1 as outcome
from events
where
'{as_of_date}' <= outcome_date
and outcome_date < '{as_of_date}'::timestamp + interval '{label_timespan}'
"""
with testing.postgresql.Postgresql() as postgresql:
engine = create_engine(postgresql.url())
create_binary_outcome_events(engine, "events", events_data)
label_generator = LabelGenerator(db_engine=engine, query=BAD_LABEL_GENERATE_QUERY, replace=True)
with pytest.raises(ValueError):
label_generator.generate_all_labels(
labels_table=LABELS_TABLE_NAME,
as_of_dates=["2014-09-30", "2015-03-30"],
label_timespans=["6month", "3month"],
)