Skip to content

Commit b7350bd

Browse files
authored
automl: move last sample on branch and do some spot bugs cleanup (GoogleCloudPlatform#2766)
* automl: move last sample on branch and do some spot bugs cleanup * switch to string format
1 parent 82c66ef commit b7350bd

18 files changed

+166
-50
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.automl;
18+
19+
// [START automl_cancel_operation_beta]
20+
import com.google.cloud.automl.v1beta1.AutoMlClient;
21+
import java.io.IOException;
22+
23+
class CancelOperation {
24+
25+
static void cancelOperation() throws IOException {
26+
// TODO(developer): Replace these variables before running the sample.
27+
String projectId = "YOUR_PROJECT_ID";
28+
String location = "us-central1";
29+
String operationId = "YOUR_OPERATION_ID";
30+
String operationFullId =
31+
String.format("projects/%s/locations/%s/operations/%s", projectId, location, operationId);
32+
cancelOperation(operationFullId);
33+
}
34+
35+
static void cancelOperation(String operationFullId) throws IOException {
36+
// Initialize client that will be used to send requests. This client only needs to be created
37+
// once, and can be reused for multiple requests. After completing all of your requests, call
38+
// the "close" method on the client to safely clean up any remaining background resources.
39+
try (AutoMlClient client = AutoMlClient.create()) {
40+
client.getOperationsClient().cancelOperation(operationFullId);
41+
System.out.println("Operation cancelled");
42+
}
43+
}
44+
}
45+
// [END automl_cancel_operation_beta]

automl/beta/src/main/java/com/example/automl/DeleteDataset.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static void deleteDataset(String projectId, String datasetId)
4242
// Get the full path of the dataset.
4343
DatasetName datasetFullId = DatasetName.of(projectId, "us-central1", datasetId);
4444
Empty response = client.deleteDatasetAsync(datasetFullId).get();
45-
System.out.format("Dataset deleted. %s\n", response);
45+
System.out.format("Dataset deleted. %s%n", response);
4646
}
4747
}
4848
}

automl/beta/src/main/java/com/example/automl/GetModel.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,18 @@ static void getModel(String projectId, String modelId) throws IOException {
4242
Model model = client.getModel(modelFullId);
4343

4444
// Display the model information.
45-
System.out.format("Model name: %s\n", model.getName());
45+
System.out.format("Model name: %s%n", model.getName());
4646
// To get the model id, you have to parse it out of the `name` field. As models Ids are
4747
// required for other methods.
4848
// Name Format: `projects/{project_id}/locations/{location_id}/models/{model_id}`
4949
String[] names = model.getName().split("/");
5050
String retrievedModelId = names[names.length - 1];
51-
System.out.format("Model id: %s\n", retrievedModelId);
52-
System.out.format("Model display name: %s\n", model.getDisplayName());
51+
System.out.format("Model id: %s%n", retrievedModelId);
52+
System.out.format("Model display name: %s%n", model.getDisplayName());
5353
System.out.println("Model create time:");
54-
System.out.format("\tseconds: %s\n", model.getCreateTime().getSeconds());
55-
System.out.format("\tnanos: %s\n", model.getCreateTime().getNanos());
56-
System.out.format("Model deployment state: %s\n", model.getDeploymentState());
54+
System.out.format("\tseconds: %s%n", model.getCreateTime().getSeconds());
55+
System.out.format("\tnanos: %s%n", model.getCreateTime().getNanos());
56+
System.out.format("Model deployment state: %s%n", model.getDeploymentState());
5757
}
5858
}
5959
}

automl/beta/src/main/java/com/example/automl/GetModelEvaluation.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,23 @@ static void getModelEvaluation(String projectId, String modelId, String modelEva
4747
// Get complete detail of the model evaluation.
4848
ModelEvaluation modelEvaluation = client.getModelEvaluation(modelEvaluationFullId);
4949

50-
System.out.format("Model Evaluation Name: %s\n", modelEvaluation.getName());
50+
System.out.format("Model Evaluation Name: %s%n", modelEvaluation.getName());
5151
System.out.format("Model Annotation Spec Id: %s", modelEvaluation.getAnnotationSpecId());
5252
System.out.println("Create Time:");
53-
System.out.format("\tseconds: %s\n", modelEvaluation.getCreateTime().getSeconds());
53+
System.out.format("\tseconds: %s%n", modelEvaluation.getCreateTime().getSeconds());
5454
System.out.format("\tnanos: %s", modelEvaluation.getCreateTime().getNanos() / 1e9);
5555
System.out.format(
56-
"Evalution Example Count: %d\n", modelEvaluation.getEvaluatedExampleCount());
56+
"Evalution Example Count: %d%n", modelEvaluation.getEvaluatedExampleCount());
5757

