|
| 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 | +"""Dataset Builder |
| 14 | +
|
| 15 | +A Dataset Builder is a builder class for generating a dataset by providing conditions. |
| 16 | +""" |
| 17 | +from __future__ import absolute_import |
| 18 | + |
| 19 | +import datetime |
| 20 | +from typing import Sequence, Union |
| 21 | + |
| 22 | +import attr |
| 23 | +import pandas as pd |
| 24 | + |
| 25 | +from sagemaker.feature_store.feature_group import FeatureGroup |
| 26 | + |
| 27 | + |
| 28 | +@attr.s |
| 29 | +class DatasetBuilder: |
| 30 | + """DatasetBuilder definition. |
| 31 | +
|
| 32 | + This class instantiates a DatasetBuilder object that comprises a base, a list of feature names, |
| 33 | + an output path and a KMS key ID. |
| 34 | +
|
| 35 | + Attributes: |
| 36 | + _base (Union[FeatureGroup, DataFrame]): A base which can be either a FeatureGroup or a |
| 37 | + pandas.DataFrame and will be used to merge other FeatureGroups and generate a Dataset. |
| 38 | + _output_path (str): An S3 URI which stores the output .csv file. |
| 39 | + _record_identifier_feature_name (str): A string representing the record identifier feature |
| 40 | + if base is a DataFrame. |
| 41 | + _event_time_identifier_feature_name (str): A string representing the event time identifier |
| 42 | + feature if base is a DataFrame. |
| 43 | + _included_feature_names (List[str]): A list of features to be included in the output. |
| 44 | + _kms_key_id (str): An KMS key id. If set, will be used to encrypt the result file. |
| 45 | + _point_in_time_accurate_join (bool): A boolean representing whether using point in time join |
| 46 | + or not. |
| 47 | + _include_duplicated_records (bool): A boolean representing whether including duplicated |
| 48 | + records or not. |
| 49 | + _include_deleted_records (bool): A boolean representing whether including deleted records or |
| 50 | + not. |
| 51 | + _number_of_recent_records (int): An int that how many records will be returned for each |
| 52 | + record identifier. |
| 53 | + _number_of_records (int): An int that how many records will be returned. |
| 54 | + _write_time_ending_timestamp (datetime.datetime): A datetime that all records' write time in |
| 55 | + dataset will be before it. |
| 56 | + _event_time_starting_timestamp (datetime.datetime): A datetime that all records' event time |
| 57 | + in dataset will be after it. |
| 58 | + _event_time_ending_timestamp (datetime.datetime): A datetime that all records' event time in |
| 59 | + dataset will be before it. |
| 60 | + """ |
| 61 | + |
| 62 | + _base: Union[FeatureGroup, pd.DataFrame] = attr.ib() |
| 63 | + _output_path: str = attr.ib() |
| 64 | + _record_identifier_feature_name: str = attr.ib(default=None) |
| 65 | + _event_time_identifier_feature_name: str = attr.ib(default=None) |
| 66 | + _included_feature_names: Sequence[str] = attr.ib(default=None) |
| 67 | + _kms_key_id: str = attr.ib(default=None) |
| 68 | + |
| 69 | + _point_in_time_accurate_join: bool = attr.ib(init=False, default=False) |
| 70 | + _include_duplicated_records: bool = attr.ib(init=False, default=False) |
| 71 | + _include_deleted_records: bool = attr.ib(init=False, default=False) |
| 72 | + _number_of_recent_records: int = attr.ib(init=False, default=1) |
| 73 | + _number_of_records: int = attr.ib(init=False, default=None) |
| 74 | + _write_time_ending_timestamp: datetime.datetime = attr.ib(init=False, default=None) |
| 75 | + _event_time_starting_timestamp: datetime.datetime = attr.ib(init=False, default=None) |
| 76 | + _event_time_ending_timestamp: datetime.datetime = attr.ib(init=False, default=None) |
| 77 | + |
| 78 | + def point_in_time_accurate_join(self): |
| 79 | + """Set join type as point in time accurate join. |
| 80 | +
|
| 81 | + Returns: |
| 82 | + This DatasetBuilder object. |
| 83 | + """ |
| 84 | + self._point_in_time_accurate_join = True |
| 85 | + return self |
| 86 | + |
| 87 | + def include_duplicated_records(self): |
| 88 | + """Include duplicated records in dataset. |
| 89 | +
|
| 90 | + Returns: |
| 91 | + This DatasetBuilder object. |
| 92 | + """ |
| 93 | + self._include_duplicated_records = True |
| 94 | + return self |
| 95 | + |
| 96 | + def include_deleted_records(self): |
| 97 | + """Include deleted records in dataset. |
| 98 | +
|
| 99 | + Returns: |
| 100 | + This DatasetBuilder object. |
| 101 | + """ |
| 102 | + self._include_deleted_records = True |
| 103 | + return self |
| 104 | + |
| 105 | + def with_number_of_recent_records_by_record_identifier(self, number_of_recent_records: int): |
| 106 | + """Set number_of_recent_records field with provided input. |
| 107 | +
|
| 108 | + Args: |
| 109 | + number_of_recent_records (int): An int that how many recent records will be returned for |
| 110 | + each record identifier. |
| 111 | + Returns: |
| 112 | + This DatasetBuilder object. |
| 113 | + """ |
| 114 | + self._number_of_recent_records = number_of_recent_records |
| 115 | + return self |
| 116 | + |
| 117 | + def with_number_of_records_from_query_results(self, number_of_records: int): |
| 118 | + """Set number_of_records field with provided input. |
| 119 | +
|
| 120 | + Args: |
| 121 | + number_of_records (int): An int that how many records will be returned. |
| 122 | + Returns: |
| 123 | + This DatasetBuilder object. |
| 124 | + """ |
| 125 | + self._number_of_records = number_of_records |
| 126 | + return self |
| 127 | + |
| 128 | + def as_of(self, timestamp: datetime.datetime): |
| 129 | + """Set write_time_ending_timestamp field with provided input. |
| 130 | +
|
| 131 | + Args: |
| 132 | + timestamp (datetime.datetime): A datetime that all records' write time in dataset will |
| 133 | + be before it. |
| 134 | + Returns: |
| 135 | + This DatasetBuilder object. |
| 136 | + """ |
| 137 | + self._write_time_ending_timestamp = timestamp |
| 138 | + return self |
| 139 | + |
| 140 | + def with_event_time_range( |
| 141 | + self, |
| 142 | + starting_timestamp: datetime.datetime = None, |
| 143 | + ending_timestamp: datetime.datetime = None, |
| 144 | + ): |
| 145 | + """Set event_time_starting_timestamp and event_time_ending_timestamp with provided inputs. |
| 146 | +
|
| 147 | + Args: |
| 148 | + starting_timestamp (datetime.datetime): A datetime that all records' event time in |
| 149 | + dataset will be after it (default: None). |
| 150 | + ending_timestamp (datetime.datetime): A datetime that all records' event time in dataset |
| 151 | + will be before it (default: None). |
| 152 | + Returns: |
| 153 | + This DatasetBuilder object. |
| 154 | + """ |
| 155 | + self._event_time_starting_timestamp = starting_timestamp |
| 156 | + self._event_time_ending_timestamp = ending_timestamp |
| 157 | + return self |
0 commit comments