-
Notifications
You must be signed in to change notification settings - Fork 948
/
test_universal_historical_retrieval.py
881 lines (784 loc) · 28.8 KB
/
test_universal_historical_retrieval.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
import random
import time
from datetime import datetime, timedelta
from typing import Any, Dict, List, Optional
import numpy as np
import pandas as pd
import pytest
from pandas.testing import assert_frame_equal as pd_assert_frame_equal
from pytz import utc
from feast import utils
from feast.entity import Entity
from feast.errors import (
FeatureNameCollisionError,
RequestDataNotFoundInEntityDfException,
)
from feast.feature_service import FeatureService
from feast.feature_view import FeatureView
from feast.field import Field
from feast.infra.offline_stores.offline_utils import (
DEFAULT_ENTITY_DF_EVENT_TIMESTAMP_COL,
)
from feast.types import Float32, Int32
from tests.integration.feature_repos.repo_configuration import (
construct_universal_feature_views,
table_name_from_data_source,
)
from tests.integration.feature_repos.universal.data_sources.snowflake import (
SnowflakeDataSourceCreator,
)
from tests.integration.feature_repos.universal.entities import (
customer,
driver,
location,
)
np.random.seed(0)
def convert_timestamp_records_to_utc(
records: List[Dict[str, Any]], column: str
) -> List[Dict[str, Any]]:
for record in records:
record[column] = utils.make_tzaware(record[column]).astimezone(utc)
return records
# Find the latest record in the given time range and filter
def find_asof_record(
records: List[Dict[str, Any]],
ts_key: str,
ts_start: datetime,
ts_end: datetime,
filter_keys: Optional[List[str]] = None,
filter_values: Optional[List[Any]] = None,
) -> Dict[str, Any]:
filter_keys = filter_keys or []
filter_values = filter_values or []
assert len(filter_keys) == len(filter_values)
found_record: Dict[str, Any] = {}
for record in records:
if (
all(
[
record[filter_key] == filter_value
for filter_key, filter_value in zip(filter_keys, filter_values)
]
)
and ts_start <= record[ts_key] <= ts_end
):
if not found_record or found_record[ts_key] < record[ts_key]:
found_record = record
return found_record
def get_expected_training_df(
customer_df: pd.DataFrame,
customer_fv: FeatureView,
driver_df: pd.DataFrame,
driver_fv: FeatureView,
orders_df: pd.DataFrame,
order_fv: FeatureView,
location_df: pd.DataFrame,
location_fv: FeatureView,
global_df: pd.DataFrame,
global_fv: FeatureView,
field_mapping_df: pd.DataFrame,
field_mapping_fv: FeatureView,
entity_df: pd.DataFrame,
event_timestamp: str,
full_feature_names: bool = False,
):
# Convert all pandas dataframes into records with UTC timestamps
customer_records = convert_timestamp_records_to_utc(
customer_df.to_dict("records"), customer_fv.batch_source.timestamp_field
)
driver_records = convert_timestamp_records_to_utc(
driver_df.to_dict("records"), driver_fv.batch_source.timestamp_field
)
order_records = convert_timestamp_records_to_utc(
orders_df.to_dict("records"), event_timestamp
)
location_records = convert_timestamp_records_to_utc(
location_df.to_dict("records"), location_fv.batch_source.timestamp_field
)
global_records = convert_timestamp_records_to_utc(
global_df.to_dict("records"), global_fv.batch_source.timestamp_field
)
field_mapping_records = convert_timestamp_records_to_utc(
field_mapping_df.to_dict("records"),
field_mapping_fv.batch_source.timestamp_field,
)
entity_rows = convert_timestamp_records_to_utc(
entity_df.to_dict("records"), event_timestamp
)
# Manually do point-in-time join of driver, customer, and order records against
# the entity df
for entity_row in entity_rows:
customer_record = find_asof_record(
customer_records,
ts_key=customer_fv.batch_source.timestamp_field,
ts_start=entity_row[event_timestamp] - customer_fv.ttl,
ts_end=entity_row[event_timestamp],
filter_keys=["customer_id"],
filter_values=[entity_row["customer_id"]],
)
driver_record = find_asof_record(
driver_records,
ts_key=driver_fv.batch_source.timestamp_field,
ts_start=entity_row[event_timestamp] - driver_fv.ttl,
ts_end=entity_row[event_timestamp],
filter_keys=["driver_id"],
filter_values=[entity_row["driver_id"]],
)
order_record = find_asof_record(
order_records,
ts_key=customer_fv.batch_source.timestamp_field,
ts_start=entity_row[event_timestamp] - order_fv.ttl,
ts_end=entity_row[event_timestamp],
filter_keys=["customer_id", "driver_id"],
filter_values=[entity_row["customer_id"], entity_row["driver_id"]],
)
origin_record = find_asof_record(
location_records,
ts_key=location_fv.batch_source.timestamp_field,
ts_start=order_record[event_timestamp] - location_fv.ttl,
ts_end=order_record[event_timestamp],
filter_keys=["location_id"],
filter_values=[order_record["origin_id"]],
)
destination_record = find_asof_record(
location_records,
ts_key=location_fv.batch_source.timestamp_field,
ts_start=order_record[event_timestamp] - location_fv.ttl,
ts_end=order_record[event_timestamp],
filter_keys=["location_id"],
filter_values=[order_record["destination_id"]],
)
global_record = find_asof_record(
global_records,
ts_key=global_fv.batch_source.timestamp_field,
ts_start=order_record[event_timestamp] - global_fv.ttl,
ts_end=order_record[event_timestamp],
)
field_mapping_record = find_asof_record(
field_mapping_records,
ts_key=field_mapping_fv.batch_source.timestamp_field,
ts_start=order_record[event_timestamp] - field_mapping_fv.ttl,
ts_end=order_record[event_timestamp],
)
entity_row.update(
{
(
f"customer_profile__{k}" if full_feature_names else k
): customer_record.get(k, None)
for k in (
"current_balance",
"avg_passenger_count",
"lifetime_trip_count",
)
}
)
entity_row.update(
{
(f"driver_stats__{k}" if full_feature_names else k): driver_record.get(
k, None
)
for k in ("conv_rate", "avg_daily_trips")
}
)
entity_row.update(
{
(f"order__{k}" if full_feature_names else k): order_record.get(k, None)
for k in ("order_is_success",)
}
)
entity_row.update(
{
"origin__temperature": origin_record.get("temperature", None),
"destination__temperature": destination_record.get("temperature", None),
}
)
entity_row.update(
{
(f"global_stats__{k}" if full_feature_names else k): global_record.get(
k, None
)
for k in ("num_rides", "avg_ride_length",)
}
)
# get field_mapping_record by column name, but label by feature name
entity_row.update(
{
(
f"field_mapping__{feature}" if full_feature_names else feature
): field_mapping_record.get(column, None)
for (
column,
feature,
) in field_mapping_fv.batch_source.field_mapping.items()
}
)
# Convert records back to pandas dataframe
expected_df = pd.DataFrame(entity_rows)
# Move "event_timestamp" column to front
current_cols = expected_df.columns.tolist()
current_cols.remove(event_timestamp)
expected_df = expected_df[[event_timestamp] + current_cols]
# Cast some columns to expected types, since we lose information when converting pandas DFs into Python objects.
if full_feature_names:
expected_column_types = {
"order__order_is_success": "int32",
"driver_stats__conv_rate": "float32",
"customer_profile__current_balance": "float32",
"customer_profile__avg_passenger_count": "float32",
"global_stats__avg_ride_length": "float32",
"field_mapping__feature_name": "int32",
}
else:
expected_column_types = {
"order_is_success": "int32",
"conv_rate": "float32",
"current_balance": "float32",
"avg_passenger_count": "float32",
"avg_ride_length": "float32",
"feature_name": "int32",
}
for col, typ in expected_column_types.items():
expected_df[col] = expected_df[col].astype(typ)
conv_feature_name = "driver_stats__conv_rate" if full_feature_names else "conv_rate"
conv_plus_feature_name = response_feature_name(
"conv_rate_plus_100", full_feature_names
)
expected_df[conv_plus_feature_name] = expected_df[conv_feature_name] + 100
expected_df[
response_feature_name("conv_rate_plus_100_rounded", full_feature_names)
] = (
expected_df[conv_plus_feature_name]
.astype("float")
.round()
.astype(pd.Int32Dtype())
)
if "val_to_add" in expected_df.columns:
expected_df[
response_feature_name("conv_rate_plus_val_to_add", full_feature_names)
] = (expected_df[conv_feature_name] + expected_df["val_to_add"])
return expected_df
@pytest.mark.integration
@pytest.mark.universal_offline_stores
@pytest.mark.parametrize("full_feature_names", [True, False], ids=lambda v: f"full:{v}")
def test_historical_features(environment, universal_data_sources, full_feature_names):
store = environment.feature_store
(entities, datasets, data_sources) = universal_data_sources
feature_views = construct_universal_feature_views(data_sources)
entity_df_with_request_data = datasets.entity_df.copy(deep=True)
entity_df_with_request_data["val_to_add"] = [
i for i in range(len(entity_df_with_request_data))
]
entity_df_with_request_data["driver_age"] = [
i + 100 for i in range(len(entity_df_with_request_data))
]
feature_service = FeatureService(
name="convrate_plus100",
features=[feature_views.driver[["conv_rate"]], feature_views.driver_odfv],
)
feature_service_entity_mapping = FeatureService(
name="entity_mapping",
features=[
feature_views.location.with_name("origin").with_join_key_map(
{"location_id": "origin_id"}
),
feature_views.location.with_name("destination").with_join_key_map(
{"location_id": "destination_id"}
),
],
)
store.apply(
[
driver(),
customer(),
location(),
feature_service,
feature_service_entity_mapping,
*feature_views.values(),
]
)
event_timestamp = (
DEFAULT_ENTITY_DF_EVENT_TIMESTAMP_COL
if DEFAULT_ENTITY_DF_EVENT_TIMESTAMP_COL in datasets.orders_df.columns
else "e_ts"
)
full_expected_df = get_expected_training_df(
datasets.customer_df,
feature_views.customer,
datasets.driver_df,
feature_views.driver,
datasets.orders_df,
feature_views.order,
datasets.location_df,
feature_views.location,
datasets.global_df,
feature_views.global_fv,
datasets.field_mapping_df,
feature_views.field_mapping,
entity_df_with_request_data,
event_timestamp,
full_feature_names,
)
# Only need the shadow entities features in the FeatureService test
expected_df = full_expected_df.drop(
columns=["origin__temperature", "destination__temperature"],
)
job_from_df = store.get_historical_features(
entity_df=entity_df_with_request_data,
features=[
"driver_stats:conv_rate",
"driver_stats:avg_daily_trips",
"customer_profile:current_balance",
"customer_profile:avg_passenger_count",
"customer_profile:lifetime_trip_count",
"conv_rate_plus_100:conv_rate_plus_100",
"conv_rate_plus_100:conv_rate_plus_100_rounded",
"conv_rate_plus_100:conv_rate_plus_val_to_add",
"order:order_is_success",
"global_stats:num_rides",
"global_stats:avg_ride_length",
"field_mapping:feature_name",
],
full_feature_names=full_feature_names,
)
start_time = datetime.utcnow()
actual_df_from_df_entities = job_from_df.to_df()
print(f"actual_df_from_df_entities shape: {actual_df_from_df_entities.shape}")
end_time = datetime.utcnow()
print(str(f"Time to execute job_from_df.to_df() = '{(end_time - start_time)}'\n"))
assert sorted(expected_df.columns) == sorted(actual_df_from_df_entities.columns)
assert_frame_equal(
expected_df,
actual_df_from_df_entities,
keys=[event_timestamp, "order_id", "driver_id", "customer_id"],
)
assert_feature_service_correctness(
store,
feature_service,
full_feature_names,
entity_df_with_request_data,
expected_df,
event_timestamp,
)
assert_feature_service_entity_mapping_correctness(
store,
feature_service_entity_mapping,
full_feature_names,
entity_df_with_request_data,
full_expected_df,
event_timestamp,
)
table_from_df_entities: pd.DataFrame = job_from_df.to_arrow().to_pandas()
assert_frame_equal(
expected_df,
table_from_df_entities,
keys=[event_timestamp, "order_id", "driver_id", "customer_id"],
)
@pytest.mark.integration
@pytest.mark.universal_offline_stores
@pytest.mark.parametrize("full_feature_names", [True, False], ids=lambda v: str(v))
def test_historical_features_with_shared_batch_source(
environment, universal_data_sources, full_feature_names
):
# Addresses https://github.com/feast-dev/feast/issues/2576
store = environment.feature_store
entities, datasets, data_sources = universal_data_sources
driver_stats_v1 = FeatureView(
name="driver_stats_v1",
entities=["driver"],
schema=[Field(name="avg_daily_trips", dtype=Int32)],
source=data_sources.driver,
)
driver_stats_v2 = FeatureView(
name="driver_stats_v2",
entities=["driver"],
schema=[
Field(name="avg_daily_trips", dtype=Int32),
Field(name="conv_rate", dtype=Float32),
],
source=data_sources.driver,
)
store.apply([driver(), driver_stats_v1, driver_stats_v2])
with pytest.raises(KeyError):
store.get_historical_features(
entity_df=datasets.entity_df,
features=[
# `driver_stats_v1` does not have `conv_rate`
"driver_stats_v1:conv_rate",
],
full_feature_names=full_feature_names,
).to_df()
@pytest.mark.integration
@pytest.mark.universal_offline_stores
def test_historical_features_with_missing_request_data(
environment, universal_data_sources
):
store = environment.feature_store
(_, datasets, data_sources) = universal_data_sources
feature_views = construct_universal_feature_views(data_sources)
store.apply([driver(), customer(), location(), *feature_views.values()])
# If request data is missing that's needed for on demand transform, throw an error
with pytest.raises(RequestDataNotFoundInEntityDfException):
store.get_historical_features(
entity_df=datasets.entity_df,
features=[
"customer_profile:current_balance",
"customer_profile:avg_passenger_count",
"customer_profile:lifetime_trip_count",
"conv_rate_plus_100:conv_rate_plus_100",
"conv_rate_plus_100:conv_rate_plus_val_to_add",
"global_stats:num_rides",
"global_stats:avg_ride_length",
"field_mapping:feature_name",
],
full_feature_names=True,
)
@pytest.mark.integration
@pytest.mark.universal_offline_stores
@pytest.mark.parametrize("full_feature_names", [True, False], ids=lambda v: str(v))
def test_historical_features_with_entities_from_query(
environment, universal_data_sources, full_feature_names
):
store = environment.feature_store
(entities, datasets, data_sources) = universal_data_sources
feature_views = construct_universal_feature_views(data_sources)
orders_table = table_name_from_data_source(data_sources.orders)
if not orders_table:
raise pytest.skip("Offline source is not sql-based")
data_source_creator = environment.test_repo_config.offline_store_creator
if data_source_creator.__name__ == SnowflakeDataSourceCreator.__name__:
entity_df_query = f"""
SELECT "customer_id", "driver_id", "order_id", "origin_id", "destination_id", "event_timestamp"
FROM "{orders_table}"
"""
else:
entity_df_query = f"""
SELECT customer_id, driver_id, order_id, origin_id, destination_id, event_timestamp
FROM {orders_table}
"""
store.apply([driver(), customer(), location(), *feature_views.values()])
job_from_sql = store.get_historical_features(
entity_df=entity_df_query,
features=[
"customer_profile:current_balance",
"customer_profile:avg_passenger_count",
"customer_profile:lifetime_trip_count",
"order:order_is_success",
"global_stats:num_rides",
"global_stats:avg_ride_length",
"field_mapping:feature_name",
],
full_feature_names=full_feature_names,
)
start_time = datetime.utcnow()
actual_df_from_sql_entities = job_from_sql.to_df()
end_time = datetime.utcnow()
print(str(f"\nTime to execute job_from_sql.to_df() = '{(end_time - start_time)}'"))
event_timestamp = (
DEFAULT_ENTITY_DF_EVENT_TIMESTAMP_COL
if DEFAULT_ENTITY_DF_EVENT_TIMESTAMP_COL in datasets.orders_df.columns
else "e_ts"
)
full_expected_df = get_expected_training_df(
datasets.customer_df,
feature_views.customer,
datasets.driver_df,
feature_views.driver,
datasets.orders_df,
feature_views.order,
datasets.location_df,
feature_views.location,
datasets.global_df,
feature_views.global_fv,
datasets.field_mapping_df,
feature_views.field_mapping,
datasets.entity_df,
event_timestamp,
full_feature_names,
)
# Not requesting the on demand transform with an entity_df query (can't add request data in them)
expected_df_query = full_expected_df.drop(
columns=[
response_feature_name("conv_rate_plus_100", full_feature_names),
response_feature_name("conv_rate_plus_100_rounded", full_feature_names),
response_feature_name("avg_daily_trips", full_feature_names),
response_feature_name("conv_rate", full_feature_names),
"origin__temperature",
"destination__temperature",
]
)
assert_frame_equal(
expected_df_query,
actual_df_from_sql_entities,
keys=[event_timestamp, "order_id", "driver_id", "customer_id"],
)
table_from_sql_entities = job_from_sql.to_arrow().to_pandas()
for col in table_from_sql_entities.columns:
expected_df_query[col] = expected_df_query[col].astype(
table_from_sql_entities[col].dtype
)
assert_frame_equal(
expected_df_query,
table_from_sql_entities,
keys=[event_timestamp, "order_id", "driver_id", "customer_id"],
)
@pytest.mark.integration
@pytest.mark.universal_offline_stores
@pytest.mark.parametrize("full_feature_names", [True, False], ids=lambda v: str(v))
def test_historical_features_persisting(
environment, universal_data_sources, full_feature_names
):
store = environment.feature_store
(entities, datasets, data_sources) = universal_data_sources
feature_views = construct_universal_feature_views(data_sources)
store.apply([driver(), customer(), location(), *feature_views.values()])
entity_df = datasets.entity_df.drop(
columns=["order_id", "origin_id", "destination_id"]
)
job = store.get_historical_features(
entity_df=entity_df,
features=[
"customer_profile:current_balance",
"customer_profile:avg_passenger_count",
"customer_profile:lifetime_trip_count",
"order:order_is_success",
"global_stats:num_rides",
"global_stats:avg_ride_length",
"field_mapping:feature_name",
],
full_feature_names=full_feature_names,
)
saved_dataset = store.create_saved_dataset(
from_=job,
name="saved_dataset",
storage=environment.data_source_creator.create_saved_dataset_destination(),
tags={"env": "test"},
)
event_timestamp = DEFAULT_ENTITY_DF_EVENT_TIMESTAMP_COL
expected_df = get_expected_training_df(
datasets.customer_df,
feature_views.customer,
datasets.driver_df,
feature_views.driver,
datasets.orders_df,
feature_views.order,
datasets.location_df,
feature_views.location,
datasets.global_df,
feature_views.global_fv,
datasets.field_mapping_df,
feature_views.field_mapping,
entity_df,
event_timestamp,
full_feature_names,
).drop(
columns=[
response_feature_name("conv_rate_plus_100", full_feature_names),
response_feature_name("conv_rate_plus_100_rounded", full_feature_names),
response_feature_name("avg_daily_trips", full_feature_names),
response_feature_name("conv_rate", full_feature_names),
"origin__temperature",
"destination__temperature",
]
)
assert_frame_equal(
expected_df,
saved_dataset.to_df(),
keys=[event_timestamp, "driver_id", "customer_id"],
)
assert_frame_equal(
job.to_df(),
saved_dataset.to_df(),
keys=[event_timestamp, "driver_id", "customer_id"],
)
@pytest.mark.integration
@pytest.mark.universal_offline_stores
def test_historical_features_from_bigquery_sources_containing_backfills(environment):
store = environment.feature_store
now = datetime.now().replace(microsecond=0, second=0, minute=0)
tomorrow = now + timedelta(days=1)
day_after_tomorrow = now + timedelta(days=2)
entity_df = pd.DataFrame(
data=[
{"driver_id": 1001, "event_timestamp": day_after_tomorrow},
{"driver_id": 1002, "event_timestamp": day_after_tomorrow},
]
)
driver_stats_df = pd.DataFrame(
data=[
# Duplicated rows simple case
{
"driver_id": 1001,
"avg_daily_trips": 10,
"event_timestamp": now,
"created": now,
},
{
"driver_id": 1001,
"avg_daily_trips": 20,
"event_timestamp": now,
"created": tomorrow,
},
# Duplicated rows after a backfill
{
"driver_id": 1002,
"avg_daily_trips": 30,
"event_timestamp": now,
"created": tomorrow,
},
{
"driver_id": 1002,
"avg_daily_trips": 40,
"event_timestamp": tomorrow,
"created": now,
},
]
)
expected_df = pd.DataFrame(
data=[
{
"driver_id": 1001,
"event_timestamp": day_after_tomorrow,
"avg_daily_trips": 20,
},
{
"driver_id": 1002,
"event_timestamp": day_after_tomorrow,
"avg_daily_trips": 40,
},
]
)
driver_stats_data_source = environment.data_source_creator.create_data_source(
df=driver_stats_df,
destination_name=f"test_driver_stats_{int(time.time_ns())}_{random.randint(1000, 9999)}",
timestamp_field="event_timestamp",
created_timestamp_column="created",
)
driver = Entity(name="driver", join_keys=["driver_id"])
driver_fv = FeatureView(
name="driver_stats",
entities=[driver],
schema=[Field(name="avg_daily_trips", dtype=Int32)],
batch_source=driver_stats_data_source,
ttl=None,
)
store.apply([driver, driver_fv])
offline_job = store.get_historical_features(
entity_df=entity_df,
features=["driver_stats:avg_daily_trips"],
full_feature_names=False,
)
start_time = datetime.utcnow()
actual_df = offline_job.to_df()
print(f"actual_df shape: {actual_df.shape}")
end_time = datetime.utcnow()
print(str(f"Time to execute job_from_df.to_df() = '{(end_time - start_time)}'\n"))
assert sorted(expected_df.columns) == sorted(actual_df.columns)
assert_frame_equal(expected_df, actual_df, keys=["driver_id"])
def response_feature_name(feature: str, full_feature_names: bool) -> str:
if feature in {"conv_rate", "avg_daily_trips"} and full_feature_names:
return f"driver_stats__{feature}"
if (
feature
in {
"conv_rate_plus_100",
"conv_rate_plus_100_rounded",
"conv_rate_plus_val_to_add",
}
and full_feature_names
):
return f"conv_rate_plus_100__{feature}"
return feature
def assert_feature_service_correctness(
store, feature_service, full_feature_names, entity_df, expected_df, event_timestamp
):
job_from_df = store.get_historical_features(
entity_df=entity_df,
features=feature_service,
full_feature_names=full_feature_names,
)
actual_df_from_df_entities = job_from_df.to_df()
expected_df = expected_df[
[
event_timestamp,
"order_id",
"driver_id",
"customer_id",
response_feature_name("conv_rate", full_feature_names),
response_feature_name("conv_rate_plus_100", full_feature_names),
"driver_age",
]
]
assert_frame_equal(
expected_df,
actual_df_from_df_entities,
keys=[event_timestamp, "order_id", "driver_id", "customer_id"],
)
def assert_feature_service_entity_mapping_correctness(
store, feature_service, full_feature_names, entity_df, expected_df, event_timestamp
):
if full_feature_names:
job_from_df = store.get_historical_features(
entity_df=entity_df,
features=feature_service,
full_feature_names=full_feature_names,
)
actual_df_from_df_entities = job_from_df.to_df()
expected_df: pd.DataFrame = (
expected_df.sort_values(
by=[
event_timestamp,
"order_id",
"driver_id",
"customer_id",
"origin_id",
"destination_id",
]
)
.drop_duplicates()
.reset_index(drop=True)
)
expected_df = expected_df[
[
event_timestamp,
"order_id",
"driver_id",
"customer_id",
"origin_id",
"destination_id",
"origin__temperature",
"destination__temperature",
]
]
assert_frame_equal(
expected_df,
actual_df_from_df_entities,
keys=[
event_timestamp,
"order_id",
"driver_id",
"customer_id",
"origin_id",
"destination_id",
],
)
else:
# using 2 of the same FeatureView without full_feature_names=True will result in collision
with pytest.raises(FeatureNameCollisionError):
job_from_df = store.get_historical_features(
entity_df=entity_df,
features=feature_service,
full_feature_names=full_feature_names,
)
def assert_frame_equal(expected_df, actual_df, keys):
expected_df: pd.DataFrame = (
expected_df.sort_values(by=keys).drop_duplicates().reset_index(drop=True)
)
actual_df = (
actual_df[expected_df.columns]
.sort_values(by=keys)
.drop_duplicates()
.reset_index(drop=True)
)
pd_assert_frame_equal(
expected_df, actual_df, check_dtype=False,
)