5858
// [END automl_video_object_tracking_get_model_evaluation_beta]
5959
System.out.format(
60-
"Classification Model Evaluation Metrics: %s\n",
60+
"Classification Model Evaluation Metrics: %s%n",
6161
modelEvaluation.getClassificationEvaluationMetrics());
6262
// [END automl_video_classification_get_model_evaluation_beta]
6363

6464
// [START automl_video_object_tracking_get_model_evaluation_beta]
6565
System.out.format(
66-
"Video Object Tracking Evaluation Metrics: %s\n",
66+
"Video Object Tracking Evaluation Metrics: %s%n",
6767
modelEvaluation.getVideoObjectTrackingEvaluationMetrics());
6868
// [START automl_video_classification_get_model_evaluation_beta]
6969
}

automl/beta/src/main/java/com/example/automl/GetOperationStatus.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,16 @@ static void getOperationStatus(String operationFullId) throws IOException {
4040

4141
// Display operation details.
4242
System.out.println("Operation details:");
43-
System.out.format("\tName: %s\n", operation.getName());
44-
System.out.format("\tMetadata Type Url: %s\n", operation.getMetadata().getTypeUrl());
45-
System.out.format("\tDone: %s\n", operation.getDone());
43+
System.out.format("\tName: %s%n", operation.getName());
44+
System.out.format("\tMetadata Type Url: %s%n", operation.getMetadata().getTypeUrl());
45+
System.out.format("\tDone: %s%n", operation.getDone());
4646
if (operation.hasResponse()) {
47-
System.out.format("\tResponse Type Url: %s\n", operation.getResponse().getTypeUrl());
47+
System.out.format("\tResponse Type Url: %s%n", operation.getResponse().getTypeUrl());
4848
}
4949
if (operation.hasError()) {
5050
System.out.println("\tResponse:");
51-
System.out.format("\t\tError code: %s\n", operation.getError().getCode());
52-
System.out.format("\t\tError message: %s\n", operation.getError().getMessage());
51+
System.out.format("\t\tError code: %s%n", operation.getError().getCode());
52+
System.out.format("\t\tError message: %s%n", operation.getError().getMessage());
5353
}
5454
}
5555
}

automl/beta/src/main/java/com/example/automl/ImportDataset.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static void importDataset(String projectId, String datasetId, String path)
5656
System.out.println("Processing import...");
5757

5858
Empty response = client.importDataAsync(datasetFullId, inputConfig).get();
59-
System.out.format("Dataset imported. %s\n", response);
59+
System.out.format("Dataset imported. %s%n", response);
6060
}
6161
}
6262
}

automl/beta/src/main/java/com/example/automl/ListDatasets.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,33 +48,33 @@ static void listDatasets(String projectId) throws IOException {
4848
System.out.println("List of datasets:");
4949
for (Dataset dataset : client.listDatasets(request).iterateAll()) {
5050
// Display the dataset information
51-
System.out.format("\nDataset name: %s\n", dataset.getName());
51+
System.out.format("%nDataset name: %s%n", dataset.getName());
5252
// To get the dataset id, you have to parse it out of the `name` field. As dataset Ids are
5353
// required for other methods.
5454
// Name Form: `projects/{project_id}/locations/{location_id}/datasets/{dataset_id}`
5555
String[] names = dataset.getName().split("/");
5656
String retrievedDatasetId = names[names.length - 1];
57-
System.out.format("Dataset id: %s\n", retrievedDatasetId);
58-
System.out.format("Dataset display name: %s\n", dataset.getDisplayName());
57+
System.out.format("Dataset id: %s%n", retrievedDatasetId);
58+
System.out.format("Dataset display name: %s%n", dataset.getDisplayName());
5959
System.out.println("Dataset create time:");
60-
System.out.format("\tseconds: %s\n", dataset.getCreateTime().getSeconds());
61-
System.out.format("\tnanos: %s\n", dataset.getCreateTime().getNanos());
60+
System.out.format("\tseconds: %s%n", dataset.getCreateTime().getSeconds());
61+
System.out.format("\tnanos: %s%n", dataset.getCreateTime().getNanos());
6262

6363
// [END automl_video_object_tracking_list_datasets_beta]
6464
// [END automl_tables_list_datasets_beta]
6565
System.out.format(
66-
"Video classification dataset metadata: %s\n",
66+
"Video classification dataset metadata: %s%n",
6767
dataset.getVideoClassificationDatasetMetadata());
6868
// [END automl_video_classification_list_datasets_beta]
6969

7070
// [START automl_video_object_tracking_list_datasets_beta]
7171
System.out.format(
72-
"Video object tracking dataset metadata: %s\n",
72+
"Video object tracking dataset metadata: %s%n",
7373
dataset.getVideoObjectTrackingDatasetMetadata());
7474
// [END automl_video_object_tracking_list_datasets_beta]
7575

7676
// [START automl_tables_list_datasets_beta]
77-
System.out.format("Tables dataset metadata: %s\n", dataset.getTablesDatasetMetadata());
77+
System.out.format("Tables dataset metadata: %s%n", dataset.getTablesDatasetMetadata());
7878

7979
// [START automl_video_classification_list_datasets_beta]
8080
// [START automl_video_object_tracking_list_datasets_beta]

automl/beta/src/main/java/com/example/automl/ListModelEvaluations.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,16 @@ static void listModelEvaluations(String projectId, String modelId) throws IOExce
4848
for (ModelEvaluation modelEvaluation :
4949
client.listModelEvaluations(modelEvaluationsrequest).iterateAll()) {
5050

51-
System.out.format("Model Evaluation Name: %s\n", modelEvaluation.getName());
51+
System.out.format("Model Evaluation Name: %s%n", modelEvaluation.getName());
5252
System.out.format("Model Annotation Spec Id: %s", modelEvaluation.getAnnotationSpecId());
5353
System.out.println("Create Time:");
54-
System.out.format("\tseconds: %s\n", modelEvaluation.getCreateTime().getSeconds());
54+
System.out.format("\tseconds: %s%n", modelEvaluation.getCreateTime().getSeconds());
5555
System.out.format("\tnanos: %s", modelEvaluation.getCreateTime().getNanos() / 1e9);
5656
System.out.format(
57-
"Evalution Example Count: %d\n", modelEvaluation.getEvaluatedExampleCount());
57+
"Evalution Example Count: %d%n", modelEvaluation.getEvaluatedExampleCount());
5858

5959
System.out.format(
60-
"Tables Model Evaluation Metrics: %s\n",
60+
"Tables Model Evaluation Metrics: %s%n",
6161
modelEvaluation.getClassificationEvaluationMetrics());
6262
}
6363
}

