in 04-module-working-with-online-store/custom_library/helper.py [0:0]
def get_latest_featureset_values(id_dict, features, verbose=False):
## TODO: BatchGetRecord does not honor the order of the features requested, so this function
## should enforce reordering of results to match the requested order. This is important when mapping
## to a feature vector to feed a model prediction.
_feature_types = {}
_resp = None
_features_df = _feature_list_to_df(features)
if verbose:
print(_features_df.head())
_gb = _features_df.groupby('fg_name')
_fg_requests = []
for _g in _gb:
_curr_features = []
_fg_name = _g[0]
for _f in _g[1].iterrows():
_curr_features.append(_f[1]['feature_name'])
_feature_defs = describe_feature_group(_fg_name)['FeatureDefinitions']
for _fd in _feature_defs:
_feature_types[_fd['FeatureName']] = _fd['FeatureType']
_id_feature_name = describe_feature_group(_fg_name)['RecordIdentifierFeatureName']
_id_value = id_dict[_id_feature_name]
if verbose:
print(f'fg name: {_fg_name}, id: {_id_feature_name}, id val: {_id_value}, features: {_curr_features}')
_fg_requests.append({'FeatureGroupName': _fg_name,
'RecordIdentifiersValueAsString': [str(_id_value)],
'FeatureNames': _curr_features})
if verbose:
print(_fg_requests)
print(_feature_types)
_resp = featurestore_runtime.batch_get_record(Identifiers=_fg_requests)
if verbose:
_num_recs = len(_resp['Records'])
print(f'got back {_num_recs} records')
_results_dict = []
_all_records = _resp['Records']
if verbose:
print(f'After calling batch_get_record on {id_dict}, here are the {len(_all_records)} records returned')
print('=== start records ===')
print(_all_records)
print('=== end records ===')
for _req in _fg_requests:
_fg_name = _req['FeatureGroupName']
# enforce the original order of feature groups
_curr_record = next(_item for _item in _all_records if _item['FeatureGroupName'] == _fg_name)['Record']
if verbose:
print(_curr_record)
## TODO: extend _record_to_dict to take a feature name list and enforce that order in the return value
_curr_record_dict = _record_to_dict(_curr_record, _feature_types)
if verbose:
print(_results_dict)
print(_curr_record_dict)
_results_dict = dict(_results_dict, **_curr_record_dict)
return _results_dict