-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial scaffolding for on demand feature view #1803
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1fb6c12
Initial scaffolding for on demand feature view, with initial support …
adchia 123d129
Fixing comments
adchia 26a567d
Comments
adchia 13b612b
Added basic test
adchia d34e227
Simplifying function serialization
adchia 61c9f4d
Refactor logic into odfv
adchia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// Copyright 2020 The Feast Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
|
||
syntax = "proto3"; | ||
package feast.core; | ||
|
||
option go_package = "github.com/feast-dev/feast/sdk/go/protos/feast/core"; | ||
option java_outer_classname = "OnDemandFeatureViewProto"; | ||
option java_package = "feast.proto.core"; | ||
|
||
import "feast/core/FeatureView.proto"; | ||
import "feast/core/Feature.proto"; | ||
|
||
message OnDemandFeatureView { | ||
// User-specified specifications of this feature view. | ||
OnDemandFeatureViewSpec spec = 1; | ||
} | ||
|
||
message OnDemandFeatureViewSpec { | ||
// Name of the feature view. Must be unique. Not updated. | ||
string name = 1; | ||
|
||
// Name of Feast project that this feature view belongs to. | ||
string project = 2; | ||
|
||
// List of features specifications for each feature defined with this feature view. | ||
repeated FeatureSpecV2 features = 3; | ||
|
||
adchia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// List of features specifications for each feature defined with this feature view. | ||
// TODO(adchia): add support for request data | ||
map<string, FeatureView> inputs = 4; | ||
adchia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
UserDefinedFunction user_defined_function = 5; | ||
} | ||
|
||
// Serialized representation of python function. | ||
message UserDefinedFunction { | ||
// The function name | ||
string name = 1; | ||
|
||
// The python-syntax function body (serialized by dill) | ||
bytes body = 2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import functools | ||
from types import MethodType | ||
from typing import Dict, List | ||
|
||
import dill | ||
import pandas as pd | ||
|
||
from feast.feature import Feature | ||
from feast.feature_view import FeatureView | ||
from feast.protos.feast.core.OnDemandFeatureView_pb2 import ( | ||
OnDemandFeatureView as OnDemandFeatureViewProto, | ||
) | ||
from feast.protos.feast.core.OnDemandFeatureView_pb2 import OnDemandFeatureViewSpec | ||
from feast.protos.feast.core.OnDemandFeatureView_pb2 import ( | ||
UserDefinedFunction as UserDefinedFunctionProto, | ||
) | ||
from feast.usage import log_exceptions | ||
from feast.value_type import ValueType | ||
|
||
|
||
class OnDemandFeatureView: | ||
""" | ||
An OnDemandFeatureView defines on demand transformations on existing feature view values and request data. | ||
|
||
Args: | ||
name: Name of the group of features. | ||
features: Output schema of transformation with feature names | ||
inputs: The input feature views passed into the transform. | ||
udf: User defined transformation function that takes as input pandas dataframes | ||
""" | ||
|
||
name: str | ||
features: List[Feature] | ||
inputs: Dict[str, FeatureView] | ||
udf: MethodType | ||
|
||
@log_exceptions | ||
def __init__( | ||
self, | ||
name: str, | ||
features: List[Feature], | ||
inputs: Dict[str, FeatureView], | ||
udf: MethodType, | ||
): | ||
""" | ||
Creates an OnDemandFeatureView object. | ||
""" | ||
|
||
self.name = name | ||
self.features = features | ||
self.inputs = inputs | ||
self.udf = udf | ||
|
||
def to_proto(self) -> OnDemandFeatureViewProto: | ||
""" | ||
Converts an on demand feature view object to its protobuf representation. | ||
|
||
Returns: | ||
A OnDemandFeatureViewProto protobuf. | ||
""" | ||
spec = OnDemandFeatureViewSpec( | ||
name=self.name, | ||
features=[feature.to_proto() for feature in self.features], | ||
inputs={k: fv.to_proto() for k, fv in self.inputs.items()}, | ||
user_defined_function=UserDefinedFunctionProto( | ||
name=self.udf.__name__, body=dill.dumps(self.udf, recurse=True), | ||
), | ||
) | ||
|
||
return OnDemandFeatureViewProto(spec=spec) | ||
|
||
@classmethod | ||
def from_proto(cls, on_demand_feature_view_proto: OnDemandFeatureViewProto): | ||
""" | ||
Creates an on demand feature view from a protobuf representation. | ||
|
||
Args: | ||
on_demand_feature_view_proto: A protobuf representation of an on-demand feature view. | ||
|
||
Returns: | ||
A OnDemandFeatureView object based on the on-demand feature view protobuf. | ||
""" | ||
on_demand_feature_view_obj = cls( | ||
name=on_demand_feature_view_proto.spec.name, | ||
features=[ | ||
Feature( | ||
name=feature.name, | ||
dtype=ValueType(feature.value_type), | ||
labels=dict(feature.labels), | ||
) | ||
for feature in on_demand_feature_view_proto.spec.features | ||
], | ||
inputs={ | ||
feature_view_name: FeatureView.from_proto(feature_view_proto) | ||
for feature_view_name, feature_view_proto in on_demand_feature_view_proto.spec.inputs.items() | ||
}, | ||
udf=dill.loads( | ||
on_demand_feature_view_proto.spec.user_defined_function.body | ||
), | ||
) | ||
|
||
return on_demand_feature_view_obj | ||
|
||
def get_transformed_features_df( | ||
self, full_feature_names: bool, df_with_features: pd.DataFrame | ||
) -> pd.DataFrame: | ||
# Apply on demand transformations | ||
# TODO(adchia): Include only the feature values from the specified input FVs in the ODFV. | ||
# Copy over un-prefixed features even if not requested since transform may need it | ||
columns_to_cleanup = [] | ||
if full_feature_names: | ||
for input_fv in self.inputs.values(): | ||
for feature in input_fv.features: | ||
full_feature_ref = f"{input_fv.name}__{feature.name}" | ||
if full_feature_ref in df_with_features.keys(): | ||
df_with_features[feature.name] = df_with_features[ | ||
full_feature_ref | ||
] | ||
columns_to_cleanup.append(feature.name) | ||
|
||
# Compute transformed values and apply to each result row | ||
df_with_transformed_features = self.udf.__call__(df_with_features) | ||
|
||
# Cleanup extra columns used for transformation | ||
df_with_features.drop(columns=columns_to_cleanup, inplace=True) | ||
return df_with_transformed_features | ||
|
||
|
||
def on_demand_feature_view(features: List[Feature], inputs: Dict[str, FeatureView]): | ||
""" | ||
Declare an on-demand feature view | ||
|
||
:param features: Output schema with feature names | ||
:param inputs: The inputs passed into the transform. | ||
:return: An On Demand Feature View. | ||
""" | ||
|
||
def decorator(user_function): | ||
on_demand_feature_view_obj = OnDemandFeatureView( | ||
name=user_function.__name__, | ||
inputs=inputs, | ||
features=features, | ||
udf=user_function, | ||
) | ||
functools.update_wrapper( | ||
wrapper=on_demand_feature_view_obj, wrapped=user_function | ||
) | ||
return on_demand_feature_view_obj | ||
|
||
return decorator |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit, 2021?