GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(automl): fix TablesClient.predict for array and struct (#9991) · googleapis/google-cloud-python@4fbad6c

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages/google-cloud-automl
    • google/cloud/automl_v1beta1/tables
      • tables_client.py
    • tests/unit/gapic/v1beta1
      • test_tables_client_v1beta1.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/google-cloud-automl/google/cloud/automl_v1beta1/tables/tables_client.py‎

Copy file name to clipboardExpand all lines: packages/google-cloud-automl/google/cloud/automl_v1beta1/tables/tables_client.py
+36-15Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
from google.api_core.gapic_v1 import client_info
2323
from google.api_core import exceptions
2424
from google.cloud.automl_v1beta1 import gapic
25-
from google.cloud.automl_v1beta1.proto import data_types_pb2
25+
from google.cloud.automl_v1beta1.proto import data_types_pb2, data_items_pb2
2626
from google.cloud.automl_v1beta1.tables import gcs_client
27+
from google.protobuf import struct_pb2
28+
2729

2830
_GAPIC_LIBRARY_VERSION = pkg_resources.get_distribution("google-cloud-automl").version
2931
_LOGGER = logging.getLogger(__name__)
@@ -402,21 +404,39 @@ def __column_spec_name_from_args(
402404

403405
return column_spec_name
404406

405-
def __type_code_to_value_type(self, type_code, value):
407+
def __data_type_to_proto_value(self, data_type, value):
408+
type_code = data_type.type_code
406409
if value is None:
407-
return {"null_value": 0}
410+
return struct_pb2.Value(null_value=struct_pb2.NullValue.NULL_VALUE)
408411
elif type_code == data_types_pb2.FLOAT64:
409-
return {"number_value": value}
410-
elif type_code == data_types_pb2.TIMESTAMP:
411-
return {"string_value": value}
412-
elif type_code == data_types_pb2.STRING:
413-
return {"string_value": value}
412+
return struct_pb2.Value(number_value=value)
413+
elif (
414+
type_code == data_types_pb2.TIMESTAMP
415+
or type_code == data_types_pb2.STRING
416+
or type_code == data_types_pb2.CATEGORY
417+
):
418+
return struct_pb2.Value(string_value=value)
414419
elif type_code == data_types_pb2.ARRAY:
415-
return {"list_value": value}
420+
if isinstance(value, struct_pb2.ListValue):
421+
# in case the user passed in a ListValue.
422+
return struct_pb2.Value(list_value=value)
423+
array = []
424+
for item in value:
425+
array.append(
426+
self.__data_type_to_proto_value(data_type.list_element_type, item)
427+
)
428+
return struct_pb2.Value(list_value=struct_pb2.ListValue(values=array))
416429
elif type_code == data_types_pb2.STRUCT:
417-
return {"struct_value": value}
418-
elif type_code == data_types_pb2.CATEGORY:
419-
return {"string_value": value}
430+
if isinstance(value, struct_pb2.Struct):
431+
# in case the user passed in a Struct.
432+
return struct_pb2.Value(struct_value=value)
433+
struct_value = struct_pb2.Struct()
434+
for k, v in value.items():
435+
field_value = self.__data_type_to_proto_value(
436+
data_type.struct_type.fields[k], v
437+
)
438+
struct_value.fields[k].CopyFrom(field_value)
439+
return struct_pb2.Value(struct_value=struct_value)
420440
else:
421441
raise ValueError("Unknown type_code: {}".format(type_code))
422442

@@ -2694,16 +2714,17 @@ def predict(
26942714

26952715
values = []
26962716
for i, c in zip(inputs, column_specs):
2697-
value_type = self.__type_code_to_value_type(c.data_type.type_code, i)
2717+
value_type = self.__data_type_to_proto_value(c.data_type, i)
26982718
values.append(value_type)
26992719

2700-
request = {"row": {"values": values}}
2720+
row = data_items_pb2.Row(values=values)
2721+
payload = data_items_pb2.ExamplePayload(row=row)
27012722

27022723
params = None
27032724
if feature_importance:
27042725
params = {"feature_importance": "true"}
27052726

2706-
return self.prediction_client.predict(model.name, request, params, **kwargs)
2727+
return self.prediction_client.predict(model.name, payload, params, **kwargs)
27072728

27082729
def batch_predict(
27092730
self,
Collapse file

‎packages/google-cloud-automl/tests/unit/gapic/v1beta1/test_tables_client_v1beta1.py‎

Copy file name to clipboardExpand all lines: packages/google-cloud-automl/tests/unit/gapic/v1beta1/test_tables_client_v1beta1.py
+63-33Lines changed: 63 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
from google.api_core import exceptions
2424
from google.auth.credentials import AnonymousCredentials
2525
from google.cloud import automl_v1beta1
26-
from google.cloud.automl_v1beta1.proto import data_types_pb2
26+
from google.cloud.automl_v1beta1.proto import data_types_pb2, data_items_pb2
27+
from google.protobuf import struct_pb2
2728

2829
PROJECT = "project"
2930
REGION = "region"
@@ -1116,9 +1117,10 @@ def test_predict_from_array(self):
11161117
model.configure_mock(tables_model_metadata=model_metadata, name="my_model")
11171118
client = self.tables_client({"get_model.return_value": model}, {})
11181119
client.predict(["1"], model_name="my_model")
1119-
client.prediction_client.predict.assert_called_with(
1120-
"my_model", {"row": {"values": [{"string_value": "1"}]}}, None
1120+
payload = data_items_pb2.ExamplePayload(
1121+
row=data_items_pb2.Row(values=[struct_pb2.Value(string_value="1")])
11211122
)
1123+
client.prediction_client.predict.assert_called_with("my_model", payload, None)
11221124

11231125
def test_predict_from_dict(self):
11241126
data_type = mock.Mock(type_code=data_types_pb2.CATEGORY)
@@ -1131,11 +1133,15 @@ def test_predict_from_dict(self):
11311133
model.configure_mock(tables_model_metadata=model_metadata, name="my_model")
11321134
client = self.tables_client({"get_model.return_value": model}, {})
11331135
client.predict({"a": "1", "b": "2"}, model_name="my_model")
1134-
client.prediction_client.predict.assert_called_with(
1135-
"my_model",
1136-
{"row": {"values": [{"string_value": "1"}, {"string_value": "2"}]}},
1137-
None,
1136+
payload = data_items_pb2.ExamplePayload(
1137+
row=data_items_pb2.Row(
1138+
values=[
1139+
struct_pb2.Value(string_value="1"),
1140+
struct_pb2.Value(string_value="2"),
1141+
]
1142+
)
11381143
)
1144+
client.prediction_client.predict.assert_called_with("my_model", payload, None)
11391145

11401146
def test_predict_from_dict_with_feature_importance(self):
11411147
data_type = mock.Mock(type_code=data_types_pb2.CATEGORY)
@@ -1150,10 +1156,16 @@ def test_predict_from_dict_with_feature_importance(self):
11501156
client.predict(
11511157
{"a": "1", "b": "2"}, model_name="my_model", feature_importance=True
11521158
)
1159+
payload = data_items_pb2.ExamplePayload(
1160+
row=data_items_pb2.Row(
1161+
values=[
1162+
struct_pb2.Value(string_value="1"),
1163+
struct_pb2.Value(string_value="2"),
1164+
]
1165+
)
1166+
)
11531167
client.prediction_client.predict.assert_called_with(
1154-
"my_model",
1155-
{"row": {"values": [{"string_value": "1"}, {"string_value": "2"}]}},
1156-
{"feature_importance": "true"},
1168+
"my_model", payload, {"feature_importance": "true"}
11571169
)
11581170

11591171
def test_predict_from_dict_missing(self):
@@ -1167,18 +1179,32 @@ def test_predict_from_dict_missing(self):
11671179
model.configure_mock(tables_model_metadata=model_metadata, name="my_model")
11681180
client = self.tables_client({"get_model.return_value": model}, {})
11691181
client.predict({"a": "1"}, model_name="my_model")
1170-
client.prediction_client.predict.assert_called_with(
1171-
"my_model",
1172-
{"row": {"values": [{"string_value": "1"}, {"null_value": 0}]}},
1173-
None,
1182+
payload = data_items_pb2.ExamplePayload(
1183+
row=data_items_pb2.Row(
1184+
values=[
1185+
struct_pb2.Value(string_value="1"),
1186+
struct_pb2.Value(null_value=struct_pb2.NullValue.NULL_VALUE),
1187+
]
1188+
)
11741189
)
1190+
client.prediction_client.predict.assert_called_with("my_model", payload, None)
11751191

11761192
def test_predict_all_types(self):
11771193
float_type = mock.Mock(type_code=data_types_pb2.FLOAT64)
11781194
timestamp_type = mock.Mock(type_code=data_types_pb2.TIMESTAMP)
11791195
string_type = mock.Mock(type_code=data_types_pb2.STRING)
1180-
array_type = mock.Mock(type_code=data_types_pb2.ARRAY)
1181-
struct_type = mock.Mock(type_code=data_types_pb2.STRUCT)
1196+
array_type = mock.Mock(
1197+
type_code=data_types_pb2.ARRAY,
1198+
list_element_type=mock.Mock(type_code=data_types_pb2.FLOAT64),
1199+
)
1200+
struct = data_types_pb2.StructType()
1201+
struct.fields["a"].CopyFrom(
1202+
data_types_pb2.DataType(type_code=data_types_pb2.CATEGORY)
1203+
)
1204+
struct.fields["b"].CopyFrom(
1205+
data_types_pb2.DataType(type_code=data_types_pb2.CATEGORY)
1206+
)
1207+
struct_type = mock.Mock(type_code=data_types_pb2.STRUCT, struct_type=struct)
11821208
category_type = mock.Mock(type_code=data_types_pb2.CATEGORY)
11831209
column_spec_float = mock.Mock(display_name="float", data_type=float_type)
11841210
column_spec_timestamp = mock.Mock(
@@ -1211,29 +1237,33 @@ def test_predict_all_types(self):
12111237
"timestamp": "EST",
12121238
"string": "text",
12131239
"array": [1],
1214-
"struct": {"a": "b"},
1240+
"struct": {"a": "label_a", "b": "label_b"},
12151241
"category": "a",
12161242
"null": None,
12171243
},
12181244
model_name="my_model",
12191245
)
1220-
client.prediction_client.predict.assert_called_with(
1221-
"my_model",
1222-
{
1223-
"row": {
1224-
"values": [
1225-
{"number_value": 1.0},
1226-
{"string_value": "EST"},
1227-
{"string_value": "text"},
1228-
{"list_value": [1]},
1229-
{"struct_value": {"a": "b"}},
1230-
{"string_value": "a"},
1231-
{"null_value": 0},
1232-
]
1233-
}
1234-
},
1235-
None,
1246+
struct = struct_pb2.Struct()
1247+
struct.fields["a"].CopyFrom(struct_pb2.Value(string_value="label_a"))
1248+
struct.fields["b"].CopyFrom(struct_pb2.Value(string_value="label_b"))
1249+
payload = data_items_pb2.ExamplePayload(
1250+
row=data_items_pb2.Row(
1251+
values=[
1252+
struct_pb2.Value(number_value=1.0),
1253+
struct_pb2.Value(string_value="EST"),
1254+
struct_pb2.Value(string_value="text"),
1255+
struct_pb2.Value(
1256+
list_value=struct_pb2.ListValue(
1257+
values=[struct_pb2.Value(number_value=1.0)]
1258+
)
1259+
),
1260+
struct_pb2.Value(struct_value=struct),
1261+
struct_pb2.Value(string_value="a"),
1262+
struct_pb2.Value(null_value=struct_pb2.NullValue.NULL_VALUE),
1263+
]
1264+
)
12361265
)
1266+
client.prediction_client.predict.assert_called_with("my_model", payload, None)
12371267

12381268
def test_predict_from_array_missing(self):
12391269
data_type = mock.Mock(type_code=data_types_pb2.CATEGORY)

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.