GeezerVison is showing github.com — feat: adds function/method enhancements, demo samples (#122) · orionnye/python-aiplatform@1a302d2
This repository was archived by the owner on Oct 15, 2025. It is now read-only.
File treeExpand file treeCollapse file tree Open diff view settings tests/unit/enhanced_library
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .github/CODEOWNERS +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 9 | 9 |
| 10 | 10 | # The python-samples-owners team is the default owner for samples | 11 | 11 | /samples/**/*.py @dizcology @googleapis/python-samples-owners | | 12 | + | | 13 | +# The enhanced client library tests are owned by @telpirion | | 14 | +/tests/unit/enhanced_library/*.py @telpirion |
Collapse file Copy file name to clipboard +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +from google.cloud.aiplatform.helpers import value_converter | | 2 | + | | 3 | +__all__ = (value_converter,) |
Collapse file Copy file name to clipboard +70Lines changed: 70 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Copyright 2020 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 | +# http://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 | +from __future__ import absolute_import | | 15 | +from google.cloud.aiplatform.helpers import value_converter | | 16 | + | | 17 | +from proto.marshal import Marshal | | 18 | +from proto.marshal.rules.struct import ValueRule | | 19 | +from google.protobuf.struct_pb2 import Value | | 20 | + | | 21 | + | | 22 | +class ConversionValueRule(ValueRule): | | 23 | + def to_python(self, value, *, absent: bool = None): | | 24 | + return super().to_python(value, absent=absent) | | 25 | + | | 26 | + def to_proto(self, value): | | 27 | + | | 28 | + # Need to check whether value is an instance | | 29 | + # of an enhanced type | | 30 | + if callable(getattr(value, "to_value", None)): | | 31 | + return value.to_value() | | 32 | + else: | | 33 | + return super().to_proto(value) | | 34 | + | | 35 | + | | 36 | +def _add_methods_to_classes_in_package(pkg): | | 37 | + classes = dict( | | 38 | + [(name, cls) for name, cls in pkg.__dict__.items() if isinstance(cls, type)] | | 39 | + ) | | 40 | + | | 41 | + for class_name, cls in classes.items(): | | 42 | + # Add to_value() method to class with docstring | | 43 | + setattr(cls, "to_value", value_converter.to_value) | | 44 | + cls.to_value.__doc__ = value_converter.to_value.__doc__ | | 45 | + | | 46 | + # Add from_value() method to class with docstring | | 47 | + setattr(cls, "from_value", _add_from_value_to_class(cls)) | | 48 | + cls.from_value.__doc__ = value_converter.from_value.__doc__ | | 49 | + | | 50 | + # Add from_map() method to class with docstring | | 51 | + setattr(cls, "from_map", _add_from_map_to_class(cls)) | | 52 | + cls.from_map.__doc__ = value_converter.from_map.__doc__ | | 53 | + | | 54 | + | | 55 | +def _add_from_value_to_class(cls): | | 56 | + def _from_value(value): | | 57 | + return value_converter.from_value(cls, value) | | 58 | + | | 59 | + return _from_value | | 60 | + | | 61 | + | | 62 | +def _add_from_map_to_class(cls): | | 63 | + def _from_map(map_): | | 64 | + return value_converter.from_map(cls, map_) | | 65 | + | | 66 | + return _from_map | | 67 | + | | 68 | + | | 69 | +marshal = Marshal(name="google.cloud.aiplatform.v1beta1") | | 70 | +marshal.register(Value, ConversionValueRule(marshal=marshal)) |
Collapse file Copy file name to clipboard +60Lines changed: 60 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Copyright 2020 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 | +from __future__ import absolute_import | | 15 | +from google.protobuf.struct_pb2 import Value | | 16 | +from google.protobuf import json_format | | 17 | +from proto.marshal.collections.maps import MapComposite | | 18 | +from proto.marshal import Marshal | | 19 | +from proto import Message | | 20 | +from proto.message import MessageMeta | | 21 | + | | 22 | + | | 23 | +def to_value(self: Message) -> Value: | | 24 | + """Converts a message type to a :class:`~google.protobuf.struct_pb2.Value` object. | | 25 | +
| | 26 | + Args: | | 27 | + message: the message to convert | | 28 | +
| | 29 | + Returns: | | 30 | + the message as a :class:`~google.protobuf.struct_pb2.Value` object | | 31 | + """ | | 32 | + tmp_dict = json_format.MessageToDict(self._pb) | | 33 | + return json_format.ParseDict(tmp_dict, Value()) | | 34 | + | | 35 | + | | 36 | +def from_value(cls: MessageMeta, value: Value) -> Message: | | 37 | + """Creates instance of class from a :class:`~google.protobuf.struct_pb2.Value` object. | | 38 | +
| | 39 | + Args: | | 40 | + value: a :class:`~google.protobuf.struct_pb2.Value` object | | 41 | +
| | 42 | + Returns: | | 43 | + Instance of class | | 44 | + """ | | 45 | + value_dict = json_format.MessageToDict(value) | | 46 | + return json_format.ParseDict(value_dict, cls()._pb) | | 47 | + | | 48 | + | | 49 | +def from_map(cls: MessageMeta, map_: MapComposite) -> Message: | | 50 | + """Creates instance of class from a :class:`~proto.marshal.collections.maps.MapComposite` object. | | 51 | +
| | 52 | + Args: | | 53 | + map_: a :class:`~proto.marshal.collections.maps.MapComposite` object | | 54 | +
| | 55 | + Returns: | | 56 | + Instance of class | | 57 | + """ | | 58 | + marshal = Marshal(name="marshal") | | 59 | + pb = marshal.to_proto(Value, map_) | | 60 | + return from_value(cls, pb) |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/aiplatform/v1beta1/schema/predict/instance/__init__.py +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 14 | 14 | # See the License for the specific language governing permissions and | 15 | 15 | # limitations under the License. | 16 | 16 | # | | 17 | +from google.cloud.aiplatform.helpers import _decorators | | 18 | +import google.cloud.aiplatform.v1beta1.schema.predict.instance_v1beta1.types as pkg | 17 | 19 |
| 18 | 20 | from google.cloud.aiplatform.v1beta1.schema.predict.instance_v1beta1.types.image_classification import ( | 19 | 21 | ImageClassificationPredictionInstance, | | 54 | 56 | "VideoClassificationPredictionInstance", | 55 | 57 | "VideoObjectTrackingPredictionInstance", | 56 | 58 | ) | | 59 | +_decorators._add_methods_to_classes_in_package(pkg) |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/aiplatform/v1beta1/schema/predict/params/__init__.py +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 14 | 14 | # See the License for the specific language governing permissions and | 15 | 15 | # limitations under the License. | 16 | 16 | # | | 17 | +from google.cloud.aiplatform.helpers import _decorators | | 18 | +import google.cloud.aiplatform.v1beta1.schema.predict.params_v1beta1.types as pkg | 17 | 19 |
| 18 | 20 | from google.cloud.aiplatform.v1beta1.schema.predict.params_v1beta1.types.image_classification import ( | 19 | 21 | ImageClassificationPredictionParams, | | 42 | 44 | "VideoClassificationPredictionParams", | 43 | 45 | "VideoObjectTrackingPredictionParams", | 44 | 46 | ) | | 47 | +_decorators._add_methods_to_classes_in_package(pkg) |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/aiplatform/v1beta1/schema/predict/prediction/__init__.py +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 14 | 14 | # See the License for the specific language governing permissions and | 15 | 15 | # limitations under the License. | 16 | 16 | # | | 17 | +from google.cloud.aiplatform.helpers import _decorators | | 18 | +import google.cloud.aiplatform.v1beta1.schema.predict.prediction_v1beta1.types as pkg | 17 | 19 |
| 18 | 20 | from google.cloud.aiplatform.v1beta1.schema.predict.prediction_v1beta1.types.classification import ( | 19 | 21 | ClassificationPredictionResult, | | 62 | 64 | "VideoClassificationPredictionResult", | 63 | 65 | "VideoObjectTrackingPredictionResult", | 64 | 66 | ) | | 67 | +_decorators._add_methods_to_classes_in_package(pkg) |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/aiplatform/v1beta1/schema/predict/prediction_v1beta1/types/text_sentiment.py +5-5Lines changed: 5 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 17 | 17 |
| 18 | 18 | import proto # type: ignore | 19 | 19 |
| 20 | | - | 21 | | -from google.cloud.aiplatform.v1beta1.schema.predict.instance import text_sentiment_pb2 as gcaspi_text_sentiment # type: ignore | | 20 | +# DO NOT OVERWRITE FOLLOWING LINE: it was manually edited. | | 21 | +from google.cloud.aiplatform.v1beta1.schema.predict.instance import ( | | 22 | + TextSentimentPredictionInstance, | | 23 | +) | 22 | 24 |
| 23 | 25 |
| 24 | 26 | __protobuf__ = proto.module( | @@ -57,9 +59,7 @@ class Prediction(proto.Message): | 57 | 59 | sentiment = proto.Field(proto.INT32, number=1) | 58 | 60 |
| 59 | 61 | instance = proto.Field( | 60 | | - proto.MESSAGE, | 61 | | - number=1, | 62 | | - message=gcaspi_text_sentiment.TextSentimentPredictionInstance, | | 62 | + proto.MESSAGE, number=1, message=TextSentimentPredictionInstance, | 63 | 63 | ) | 64 | 64 |
| 65 | 65 | prediction = proto.Field(proto.MESSAGE, number=2, message=Prediction,) | |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/aiplatform/v1beta1/schema/trainingjob/definition/__init__.py +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 14 | 14 | # See the License for the specific language governing permissions and | 15 | 15 | # limitations under the License. | 16 | 16 | # | | 17 | +from google.cloud.aiplatform.helpers import _decorators | | 18 | +import google.cloud.aiplatform.v1beta1.schema.trainingjob.definition_v1beta1.types as pkg | 17 | 19 |
| 18 | 20 | from google.cloud.aiplatform.v1beta1.schema.trainingjob.definition_v1beta1.types.automl_forecasting import ( | 19 | 21 | AutoMlForecasting, | | 130 | 132 | "AutoMlVideoObjectTrackingInputs", | 131 | 133 | "ExportEvaluatedDataItemsConfig", | 132 | 134 | ) | | 135 | +_decorators._add_methods_to_classes_in_package(pkg) |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/aiplatform/v1beta1/schema/trainingjob/definition_v1beta1/types/automl_forecasting.py +7-7Lines changed: 7 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -78,14 +78,14 @@ class AutoMlForecastingInputs(proto.Message): | 78 | 78 | function over the validation set. | 79 | 79 |
| 80 | 80 | The supported optimization objectives: | 81 | | - "minimize-rmse" (default) - Minimize root- | | 81 | + "minimize-rmse" (default) - Minimize root- | 82 | 82 | mean-squared error (RMSE). "minimize-mae" - | 83 | 83 | Minimize mean-absolute error (MAE). "minimize- | 84 | 84 | rmsle" - Minimize root-mean-squared log error | 85 | 85 | (RMSLE). "minimize-rmspe" - Minimize root- | 86 | 86 | mean-squared percentage error (RMSPE). | 87 | 87 | "minimize-wape-mae" - Minimize the combination | 88 | | - of weighted absolute percentage error (WAPE) | | 88 | + of weighted absolute percentage error (WAPE) | 89 | 89 | and mean-absolute-error (MAE). | 90 | 90 | train_budget_milli_node_hours (int): | 91 | 91 | Required. The train budget of creating this | @@ -418,11 +418,11 @@ class Period(proto.Message): | 418 | 418 | unit (str): | 419 | 419 | The time granularity unit of this time | 420 | 420 | period. The supported unit are: | 421 | | - "hour" | 422 | | - "day" | 423 | | - "week" | 424 | | - "month" | 425 | | - "year". | | 421 | + "hour" | | 422 | + "day" | | 423 | + "week" | | 424 | + "month" | | 425 | + "year". | 426 | 426 | quantity (int): | 427 | 427 | The number of units per period, e.g. 3 weeks | 428 | 428 | or 2 months. | |
|