automl/beta/src/main/java/com/example/automl/ListModels.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,18 @@ static void listModels(String projectId) throws IOException {
5151
System.out.println("List of models:");
5252
for (Model model : client.listModels(listModlesRequest).iterateAll()) {
5353
// Display the model information.
54-
System.out.format("Model name: %s\n", model.getName());
54+
System.out.format("Model name: %s%n", model.getName());
5555
// To get the model id, you have to parse it out of the `name` field. As models Ids are
5656
// required for other methods.
5757
// Name Format: `projects/{project_id}/locations/{location_id}/models/{model_id}`
5858
String[] names = model.getName().split("/");
5959
String retrievedModelId = names[names.length - 1];
60-
System.out.format("Model id: %s\n", retrievedModelId);
61-
System.out.format("Model display name: %s\n", model.getDisplayName());
60+
System.out.format("Model id: %s%n", retrievedModelId);
61+
System.out.format("Model display name: %s%n", model.getDisplayName());
6262
System.out.println("Model create time:");
63-
System.out.format("\tseconds: %s\n", model.getCreateTime().getSeconds());
64-
System.out.format("\tnanos: %s\n", model.getCreateTime().getNanos());
65-
System.out.format("Model deployment state: %s\n", model.getDeploymentState());
63+
System.out.format("\tseconds: %s%n", model.getCreateTime().getSeconds());
64+
System.out.format("\tnanos: %s%n", model.getCreateTime().getNanos());
65+
System.out.format("Model deployment state: %s%n", model.getDeploymentState());
6666
}
6767
}
6868
}

automl/beta/src/main/java/com/example/automl/TablesCreateDataset.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ static void createDataset(String projectId, String displayName) throws IOExcepti
5050
Dataset createdDataset = client.createDataset(projectLocation, dataset);
5151

5252
// Display the dataset information.
53-
System.out.format("Dataset name: %s\n", createdDataset.getName());
53+
System.out.format("Dataset name: %s%n", createdDataset.getName());
5454
// To get the dataset id, you have to parse it out of the `name` field. As dataset Ids are
5555
// required for other methods.
5656
// Name Form: `projects/{project_id}/locations/{location_id}/datasets/{dataset_id}`
5757
String[] names = createdDataset.getName().split("/");
5858
String datasetId = names[names.length - 1];
59-
System.out.format("Dataset id: %s\n", datasetId);
59+
System.out.format("Dataset id: %s%n", datasetId);
6060
}
6161
}
6262
}

automl/beta/src/main/java/com/example/automl/TablesCreateModel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ static void createModel(
8484
// OperationFuture.get() will block until the model is created, which may take several hours.
8585
// You can use OperationFuture.getInitialFuture to get a future representing the initial
8686
// response to the request, which contains information while the operation is in progress.
87-
System.out.format("Training operation name: %s\n", future.getInitialFuture().get().getName());
87+
System.out.format("Training operation name: %s%n", future.getInitialFuture().get().getName());
8888
System.out.println("Training started...");
8989
}
9090
}

