Пример #1
0
    def list_features_by_ref(
            self,
            project: str = None,
            entities: List[str] = list(),
            labels: Dict[str, str] = dict(),
    ) -> Dict[FeatureRef, Feature]:
        """
        Retrieve a dictionary of feature reference to feature from Feast Core based on filters provided.

        Args:
            project: Feast project that these features belongs to
            entities: Feast entity that these features are associated with
            labels: Feast labels that these features are associated with

        Returns:
            Dictionary of <feature references: features>

        Examples:
            >>> from feast import Client
            >>>
            >>> feast_client = Client(core_url="localhost:6565")
            >>> features = feast_client.list_features(project="test_project", entities=["driver_id"], labels={"key1":"val1","key2":"val2"})
            >>> print(features)
        """

        if self._use_object_store_registry:
            raise NotImplementedError(
                "This function is not implemented for object store registry.")
        else:
            if project is None:
                project = self.project

            filter = ListFeaturesRequest.Filter(project=project,
                                                entities=entities,
                                                labels=labels)

            feature_protos = self._core_service.ListFeatures(
                ListFeaturesRequest(filter=filter),
                metadata=self._get_grpc_metadata(),
            )  # type: ListFeaturesResponse

            # Extract features and return
            features_dict = {}
            for ref_str, feature_proto in feature_protos.features.items():
                feature_ref = FeatureRef.from_str(ref_str)
                feature = Feature.from_proto(feature_proto)
                features_dict[feature_ref] = feature

            return features_dict
Пример #2
0
    def list_features_by_ref(
            self,
            project: str = None,
            entities: List[str] = list(),
            labels: Dict[str, str] = dict(),
    ) -> Dict[FeatureRef, Feature]:
        """
        Returns a list of features based on filters provided.

        Args:
            project: Feast project that these features belongs to
            entities: Feast entity that these features are associated with
            labels: Feast labels that these features are associated with

        Returns:
            Dictionary of <feature references: features>

        Examples:
            >>> from feast import Client
            >>>
            >>> feast_client = Client(core_url="localhost:6565")
            >>> features = list_features_by_ref(project="test_project", entities=["driver_id"], labels={"key1":"val1","key2":"val2"})
            >>> print(features)
        """
        self._connect_core()

        if project is None:
            if self.project is not None:
                project = self.project
            else:
                project = "default"

        filter = ListFeaturesRequest.Filter(project=project,
                                            entities=entities,
                                            labels=labels)

        feature_protos = self._core_service_stub.ListFeatures(
            ListFeaturesRequest(filter=filter))  # type: ListFeaturesResponse

        features_dict = {}
        for ref_str, feature_proto in feature_protos.features.items():
            feature_ref = FeatureRef.from_str(ref_str, ignore_project=True)
            feature = Feature.from_proto(feature_proto)
            features_dict[feature_ref] = feature

        return features_dict
Пример #3
0
def _build_feature_references(
    feature_ref_strs: List[str], project: Optional[str] = None
) -> List[FeatureReference]:
    """
    Builds a list of FeatureReference protos from string feature set references

    Args:
        feature_ref_strs: List of string feature references
        project: Optionally specifies the project in the parsed feature references.

    Returns:
        A list of FeatureReference protos parsed from args.
    """
    feature_refs = [FeatureRef.from_str(ref_str) for ref_str in feature_ref_strs]
    feature_ref_protos = [ref.to_proto() for ref in feature_refs]
    # apply project if specified
    if project is not None:
        for feature_ref_proto in feature_ref_protos:
            feature_ref_proto.project = project
    return feature_ref_protos
