Repository navigation
Expand file tree
/
Copy pathtest_aggregation_ops.py
More file actions
290 lines (241 loc) · 9.44 KB
/
Copy pathtest_aggregation_ops.py
File metadata and controls
290 lines (241 loc) · 9.44 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
from datetime import timedelta
import pandas as pd
import pytest
from feast.aggregation import Aggregation, aggregation_specs_to_agg_ops
from feast.aggregation.tiling.base import get_ir_metadata_for_aggregation
class DummyAggregation:
def __init__(self, *, function: str, column: str, time_window=None, name: str = ""):
self.function = function
self.column = column
self.time_window = time_window
self.name = name
def test_aggregation_specs_to_agg_ops_success():
agg_specs = [
DummyAggregation(function="sum", column="trips"),
DummyAggregation(function="mean", column="fare"),
]
agg_ops = aggregation_specs_to_agg_ops(
agg_specs,
time_window_unsupported_error_message="Time window aggregation is not supported.",
)
assert agg_ops == {
"sum_trips": ("sum", "trips"),
"mean_fare": ("mean", "fare"),
}
@pytest.mark.parametrize(
"error_message",
[
"Time window aggregation is not supported in online serving.",
"Time window aggregation is not supported in the local compute engine.",
],
)
def test_aggregation_specs_to_agg_ops_time_window_unsupported(error_message: str):
agg_specs = [DummyAggregation(function="sum", column="trips", time_window=1)]
with pytest.raises(ValueError, match=error_message):
aggregation_specs_to_agg_ops(
agg_specs,
time_window_unsupported_error_message=error_message,
)
def test_aggregation_specs_to_agg_ops_custom_name():
agg_specs = [
DummyAggregation(
function="sum",
column="seconds_watched",
name="sum_seconds_watched_per_ad_1d",
),
]
agg_ops = aggregation_specs_to_agg_ops(
agg_specs,
time_window_unsupported_error_message="Time window aggregation is not supported.",
)
assert agg_ops == {
"sum_seconds_watched_per_ad_1d": ("sum", "seconds_watched"),
}
def test_aggregation_specs_to_agg_ops_mixed_names():
agg_specs = [
DummyAggregation(function="sum", column="trips", name="total_trips"),
DummyAggregation(function="mean", column="fare"),
]
agg_ops = aggregation_specs_to_agg_ops(
agg_specs,
time_window_unsupported_error_message="Time window aggregation is not supported.",
)
assert agg_ops == {
"total_trips": ("sum", "trips"),
"mean_fare": ("mean", "fare"),
}
def test_aggregation_round_trip_with_name():
agg = Aggregation(
column="seconds_watched",
function="sum",
time_window=timedelta(days=1),
name="sum_seconds_watched_per_ad_1d",
)
proto = agg.to_proto()
assert proto.name == "sum_seconds_watched_per_ad_1d"
restored = Aggregation.from_proto(proto)
assert restored.name == "sum_seconds_watched_per_ad_1d"
assert restored == agg
def test_count_distinct_agg_ops():
"""aggregation_specs_to_agg_ops maps count_distinct to the nunique pandas function."""
agg_specs = [DummyAggregation(function="count_distinct", column="item_id")]
agg_ops = aggregation_specs_to_agg_ops(
agg_specs,
time_window_unsupported_error_message="no windows",
)
assert agg_ops == {"count_distinct_item_id": ("nunique", "item_id")}
def test_count_distinct_result():
"""count_distinct via nunique returns the number of unique values per group."""
from feast.infra.compute_engines.backends.pandas_backend import PandasBackend
agg_specs = [DummyAggregation(function="count_distinct", column="item_id")]
agg_ops = aggregation_specs_to_agg_ops(
agg_specs,
time_window_unsupported_error_message="no windows",
)
df = pd.DataFrame({"user": ["A", "A", "B"], "item_id": [1, 2, 1]})
result = PandasBackend().groupby_agg(df, ["user"], agg_ops)
result = result.set_index("user")
assert result.loc["A", "count_distinct_item_id"] == 2
assert result.loc["B", "count_distinct_item_id"] == 1
def test_count_distinct_tiling_raises():
"""get_ir_metadata_for_aggregation raises ValueError for count_distinct."""
agg = Aggregation(column="item_id", function="count_distinct")
with pytest.raises(ValueError, match="count_distinct does not support tiling"):
get_ir_metadata_for_aggregation(agg, "count_distinct_item_id")
@pytest.mark.parametrize(
"function, expected_fn",
[
("avg", lambda s: s.mean()),
("std", lambda s: s.std()),
("var", lambda s: s.var()),
],
)
def test_tiled_holistic_aggregation_matches_pandas(function, expected_fn):
"""Tiled avg/std/var over a sliding window match the pandas result."""
from feast.aggregation.tiling.orchestrator import apply_sawtooth_window_tiling
from feast.aggregation.tiling.tile_subtraction import (
convert_cumulative_to_windowed,
)
start = pd.Timestamp("2024-01-01")
values = [1.0, 2.0, 3.0, 10.0, 4.0, 7.0, 5.0, 8.0]
df = pd.DataFrame(
{
"driver_id": [1] * len(values),
"event_timestamp": [
start + timedelta(minutes=5 * i) for i in range(len(values))
],
"trips": values,
}
)
window = timedelta(minutes=20)
agg = Aggregation(column="trips", function=function, time_window=window)
tiles = apply_sawtooth_window_tiling(
df=df.copy(),
aggregations=[agg],
group_by_keys=["driver_id"],
timestamp_col="event_timestamp",
window_size=window,
hop_size=timedelta(minutes=5),
)
result = convert_cumulative_to_windowed(
tiles_df=tiles,
entity_keys=["driver_id"],
timestamp_col="event_timestamp",
window_size=window,
aggregations=[agg],
)
feature_name = agg.resolved_name(window)
for _, row in result.iterrows():
tile_end = row["event_timestamp"]
in_window = df[
(df["event_timestamp"] >= tile_end - window)
& (df["event_timestamp"] < tile_end)
]["trips"]
assert row[feature_name] == pytest.approx(expected_fn(in_window), nan_ok=True)
@pytest.mark.parametrize("function", ["max", "min"])
def test_tiled_max_min_aggregation_matches_pandas(function):
"""Tiled max/min only consider the hops inside the sliding window."""
from feast.aggregation.tiling.orchestrator import apply_sawtooth_window_tiling
from feast.aggregation.tiling.tile_subtraction import (
convert_cumulative_to_windowed,
)
start = pd.Timestamp("2024-01-01")
# No events between minutes 20 and 35, so two hops are empty.
minutes = [0, 5, 10, 15, 20, 35, 40, 45]
values = [-1.0, -2.0, 30.0, -10.0, 4.0, -7.0, 5.0, -8.0]
df = pd.DataFrame(
{
"driver_id": [1] * len(values),
"event_timestamp": [start + timedelta(minutes=m) for m in minutes],
"trips": values,
}
)
window = timedelta(minutes=20)
agg = Aggregation(column="trips", function=function, time_window=window)
tiles = apply_sawtooth_window_tiling(
df=df.copy(),
aggregations=[agg],
group_by_keys=["driver_id"],
timestamp_col="event_timestamp",
window_size=window,
hop_size=timedelta(minutes=5),
)
result = convert_cumulative_to_windowed(
tiles_df=tiles,
entity_keys=["driver_id"],
timestamp_col="event_timestamp",
window_size=window,
aggregations=[agg],
)
feature_name = agg.resolved_name(window)
assert len(result) == 10
for _, row in result.iterrows():
tile_end = row["event_timestamp"]
in_window = df[
(df["event_timestamp"] >= tile_end - window)
& (df["event_timestamp"] < tile_end)
]["trips"]
assert row[feature_name] == getattr(in_window, function)()
@pytest.mark.parametrize("dtype", ["datetime64[us]", "datetime64[ms, UTC]"])
def test_tiled_aggregation_does_not_depend_on_timestamp_resolution(dtype):
"""Timestamps stored in a non-nanosecond unit give the same tiles."""
from feast.aggregation.tiling.orchestrator import apply_sawtooth_window_tiling
from feast.aggregation.tiling.tile_subtraction import (
convert_cumulative_to_windowed,
)
start = pd.Timestamp("2024-01-01")
minutes = [0, 5, 10, 30, 35]
df = pd.DataFrame(
{
"driver_id": [1] * len(minutes),
"event_timestamp": [start + timedelta(minutes=m) for m in minutes],
"trips": [1.0, 2.0, 3.0, 4.0, 5.0],
}
)
window = timedelta(minutes=20)
agg = Aggregation(column="trips", function="sum", time_window=window)
def windowed(frame):
tiles = apply_sawtooth_window_tiling(
df=frame,
aggregations=[agg],
group_by_keys=["driver_id"],
timestamp_col="event_timestamp",
window_size=window,
hop_size=timedelta(minutes=5),
)
return convert_cumulative_to_windowed(
tiles_df=tiles,
entity_keys=["driver_id"],
timestamp_col="event_timestamp",
window_size=window,
aggregations=[agg],
)
expected = windowed(df.copy())
ts = df["event_timestamp"]
if "UTC" in dtype:
ts = ts.dt.tz_localize("UTC")
result = windowed(df.assign(event_timestamp=ts.astype(dtype)))
feature_name = agg.resolved_name(window)
assert len(expected) == 8
assert result["event_timestamp"].tolist() == expected["event_timestamp"].tolist()
assert result[feature_name].tolist() == expected[feature_name].tolist()