-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathtest_on_demand_pandas_transformation.py
343 lines (302 loc) · 12.6 KB
/
test_on_demand_pandas_transformation.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
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
import os
import re
import tempfile
from datetime import datetime, timedelta
import pandas as pd
import pytest
from feast import (
Entity,
FeatureStore,
FeatureView,
FileSource,
RepoConfig,
RequestSource,
)
from feast.driver_test_data import create_driver_hourly_stats_df
from feast.field import Field
from feast.infra.online_stores.sqlite import SqliteOnlineStoreConfig
from feast.on_demand_feature_view import on_demand_feature_view
from feast.types import (
Array,
Bool,
Float32,
Float64,
Int64,
String,
)
def test_pandas_transformation():
with tempfile.TemporaryDirectory() as data_dir:
store = FeatureStore(
config=RepoConfig(
project="test_on_demand_python_transformation",
registry=os.path.join(data_dir, "registry.db"),
provider="local",
entity_key_serialization_version=2,
online_store=SqliteOnlineStoreConfig(
path=os.path.join(data_dir, "online.db")
),
)
)
# Generate test data.
end_date = datetime.now().replace(microsecond=0, second=0, minute=0)
start_date = end_date - timedelta(days=15)
driver_entities = [1001, 1002, 1003, 1004, 1005]
driver_df = create_driver_hourly_stats_df(driver_entities, start_date, end_date)
driver_stats_path = os.path.join(data_dir, "driver_stats.parquet")
driver_df.to_parquet(path=driver_stats_path, allow_truncated_timestamps=True)
driver = Entity(name="driver", join_keys=["driver_id"])
driver_stats_source = FileSource(
name="driver_hourly_stats_source",
path=driver_stats_path,
timestamp_field="event_timestamp",
created_timestamp_column="created",
)
driver_stats_fv = FeatureView(
name="driver_hourly_stats",
entities=[driver],
ttl=timedelta(days=0),
schema=[
Field(name="conv_rate", dtype=Float32),
Field(name="acc_rate", dtype=Float32),
Field(name="avg_daily_trips", dtype=Int64),
],
online=True,
source=driver_stats_source,
)
@on_demand_feature_view(
sources=[driver_stats_fv],
schema=[Field(name="conv_rate_plus_acc", dtype=Float64)],
mode="pandas",
)
def pandas_view(inputs: pd.DataFrame) -> pd.DataFrame:
df = pd.DataFrame()
df["conv_rate_plus_acc"] = inputs["conv_rate"] + inputs["acc_rate"]
return df
store.apply([driver, driver_stats_source, driver_stats_fv, pandas_view])
entity_rows = [
{
"driver_id": 1001,
}
]
store.write_to_online_store(
feature_view_name="driver_hourly_stats", df=driver_df
)
online_response = store.get_online_features(
entity_rows=entity_rows,
features=[
"driver_hourly_stats:conv_rate",
"driver_hourly_stats:acc_rate",
"driver_hourly_stats:avg_daily_trips",
"pandas_view:conv_rate_plus_acc",
],
).to_df()
assert online_response["conv_rate_plus_acc"].equals(
online_response["conv_rate"] + online_response["acc_rate"]
)
def test_pandas_transformation_returning_all_data_types():
with tempfile.TemporaryDirectory() as data_dir:
store = FeatureStore(
config=RepoConfig(
project="test_on_demand_python_transformation",
registry=os.path.join(data_dir, "registry.db"),
provider="local",
entity_key_serialization_version=2,
online_store=SqliteOnlineStoreConfig(
path=os.path.join(data_dir, "online.db")
),
)
)
# Generate test data.
end_date = datetime.now().replace(microsecond=0, second=0, minute=0)
start_date = end_date - timedelta(days=15)
driver_entities = [1001, 1002, 1003, 1004, 1005]
driver_df = create_driver_hourly_stats_df(driver_entities, start_date, end_date)
driver_stats_path = os.path.join(data_dir, "driver_stats.parquet")
driver_df.to_parquet(path=driver_stats_path, allow_truncated_timestamps=True)
driver = Entity(name="driver", join_keys=["driver_id"])
driver_stats_source = FileSource(
name="driver_hourly_stats_source",
path=driver_stats_path,
timestamp_field="event_timestamp",
created_timestamp_column="created",
)
driver_stats_fv = FeatureView(
name="driver_hourly_stats",
entities=[driver],
ttl=timedelta(days=0),
schema=[
Field(name="conv_rate", dtype=Float32),
Field(name="acc_rate", dtype=Float32),
Field(name="avg_daily_trips", dtype=Int64),
],
online=True,
source=driver_stats_source,
)
request_source = RequestSource(
name="request_source",
schema=[
Field(name="avg_daily_trip_rank_thresholds", dtype=Array(Int64)),
Field(name="avg_daily_trip_rank_names", dtype=Array(String)),
],
)
@on_demand_feature_view(
sources=[request_source, driver_stats_fv],
schema=[
Field(name="highest_achieved_rank", dtype=String),
Field(name="avg_daily_trips_plus_one", dtype=Int64),
Field(name="conv_rate_plus_acc", dtype=Float64),
Field(name="is_highest_rank", dtype=Bool),
Field(name="achieved_ranks", dtype=Array(String)),
Field(name="trips_until_next_rank_int", dtype=Array(Int64)),
Field(name="trips_until_next_rank_float", dtype=Array(Float64)),
Field(name="achieved_ranks_mask", dtype=Array(Bool)),
],
mode="pandas",
)
def pandas_view(inputs: pd.DataFrame) -> pd.DataFrame:
df = pd.DataFrame()
df["conv_rate_plus_acc"] = inputs["conv_rate"] + inputs["acc_rate"]
df["avg_daily_trips_plus_one"] = inputs["avg_daily_trips"] + 1
df["trips_until_next_rank_int"] = inputs[
["avg_daily_trips", "avg_daily_trip_rank_thresholds"]
].apply(
lambda x: [max(threshold - x.iloc[0], 0) for threshold in x.iloc[1]],
axis=1,
)
df["trips_until_next_rank_float"] = df["trips_until_next_rank_int"].map(
lambda values: [float(value) for value in values]
)
df["achieved_ranks_mask"] = df["trips_until_next_rank_int"].map(
lambda values: [value <= 0 for value in values]
)
temp = pd.concat(
[df[["achieved_ranks_mask"]], inputs[["avg_daily_trip_rank_names"]]],
axis=1,
)
df["achieved_ranks"] = temp.apply(
lambda x: [
rank if achieved else "Locked"
for achieved, rank in zip(x.iloc[0], x.iloc[1])
],
axis=1,
)
df["highest_achieved_rank"] = (
df["achieved_ranks"]
.map(
lambda ranks: str(
([rank for rank in ranks if rank != "Locked"][-1:] or ["None"])[
0
]
)
)
.astype("string")
)
df["is_highest_rank"] = df["achieved_ranks"].map(
lambda ranks: ranks[-1] != "Locked"
)
return df
store.apply([driver, driver_stats_source, driver_stats_fv, pandas_view])
entity_rows = [
{
"driver_id": 1001,
"avg_daily_trip_rank_thresholds": [100, 250, 500, 1000],
"avg_daily_trip_rank_names": ["Bronze", "Silver", "Gold", "Platinum"],
}
]
store.write_to_online_store(
feature_view_name="driver_hourly_stats", df=driver_df
)
online_response = store.get_online_features(
entity_rows=entity_rows,
features=[
"driver_hourly_stats:conv_rate",
"driver_hourly_stats:acc_rate",
"driver_hourly_stats:avg_daily_trips",
"pandas_view:avg_daily_trips_plus_one",
"pandas_view:conv_rate_plus_acc",
"pandas_view:trips_until_next_rank_int",
"pandas_view:trips_until_next_rank_float",
"pandas_view:achieved_ranks_mask",
"pandas_view:achieved_ranks",
"pandas_view:highest_achieved_rank",
"pandas_view:is_highest_rank",
],
).to_df()
# We use to_df here to ensure we use the pandas backend, but convert to a dict for comparisons
result = online_response.to_dict(orient="records")[0]
# Type assertions
# Materialized view
assert type(result["conv_rate"]) == float
assert type(result["acc_rate"]) == float
assert type(result["avg_daily_trips"]) == int
# On-demand view
assert type(result["avg_daily_trips_plus_one"]) == int
assert type(result["conv_rate_plus_acc"]) == float
assert type(result["highest_achieved_rank"]) == str
assert type(result["is_highest_rank"]) == bool
assert type(result["trips_until_next_rank_int"]) == list
assert all([type(e) == int for e in result["trips_until_next_rank_int"]])
assert type(result["trips_until_next_rank_float"]) == list
assert all([type(e) == float for e in result["trips_until_next_rank_float"]])
assert type(result["achieved_ranks"]) == list
assert all([type(e) == str for e in result["achieved_ranks"]])
assert type(result["achieved_ranks_mask"]) == list
assert all([type(e) == bool for e in result["achieved_ranks_mask"]])
# Value assertions
expected_trips_until_next_rank = [
max(threshold - result["avg_daily_trips"], 0)
for threshold in entity_rows[0]["avg_daily_trip_rank_thresholds"]
]
expected_mask = [value <= 0 for value in expected_trips_until_next_rank]
expected_ranks = [
rank if achieved else "Locked"
for achieved, rank in zip(
expected_mask, entity_rows[0]["avg_daily_trip_rank_names"]
)
]
highest_rank = (
[rank for rank in expected_ranks if rank != "Locked"][-1:] or ["None"]
)[0]
assert result["conv_rate_plus_acc"] == result["conv_rate"] + result["acc_rate"]
assert result["avg_daily_trips_plus_one"] == result["avg_daily_trips"] + 1
assert result["highest_achieved_rank"] == highest_rank
assert result["is_highest_rank"] == (expected_ranks[-1] != "Locked")
assert result["trips_until_next_rank_int"] == expected_trips_until_next_rank
assert result["trips_until_next_rank_float"] == [
float(value) for value in expected_trips_until_next_rank
]
assert result["achieved_ranks_mask"] == expected_mask
assert result["achieved_ranks"] == expected_ranks
def test_invalid_pandas_transformation_raises_type_error_on_apply():
with tempfile.TemporaryDirectory() as data_dir:
store = FeatureStore(
config=RepoConfig(
project="test_on_demand_python_transformation",
registry=os.path.join(data_dir, "registry.db"),
provider="local",
entity_key_serialization_version=2,
online_store=SqliteOnlineStoreConfig(
path=os.path.join(data_dir, "online.db")
),
)
)
request_source = RequestSource(
name="request_source",
schema=[
Field(name="driver_name", dtype=String),
],
)
@on_demand_feature_view(
sources=[request_source],
schema=[Field(name="driver_name_lower", dtype=String)],
mode="pandas",
)
def pandas_view(inputs: pd.DataFrame) -> pd.DataFrame:
return pd.DataFrame({"driver_name_lower": []})
with pytest.raises(
TypeError,
match=re.escape(
"Failed to infer type for feature 'driver_name_lower' with value '[]' since no items were returned by the UDF."
),
):
store.apply([request_source, pandas_view])