automl/beta/src/main/java/com/example/automl/TablesImportDataset.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ static void importDataset(String projectId, String datasetId, String path)
6767
System.out.println("Processing import...");
6868

6969
Empty response = client.importDataAsync(datasetFullId, inputConfigBuilder.build()).get();
70-
System.out.format("Dataset imported. %s\n", response);
70+
System.out.format("Dataset imported. %s%n", response);
7171
}
7272
}
7373
}

automl/beta/src/main/java/com/example/automl/TablesPredict.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ static void predict(String projectId, String modelId, List<Value> values) throws
7070
for (AnnotationPayload annotationPayload : response.getPayloadList()) {
7171
TablesAnnotation tablesAnnotation = annotationPayload.getTables();
7272
System.out.format(
73-
"Classification label: %s\n", tablesAnnotation.getValue().getStringValue());
74-
System.out.format("Classification score: %.3f\n", tablesAnnotation.getScore());
73+
"Classification label: %s%n", tablesAnnotation.getValue().getStringValue());
74+
System.out.format("Classification score: %.3f%n", tablesAnnotation.getScore());
7575
// Get features of top importance
7676
tablesAnnotation
7777
.getTablesModelColumnInfoList()
7878
.forEach(
7979
info ->
8080
System.out.format(
81-
"\tColumn: %s - Importance: %.2f\n",
81+
"\tColumn: %s - Importance: %.2f%n",
8282
info.getColumnDisplayName(), info.getFeatureImportance()));
8383
}
8484
}

automl/beta/src/main/java/com/example/automl/VideoClassificationCreateDataset.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ static void createDataset(String projectId, String displayName) throws IOExcepti
5151
Dataset createdDataset = client.createDataset(projectLocation, dataset);
5252

5353
// Display the dataset information.
54-
System.out.format("Dataset name: %s\n", createdDataset.getName());
54+
System.out.format("Dataset name: %s%n", createdDataset.getName());
5555
// To get the dataset id, you have to parse it out of the `name` field. As dataset Ids are
5656
// required for other methods.
5757
// Name Form: `projects/{project_id}/locations/{location_id}/datasets/{dataset_id}`
5858
String[] names = createdDataset.getName().split("/");
5959
String datasetId = names[names.length - 1];
60-
System.out.format("Dataset id: %s\n", datasetId);
60+
System.out.format("Dataset id: %s%n", datasetId);
6161
}
6262
}
6363
}

automl/beta/src/main/java/com/example/automl/VideoClassificationCreateModel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static void createModel(String projectId, String datasetId, String displayName)
6262
// OperationFuture.get() will block until the model is created, which may take several hours.
6363
// You can use OperationFuture.getInitialFuture to get a future representing the initial
6464
// response to the request, which contains information while the operation is in progress.
65-
System.out.format("Training operation name: %s\n", future.getInitialFuture().get().getName());
65+
System.out.format("Training operation name: %s%n", future.getInitialFuture().get().getName());
6666
System.out.println("Training started...");
6767
}
6868
}

automl/beta/src/main/java/com/example/automl/VideoObjectTrackingCreateDataset.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ static void createDataset(String projectId, String displayName) throws IOExcepti
5151
Dataset createdDataset = client.createDataset(projectLocation, dataset);
5252

5353
// Display the dataset information.
54-
System.out.format("Dataset name: %s\n", createdDataset.getName());
54+
System.out.format("Dataset name: %s%n", createdDataset.getName());
5555
// To get the dataset id, you have to parse it out of the `name` field. As dataset Ids are
5656
// required for other methods.
5757
// Name Form: `projects/{project_id}/locations/{location_id}/datasets/{dataset_id}`
5858
String[] names = createdDataset.getName().split("/");
5959
String datasetId = names[names.length - 1];
60-
System.out.format("Dataset id: %s\n", datasetId);
60+
System.out.format("Dataset id: %s%n", datasetId);
6161
}
6262
}
6363
}

automl/beta/src/main/java/com/example/automl/VideoObjectTrackingCreateModel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static void createModel(String projectId, String datasetId, String displayName)
6262
// OperationFuture.get() will block until the model is created, which may take several hours.
6363
// You can use OperationFuture.getInitialFuture to get a future representing the initial
6464
// response to the request, which contains information while the operation is in progress.
65-
System.out.format("Training operation name: %s\n", future.getInitialFuture().get().getName());
65+
System.out.format("Training operation name: %s%n", future.getInitialFuture().get().getName());
6666
System.out.println("Training started...");
6767
}
6868
}

0 commit comments

Comments
 (0)