Exemplo n.º 1
0
    def test_list_features(self, test_client, mocker):
        mocker.patch.object(
            test_client,
            "_core_service_stub",
            return_value=Core.CoreServiceStub(grpc.insecure_channel("")),
        )

        feature1_proto = FeatureSpecProto(
            name="feature_1", value_type=ValueProto.ValueType.FLOAT)
        feature2_proto = FeatureSpecProto(
            name="feature_2", value_type=ValueProto.ValueType.STRING)

        mocker.patch.object(
            test_client._core_service_stub,
            "ListFeatures",
            return_value=ListFeaturesResponse(
                features={
                    "driver_car:feature_1": feature1_proto,
                    "driver_car:feature_2": feature2_proto,
                }),
        )

        features = test_client.list_features_by_ref(project="test")
        assert len(features) == 2

        native_feature_list = []
        for _, feature_proto in features.items():
            native_feature_list.append(feature_proto)

        assert sorted(native_feature_list) == sorted([
            Feature.from_proto(feature1_proto),
            Feature.from_proto(feature2_proto)
        ])
Exemplo n.º 2
0
    def from_proto(cls, feature_set_proto: FeatureSetProto):
        """
        Creates a feature set from a protobuf representation of a feature set

        Args:
            feature_set_proto: A protobuf representation of a feature set

        Returns:
            Returns a FeatureSet object based on the feature set protobuf
        """

        feature_set = cls(
            name=feature_set_proto.spec.name,
            features=[
                Feature.from_proto(feature)
                for feature in feature_set_proto.spec.features
            ],
            entities=[
                Entity.from_proto(entity)
                for entity in feature_set_proto.spec.entities
            ],
            max_age=feature_set_proto.spec.max_age,
            source=(None if feature_set_proto.spec.source.type == 0 else
                    Source.from_proto(feature_set_proto.spec.source)),
            project=feature_set_proto.spec.project
            if len(feature_set_proto.spec.project) == 0 else
            feature_set_proto.spec.project,
        )
        feature_set._version = feature_set_proto.spec.version
        feature_set._status = feature_set_proto.meta.status
        feature_set._created_timestamp = feature_set_proto.meta.created_timestamp
        return feature_set
Exemplo n.º 3
0
    def from_proto(cls, feature_set_proto: FeatureSetSpecProto):
        """
        Creates a feature set from a protobuf representation of a feature set

        Args:
            from_proto: A protobuf representation of a feature set

        Returns:
            Returns a FeatureSet object based on the feature set protobuf
        """

        feature_set = cls(
            name=feature_set_proto.name,
            features=[
                Feature.from_proto(feature) for feature in feature_set_proto.features
            ],
            entities=[
                Entity.from_proto(entity) for entity in feature_set_proto.entities
            ],
            max_age=feature_set_proto.max_age,
            source=(
                None
                if feature_set_proto.source.type == 0
                else Source.from_proto(feature_set_proto.source)
            ),
        )
        feature_set._version = feature_set_proto.version
        feature_set._is_dirty = False
        return feature_set
Exemplo n.º 4
0
    def from_proto(cls, feature_table_proto: FeatureTableProto):
        """
        Creates a feature table from a protobuf representation of a feature table

        Args:
            feature_table_proto: A protobuf representation of a feature table

        Returns:
            Returns a FeatureTableProto object based on the feature table protobuf
        """

        feature_table = cls(
            name=feature_table_proto.spec.name,
            entities=[entity for entity in feature_table_proto.spec.entities],
            features=[
                Feature.from_proto(feature)
                for feature in feature_table_proto.spec.features
            ],
            labels=feature_table_proto.spec.labels,
            max_age=(None if feature_table_proto.spec.max_age.seconds == 0
                     and feature_table_proto.spec.max_age.nanos == 0 else
                     feature_table_proto.spec.max_age),
            batch_source=DataSource.from_proto(
                feature_table_proto.spec.batch_source),
            stream_source=(
                None
                if not feature_table_proto.spec.stream_source.ByteSize() else
                DataSource.from_proto(feature_table_proto.spec.stream_source)),
        )

        feature_table._created_timestamp = feature_table_proto.meta.created_timestamp

        return feature_table
Exemplo n.º 5
0
    def from_proto(proto: FeatureViewProjectionProto):
        ref = FeatureViewProjection(
            name=proto.feature_view_name,
            name_alias=proto.feature_view_name_alias,
            features=[],
            join_key_map=dict(proto.join_key_map),
        )
        for feature_column in proto.feature_columns:
            ref.features.append(Feature.from_proto(feature_column))

        return ref
Exemplo n.º 6
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
Exemplo n.º 7
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
Exemplo n.º 8
0
 def from_proto(cls, feature_set_proto: FeatureSetSpecProto):
     feature_set = cls(
         name=feature_set_proto.name,
         features=[
             Feature.from_proto(feature) for feature in feature_set_proto.features
         ],
         entities=[
             Entity.from_proto(entity) for entity in feature_set_proto.entities
         ],
         max_age=feature_set_proto.max_age,
         source=(
             None
             if feature_set_proto.source.type == 0
             else Source.from_proto(feature_set_proto.source)
         ),
     )
     feature_set._version = feature_set_proto.version
     feature_set._is_dirty = False
     return feature_set
Exemplo n.º 9
0
    def from_proto(proto: FeatureViewProjectionProto):
        ref = FeatureViewProjection(name=proto.feature_view_name, features=[])
        for feature_column in proto.feature_columns:
            ref.features.append(Feature.from_proto(feature_column))

        return ref