forked from feast-dev/feast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_inference.py
334 lines (286 loc) · 11.9 KB
/
test_inference.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
from copy import deepcopy
import pandas as pd
import pytest
from feast import (
BigQuerySource,
Entity,
Feature,
FileSource,
RedshiftSource,
RepoConfig,
SnowflakeSource,
ValueType,
)
from feast.data_source import RequestSource
from feast.errors import (
DataSourceNoNameException,
RegistryInferenceFailure,
SpecifiedFeaturesNotPresentError,
)
from feast.feature_view import FeatureView
from feast.field import Field
from feast.inference import (
update_data_sources_with_inferred_event_timestamp_col,
update_entities_with_inferred_types_from_feature_views,
update_feature_views_with_inferred_features,
)
from feast.infra.offline_stores.contrib.spark_offline_store.spark_source import (
SparkSource,
)
from feast.on_demand_feature_view import on_demand_feature_view
from feast.types import Float32, PrimitiveFeastType, String, UnixTimestamp
from tests.utils.data_source_utils import (
prep_file_source,
simple_bq_source_using_query_arg,
simple_bq_source_using_table_arg,
)
def test_update_entities_with_inferred_types_from_feature_views(
simple_dataset_1, simple_dataset_2
):
with prep_file_source(
df=simple_dataset_1, event_timestamp_column="ts_1"
) as file_source, prep_file_source(
df=simple_dataset_2, event_timestamp_column="ts_1"
) as file_source_2:
fv1 = FeatureView(
name="fv1", entities=["id"], batch_source=file_source, ttl=None,
)
fv2 = FeatureView(
name="fv2", entities=["id"], batch_source=file_source_2, ttl=None,
)
actual_1 = Entity(name="id", join_key="id_join_key")
actual_2 = Entity(name="id", join_key="id_join_key")
update_entities_with_inferred_types_from_feature_views(
[actual_1], [fv1], RepoConfig(provider="local", project="test")
)
update_entities_with_inferred_types_from_feature_views(
[actual_2], [fv2], RepoConfig(provider="local", project="test")
)
assert actual_1 == Entity(
name="id", join_key="id_join_key", value_type=ValueType.INT64
)
assert actual_2 == Entity(
name="id", join_key="id_join_key", value_type=ValueType.STRING
)
with pytest.raises(RegistryInferenceFailure):
# two viable data types
update_entities_with_inferred_types_from_feature_views(
[Entity(name="id", join_key="id_join_key")],
[fv1, fv2],
RepoConfig(provider="local", project="test"),
)
def test_infer_datasource_names_file():
file_path = "path/to/test.csv"
data_source = FileSource(path=file_path)
assert data_source.name == file_path
source_name = "my_name"
data_source = FileSource(name=source_name, path=file_path)
assert data_source.name == source_name
def test_infer_datasource_names_dwh():
table = "project.table"
dwh_classes = [BigQuerySource, RedshiftSource, SnowflakeSource, SparkSource]
for dwh_class in dwh_classes:
data_source = dwh_class(table=table)
assert data_source.name == table
source_name = "my_name"
data_source_with_table = dwh_class(name=source_name, table=table)
assert data_source_with_table.name == source_name
data_source_with_query = dwh_class(
name=source_name, query=f"SELECT * from {table}"
)
assert data_source_with_query.name == source_name
# If we have a query and no name, throw an error
if dwh_class == SparkSource:
with pytest.raises(DataSourceNoNameException):
print(f"Testing dwh {dwh_class}")
data_source = dwh_class(query="test_query")
else:
data_source = dwh_class(query="test_query")
assert data_source.name == ""
@pytest.mark.integration
def test_update_file_data_source_with_inferred_event_timestamp_col(simple_dataset_1):
df_with_two_viable_timestamp_cols = simple_dataset_1.copy(deep=True)
df_with_two_viable_timestamp_cols["ts_2"] = simple_dataset_1["ts_1"]
with prep_file_source(df=simple_dataset_1) as file_source:
data_sources = [
file_source,
simple_bq_source_using_table_arg(simple_dataset_1),
simple_bq_source_using_query_arg(simple_dataset_1),
]
update_data_sources_with_inferred_event_timestamp_col(
data_sources, RepoConfig(provider="local", project="test")
)
actual_event_timestamp_cols = [
source.timestamp_field for source in data_sources
]
assert actual_event_timestamp_cols == ["ts_1", "ts_1", "ts_1"]
with prep_file_source(df=df_with_two_viable_timestamp_cols) as file_source:
with pytest.raises(RegistryInferenceFailure):
# two viable timestamp_fields
update_data_sources_with_inferred_event_timestamp_col(
[file_source], RepoConfig(provider="local", project="test")
)
@pytest.mark.integration
@pytest.mark.universal
def test_update_data_sources_with_inferred_event_timestamp_col(universal_data_sources):
(_, _, data_sources) = universal_data_sources
data_sources_copy = deepcopy(data_sources)
# remove defined timestamp_field to allow for inference
for data_source in data_sources_copy.values():
data_source.timestamp_field = None
data_source.event_timestamp_column = None
update_data_sources_with_inferred_event_timestamp_col(
data_sources_copy.values(), RepoConfig(provider="local", project="test"),
)
actual_event_timestamp_cols = [
source.timestamp_field for source in data_sources_copy.values()
]
assert actual_event_timestamp_cols == ["event_timestamp"] * len(
data_sources_copy.values()
)
def test_on_demand_features_type_inference():
# Create Feature Views
date_request = RequestSource(
name="date_request", schema=[Field(name="some_date", dtype=UnixTimestamp)],
)
@on_demand_feature_view(
sources={"date_request": date_request},
schema=[
Field(name="output", dtype=UnixTimestamp),
Field(name="string_output", dtype=String),
],
)
def test_view(features_df: pd.DataFrame) -> pd.DataFrame:
data = pd.DataFrame()
data["output"] = features_df["some_date"]
data["string_output"] = features_df["some_date"].astype(pd.StringDtype())
return data
test_view.infer_features()
@on_demand_feature_view(
# Note: we deliberately use `inputs` instead of `sources` to test that `inputs`
# still works correctly, even though it is deprecated.
# TODO(felixwang9817): Remove references to `inputs` once it is fully deprecated.
inputs={"date_request": date_request},
features=[
Feature(name="output", dtype=ValueType.UNIX_TIMESTAMP),
Feature(name="object_output", dtype=ValueType.STRING),
],
)
def invalid_test_view(features_df: pd.DataFrame) -> pd.DataFrame:
data = pd.DataFrame()
data["output"] = features_df["some_date"]
data["object_output"] = features_df["some_date"].astype(str)
return data
with pytest.raises(ValueError, match="Value with native type object"):
invalid_test_view.infer_features()
@on_demand_feature_view(
# Note: we deliberately use positional arguments here to test that they work correctly,
# even though positional arguments are deprecated in favor of keyword arguments.
# TODO(felixwang9817): Remove positional arguments once they are fully deprecated.
[
Feature(name="output", dtype=ValueType.UNIX_TIMESTAMP),
Feature(name="missing", dtype=ValueType.STRING),
],
{"date_request": date_request},
)
def test_view_with_missing_feature(features_df: pd.DataFrame) -> pd.DataFrame:
data = pd.DataFrame()
data["output"] = features_df["some_date"]
return data
with pytest.raises(SpecifiedFeaturesNotPresentError):
test_view_with_missing_feature.infer_features()
# TODO(kevjumba): remove this in feast 0.23 when deprecating
@pytest.mark.parametrize(
"request_source_schema",
[
[Field(name="some_date", dtype=PrimitiveFeastType.UNIX_TIMESTAMP)],
{"some_date": ValueType.UNIX_TIMESTAMP},
],
)
def test_datasource_inference(request_source_schema):
# Create Feature Views
date_request = RequestSource(name="date_request", schema=request_source_schema,)
@on_demand_feature_view(
# Note: we deliberately use positional arguments here to test that they work correctly,
# even though positional arguments are deprecated in favor of keyword arguments.
# TODO(felixwang9817): Remove positional arguments once they are fully deprecated.
[
Feature(name="output", dtype=ValueType.UNIX_TIMESTAMP),
Feature(name="string_output", dtype=ValueType.STRING),
],
sources={"date_request": date_request},
)
def test_view(features_df: pd.DataFrame) -> pd.DataFrame:
data = pd.DataFrame()
data["output"] = features_df["some_date"]
data["string_output"] = features_df["some_date"].astype(pd.StringDtype())
return data
test_view.infer_features()
@on_demand_feature_view(
sources={"date_request": date_request},
schema=[
Field(name="output", dtype=UnixTimestamp),
Field(name="object_output", dtype=String),
],
)
def invalid_test_view(features_df: pd.DataFrame) -> pd.DataFrame:
data = pd.DataFrame()
data["output"] = features_df["some_date"]
data["object_output"] = features_df["some_date"].astype(str)
return data
with pytest.raises(ValueError, match="Value with native type object"):
invalid_test_view.infer_features()
@on_demand_feature_view(
sources={"date_request": date_request},
features=[
Feature(name="output", dtype=ValueType.UNIX_TIMESTAMP),
Feature(name="missing", dtype=ValueType.STRING),
],
)
def test_view_with_missing_feature(features_df: pd.DataFrame) -> pd.DataFrame:
data = pd.DataFrame()
data["output"] = features_df["some_date"]
return data
with pytest.raises(SpecifiedFeaturesNotPresentError):
test_view_with_missing_feature.infer_features()
def test_update_feature_views_with_inferred_features():
file_source = FileSource(name="test", path="test path")
entity1 = Entity(name="test1", join_key="test_column_1")
entity2 = Entity(name="test2", join_key="test_column_2")
feature_view_1 = FeatureView(
name="test1",
entities=[entity1],
schema=[
Field(name="feature", dtype=Float32),
Field(name="test_column_1", dtype=String),
],
source=file_source,
)
feature_view_2 = FeatureView(
name="test2",
entities=[entity1, entity2],
schema=[
Field(name="feature", dtype=Float32),
Field(name="test_column_1", dtype=String),
Field(name="test_column_2", dtype=String),
],
source=file_source,
)
assert len(feature_view_1.schema) == 2
assert len(feature_view_1.features) == 2
# The entity field should be deleted from the schema and features of the feature view.
update_feature_views_with_inferred_features(
[feature_view_1], [entity1], RepoConfig(provider="local", project="test")
)
assert len(feature_view_1.schema) == 1
assert len(feature_view_1.features) == 1
assert len(feature_view_2.schema) == 3
assert len(feature_view_2.features) == 3
# The entity fields should be deleted from the schema and features of the feature view.
update_feature_views_with_inferred_features(
[feature_view_2],
[entity1, entity2],
RepoConfig(provider="local", project="test"),
)
assert len(feature_view_2.schema) == 1
assert len(feature_view_2.features) == 1