From 12e5ac3519520798622b81105906f278526fa93e Mon Sep 17 00:00:00 2001 From: Nirupa Anantha Kumar Date: Fri, 5 Apr 2019 13:25:53 -0700 Subject: [PATCH] docs(samples): automl tables beta samples (#155) * Automl Tables beta samples * skipping tests * skipping tests * skipping tests * skipping tests --- automl/package.json | 19 +- automl/tables/create-dataset.v1beta1.js | 67 +++++++ automl/tables/create-model.v1beta1.js | 84 ++++++++ automl/tables/delete-dataset.v1beta1.js | 59 ++++++ automl/tables/delete-model.v1beta1.js | 59 ++++++ automl/tables/deploy-model.v1beta1.js | 53 +++++ automl/tables/display-evaluation.v1beta1.js | 155 +++++++++++++++ .../tables/export-data-to-bigquery.v1beta1.js | 70 +++++++ automl/tables/export-data-to-csv.v1beta1.js | 72 +++++++ .../export-evaluated-examples.v1beta1.js | 60 ++++++ automl/tables/get-column-spec.v1beta1.js | 68 +++++++ automl/tables/get-dataset.v1beta1.js | 77 ++++++++ automl/tables/get-model-evaluation.v1beta1.js | 185 +++++++++++++++++ automl/tables/get-model.v1beta1.js | 64 ++++++ automl/tables/get-operation-status.v1beta1.js | 57 ++++++ automl/tables/get-table-spec.v1beta1.js | 69 +++++++ automl/tables/import-data.v1beta1.js | 83 ++++++++ automl/tables/list-column-specs.v1beta1.js | 74 +++++++ automl/tables/list-datasets.v1beta1.js | 80 ++++++++ .../tables/list-model-evaluations.v1beta1.js | 186 ++++++++++++++++++ automl/tables/list-models.v1beta1.js | 69 +++++++ .../tables/list-operation-status.v1beta1.js | 68 +++++++ automl/tables/list-table-specs.v1beta1.js | 67 +++++++ .../predict-bq-source-bq-dest.v1beta1.js | 77 ++++++++ .../predict-bq-source-gcs-dest.v1beta1.js | 78 ++++++++ .../predict-gcs-source-bq-dest.v1beta1.js | 77 ++++++++ .../predict-gcs-source-gcs-dest.v1beta1.js | 78 ++++++++ automl/tables/predict.v1beta1.js | 78 ++++++++ automl/tables/undeploy-model.v1beta1.js | 53 +++++ automl/tables/update-column-spec.v1beta1.js | 82 ++++++++ automl/tables/update-dataset.v1beta1.js | 67 +++++++ ...nguageSentimentPrediction.v1beta1.test.js} | 0 .../test/automlTablesDataset.v1beta1.test.js | 141 +++++++++++++ automl/test/automlTablesModel.v1beta1.test.js | 130 ++++++++++++ .../test/automlTablesPredict.v1beta1.test.js | 88 +++++++++ 35 files changed, 2786 insertions(+), 8 deletions(-) create mode 100644 automl/tables/create-dataset.v1beta1.js create mode 100644 automl/tables/create-model.v1beta1.js create mode 100644 automl/tables/delete-dataset.v1beta1.js create mode 100644 automl/tables/delete-model.v1beta1.js create mode 100644 automl/tables/deploy-model.v1beta1.js create mode 100644 automl/tables/display-evaluation.v1beta1.js create mode 100644 automl/tables/export-data-to-bigquery.v1beta1.js create mode 100644 automl/tables/export-data-to-csv.v1beta1.js create mode 100644 automl/tables/export-evaluated-examples.v1beta1.js create mode 100644 automl/tables/get-column-spec.v1beta1.js create mode 100644 automl/tables/get-dataset.v1beta1.js create mode 100644 automl/tables/get-model-evaluation.v1beta1.js create mode 100644 automl/tables/get-model.v1beta1.js create mode 100644 automl/tables/get-operation-status.v1beta1.js create mode 100644 automl/tables/get-table-spec.v1beta1.js create mode 100644 automl/tables/import-data.v1beta1.js create mode 100644 automl/tables/list-column-specs.v1beta1.js create mode 100644 automl/tables/list-datasets.v1beta1.js create mode 100644 automl/tables/list-model-evaluations.v1beta1.js create mode 100644 automl/tables/list-models.v1beta1.js create mode 100644 automl/tables/list-operation-status.v1beta1.js create mode 100644 automl/tables/list-table-specs.v1beta1.js create mode 100644 automl/tables/predict-bq-source-bq-dest.v1beta1.js create mode 100644 automl/tables/predict-bq-source-gcs-dest.v1beta1.js create mode 100644 automl/tables/predict-gcs-source-bq-dest.v1beta1.js create mode 100644 automl/tables/predict-gcs-source-gcs-dest.v1beta1.js create mode 100644 automl/tables/predict.v1beta1.js create mode 100644 automl/tables/undeploy-model.v1beta1.js create mode 100644 automl/tables/update-column-spec.v1beta1.js create mode 100644 automl/tables/update-dataset.v1beta1.js rename automl/test/{automlLanguageSentimentPrediction.v1beta1.js => automlLanguageSentimentPrediction.v1beta1.test.js} (100%) create mode 100644 automl/test/automlTablesDataset.v1beta1.test.js create mode 100644 automl/test/automlTablesModel.v1beta1.test.js create mode 100644 automl/test/automlTablesPredict.v1beta1.test.js diff --git a/automl/package.json b/automl/package.json index 3f6d9d5086..f4833cca35 100644 --- a/automl/package.json +++ b/automl/package.json @@ -1,27 +1,30 @@ { "name": "@google-cloud/automl-samples", "description": "Samples for the Cloud AutoML Client Library for Node.js.", + "version": "0.0.1", "license": "Apache-2.0", "author": "Google LLC", "engines": { "node": ">=8" }, - "files": [ - "!test/*" - ], "repository": "googleapis/nodejs-automl", "private": true, + "nyc": { + "exclude": [ + "**/*.test.js" + ] + }, "scripts": { - "test": "mocha --timeout 600000 --recursive" + "test": "mocha --timeout 600000" }, "dependencies": { "@google-cloud/automl": "^0.2.0", + "chai": "^4.2.0", + "execa": "^1.0.0", "mathjs": "^5.5.0", - "yargs": "^13.2.1", - "execa":"^1.0.0" + "yargs": "^13.2.1" }, "devDependencies": { - "mocha": "^6.0.1", - "chai": "^4.2.0" + "mocha": "^6.0.1" } } diff --git a/automl/tables/create-dataset.v1beta1.js b/automl/tables/create-dataset.v1beta1.js new file mode 100644 index 0000000000..11c65f336d --- /dev/null +++ b/automl/tables/create-dataset.v1beta1.js @@ -0,0 +1,67 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + datasetName = 'YOUR_DATASET_NAME' +) { + // [START automl_tables_create_dataset] + const automl = require(`@google-cloud/automl`); + const util = require(`util`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to create a dataset + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const datasetName = '[DATASET_NAME]' e.g., “myDataset”; + + // A resource that represents Google Cloud Platform location. + const projectLocation = client.locationPath(projectId, computeRegion); + + // Set dataset name and metadata. + const myDataset = { + displayName: datasetName, + tablesDatasetMetadata: {}, + }; + + // Create a dataset with the dataset metadata in the region. + client + .createDataset({parent: projectLocation, dataset: myDataset}) + .then(responses => { + const dataset = responses[0]; + // Display the dataset information. + console.log(`Dataset name: ${dataset.name}`); + console.log(`Dataset Id: ${dataset.name.split(`/`).pop(-1)}`); + console.log(`Dataset display name: ${dataset.displayName}`); + console.log(`Dataset example count: ${dataset.exampleCount}`); + console.log( + `Tables dataset metadata: ${util.inspect( + dataset.tablesDatasetMetadata, + false, + null + )}` + ); + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_create_dataset] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/create-model.v1beta1.js b/automl/tables/create-model.v1beta1.js new file mode 100644 index 0000000000..daf0d7716d --- /dev/null +++ b/automl/tables/create-model.v1beta1.js @@ -0,0 +1,84 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + datasetId = 'YOUR_DATASET_ID', + tableId = 'TABLE_ID', + columnId = 'COLUMN_ID', + modelName = 'MODEL_NAME', + trainBudget = 'TRAIN_BUDGET' +) { + // [START automl_tables_create_model] + const automl = require(`@google-cloud/automl`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to create a model. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const datasetId = '[DATASET_ID]' e.g., "TBL2246891593778855936"; + // const tableId = '[TABLE_ID]' e.g., "1991013247762825216"; + // const columnId = '[COLUMN_ID]' e.g., "773141392279994368"; + // const modelName = '[MODEL_NAME]' e.g., "testModel"; + // const trainBudget = '[TRAIN_BUDGET]' e.g., "1000", + // `Train budget in milli node hours`; + + // A resource that represents Google Cloud Platform location. + const projectLocation = client.locationPath(projectId, computeRegion); + + // Get the full path of the column. + const columnSpecId = client.columnSpecPath( + projectId, + computeRegion, + datasetId, + tableId, + columnId + ); + + // Set target column to train the model. + const targetColumnSpec = {name: columnSpecId}; + + // Set tables model metadata. + const tablesModelMetadata = { + targetColumnSpec: targetColumnSpec, + trainBudgetMilliNodeHours: trainBudget, + }; + + // Set datasetId, model name and model metadata for the dataset. + const myModel = { + datasetId: datasetId, + displayName: modelName, + tablesModelMetadata: tablesModelMetadata, + }; + + // Create a model with the model metadata in the region. + client + .createModel({parent: projectLocation, model: myModel}) + .then(responses => { + const initialApiResponse = responses[1]; + console.log(`Training operation name: ${initialApiResponse.name}`); + console.log(`Training started...`); + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_create_model] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/delete-dataset.v1beta1.js b/automl/tables/delete-dataset.v1beta1.js new file mode 100644 index 0000000000..0b6c82998b --- /dev/null +++ b/automl/tables/delete-dataset.v1beta1.js @@ -0,0 +1,59 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + datasetId = 'YOUR_DATASET_ID' +) { + // [START automl_tables_delete_dataset] + const automl = require(`@google-cloud/automl`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to delete a dataset. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const datasetId = '[DATASET_ID]' e.g., "TBL2246891593778855936"; + + // Get the full path of the dataset. + const datasetFullId = client.datasetPath(projectId, computeRegion, datasetId); + + // Delete a dataset. + client + .deleteDataset({name: datasetFullId}) + .then(responses => { + const operation = responses[0]; + return operation.promise(); + }) + .then(responses => { + // The final result of the operation. + const operationDetails = responses[2]; + + // Get the dataset delete details. + console.log('Dataset delete details:'); + console.log(`\tOperation details:`); + console.log(`\t\tName: ${operationDetails.name}`); + console.log(`\t\tDone: ${operationDetails.done}`); + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_delete_dataset] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/delete-model.v1beta1.js b/automl/tables/delete-model.v1beta1.js new file mode 100644 index 0000000000..c9dd769c19 --- /dev/null +++ b/automl/tables/delete-model.v1beta1.js @@ -0,0 +1,59 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + modelId = 'MODEL_ID' +) { + // [START automl_tables_delete_model] + const automl = require(`@google-cloud/automl`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to delete a model. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "TBL4704590352927948800"; + + // Get the full path of the model. + const modelFullId = client.modelPath(projectId, computeRegion, modelId); + + // Delete a model. + client + .deleteModel({name: modelFullId}) + .then(responses => { + const operation = responses[0]; + return operation.promise(); + }) + .then(responses => { + // The final result of the operation. + const operationDetails = responses[2]; + + // Get the Model delete details. + console.log('Model delete details:'); + console.log(`\tOperation details:`); + console.log(`\t\tName: ${operationDetails.name}`); + console.log(`\tDone: ${operationDetails.done}`); + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_delete_model] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/deploy-model.v1beta1.js b/automl/tables/deploy-model.v1beta1.js new file mode 100644 index 0000000000..59eeef1a22 --- /dev/null +++ b/automl/tables/deploy-model.v1beta1.js @@ -0,0 +1,53 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + modelId = 'MODEL_ID' +) { + // [START automl_tables_deploy_model] + const automl = require(`@google-cloud/automl`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to deploy model. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "TBL4704590352927948800"; + + // Get the full path of the model. + const modelFullId = client.modelPath(projectId, computeRegion, modelId); + + // Deploy a model with the deploy model request. + client + .deployModel({name: modelFullId}) + .then(responses => { + const response = responses[0]; + console.log(`Deployment Details:`); + console.log(`\tName: ${response.name}`); + console.log(`\tMetadata:`); + console.log(`\t\tType Url: ${response.metadata.typeUrl}`); + console.log(`\tDone: ${response.done}`); + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_deploy_model] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/display-evaluation.v1beta1.js b/automl/tables/display-evaluation.v1beta1.js new file mode 100644 index 0000000000..d15470a844 --- /dev/null +++ b/automl/tables/display-evaluation.v1beta1.js @@ -0,0 +1,155 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + modelId = 'MODEL_ID', + filter = 'FILTER_EXPRESSION' +) { + // [START automl_tables_display_evaluation] + const automl = require(`@google-cloud/automl`); + const math = require(`mathjs`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to display model evaluation. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "TBL4704590352927948800"; + // const filter_ = '[FILTER_EXPRESSIONS]' e.g., "tablesModelMetadata:*"; + + // Get the full path of the model. + const modelFullId = client.modelPath(projectId, computeRegion, modelId); + + // List all the model evaluations in the model by applying filter. + client + .listModelEvaluations({parent: modelFullId, filter: filter}) + .then(respond => { + const response = respond[0]; + + // Iterate through the results. + let modelEvaluationId = ``; + for (const element of response) { + // There is evaluation for each class in a model and for overall model. + // Get only the evaluation of overall model. + if (!element.annotationSpecId) { + modelEvaluationId = element.name.split(`/`).pop(-1); + } + } + console.log(`Model Evaluation ID: ${modelEvaluationId}`); + + // Resource name for the model evaluation. + const modelEvaluationFullId = client.modelEvaluationPath( + projectId, + computeRegion, + modelId, + modelEvaluationId + ); + + // Get a model evaluation. + client + .getModelEvaluation({name: modelEvaluationFullId}) + .then(responses => { + const modelEvaluation = responses[0]; + const classMetrics = modelEvaluation.classificationEvaluationMetrics; + + if (classMetrics) { + const confidenceMetricsEntries = + classMetrics.confidenceMetricsEntry; + + if (confidenceMetricsEntries.length > 0) { + // Showing model score based on threshold of 0.5 + for (const confidenceMetricsEntry of confidenceMetricsEntries) { + if (confidenceMetricsEntry.confidenceThreshold === 0.5) { + console.log( + `\tPrecision and recall are based on ` + + `a score threshold of 0.5` + ); + console.log( + `\t\tModel precision: ${math.round( + confidenceMetricsEntry.precision * 100, + 2 + )} %` + ); + console.log( + `\t\tModel recall: ${math.round( + confidenceMetricsEntry.recall * 100, + 2 + )} %` + ); + console.log( + `\t\tModel f1 score: ${math.round( + confidenceMetricsEntry.f1Score * 100, + 2 + )} %` + ); + console.log( + `\t\tModel precision@1: ${math.round( + confidenceMetricsEntry.precisionAt1 * 100, + 2 + )} % + ` + ); + console.log( + `\t\tModel recall@1: ${math.round( + confidenceMetricsEntry.recallAt1 * 100, + 2 + )} %` + ); + console.log( + `\t\tModel f1 score@1: ${math.round( + confidenceMetricsEntry.f1ScoreAt1 * 100, + 2 + )} % + ` + ); + } + } + } else { + const regressionMetrics = + modelEvaluation.regressionEvaluationMetrics; + console.log( + `\tModel root mean squared error: ${ + regressionMetrics.rootMeanSquaredError + }` + ); + console.log( + `\tModel mean absolute error: ${ + regressionMetrics.meanAbsoluteError + }` + ); + console.log( + `\tModel mean absolute percentage error: ${ + regressionMetrics.meanAbsolutePercentageError + }` + ); + console.log(`\tModel rsquared: ${regressionMetrics.rSquared}`); + } + } + }) + .catch(err => { + console.error(err); + }); + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_display_evaluation] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/export-data-to-bigquery.v1beta1.js b/automl/tables/export-data-to-bigquery.v1beta1.js new file mode 100644 index 0000000000..0a71527325 --- /dev/null +++ b/automl/tables/export-data-to-bigquery.v1beta1.js @@ -0,0 +1,70 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + datasetId = 'YOUR_DATASET_ID', + bigQueryOutputUri = 'BIGQUERY_DIRECTORY' +) { + // [START automl_tables_export_data_to_bigquery] + const automl = require(`@google-cloud/automl`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to export a dataset to a BigQuery. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const datasetId = '[DATASET_ID]' e.g., "TBL2246891593778855936"; + // const bigQueryOutputUri = '[BIGQUERY_DIRECTORY]' e.g., “bq://”, + // `BigQuery URI for the export directory`; + + // Get the full path of the dataset. + const datasetFullId = client.datasetPath(projectId, computeRegion, datasetId); + + // Set the output URI + const outputConfig = { + bigqueryDestination: { + outputUri: bigQueryOutputUri, + }, + }; + + // Export the data to the output URI. + client + .exportData({name: datasetFullId, outputConfig: outputConfig}) + .then(responses => { + const operation = responses[0]; + console.log(`Processing export...`); + return operation.promise(); + }) + .then(responses => { + // The final result of the operation. + const operationDetails = responses[2]; + + // Get the data export details. + console.log('Data export details:'); + console.log(`\tOperation details:`); + console.log(`\t\tName: ${operationDetails.name}`); + console.log(`\t\tDone: ${operationDetails.done}`); + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_export_data_to_bigquery] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/export-data-to-csv.v1beta1.js b/automl/tables/export-data-to-csv.v1beta1.js new file mode 100644 index 0000000000..0bcf4ba71b --- /dev/null +++ b/automl/tables/export-data-to-csv.v1beta1.js @@ -0,0 +1,72 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + datasetId = 'YOUR_DATASET_ID', + gcsOutputUri = 'GCS_DIRECTORY' +) { + // [START automl_tables_export_data_to_csv] + const automl = require(`@google-cloud/automl`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to export a dataset to a + * Google Cloud Storage bucket. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const datasetId = '[DATASET_ID]' e.g., "TBL2246891593778855936"; + // const gcsOutputUri = '[GCS_DIRECTORY]' + // e.g., "gs:///", + // `Google Cloud Storage URI for the export directory`; + + // Get the full path of the dataset. + const datasetFullId = client.datasetPath(projectId, computeRegion, datasetId); + + // Set the output URI + const outputConfig = { + gcsDestination: { + outputUriPrefix: gcsOutputUri, + }, + }; + + // Export the data to the output URI. + client + .exportData({name: datasetFullId, outputConfig: outputConfig}) + .then(responses => { + const operation = responses[0]; + console.log(`Processing export...`); + return operation.promise(); + }) + .then(responses => { + // The final result of the operation. + const operationDetails = responses[2]; + + // Get the data export details. + console.log('Data export details:'); + console.log(`\tOperation details:`); + console.log(`\t\tName: ${operationDetails.name}`); + console.log(`\t\tDone: ${operationDetails.done}`); + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_export_data_to_csv] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/export-evaluated-examples.v1beta1.js b/automl/tables/export-evaluated-examples.v1beta1.js new file mode 100644 index 0000000000..f12e8220f9 --- /dev/null +++ b/automl/tables/export-evaluated-examples.v1beta1.js @@ -0,0 +1,60 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + modelId = 'MODEL_ID', + bigQueryOutputUri = 'BIGQUERY_DIRECTORY' +) { + // [START automl_tables_export_evaluated_examples] + const automl = require(`@google-cloud/automl`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to export evaluated examples. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "TBL4704590352927948800"; + // const outputUri = '[BIGQUERY_DIRECTORY]' e.g., “bq://”, + // `BigQuery URI for the export directory`; + + // Get the full path of the model. + const modelFullId = client.modelPath(projectId, computeRegion, modelId); + + // Set the output URI. + const outputConfig = { + bigqueryDestination: { + outputUri: bigQueryOutputUri, + }, + }; + + // Export the examples on which the model was evaluated to the + // bigQuery output URI. + client + .exportEvaluatedExamples({name: modelFullId, outputConfig: outputConfig}) + .then(responses => { + const initialApiResponse = responses[1]; + console.log(`Operation name: ${initialApiResponse.name}`); + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_export_evaluated_examples] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/get-column-spec.v1beta1.js b/automl/tables/get-column-spec.v1beta1.js new file mode 100644 index 0000000000..a16d5b72a1 --- /dev/null +++ b/automl/tables/get-column-spec.v1beta1.js @@ -0,0 +1,68 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + datasetId = 'YOUR_DATASET_ID', + tableId = 'TABLE_ID', + columnId = 'COLUMN_ID' +) { + // [START automl_tables_get_column_spec] + const automl = require(`@google-cloud/automl`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to get all column specs + * information in table colums. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const datasetId = '[DATASET_ID]' e.g., "TBL2246891593778855936"; + // const tableId = '[TABLE_ID]' e.g., "1991013247762825216"`; + // const columnId = '[COLUMN_ID]' e.g., "773141392279994368"; + + // Get the full path of the column. + const columnSpecId = client.columnSpecPath( + projectId, + computeRegion, + datasetId, + tableId, + columnId + ); + + // Get all the information about a given columnSpec of a particular + // table in a dataset. + client + .getColumnSpec({name: columnSpecId}) + .then(responses => { + const column = responses[0]; + // Display the column spec information. + console.log(`Column name: ${column.name}`); + console.log(`Column Id: ${column.name.split(`/`).pop(-1)}`); + console.log(`Column display name: ${column.displayName}`); + console.log(`Column datatype: ${column.dataType.typeCode}`); + console.log( + `Column distinct value count: ${column.dataStats.distinctValueCount}` + ); + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_get_column_sspec] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/get-dataset.v1beta1.js b/automl/tables/get-dataset.v1beta1.js new file mode 100644 index 0000000000..7d6124ac04 --- /dev/null +++ b/automl/tables/get-dataset.v1beta1.js @@ -0,0 +1,77 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + datasetId = 'YOUR_DATASET_ID' +) { + // [START automl_tables_get_dataset] + const automl = require(`@google-cloud/automl`); + const util = require(`util`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to get a dataset by ID. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const datasetId = '[DATASET_ID]' e.g., "TBL2246891593778855936"; + + // Get the full path of the dataset. + const datasetFullId = client.datasetPath(projectId, computeRegion, datasetId); + + // Get complete detail of the dataset. + client + .getDataset({name: datasetFullId}) + .then(responses => { + const dataset = responses[0]; + + const tablesDatasetMetadata = dataset.tablesDatasetMetadata; + + // Display the dataset information. + console.log(`Dataset name: ${dataset.name}`); + console.log(`Dataset Id: ${dataset.name.split(`/`).pop(-1)}`); + console.log(`Dataset display name: ${dataset.displayName}`); + console.log(`Dataset example count: ${dataset.exampleCount}`); + console.log(`Tables dataset metadata:`); + console.log( + `\tTarget column correlations: ${util.inspect( + tablesDatasetMetadata.targetColumnCorrelations, + false, + null + )}` + ); + console.log( + `\tPrimary table spec Id: ${tablesDatasetMetadata.primaryTableSpecId}` + ); + console.log( + `\tTarget column spec Id: ${tablesDatasetMetadata.targetColumnSpecId}` + ); + console.log( + `\tWeight column spec Id: ${tablesDatasetMetadata.weightColumnSpecId}` + ); + console.log( + `\tMl use column spec Id: ${tablesDatasetMetadata.mlUseColumnSpecId}` + ); + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_get_dataset] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/get-model-evaluation.v1beta1.js b/automl/tables/get-model-evaluation.v1beta1.js new file mode 100644 index 0000000000..9eb80aa4ac --- /dev/null +++ b/automl/tables/get-model-evaluation.v1beta1.js @@ -0,0 +1,185 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + modelId = 'MODEL_ID', + modelEvaluationId = 'MODEL_EVALUATION_ID' +) { + // [START automl_tables_get_model_evaluation] + const automl = require(`@google-cloud/automl`); + const math = require(`mathjs`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to get model evaluations. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "TBL4704590352927948800"; + // const modelEvaluationId = '[MODEL_EVALUATION_ID]' + // e.g., "8471321031908237216"; + + // Get the full path of the model evaluation. + const modelEvaluationFullId = client.modelEvaluationPath( + projectId, + computeRegion, + modelId, + modelEvaluationId + ); + + // Get complete detail of the model evaluation. + client + .getModelEvaluation({name: modelEvaluationFullId}) + .then(responses => { + const response = responses[0]; + + const classMetrics = response.classificationEvaluationMetrics; + const regressionMetrics = response.regressionEvaluationMetrics; + const evaluationId = response.name.split(`/`)[7].split('`')[0]; + + // Display the model evaluation information. + console.log(`Model evaluation name: ${response.name}`); + console.log(`Model evaluation Id: ${evaluationId}`); + console.log( + `Model evaluation annotation spec Id: ${response.annotationSpecId}` + ); + console.log(`Model evaluation display name: ${response.displayName}`); + console.log( + `Model evaluation example count: ${response.evaluatedExampleCount}` + ); + if (classMetrics) { + const confidenceMetricsEntries = classMetrics.confidenceMetricsEntry; + + console.log(`Table classification evaluation metrics:`); + console.log(`\tModel auPrc: ${math.round(classMetrics.auPrc, 6)}`); + console.log(`\tModel auRoc: ${math.round(classMetrics.auRoc, 6)}`); + console.log(`\tModel log loss: ${math.round(classMetrics.logLoss, 6)}`); + + if (confidenceMetricsEntries.length > 0) { + console.log(`\tConfidence metrics entries:`); + + for (const confidenceMetricsEntry of confidenceMetricsEntries) { + console.log( + `\t\tModel confidence threshold: ${math.round( + confidenceMetricsEntry.confidenceThreshold, + 6 + )}` + ); + console.log( + `\t\tModel position threshold: ${math.round( + confidenceMetricsEntry.positionThreshold, + 4 + )}` + ); + console.log( + `\t\tModel recall: ${math.round( + confidenceMetricsEntry.recall * 100, + 2 + )} %` + ); + console.log( + `\t\tModel precision: ${math.round( + confidenceMetricsEntry.precision * 100, + 2 + )} %` + ); + console.log( + `\t\tModel false positive rate: ${ + confidenceMetricsEntry.falsePositiveRate + }` + ); + console.log( + `\t\tModel f1 score: ${math.round( + confidenceMetricsEntry.f1Score * 100, + 2 + )} %` + ); + console.log( + `\t\tModel recall@1: ${math.round( + confidenceMetricsEntry.recallAt1 * 100, + 2 + )} %` + ); + console.log( + `\t\tModel precision@1: ${math.round( + confidenceMetricsEntry.precisionAt1 * 100, + 2 + )} %` + ); + console.log( + `\t\tModel false positive rate@1: ${ + confidenceMetricsEntry.falsePositiveRateAt1 + }` + ); + console.log( + `\t\tModel f1 score@1: ${math.round( + confidenceMetricsEntry.f1ScoreAt1 * 100, + 2 + )} %` + ); + console.log( + `\t\tModel true positive count: ${ + confidenceMetricsEntry.truePositiveCount + }` + ); + console.log( + `\t\tModel false positive count: ${ + confidenceMetricsEntry.falsePositiveCount + }` + ); + console.log( + `\t\tModel false negative count: ${ + confidenceMetricsEntry.falseNegativeCount + }` + ); + console.log( + `\t\tModel true negative count: ${ + confidenceMetricsEntry.trueNegativeCount + }` + ); + console.log(`\n`); + } + } + console.log( + `\tModel annotation spec Id: ${classMetrics.annotationSpecId}` + ); + } else if (regressionMetrics) { + console.log(`Table regression evaluation metrics:`); + console.log( + `\tModel root mean squared error: ${ + regressionMetrics.rootMeanSquaredError + }` + ); + console.log( + `\tModel mean absolute error: ${regressionMetrics.meanAbsoluteError}` + ); + console.log( + `\tModel mean absolute percentage error: ${ + regressionMetrics.meanAbsolutePercentageError + }` + ); + console.log(`\tModel rSquared: ${regressionMetrics.rSquared}`); + } + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_get_model_evaluation] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/get-model.v1beta1.js b/automl/tables/get-model.v1beta1.js new file mode 100644 index 0000000000..876c16d06c --- /dev/null +++ b/automl/tables/get-model.v1beta1.js @@ -0,0 +1,64 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + modelId = 'MODEL_ID' +) { + // [START automl_tables_get_model] + const automl = require(`@google-cloud/automl`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to get model details. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "TBL4704590352927948800"; + + // Get the full path of the model. + const modelFullId = client.modelPath(projectId, computeRegion, modelId); + + // Get complete detail of the model. + client + .getModel({name: modelFullId}) + .then(responses => { + const model = responses[0]; + + // Display the model information. + console.log(`Model name: ${model.name}`); + console.log(`Model Id: ${model.name.split(`/`).pop(-1)}`); + console.log(`Model display name: ${model.displayName}`); + console.log(`Dataset Id: ${model.datasetId}`); + console.log(`Tables model metadata: `); + console.log( + `\tTraining budget: ${ + model.tablesModelMetadata.trainBudgetMilliNodeHours + }` + ); + console.log( + `\tTraining cost: ${model.tablesModelMetadata.trainCostMilliNodeHours}` + ); + console.log(`Model deployment state: ${model.deploymentState}`); + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_get_model] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/get-operation-status.v1beta1.js b/automl/tables/get-operation-status.v1beta1.js new file mode 100644 index 0000000000..f2d8efe01c --- /dev/null +++ b/automl/tables/get-operation-status.v1beta1.js @@ -0,0 +1,57 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +async function main(operationFullId = 'OPERATION_FULL_ID') { + // [START automl_tables_get_operation_status] + const automl = require(`@google-cloud/automl`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to get operation status. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const operationFullId = '[OPERATION_FULL_ID]' + // eg., "projects//locations/us-central1/operations/", + // `Full name of an operation`; + + // Get the latest state of a long-running operation. + client.operationsClient + .getOperation({name: operationFullId}) + .then(responses => { + const response = responses[0]; + console.log(`Operation details:`); + console.log(`\tName: ${response.name}`); + console.log(`\tMetadata:`); + console.log(`\t\tType Url: ${response.metadata.typeUrl}`); + console.log(`\tDone: ${response.done}`); + + if (response.response) { + console.log(`\tResponse:`); + console.log(`\t\tType Url: ${response.response.typeUrl}`); + } + + if (response.error) { + console.log(`\tResponse:`); + console.log(`\t\tError code: ${response.error.code}`); + console.log(`\t\tError message: ${response.error.message}`); + } + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_get_operation_status] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/get-table-spec.v1beta1.js b/automl/tables/get-table-spec.v1beta1.js new file mode 100644 index 0000000000..a158988b19 --- /dev/null +++ b/automl/tables/get-table-spec.v1beta1.js @@ -0,0 +1,69 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + datasetId = 'YOUR_DATASET_ID', + tableId = 'TABLE_ID' +) { + // [START automl_tables_get_table_spec] + const automl = require(`@google-cloud/automl`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to get all table specs + * information in table. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const datasetId = '[DATASET_ID]' e.g., "TBL2246891593778855936"; + // const tableId = '[TABLE_ID]' e.g., "1991013247762825216"; + + // Get the full path of the table. + const tableSpecId = client.tableSpecPath( + projectId, + computeRegion, + datasetId, + tableId + ); + + // Get all the information about a given tableSpec of particular dataset. + client + .getTableSpec({name: tableSpecId}) + .then(responses => { + const table = responses[0]; + + // Display the table spec information. + console.log(`Table name: ${table.name}`); + console.log(`Table Id: ${table.name.split(`/`).pop(-1)}`); + console.log(`Table row count: ${table.rowCount}`); + console.log(`Table column count: ${table.columnCount}`); + + console.log(`Table input config:`); + if (table.inputConfigs[0].source === `gcsSource`) { + console.log(`\t${table.inputConfigs[0].gcsSource.inputUris}`); + } else { + console.log(`\t${table.inputConfigs[0].bigquerySource.inputUri}`); + } + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_get_table_spec] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/import-data.v1beta1.js b/automl/tables/import-data.v1beta1.js new file mode 100644 index 0000000000..109e4d7520 --- /dev/null +++ b/automl/tables/import-data.v1beta1.js @@ -0,0 +1,83 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + datasetId = 'YOUR_DATASET_ID', + path = 'GCS_PATH or BIGQUERY_PATH' +) { + // [START automl_tables_import_data] + const automl = require(`@google-cloud/automl`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to import data. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const datasetId = '[DATASET_ID]' e.g., "TBL2246891593778855936"; + // const path = '[GCS_PATH]' | '[BIGQUERY_PATH]' + // e.g., "gs:///" or + // "bq://..", + // `string or array of paths in AutoML Tables format`; + + // Get the full path of the dataset. + const datasetFullId = client.datasetPath(projectId, computeRegion, datasetId); + + let inputConfig = {}; + if (path.startsWith(`bq`)) { + // Get Bigquery URI. + inputConfig = { + bigquerySource: { + inputUri: path, + }, + }; + } else { + // Get the multiple Google Cloud Storage URIs. + const inputUris = path.split(`,`); + inputConfig = { + gcsSource: { + inputUris: inputUris, + }, + }; + } + + // Import the dataset from the input URI. + client + .importData({name: datasetFullId, inputConfig: inputConfig}) + .then(responses => { + const operation = responses[0]; + console.log(`Processing import...`); + return operation.promise(); + }) + .then(responses => { + // The final result of the operation. + const operationDetails = responses[2]; + + // Get the data import details. + console.log('Data import details:'); + console.log(`\tOperation details:`); + console.log(`\t\tName: ${operationDetails.name}`); + console.log(`\t\tDone: ${operationDetails.done}`); + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_import_data] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/list-column-specs.v1beta1.js b/automl/tables/list-column-specs.v1beta1.js new file mode 100644 index 0000000000..41f176c605 --- /dev/null +++ b/automl/tables/list-column-specs.v1beta1.js @@ -0,0 +1,74 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + datasetId = 'YOUR_DATASET_ID', + tableId = 'TABLE_ID', + filter = 'FILTER_EXPRESSION' +) { + // [START automl_tables_list_column_specs] + const automl = require(`@google-cloud/automl`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to list all column specs in + * table columns. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const datasetId = '[DATASET_ID]' e.g., "TBL2246891593778855936"; + // const tableId = '[TABLE_ID]' e.g., "1991013247762825216"`; + // const filter = '[FILTER_EXPRESSIONS]' e.g., "tablesDatasetMetadata:*"; + + // Get the full path of the table. + const tableSpecId = client.tableSpecPath( + projectId, + computeRegion, + datasetId, + tableId + ); + + // List all the column specs of particular table available in the region by + // applying filter. + client + .listColumnSpecs({parent: tableSpecId, filter: filter}) + .then(responses => { + const column = responses[0]; + + // Display the table columns specs information. + console.log(`List of column specs: `); + for (let i = 0; i < column.length; i++) { + console.log(`Column name: ${column[i].name}`); + console.log(`Column Id: ${column[i].name.split(`/`).pop(-1)}`); + console.log(`Column display name: ${column[i].displayName}`); + console.log(`Column datatype: ${column[i].dataType.typeCode}`); + console.log( + `Column distinct value count: ${ + column[i].dataStats.distinctValueCount + }` + ); + console.log(`\n`); + } + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_list_column_specs] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/list-datasets.v1beta1.js b/automl/tables/list-datasets.v1beta1.js new file mode 100644 index 0000000000..582fc2e300 --- /dev/null +++ b/automl/tables/list-datasets.v1beta1.js @@ -0,0 +1,80 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + filter = 'FILTER_EXPRESSION' +) { + // [START automl_tables_list_datasets] + const automl = require(`@google-cloud/automl`); + const util = require(`util`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to list all datasets. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const filter = '[FILTER_EXPRESSIONS]' e.g., "tablesDatasetMetadata:*"; + + // A resource that represents Google Cloud Platform location. + const projectLocation = client.locationPath(projectId, computeRegion); + + // List all the datasets available in the region by applying filter. + client + .listDatasets({parent: projectLocation, filter: filter}) + .then(responses => { + const dataset = responses[0]; + + // Display the dataset information. + console.log(`List of datasets:`); + for (let i = 0; i < dataset.length; i++) { + const tablesDatasetMetadata = dataset[i].tablesDatasetMetadata; + + console.log(`Dataset name: ${dataset[i].name}`); + console.log(`Dataset Id: ${dataset[i].name.split(`/`).pop(-1)}`); + console.log(`Dataset display name: ${dataset[i].displayName}`); + console.log(`Dataset example count: ${dataset[i].exampleCount}`); + console.log(`Tables dataset metadata:`); + console.log( + `\tTarget column correlations: ${util.inspect( + tablesDatasetMetadata.targetColumnCorrelations, + false, + null + )}` + ); + console.log( + `\tPrimary table spec Id: ${tablesDatasetMetadata.primaryTableSpecId}` + ); + console.log( + `\tTarget column spec Id: ${tablesDatasetMetadata.targetColumnSpecId}` + ); + console.log( + `\tWeight column spec Id: ${tablesDatasetMetadata.weightColumnSpecId}` + ); + console.log( + `\tMl use column spec Id: ${tablesDatasetMetadata.mlUseColumnSpecId}` + ); + } + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_list_datasets] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/list-model-evaluations.v1beta1.js b/automl/tables/list-model-evaluations.v1beta1.js new file mode 100644 index 0000000000..07fcf7efdc --- /dev/null +++ b/automl/tables/list-model-evaluations.v1beta1.js @@ -0,0 +1,186 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + modelId = 'MODEL_ID', + filter = 'FILTER_EXPRESSION' +) { + // [START automl_tables_list_model_evaluations] + const automl = require(`@google-cloud/automl`); + const math = require(`mathjs`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to list model evaluations. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "TBL4704590352927948800"; + // const filter = '[FILTER_EXPRESSIONS]' e.g., "tablesModelMetadata:*"; + + // Get the full path of the model. + const modelFullId = client.modelPath(projectId, computeRegion, modelId); + + // List all the model evaluations in the model by applying filter. + client + .listModelEvaluations({parent: modelFullId, filter: filter}) + .then(responses => { + const element = responses[0]; + console.log(`List of model evaluations:`); + for (let i = 0; i < element.length; i++) { + const classMetrics = element[i].classificationEvaluationMetrics; + const regressionMetrics = element[i].regressionEvaluationMetrics; + const evaluationId = element[i].name.split(`/`)[7].split('`')[0]; + + console.log(`Model evaluation name: ${element[i].name}`); + console.log(`Model evaluation Id: ${evaluationId}`); + console.log( + `Model evaluation annotation spec Id: ${element[i].annotationSpecId}` + ); + console.log(`Model evaluation display name: ${element[i].displayName}`); + console.log( + `Model evaluation example count: ${element[i].evaluatedExampleCount}` + ); + + if (classMetrics) { + const confidenceMetricsEntries = classMetrics.confidenceMetricsEntry; + + console.log(`Table classification evaluation metrics:`); + console.log(`\tModel auPrc: ${math.round(classMetrics.auPrc, 6)}`); + console.log(`\tModel auRoc: ${math.round(classMetrics.auRoc, 6)}`); + console.log( + `\tModel log loss: ${math.round(classMetrics.logLoss, 6)}` + ); + + if (confidenceMetricsEntries.length > 0) { + console.log(`\tConfidence metrics entries:`); + + for (const confidenceMetricsEntry of confidenceMetricsEntries) { + console.log( + `\t\tModel confidence threshold: ${math.round( + confidenceMetricsEntry.confidenceThreshold, + 6 + )}` + ); + console.log( + `\t\tModel position threshold: ${math.round( + confidenceMetricsEntry.positionThreshold, + 4 + )}` + ); + console.log( + `\t\tModel recall: ${math.round( + confidenceMetricsEntry.recall * 100, + 2 + )} %` + ); + console.log( + `\t\tModel precision: ${math.round( + confidenceMetricsEntry.precision * 100, + 2 + )} %` + ); + console.log( + `\t\tModel false positive rate: ${ + confidenceMetricsEntry.falsePositiveRate + }` + ); + console.log( + `\t\tModel f1 score: ${math.round( + confidenceMetricsEntry.f1Score * 100, + 2 + )} %` + ); + console.log( + `\t\tModel recall@1: ${math.round( + confidenceMetricsEntry.recallAt1 * 100, + 2 + )} %` + ); + console.log( + `\t\tModel precision@1: ${math.round( + confidenceMetricsEntry.precisionAt1 * 100, + 2 + )} %` + ); + console.log( + `\t\tModel false positive rate@1: ${ + confidenceMetricsEntry.falsePositiveRateAt1 + }` + ); + console.log( + `\t\tModel f1 score@1: ${math.round( + confidenceMetricsEntry.f1ScoreAt1 * 100, + 2 + )} %` + ); + console.log( + `\t\tModel true positive count: ${ + confidenceMetricsEntry.truePositiveCount + }` + ); + console.log( + `\t\tModel false positive count: ${ + confidenceMetricsEntry.falsePositiveCount + }` + ); + console.log( + `\t\tModel false negative count: ${ + confidenceMetricsEntry.falseNegativeCount + }` + ); + console.log( + `\t\tModel true negative count: ${ + confidenceMetricsEntry.trueNegativeCount + }` + ); + console.log(`\n`); + } + } + console.log( + `\tModel annotation spec Id: ${classMetrics.annotationSpecId}` + ); + } else if (regressionMetrics) { + console.log(`Table regression evaluation metrics:`); + console.log( + `\tModel root mean squared error: ${ + regressionMetrics.rootMeanSquaredError + }` + ); + console.log( + `\tModel mean absolute error: ${ + regressionMetrics.meanAbsoluteError + }` + ); + console.log( + `\tModel mean absolute percentage error: ${ + regressionMetrics.meanAbsolutePercentageError + }` + ); + console.log(`\tModel rSquared: ${regressionMetrics.rSquared}`); + } + console.log(`\n`); + } + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_list_model_evaluations] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/list-models.v1beta1.js b/automl/tables/list-models.v1beta1.js new file mode 100644 index 0000000000..8257f270b0 --- /dev/null +++ b/automl/tables/list-models.v1beta1.js @@ -0,0 +1,69 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + filter = 'FILTER_EXPRESSION' +) { + // [START automl_tables_list_models] + const automl = require(`@google-cloud/automl`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to list all models. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const filter_ = '[FILTER_EXPRESSIONS]' e.g., "tablesModelMetadata:*"; + + // A resource that represents Google Cloud Platform location. + const projectLocation = client.locationPath(projectId, computeRegion); + + // List all the models available in the region by applying filter. + client + .listModels({parent: projectLocation, filter: filter}) + .then(responses => { + const model = responses[0]; + + // Display the model information. + console.log(`List of models:`); + for (let i = 0; i < model.length; i++) { + console.log(`\nModel name: ${model[i].name}`); + console.log(`Model Id: ${model[i].name.split(`/`).pop(-1)}`); + console.log(`Model display name: ${model[i].displayName}`); + console.log(`Dataset Id: ${model[i].datasetId}`); + console.log(`Tables model metadata:`); + console.log( + `\tTraining budget: ${ + model[i].tablesModelMetadata.trainBudgetMilliNodeHours + }` + ); + console.log( + `\tTraining cost: ${ + model[i].tablesModelMetadata.trainCostMilliNodeHours + }` + ); + console.log(`Model deployment state: ${model[i].deploymentState}`); + } + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_list_models] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/list-operation-status.v1beta1.js b/automl/tables/list-operation-status.v1beta1.js new file mode 100644 index 0000000000..4def49b6a4 --- /dev/null +++ b/automl/tables/list-operation-status.v1beta1.js @@ -0,0 +1,68 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + filter = 'FILTER_EXPRESSION' +) { + // [START automl_tables_list_operations_status] + const automl = require(`@google-cloud/automl`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to list all operation status. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const filter_ = '[FILTER_EXPRESSIONS]'; + + // A resource that represents Google Cloud Platform location. + const projectLocation = client.locationPath(projectId, computeRegion); + + // Get the list of latest state of a long-running operations. + client.operationsClient + .listOperations({name: projectLocation, filter: filter}) + .then(responses => { + const response = responses[0]; + console.log(`List of operations:`); + for (let i = 0; i < response.length; i++) { + // Display operation details. + console.log(`\nOperation details:`); + console.log(`\t\tName: ${response[i].name}`); + console.log(`\t\tMetadata:`); + console.log(`\t\t\tType Url: ${response[i].metadata.typeUrl}`); + console.log(`\t\tDone: ${response[i].done}`); + + if (response[i].response) { + console.log(`\t\tResponse:`); + console.log(`\t\t\tType Url: ${response[i].response.typeUrl}`); + } + + if (response[i].error) { + console.log(`\t\tResponse:`); + console.log(`\t\t\tError code: ${response[i].error.code}`); + console.log(`\t\t\tError message: ${response[i].error.message}`); + } + } + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_list_operations_status] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/list-table-specs.v1beta1.js b/automl/tables/list-table-specs.v1beta1.js new file mode 100644 index 0000000000..0c2920eca6 --- /dev/null +++ b/automl/tables/list-table-specs.v1beta1.js @@ -0,0 +1,67 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + datasetId = 'YOUR_DATASET_ID', + filter = 'FILTER_EXPRESSION' +) { + // [START automl_tables_list_table_specs] + const automl = require(`@google-cloud/automl`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to list all table specs in datasets. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const datasetId = '[DATASET_ID]' e.g., "TBL2246891593778855936"; + // const filter = '[FILTER_EXPRESSIONS]' e.g., "tablesDatasetMetadata:*"; + + // Get the full path of the dataset. + const datasetFullId = client.datasetPath(projectId, computeRegion, datasetId); + + // List all the table specs in datasets available in the region + // by applying filter. + client + .listTableSpecs({parent: datasetFullId, filter: filter}) + .then(responses => { + const table = responses[0]; + + // Display the table information. + console.log(`List of table specs: `); + for (let i = 0; i < table.length; i++) { + console.log(`Table name: ${table[i].name}`); + console.log(`Table Id: ${table[i].name.split(`/`).pop(-1)}`); + console.log(`Table row count: ${table[i].rowCount}`); + console.log(`Table column count: ${table[i].columnCount}`); + + console.log(`Table input config:`); + if (table[i].inputConfigs[0].source === `gcsSource`) { + console.log(`\t${table[i].inputConfigs[0].gcsSource.inputUris}`); + } else { + console.log(`\t${table[i].inputConfigs[0].bigquerySource.inputUri}`); + } + } + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_list_table_specs] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/predict-bq-source-bq-dest.v1beta1.js b/automl/tables/predict-bq-source-bq-dest.v1beta1.js new file mode 100644 index 0000000000..3cee0a5fe9 --- /dev/null +++ b/automl/tables/predict-bq-source-bq-dest.v1beta1.js @@ -0,0 +1,77 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + modelId = 'MODEL_ID', + inputUri = 'BIGQUERY_PATH', + outputUri = 'BIGQUERY_DIRECTORY' +) { + // [START automl_tables_predict_using_bq_source_and_bq_dest] + const automl = require(`@google-cloud/automl`); + + // Create client for prediction service. + const client = new automl.v1beta1.PredictionServiceClient(); + + /** + * Demonstrates using the AutoML client to request prediction from + * automl tables using bigQuery. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "TBL4704590352927948800"; + // const inputUri = '[BIGQUERY_PATH]' + // e.g., "bq://..", + // `The Big Query URI containing the inputs`; + // const outputUri = '[BIGQUERY_PATH]' e.g., "bq://", + // `The destination Big Query URI for storing outputs`; + + // Get the full path of the model. + const modelFullId = client.modelPath(projectId, computeRegion, modelId); + + // Get the Big Query input URI. + const inputConfig = { + bigquerySource: { + inputUri: inputUri, + }, + }; + + // Get the Big Query output URI. + const outputConfig = { + bigqueryDestination: { + outputUri: outputUri, + }, + }; + + // Get the latest state of long-running operation. + client + .batchPredict({ + name: modelFullId, + inputConfig: inputConfig, + outputConfig: outputConfig, + }) + .then(responses => { + const operation = responses[1]; + console.log(`Operation name: ${operation.name}`); + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_predict_using_bq_source_and_bq_dest] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/predict-bq-source-gcs-dest.v1beta1.js b/automl/tables/predict-bq-source-gcs-dest.v1beta1.js new file mode 100644 index 0000000000..93ebbc3f26 --- /dev/null +++ b/automl/tables/predict-bq-source-gcs-dest.v1beta1.js @@ -0,0 +1,78 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + modelId = 'MODEL_ID', + inputUri = 'BIGQUERY_PATH', + outputUriPrefix = 'GCS_DIRECTORY' +) { + // [START automl_tables_predict_using_bq_source_and_gcs_dest] + const automl = require(`@google-cloud/automl`); + + // Create client for prediction service. + const client = new automl.v1beta1.PredictionServiceClient(); + + /** + * Demonstrates using the AutoML client to request prediction from + * automl tables using bigQuery. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "TBL4704590352927948800"; + // const inputUri = '[BIGQUERY_PATH]' + // e.g., "bq://..", + // `The Big Query URI containing the inputs`; + // const outputUriPrefix = '[GCS_PATH]' + // e.g., "gs:///", + // `The destination Google Cloud Storage URI for storing outputs`; + + // Get the full path of the model. + const modelFullId = client.modelPath(projectId, computeRegion, modelId); + + // Get the Big Query input URI. + const inputConfig = { + bigquerySource: { + inputUri: inputUri, + }, + }; + + // Get the Google Cloud Storage output URI. + const outputConfig = { + gcsDestination: { + outputUriPrefix: outputUriPrefix, + }, + }; + + // Get the latest state of long-running operation. + client + .batchPredict({ + name: modelFullId, + inputConfig: inputConfig, + outputConfig: outputConfig, + }) + .then(responses => { + const operation = responses[1]; + console.log(`Operation name: ${operation.name}`); + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_predict_using_bq_source_and_gcs_dest] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/predict-gcs-source-bq-dest.v1beta1.js b/automl/tables/predict-gcs-source-bq-dest.v1beta1.js new file mode 100644 index 0000000000..3928507766 --- /dev/null +++ b/automl/tables/predict-gcs-source-bq-dest.v1beta1.js @@ -0,0 +1,77 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + modelId = 'MODEL_ID', + inputUri = 'GCS_PATH', + outputUri = 'BIGQUERY_DIRECTORY' +) { + // [START automl_tables_predict_using_gcs_source_and_bq_dest] + const automl = require(`@google-cloud/automl`); + + // Create client for prediction service. + const client = new automl.v1beta1.PredictionServiceClient(); + + /** + * Demonstrates using the AutoML client to request prediction from + * automl tables using GCS + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "TBL4704590352927948800"; + // const inputUri = '[GCS_PATH]' e.g., "gs:///", + // `The Google Cloud Storage URI containing the inputs`; + // const outputUri = '[BIGQUERY_PATH]' e.g., "bq://", + // `The destination Big Query URI for storing outputs`; + + // Get the full path of the model. + const modelFullId = client.modelPath(projectId, computeRegion, modelId); + + // Get the multiple Google Cloud Storage input URIs. + const inputUris = inputUri.split(`,`); + const inputConfig = { + gcsSource: { + inputUris: inputUris, + }, + }; + + // Get the Big Query output URIs. + const outputConfig = { + bigqueryDestination: { + outputUri: outputUri, + }, + }; + + // Get the latest state of long-running operation. + client + .batchPredict({ + name: modelFullId, + inputConfig: inputConfig, + outputConfig: outputConfig, + }) + .then(responses => { + const operation = responses[1]; + console.log(`Operation name: ${operation.name}`); + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_predict_using_gcs_source_and_bq_dest] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/predict-gcs-source-gcs-dest.v1beta1.js b/automl/tables/predict-gcs-source-gcs-dest.v1beta1.js new file mode 100644 index 0000000000..24a926fdc6 --- /dev/null +++ b/automl/tables/predict-gcs-source-gcs-dest.v1beta1.js @@ -0,0 +1,78 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + modelId = 'MODEL_ID', + inputUri = 'GCS_PATH', + outputUriPrefix = 'GCS_DIRECTORY' +) { + // [START automl_tables_predict_using_gcs_source_and_gcs_dest] + const automl = require(`@google-cloud/automl`); + + // Create client for prediction service. + const client = new automl.v1beta1.PredictionServiceClient(); + + /** + * Demonstrates using the AutoML client to request prediction from + * automl tables using GCS. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "TBL4704590352927948800"; + // const inputUri = '[GCS_PATH]' e.g., "gs:///", + // `The Google Cloud Storage URI containing the inputs`; + // const outputUriPrefix = '[GCS_PATH]' + // e.g., "gs:///", + // `The destination Google Cloud Storage URI for storing outputs`; + + // Get the full path of the model. + const modelFullId = client.modelPath(projectId, computeRegion, modelId); + + // Get the multiple Google Cloud Storage input URIs. + const inputUris = inputUri.split(`,`); + const inputConfig = { + gcsSource: { + inputUris: inputUris, + }, + }; + + // Get the Google Cloud Storage output URI. + const outputConfig = { + gcsDestination: { + outputUriPrefix: outputUriPrefix, + }, + }; + + // Get the latest state of long-running operation. + client + .batchPredict({ + name: modelFullId, + inputConfig: inputConfig, + outputConfig: outputConfig, + }) + .then(responses => { + const operation = responses[1]; + console.log(`Operation name: ${operation.name}`); + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_predict_using_gcs_source_and_gcs_dest] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/predict.v1beta1.js b/automl/tables/predict.v1beta1.js new file mode 100644 index 0000000000..53b2e0982e --- /dev/null +++ b/automl/tables/predict.v1beta1.js @@ -0,0 +1,78 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + modelId = 'MODEL_ID', + filePath = 'FILE_PATH' +) { + // [START automl_tables_predict] + const automl = require(`@google-cloud/automl`); + const fs = require(`fs`); + + // Create client for prediction service. + const client = new automl.v1beta1.PredictionServiceClient(); + + /** + * Demonstrates using the AutoML client to request prediction from + * automl tables using csv. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "TBL4704590352927948800"; + // const filePath = '[FILE_PATH]' + // e.g., "/", `local csv file path`; + + // Get the full path of the model. + const modelFullId = client.modelPath(projectId, computeRegion, modelId); + + // Read the csv file content for prediction. + const stream = fs.createReadStream(filePath).on(`data`, function(data) { + const values = []; + for (const val of data) { + values.push({string_value: val}); + } + + // Set the payload by giving the row values. + const payload = { + row: { + values: values, + }, + }; + + // Params is additional domain-specific parameters. + // Currently there is no additional parameters supported. + client + .predict({name: modelFullId, payload: payload, params: {}}) + .then(responses => { + console.log(responses); + console.log(`Prediction results:`); + + for (const result of responses[0].payload) { + console.log(`Predicted class name: ${result.displayName}`); + console.log(`Predicted class score: ${result.classification.score}`); + } + }) + .catch(err => { + console.error(err); + }); + }); + stream.read(); + // [END automl_tables_predict] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/undeploy-model.v1beta1.js b/automl/tables/undeploy-model.v1beta1.js new file mode 100644 index 0000000000..b9243f5715 --- /dev/null +++ b/automl/tables/undeploy-model.v1beta1.js @@ -0,0 +1,53 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + modelId = 'MODEL_ID' +) { + // [START automl_tables_undeploy_model] + const automl = require(`@google-cloud/automl`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to undelpoy model. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "TBL4704590352927948800"; + + // Get the full path of the model. + const modelFullId = client.modelPath(projectId, computeRegion, modelId); + + // Undeploy a model with the undeploy model request. + client + .undeployModel({name: modelFullId}) + .then(responses => { + const response = responses[0]; + console.log(`Undeployment Details:`); + console.log(`\tName: ${response.name}`); + console.log(`\tMetadata:`); + console.log(`\t\tType Url: ${response.metadata.typeUrl}`); + console.log(`\tDone: ${response.done}`); + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_undeploy_model] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/update-column-spec.v1beta1.js b/automl/tables/update-column-spec.v1beta1.js new file mode 100644 index 0000000000..895bd38aa9 --- /dev/null +++ b/automl/tables/update-column-spec.v1beta1.js @@ -0,0 +1,82 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + datasetId = 'YOUR_DATASET_ID', + tableId = 'TABLE_ID', + columnId = 'COLUMN_ID', + dataTypeCode = 'DATA_TYPE_CODE' +) { + // [START automl_tables_update_column_spec] + const automl = require(`@google-cloud/automl`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to update a column by ID. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const datasetId = '[DATASET_ID]' e.g., "TBL2246891593778855936"; + // const tableId = '[TABLE_ID]' e.g., "1991013247762825216"`; + // const columnId = '[COLUMN_ID]' e.g., "773141392279994368"; + // const dataTypeCode = '[DATA_TYPE_CODE]' e.g., "FLOAT64"; + + // Get the full path of the column. + const columnSpecId = client.columnSpecPath( + projectId, + computeRegion, + datasetId, + tableId, + columnId + ); + + // Set typecode of column to be changed. + const typeCode = {typeCode: dataTypeCode.toUpperCase()}; + + // Update the name and datatype value of the column spec. + const columnSpec = { + name: columnSpecId, + dataType: typeCode, + }; + + // Add the update mask to particular field. + const fieldMask = `dataType`; + const updateMask = {path: fieldMask}; + + // Update the column spec. + client + .updateColumnSpec({columnSpec: columnSpec, updatemask: updateMask}) + .then(responses => { + const column = responses[0]; + + // Display the column spec information. + console.log(`Column name: ${column.name}`); + console.log(`Column Id: ${column.name.split(`/`).pop(-1)}`); + console.log(`Column display name: ${column.displayName}`); + console.log(`Column datatype: ${column.dataType.typeCode}`); + console.log( + `Column distinct value count: ${column.dataStats.distinctValueCount}` + ); + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_update_column_spec] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/update-dataset.v1beta1.js b/automl/tables/update-dataset.v1beta1.js new file mode 100644 index 0000000000..874fe9f4cd --- /dev/null +++ b/automl/tables/update-dataset.v1beta1.js @@ -0,0 +1,67 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + datasetId = 'YOUR_DATASET_ID', + displayName = 'DISPLAY_NAME' +) { + // [START automl_tables_update_dataset] + const automl = require(`@google-cloud/automl`); + + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to update a dataset by ID. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const datasetId = '[DATASET_ID]' e.g., "TBL2246891593778855936"; + // const displayName = '[DISPLAY_NAME]' e.g., "myUpdateDataset"; + + // Get the full path of the dataset. + const datasetFullId = client.datasetPath(projectId, computeRegion, datasetId); + + // Update the dataset display name. + const dataset = { + name: datasetFullId, + displayName: displayName, + }; + + // Add the update mask to particular field. + const fieldMask = `displayName`; + const updateMask = {path: fieldMask}; + + // Update the information about a given dataset. + client + .updateDataset({dataset: dataset, updatemask: updateMask}) + .then(responses => { + const dataset = responses[0]; + + // Display the dataset information. + console.log(`Dataset name: ${dataset.name}`); + console.log(`Dataset Id: ${dataset.name.split(`/`).pop(-1)}`); + console.log(`Updated dataset display name: ${dataset.displayName}`); + console.log(`Dataset example count: ${dataset.exampleCount}`); + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_update_dataset] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/test/automlLanguageSentimentPrediction.v1beta1.js b/automl/test/automlLanguageSentimentPrediction.v1beta1.test.js similarity index 100% rename from automl/test/automlLanguageSentimentPrediction.v1beta1.js rename to automl/test/automlLanguageSentimentPrediction.v1beta1.test.js diff --git a/automl/test/automlTablesDataset.v1beta1.test.js b/automl/test/automlTablesDataset.v1beta1.test.js new file mode 100644 index 0000000000..fb15dfc555 --- /dev/null +++ b/automl/test/automlTablesDataset.v1beta1.test.js @@ -0,0 +1,141 @@ +/** + * Copyright 2019, 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. + */ + +'use strict'; + +const {assert} = require('chai'); +const execa = require('execa'); + +/** Tests for AutoML Tables "Dataset API" sample. */ + +const cmdDataset = 'node automlTablesDataset.js'; + +// TODO(developer): Before running the test cases, set the environment variables +// PROJECT_ID, REGION_NAME and change the values of datasetId, +// bigQueryDatasetId, importDataCsv, outputGcsUri, outputGcsUri and +// outputBigQueryUri. +//const projectId = process.env.PROJECT_ID; +//const computeRegion = process.env.REGION_NAME; +const datasetName = 'test_table_dataset'; +const filter = 'tablesDatasetMetadata:*'; +const datasetId = 'TBL2246891593778855936'; +const bigQueryDatasetId = 'TBL5314616996204118016'; +const importDataCsv = 'gs://automl-tables/input/train.csv'; +const updateDatasetDisplayName = 'test_table_dataset_01'; +const dataTypeCode = 'CATEGORY'; +const outputGcsUri = 'gs://automl-tables/export-data/'; +const outputBigQueryUri = 'bq://automl-tables-bg-output'; + +const exec = async cmd => (await execa.shell(cmd)).stdout; + +describe('Tables DatasetAPI', () => { + it.skip(`should create, import, update and delete a dataset`, async () => { + // Create dataset + let output = await exec(`${cmdDataset} create-dataset "${datasetName}"`); + const parsedOut = output.split('\n'); + const outputDatasetId = parsedOut[1].split(':')[1].trim(); + assert.match(output, /Dataset display name:/); + + // Import data + output = await exec( + `${cmdDataset} import-data "${outputDatasetId}" "${importDataCsv}"` + ); + assert.match(output, /Processing import.../); + + // Update dataset + output = await exec( + `${cmdDataset} update-dataset "${outputDatasetId}"` + + ` "${updateDatasetDisplayName}"` + ); + assert.match(output, /Dataset Id:/); + + // Delete dataset + output = await exec(`${cmdDataset} delete-dataset "${outputDatasetId}"`); + assert.match(output, /Dataset delete details:/); + }); + + it.skip(`should list datasets`, async () => { + // List dataset + const output = await exec(`${cmdDataset} list-datasets "${filter}"`); + assert.match(output, /Dataset Id:/); + }); + + it.skip(`should get preexisting dataset`, async () => { + // Get dataset + const output = await exec(`${cmdDataset} get-dataset "${datasetId}"`); + assert.match(output, /Dataset Id:/); + }); + + it.skip(`should get,list,update tablespec and columnspec`, async () => { + // List table + let output = await exec( + `${cmdDataset} list-table-specs` + ` "${datasetId}" "${filter}"` + ); + let parsedOut = output.split('\n'); + const outputTableId = parsedOut[1] + .split(':')[1] + .trim() + .split('/')[7]; + assert.match(output, /Table Id:/); + + // Get table + output = await exec( + `${cmdDataset} get-table-spec` + ` "${datasetId}" "${outputTableId}"` + ); + assert.match(output, /Table Id:/); + + // List column + output = await exec( + `${cmdDataset} list-column-specs` + ` "${datasetId}" "${outputTableId}"` + ); + parsedOut = output.split('\n'); + const outputColumnId = parsedOut[1] + .split(':')[1] + .trim() + .split('/')[9]; + assert.match(output, /Column Id:/); + + // Get column + output = await exec( + `${cmdDataset} get-column-spec` + + ` "${datasetId}" "${outputTableId}" "${outputColumnId}"` + ); + assert.match(output, /Column Id:/); + + // Update column + output = await exec( + `${cmdDataset} update-column-spec` + + ` "${datasetId}" "${outputTableId}" "${outputColumnId}" "${dataTypeCode}"` + ); + assert.match(output, /Column Id:/); + }); + + it.skip(`should export CSV dataset`, async () => { + // Export data to csv + const output = await exec( + `${cmdDataset} export-data-to-csv` + ` "${datasetId}" "${outputGcsUri}"` + ); + assert.match(output, /Processing export.../); + }); + + it.skip(`should export BigQuery dataset`, async () => { + // Export data to bigquery + const output = await exec( + `${cmdDataset} export-data-to-bigquery` + + ` "${bigQueryDatasetId}" "${outputBigQueryUri}"` + ); + assert.match(output, /Processing export.../); + }); +}); diff --git a/automl/test/automlTablesModel.v1beta1.test.js b/automl/test/automlTablesModel.v1beta1.test.js new file mode 100644 index 0000000000..aca08e6ab3 --- /dev/null +++ b/automl/test/automlTablesModel.v1beta1.test.js @@ -0,0 +1,130 @@ +/** + * Copyright 2019, 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. + */ + +'use strict'; + +const {assert} = require('chai'); +const execa = require('execa'); + +/** Tests for AutoML Tables "Model API" sample. */ + +const cmdModel = 'node automlTablesModel.js'; + +// TODO(developer): Before running the test cases, set the environment variables +// PROJECT_ID, REGION_NAME and change the values of datasetId, tableId, +// columnId, bqOutputUri, deployModelId and undeployModelId. +//const projectId = process.env.PROJECT_ID; +//const computeRegion = process.env.REGION_NAME; +const datasetId = 'TBL2246891593778855936'; +const testModelName = 'test_tables_model'; +const tableId = '2071233616125362176'; +const columnId = '773141392279994368'; +const trainBudget = '1000'; +const bqOutputUri = 'bq://automl-tables-bg-output'; +const deployModelId = 'TBL4704590352927948800'; +const undeployModelId = 'TBL4508824506625687552'; +const filter = 'tablesModelMetadata:*'; + +const exec = async cmd => (await execa.shell(cmd)).stdout; + +describe('Tables ModelAPI', () => { + it.skip(`should create a model`, async () => { + // Create model + let output = await exec( + `${cmdModel} create-model "${datasetId}" "${tableId}" "${columnId}"` + + ` "${testModelName}" "${trainBudget}"` + ); + const operationName = output + .split('\n')[0] + .split(':')[1] + .trim(); + assert.match(output, /Training started.../); + + output = await exec(`${cmdModel} get-operation-status "${operationName}"`); + assert.match(output, /Operation details:/); + }); + + it.skip(`should list models, get and delete a model. list, get, export and display + model evaluations from preexisting models`, async () => { + // List models + let output = await exec(`${cmdModel} list-models "${filter}"`); + const parsedOut = output.split('\n'); + const ouputModelId = parsedOut[3].split(':')[1].trim(); + assert.match(output, /List of models:/); + + // Get model + output = await exec(`${cmdModel} get-model "${ouputModelId}"`); + assert.match(output, /Model name:/); + + // List model evaluations + output = await exec(`${cmdModel} list-model-evaluations "${ouputModelId}"`); + const parsedModelEvaluation = output.split('\n'); + const modelEvaluationId = parsedModelEvaluation[2].split(':')[1].trim(); + assert.match(output, /Model evaluation Id:/); + + // Get model evaluation + output = await exec( + `${cmdModel} get-model-evaluation "${ouputModelId}"` + + ` "${modelEvaluationId}"` + ); + assert.match(output, /Model evaluation Id:/); + + // Display evaluation + output = await exec(`${cmdModel} display-evaluation "${ouputModelId}"`); + assert.match(output, /Model Evaluation ID:/); + + // Export evaluated examples + output = await exec( + `${cmdModel} export-evaluated-examples "${ouputModelId}"` + + ` "${bqOutputUri}"` + ); + assert.match(output, /Operation name:/); + + // Delete model + output = await exec(`${cmdModel} delete-model "${ouputModelId}"`); + assert.match(output, /Model delete details:/); + }); + + it.skip(`should deploy the model`, async () => { + // Deploy model + const output = await exec(`${cmdModel} deploy-model "${deployModelId}"`); + assert.match(output, /Name:/); + }); + + it.skip(`should undeploy the model`, async () => { + // Undeploy model + const output = await exec( + `${cmdModel} undeploy-model "${undeployModelId}"` + ); + assert.match(output, /Name:/); + }); + + it.skip(`should list and get operation status`, async () => { + // List operations status + let output = await exec(`${cmdModel} list-operations-status `); + const operationFullId = output + .split('\n')[3] + .split(':')[1] + .trim(); + assert.match(output, /Operation details:/); + + // Get operation status + // Poll operation status, here confirming that operation is not complete yet + output = await exec( + `${cmdModel} get-operation-status "${operationFullId}"` + ); + assert.match(output, /Operation details:/); + }); +}); diff --git a/automl/test/automlTablesPredict.v1beta1.test.js b/automl/test/automlTablesPredict.v1beta1.test.js new file mode 100644 index 0000000000..8cf0373e14 --- /dev/null +++ b/automl/test/automlTablesPredict.v1beta1.test.js @@ -0,0 +1,88 @@ +/** + * Copyright 2019, 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. + */ + +'use strict'; + +const {assert} = require('chai'); +const execa = require('execa'); + +/** Tests for AutoML Tables "Prediction API" sample. */ + +const cmdPredict = 'node automlTablesPrediction.js'; + +// // TODO(developer): Before running the test cases, set the environment +// variables PROJECT_ID, REGION_NAME and +// change modelId, gcsInputUri, gcsOutputUriPrefix, bqInputUri and +// bqOutputUriPrefix. +//const projectId = process.env.PROJECT_ID; +//const computeRegion = process.env.REGION_NAME; +const modelId = 'TBL3613734080685801472'; +const filePath = './resource/predictTest.csv'; +const gcsInputUri = 'gs://automl-tables/input/test.csv'; +const gcsOutputUriPrefix = 'gs://automl-tables/output'; +const bqInputUri = 'bq://automl-tables-bg-input'; +const bqOutputUriPrefix = 'bq://automl-tables-bg-output'; + +const exec = async cmd => (await execa.shell(cmd)).stdout; + +describe('Tables PredictionAPI', () => { + it.skip(`should perform single prediction`, async () => { + // Run single prediction on predictTest.csv in resource folder + const output = await exec( + `${cmdPredict} predict "${modelId}" "${filePath}"` + ); + assert.match(output, /Prediction results:/); + }); + + it.skip(`should perform batch prediction using GCS as source and + GCS as destination`, async () => { + // Run batch prediction using GCS as source and GCS as destination + const output = await exec( + `${cmdPredict} predict-using-gcs-source-and-gcs-dest "${modelId}"` + + ` "${gcsInputUri}" "${gcsOutputUriPrefix}"` + ); + assert.match(output, /Operation name:/); + }); + + it.skip(`should perform batch prediction using BQ as source and + GCS as destination`, async () => { + // Run batch prediction using BQ as source and GCS as destination + const output = await exec( + `${cmdPredict} predict-using-bq-source-and-gcs-dest "${modelId}"` + + ` "${bqInputUri}" "${gcsOutputUriPrefix}"` + ); + assert.match(output, /Operation name:/); + }); + + it.skip(`should perform batch prediction using GCS as source and + BQ as destination`, async () => { + // Run batch prediction using GCS as source and BQ as destination + const output = await exec( + `${cmdPredict} predict-using-gcs-source-and-bq-dest "${modelId}"` + + ` "${gcsInputUri}" "${bqOutputUriPrefix}"` + ); + assert.match(output, /Operation name:/); + }); + + it.skip(`should perform batch prediction using BQ as source and + BQ as destination`, async () => { + // Run batch prediction using BQ as source and BQ as destination + const output = await exec( + `${cmdPredict} predict-using-bq-source-and-bq-dest "${modelId}"` + + ` "${bqInputUri}" "${bqOutputUriPrefix}"` + ); + assert.match(output, /Operation name:/); + }); +});