This repository was archived by the owner on Sep 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds ValueConverter utility and demo samples (#108)
* feat: adds value converter utility class and demo samples * feat: samples updated for EJCL * fix: removed local file references * feat: adds ValueConverter tests Co-authored-by: yoshi-code-bot <70984784+yoshi-code-bot@users.noreply.github.com>
- Loading branch information
1 parent
e8d357a
commit cf0b763
Showing
7 changed files
with
262 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
google-cloud-aiplatform/src/main/java/com/google/cloud/aiplatform/util/ValueConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.util; | ||
|
||
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(); | ||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
...e-cloud-aiplatform/src/test/java/com/google/cloud/aiplatform/util/ValueConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* 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.util; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertThrows; | ||
|
||
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 org.junit.Test; | ||
import org.junit.function.ThrowingRunnable; | ||
|
||
public class ValueConverterTest { | ||
|
||
@Test | ||
public void testValueConverterToValue() throws InvalidProtocolBufferException { | ||
AutoMlImageClassificationInputs testObjectInputs = | ||
AutoMlImageClassificationInputs.newBuilder() | ||
.setModelType(ModelType.CLOUD) | ||
.setBudgetMilliNodeHours(8000) | ||
.setMultiLabel(true) | ||
.setDisableEarlyStopping(false) | ||
.build(); | ||
|
||
Value actualConvertedValue = ValueConverter.toValue(testObjectInputs); | ||
|
||
Struct actualStruct = actualConvertedValue.getStructValue(); | ||
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.equals("multiLabel")) { | ||
actualBoolValueEntry = entry; | ||
} else if (key.equals("modelType")) { | ||
actualStringValueEntry = entry; | ||
} else if (key.equals("budgetMilliNodeHours")) { | ||
actualNumberValueEntry = entry; | ||
} | ||
} | ||
|
||
Value actualBoolValue = (Value) actualBoolValueEntry.getValue(); | ||
assertEquals(testObjectInputs.getMultiLabel(), actualBoolValue.getBoolValue()); | ||
|
||
Value actualStringValue = (Value) actualStringValueEntry.getValue(); | ||
assertEquals("CLOUD", actualStringValue.getStringValue()); | ||
|
||
Value actualNumberValue = (Value) actualNumberValueEntry.getValue(); | ||
// protobuf stores int64 values as strings rather than numbers | ||
long actualNumber = Long.parseLong(actualNumberValue.getStringValue()); | ||
assertEquals(testObjectInputs.getBudgetMilliNodeHours(), actualNumber); | ||
} | ||
|
||
@Test | ||
public void testValueConverterFromValue() throws InvalidProtocolBufferException { | ||
|
||
JsonObject testJsonInputs = new JsonObject(); | ||
testJsonInputs.addProperty("multi_label", true); | ||
testJsonInputs.addProperty("model_type", "CLOUD"); | ||
testJsonInputs.addProperty("budget_milli_node_hours", 8000); | ||
|
||
Value.Builder valueBuilder = Value.newBuilder(); | ||
JsonFormat.parser().merge(testJsonInputs.toString(), valueBuilder); | ||
Value testValueInputs = valueBuilder.build(); | ||
|
||
AutoMlImageClassificationInputs actualInputs = | ||
(AutoMlImageClassificationInputs) | ||
ValueConverter.fromValue(AutoMlImageClassificationInputs.newBuilder(), testValueInputs); | ||
|
||
assertEquals(8000, actualInputs.getBudgetMilliNodeHours()); | ||
assertEquals(true, actualInputs.getMultiLabel()); | ||
assertEquals(ModelType.CLOUD, actualInputs.getModelType()); | ||
} | ||
|
||
@Test | ||
public void testValueConverterFromValueWithBadInputs() throws InvalidProtocolBufferException { | ||
JsonObject testBadJsonInputs = new JsonObject(); | ||
testBadJsonInputs.addProperty("wrong_key", "some_value"); | ||
|
||
Value.Builder badValueBuilder = Value.newBuilder(); | ||
JsonFormat.parser().merge(testBadJsonInputs.toString(), badValueBuilder); | ||
final Value testBadValueInputs = badValueBuilder.build(); | ||
|
||
assertThrows( | ||
InvalidProtocolBufferException.class, | ||
new ThrowingRunnable() { | ||
@Override | ||
public void run() throws Throwable { | ||
AutoMlImageClassificationInputs actualBadInput = | ||
(AutoMlImageClassificationInputs) | ||
ValueConverter.fromValue( | ||
AutoMlImageClassificationInputs.newBuilder(), testBadValueInputs); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters