Skip to content

Commit 24ece4d

Browse files
committed
samples: add feature store samples
1 parent ef3fcc8 commit 24ece4d

File tree

4 files changed

+196
-0
lines changed

4 files changed

+196
-0
lines changed

samples/model-builder/conftest.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,3 +364,48 @@ def mock_endpoint_explain(mock_endpoint):
364364
with patch.object(mock_endpoint, "explain") as mock_endpoint_explain:
365365
mock_get_endpoint.return_value = mock_endpoint
366366
yield mock_endpoint_explain
367+
368+
369+
"""
370+
----------------------------------------------------------------------------
371+
Featurestore Fixtures
372+
----------------------------------------------------------------------------
373+
"""
374+
375+
376+
@pytest.fixture
377+
def mock_featurestore():
378+
mock = MagicMock(aiplatform.featurestore.Featurestore)
379+
yield mock
380+
381+
382+
@pytest.fixture
383+
def mock_entity_type():
384+
mock = MagicMock(aiplatform.featurestore.EntityType)
385+
yield mock
386+
387+
388+
@pytest.fixture
389+
def mock_get_featurestore(mock_featurestore):
390+
with patch.object(aiplatform, "Featurestore") as mock_get_featurestore:
391+
mock_get_featurestore.return_value = mock_featurestore
392+
yield mock_get_featurestore
393+
394+
395+
@pytest.fixture
396+
def mock_batch_serve_to_df(mock_featurestore):
397+
with patch.object(mock_featurestore, "batch_serve_to_df") as mock_batch_serve_to_df:
398+
yield mock_batch_serve_to_df
399+
400+
401+
@pytest.fixture
402+
def mock_get_entity_type(mock_entity_type):
403+
with patch.object(aiplatform, "EntityType") as mock_get_entity_type:
404+
mock_get_entity_type.return_value = mock_entity_type
405+
yield mock_get_entity_type
406+
407+
408+
@pytest.fixture
409+
def mock_ingest_from_df(mock_entity_type):
410+
with patch.object(mock_entity_type, "ingest_from_df") as mock_ingest_from_df:
411+
yield mock_ingest_from_df
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
import pandas as pd
17+
18+
from google.cloud import aiplatform
19+
20+
# [START aiplatform_sdk_entity_type_ingest_feature_values_from_df_sample]
21+
def entity_type_ingest_feature_values_from_df_with_feature_time_field_sample(
22+
project: str, location: str, entity_type_name: str,
23+
):
24+
aiplatform.init(project=project, location=location)
25+
26+
et = aiplatform.EntityType(entity_type_name=entity_type_name)
27+
28+
df_source = pd.DataFrame(
29+
data=[
30+
{
31+
"movie_id": "movie_01",
32+
"average_rating": 4.9,
33+
"title": "The Shawshank Redemption",
34+
"genres": "Drama",
35+
"update_time": "2021-08-20 20:44:11.094375+00:00",
36+
},
37+
{
38+
"movie_id": "movie_02",
39+
"average_rating": 4.2,
40+
"title": "The Shining",
41+
"genres": "Horror",
42+
"update_time": "2021-08-20 20:44:11.094375+00:00",
43+
},
44+
],
45+
columns=["movie_id", "average_rating", "title", "genres", "update_time"],
46+
)
47+
48+
et.ingest_from_df(
49+
feature_ids=["movid_id", "average_rating", "title", "genres"],
50+
feature_time="update_time",
51+
df_source=df_source,
52+
entity_id_field="movie_id",
53+
)
54+
55+
return et
56+
57+
58+
# [END aiplatform_sdk_entity_type_ingest_feature_values_from_df_sample]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
import entity_type_ingest_from_df_sample
17+
18+
import test_constants as constants
19+
20+
21+
def test_image_dataset_create_classification_sample(
22+
mock_sdk_init, mock_get_entity_type, mock_ingest_from_df
23+
):
24+
entity_type_ingest_from_df_sample.entity_type_ingest_feature_values_from_df_with_feature_time_field_sample(
25+
project=constants.PROJECT,
26+
location=constants.LOCATION,
27+
)
28+
29+
mock_sdk_init.assert_called_once_with(
30+
project=constants.PROJECT, location=constants.LOCATION
31+
)
32+
mock_ingest_from_df.assert_called_once_with(
33+
)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
import pandas as pd
17+
18+
from google.cloud import aiplatform
19+
20+
# [START aiplatform_sdk_featurestore_batch_serve_feature_values_to_df_sample]
21+
def featurestore_batch_serve_feature_values_to_df_sample(
22+
project: str, location: str, featurestore_name: str,
23+
):
24+
aiplatform.init(project=project, location=location)
25+
26+
fs = aiplatform.Featurestore(featurestore_name=featurestore_name)
27+
28+
read_instances_df = pd.DataFrame(
29+
data=[
30+
["alice", "movie_01", "2021-09-15T08:28:14Z"],
31+
["bob", "movie_02", "2021-09-15T08:28:14Z"],
32+
["dav", "movie_03", "2021-09-15T08:28:14Z"],
33+
["eve", "movie_04", "2021-09-15T08:28:14Z"],
34+
["alice", "movie_03", "2021-09-14T09:35:15Z"],
35+
["bob", "movie_04", "2020-02-14T09:35:15Z"],
36+
],
37+
columns=["users_entity_type_id", "movies_entity_type_id", "timestamp"],
38+
)
39+
read_instances_df = read_instances_df.astype({"timestamp": "datetime64"})
40+
41+
df = fs.batch_serve_to_df(
42+
serving_feature_ids={
43+
"users_entity_type_id": [
44+
"user_age_feature_id",
45+
"user_gender_feature_id",
46+
"user_liked_genres_feature_id",
47+
],
48+
"movies_entity_type_id": [
49+
"movie_title_feature_id",
50+
"movie_genres_feature_id",
51+
"movie_average_rating_feature_id",
52+
],
53+
},
54+
read_instances_df=read_instances_df,
55+
)
56+
57+
return df
58+
59+
60+
# [END aiplatform_sdk_featurestore_batch_serve_feature_values_to_df_sample]

0 commit comments

Comments
 (0)