- Notifications
You must be signed in to change notification settings - Fork 22
feat: adds ValueConverter utility and demo samples #108
Changes from 9 commits
a484ba6 1df941a 980e089 393458d 4346e91 098255f 3e37edf e7b251d 1280f62 52aa8cf 1d41681 50d9fb6 5cd1bdf a6a1972 2dde132 c39ff3d File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /* | ||
| * Copyright 2020 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| | ||
| package com.google.cloud.aiplatform.utility; | ||
| | ||
| import com.google.protobuf.InvalidProtocolBufferException; | ||
| import com.google.protobuf.Message; | ||
| import com.google.protobuf.Value; | ||
| import com.google.protobuf.util.JsonFormat; | ||
| | ||
| /** | ||
| * Exposes utility methods for converting AI Platform messages to and from | ||
| * {@com.google.protobuf.Value} objects. | ||
| */ | ||
| public class ValueConverter { | ||
| | ||
| /** An empty {@com.google.protobuf.Value} message. */ | ||
| public static final Value EMPTY_VALUE = Value.newBuilder().build(); | ||
| | ||
| /** | ||
| * Converts a message type to a {@com.google.protobuf.Value}. | ||
| * | ||
| * @param message the message to convert | ||
| * @return the message as a {@com.google.protobuf.Value} | ||
| * @throws InvalidProtocolBufferException | ||
| */ | ||
| public static Value toValue(Message message) throws InvalidProtocolBufferException { | ||
| String jsonString = JsonFormat.printer().print(message); | ||
| Value.Builder value = Value.newBuilder(); | ||
| JsonFormat.parser().merge(jsonString, value); | ||
| return value.build(); | ||
| } | ||
| | ||
| /** | ||
| * Converts a {@com.google.protobuf.Value} to a {@com.google.protobuf.Message} of the provided | ||
| * {@com.google.protobuf.Message.Builder}. | ||
| * | ||
| * @param messageBuilder a builder for the message type | ||
| * @param value the Value to convert to a message | ||
| * @return the value as a message | ||
| * @throws InvalidProtocolBufferException | ||
| */ | ||
| public static Message fromValue(Message.Builder messageBuilder, Value value) | ||
| throws InvalidProtocolBufferException { | ||
| String valueString = JsonFormat.printer().print(value); | ||
| JsonFormat.parser().merge(valueString, messageBuilder); | ||
| return messageBuilder.build(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| /* | ||
| * Copyright 2020 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| | ||
| package com.google.cloud.aiplatform.utility; | ||
| | ||
| import com.google.cloud.aiplatform.v1beta1.schema.trainingjob.definition.AutoMlImageClassificationInputs; | ||
| import com.google.cloud.aiplatform.v1beta1.schema.trainingjob.definition.AutoMlImageClassificationInputs.ModelType; | ||
| import com.google.gson.JsonObject; | ||
| import com.google.protobuf.InvalidProtocolBufferException; | ||
| import com.google.protobuf.MapEntry; | ||
| import com.google.protobuf.Struct; | ||
| import com.google.protobuf.Value; | ||
| import com.google.protobuf.util.JsonFormat; | ||
| import java.util.Collection; | ||
| import java.util.stream.Collectors; | ||
telpirion marked this conversation as resolved. Outdated Show resolved Hide resolved telpirion marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| import org.junit.Assert; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| | ||
| public class ValueConverterTest { | ||
| | ||
| private static JsonObject testJsonInputs; | ||
| private static Value testValueInputs; | ||
| private static AutoMlImageClassificationInputs testObjectInputs; | ||
| | ||
| @Before | ||
| public void setUp() throws InvalidProtocolBufferException { | ||
| ||
| testJsonInputs = new JsonObject(); | ||
| testJsonInputs.addProperty("multi_label", true); | ||
| testJsonInputs.addProperty("model_type", "CLOUD"); | ||
| testJsonInputs.addProperty("budget_milli_node_hours", 8000); | ||
telpirion marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| | ||
| Value.Builder valueBuilder = Value.newBuilder(); | ||
| JsonFormat.parser().merge(testJsonInputs.toString(), valueBuilder); | ||
| testValueInputs = valueBuilder.build(); | ||
| | ||
| testObjectInputs = | ||
| AutoMlImageClassificationInputs.newBuilder() | ||
| .setModelType(ModelType.CLOUD) | ||
| .setBudgetMilliNodeHours(8000) | ||
| .setMultiLabel(true) | ||
| .setDisableEarlyStopping(false) | ||
| .build(); | ||
| } | ||
| | ||
| @Test | ||
| public void testValueConverterToValue() throws InvalidProtocolBufferException { | ||
| Value actualConvertedValue = ValueConverter.toValue(testObjectInputs); | ||
| | ||
| Struct actualStruct = actualConvertedValue.getStructValue(); | ||
| Assert.assertEquals(3, actualStruct.getFieldsCount()); | ||
| | ||
| Collection<Object> innerFields = actualStruct.getAllFields().values(); | ||
| Collection<MapEntry> fieldEntries = (Collection<MapEntry>) innerFields.toArray()[0]; | ||
| | ||
| MapEntry actualBoolValueEntry = null; | ||
| MapEntry actualStringValueEntry = null; | ||
| MapEntry actualNumberValueEntry = null; | ||
| | ||
| for (MapEntry entry : fieldEntries) { | ||
| String key = entry.getKey().toString(); | ||
| if (key.contains("multiLabel")) { | ||
| actualBoolValueEntry = entry; | ||
| } else if (key.contains("modelType")) { | ||
| actualStringValueEntry = entry; | ||
| } else if (key.contains("budgetMilliNodeHours")) { | ||
| ||
| actualNumberValueEntry = entry; | ||
| } | ||
| } | ||
| | ||
| | ||
| Value actualBoolValue = (Value) actualBoolValueEntry.getValue(); | ||
telpirion marked this conversation as resolved. Outdated Show resolved Hide resolved telpirion marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| Assert.assertEquals(testObjectInputs.getMultiLabel(), actualBoolValue.getBoolValue()); | ||
| ||
| | ||
| Value actualStringValue = (Value) actualStringValueEntry.getValue(); | ||
| Assert.assertEquals("CLOUD", actualStringValue.getStringValue()); | ||
| | ||
| Value actualNumberValue = (Value) actualNumberValueEntry.getValue(); | ||
| // protobuf stores int64 values as strings rather than numbers | ||
| long actualNumber = Long.parseLong(actualNumberValue.getStringValue()); | ||
| Assert.assertEquals(testObjectInputs.getBudgetMilliNodeHours(), actualNumber); | ||
| } | ||
| | ||
| @Test | ||
| public void testValueConverterFromValue() throws InvalidProtocolBufferException { | ||
| AutoMlImageClassificationInputs actualInputs = | ||
| (AutoMlImageClassificationInputs) | ||
| ValueConverter.fromValue(AutoMlImageClassificationInputs.newBuilder(), testValueInputs); | ||
| | ||
| Assert.assertEquals(8000, actualInputs.getBudgetMilliNodeHours()); | ||
| Assert.assertEquals(true, actualInputs.getMultiLabel()); | ||
| Assert.assertEquals(ModelType.CLOUD, actualInputs.getModelType()); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: most other utility packages use
utilThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.