|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +"""Feature Store. |
| 14 | +
|
| 15 | +Amazon SageMaker Feature Store is a fully managed, purpose-built repository to store, share, and |
| 16 | +manage features for machine learning (ML) models. |
| 17 | +""" |
| 18 | +from __future__ import absolute_import |
| 19 | + |
| 20 | +import datetime |
| 21 | +from typing import Any, Dict, Sequence, Union |
| 22 | + |
| 23 | +import attr |
| 24 | +import pandas as pd |
| 25 | + |
| 26 | +from sagemaker import Session |
| 27 | +from sagemaker.feature_store.dataset_builder import DatasetBuilder |
| 28 | +from sagemaker.feature_store.feature_group import FeatureGroup |
| 29 | + |
| 30 | + |
| 31 | +@attr.s |
| 32 | +class FeatureStore: |
| 33 | + """FeatureStore definition. |
| 34 | +
|
| 35 | + This class instantiates a FeatureStore object that comprises a SageMaker session instance. |
| 36 | +
|
| 37 | + Attributes: |
| 38 | + sagemaker_session (Session): session instance to perform boto calls. |
| 39 | + """ |
| 40 | + |
| 41 | + sagemaker_session: Session = attr.ib(default=Session) |
| 42 | + |
| 43 | + def create_dataset( |
| 44 | + self, |
| 45 | + base: Union[FeatureGroup, pd.DataFrame], |
| 46 | + output_path: str, |
| 47 | + record_identifier_feature_name: str = None, |
| 48 | + event_time_identifier_feature_name: str = None, |
| 49 | + included_feature_names: Sequence[str] = None, |
| 50 | + kms_key_id: str = None, |
| 51 | + ) -> DatasetBuilder: |
| 52 | + """Create a Dataset Builder for generating a Dataset. |
| 53 | +
|
| 54 | + Args: |
| 55 | + base (Union[FeatureGroup, DataFrame]): A base which can be either a FeatureGroup or a |
| 56 | + pandas.DataFrame and will be used to merge other FeatureGroups and generate a |
| 57 | + Dataset. |
| 58 | + output_path (str): An S3 URI which stores the output .csv file. |
| 59 | + record_identifier_feature_name (str): A string representing the record identifier |
| 60 | + feature if base is a DataFrame (default: None). |
| 61 | + event_time_identifier_feature_name (str): A string representing the event time |
| 62 | + identifier feature if base is a DataFrame (default: None). |
| 63 | + included_feature_names (List[str]): A list of features to be included in the output |
| 64 | + (default: None). |
| 65 | + kms_key_id (str): An KMS key id. If set, will be used to encrypt the result file |
| 66 | + (default: None). |
| 67 | +
|
| 68 | + Raises: |
| 69 | + ValueError: Base is a Pandas DataFrame but no record identifier feature name nor event |
| 70 | + time identifier feature name is provided. |
| 71 | + """ |
| 72 | + if isinstance(base, pd.DataFrame): |
| 73 | + if record_identifier_feature_name is None or event_time_identifier_feature_name is None: |
| 74 | + raise ValueError( |
| 75 | + "You must provide a record identifier feature name and an event time " |
| 76 | + + "identifier feature name if specify DataFrame as base." |
| 77 | + ) |
| 78 | + return DatasetBuilder( |
| 79 | + self.sagemaker_session, |
| 80 | + base, |
| 81 | + output_path, |
| 82 | + record_identifier_feature_name, |
| 83 | + event_time_identifier_feature_name, |
| 84 | + included_feature_names, |
| 85 | + kms_key_id, |
| 86 | + ) |
| 87 | + |
| 88 | + def list_feature_groups( |
| 89 | + self, |
| 90 | + name_contains: str = None, |
| 91 | + feature_group_status_equals: str = None, |
| 92 | + offline_store_status_equals: str = None, |
| 93 | + creation_time_after: datetime.datetime = None, |
| 94 | + creation_time_before: datetime.datetime = None, |
| 95 | + sort_order: str = None, |
| 96 | + sort_by: str = None, |
| 97 | + max_results: int = None, |
| 98 | + next_token: str = None, |
| 99 | + ) -> Dict[str, Any]: |
| 100 | + """List all FeatureGroups satisfying given filters. |
| 101 | +
|
| 102 | + Args: |
| 103 | + name_contains (str): A string that partially matches one or more FeatureGroups' names. |
| 104 | + Filters FeatureGroups by name. |
| 105 | + feature_group_status_equals (str): A FeatureGroup status. |
| 106 | + Filters FeatureGroups by FeatureGroup status. |
| 107 | + offline_store_status_equals (str): An OfflineStore status. |
| 108 | + Filters FeatureGroups by OfflineStore status. |
| 109 | + creation_time_after (datetime.datetime): Use this parameter to search for FeatureGroups |
| 110 | + created after a specific date and time. |
| 111 | + creation_time_before (datetime.datetime): Use this parameter to search for FeatureGroups |
| 112 | + created before a specific date and time. |
| 113 | + sort_order (str): The order in which FeatureGroups are listed. |
| 114 | + sort_by (str): The value on which the FeatureGroup list is sorted. |
| 115 | + max_results (int): The maximum number of results returned by ListFeatureGroups. |
| 116 | + next_token (str): A token to resume pagination of ListFeatureGroups results. |
| 117 | + Returns: |
| 118 | + Response dict from service. |
| 119 | + """ |
| 120 | + return self.sagemaker_session.list_feature_groups( |
| 121 | + name_contains=name_contains, |
| 122 | + feature_group_status_equals=feature_group_status_equals, |
| 123 | + offline_store_status_equals=offline_store_status_equals, |
| 124 | + creation_time_after=creation_time_after, |
| 125 | + creation_time_before=creation_time_before, |
| 126 | + sort_order=sort_order, |
| 127 | + sort_by=sort_by, |
| 128 | + max_results=max_results, |
| 129 | + next_token=next_token, |
| 130 | + ) |
0 commit comments