Пример #4
0
    def get_online_features(
        self,
        feature_refs: List[str],
        entity_rows: List[GetOnlineFeaturesRequest.EntityRow],
        project: Optional[str] = None,
    ) -> GetOnlineFeaturesResponse:
        """
        Retrieves the latest online feature data from Feast Serving

        Args:
            feature_refs: List of feature references that will be returned for each entity.
                Each feature reference should have the following format:
                "feature_set:feature" where "feature_set" & "feature" refer to
                the feature and feature set names respectively.
                Only the feature name is required.
            entity_rows: List of GetFeaturesRequest.EntityRow where each row
                contains entities. Timestamp should not be set for online
                retrieval. All entity types within a feature
            project: Specifies the project which contain the FeatureSets
                which the requested features belong to.

        Returns:
            Returns a list of maps where each item in the list contains the
            latest feature values for the provided entities
        """
        self._connect_serving()

        try:
            response = self._serving_service_stub.GetOnlineFeatures(
                GetOnlineFeaturesRequest(
                    features=_build_feature_references(
                        feature_ref_strs=feature_refs,
                        project=project
                        if project is not None else self.project,
                    ),
                    entity_rows=entity_rows,
                ))
            # collect entity row refs
            entity_refs = set()
            for entity_row in entity_rows:
                entity_refs.update(entity_row.fields.keys())

            strip_field_values = []
            for field_value in response.field_values:
                # strip the project part the string feature references returned from serving
                strip_fields = {}
                for ref_str, value in field_value.fields.items():
                    # find and ignore entities
                    if ref_str in entity_refs:
                        strip_fields[ref_str] = value
                    else:
                        strip_ref_str = repr(
                            FeatureRef.from_str(ref_str, ignore_project=True))
                        strip_fields[strip_ref_str] = value
                strip_field_values.append(
                    GetOnlineFeaturesResponse.FieldValues(fields=strip_fields))

            del response.field_values[:]
            response.field_values.extend(strip_field_values)

        except grpc.RpcError as e:
            raise grpc.RpcError(e.details())

        return response
Пример #5
0
 def test_str_ref(self):
     original_ref = FeatureRef(feature_set="test", name="test")
     ref_str = repr(original_ref)
     parsed_ref = FeatureRef.from_str(ref_str)
     assert original_ref == parsed_ref
Пример #6
0
    def get_online_features(
        self,
        feature_refs: List[str],
        entity_rows: List[GetOnlineFeaturesRequest.EntityRow],
        project: Optional[str] = None,
        omit_entities: bool = False,
    ) -> GetOnlineFeaturesResponse:
        """
        Retrieves the latest online feature data from Feast Serving

        Args:
            feature_refs: List of feature references that will be returned for each entity.
                Each feature reference should have the following format:
                "feature_set:feature" where "feature_set" & "feature" refer to
                the feature and feature set names respectively.
                Only the feature name is required.
            entity_rows: List of GetFeaturesRequest.EntityRow where each row
                contains entities. Timestamp should not be set for online
                retrieval. All entity types within a feature
            project: Specifies the project which contain the FeatureSets
                which the requested features belong to.
            omit_entities: If true will omit entity values in the returned feature data.

        Returns:
            GetOnlineFeaturesResponse containing the feature data in records.
            Each EntityRow provided will yield one record, which contains
            data fields with data value and field status metadata (if included).
        """

        try:
            response = self._serving_service.GetOnlineFeatures(
                GetOnlineFeaturesRequest(
                    omit_entities_in_response=omit_entities,
                    features=_build_feature_references(
                        feature_ref_strs=feature_refs,
                        project=project
                        if project is not None else self.project,
                    ),
                    entity_rows=entity_rows,
                ))

            entity_refs = {
                key
                for entity_row in entity_rows
                for key in entity_row.fields.keys()
            }
            # strip the project part the string feature references returned from serving
            strip = (
                lambda ref: repr(FeatureRef.from_str(ref, ignore_project=True))
                if ref not in entity_refs else ref)
            strip_field_values = []
            for field_value in response.field_values:
                keys, fields, statuses = (
                    field_value.fields.keys(),
                    field_value.fields,
                    field_value.statuses,
                )
                fields_and_statuses = [(strip(key), fields[key], statuses[key])
                                       for key in keys]
                keys, fields, statuses = zip(*fields_and_statuses)
                strip_field_values.append(
                    GetOnlineFeaturesResponse.FieldValues(
                        fields=dict(zip(keys, fields)),
                        statuses=dict(zip(keys, statuses)),
                    ))
            del response.field_values[:]
            response.field_values.extend(strip_field_values)

        except grpc.RpcError as e:
            raise grpc.RpcError(e.details())

        return response