|
| 1 | +# Copyright 2019 Google Inc. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import absolute_import |
| 16 | +from __future__ import division |
| 17 | +from __future__ import print_function |
| 18 | + |
| 19 | +import sys |
| 20 | +import logging |
| 21 | +import argparse |
| 22 | + |
| 23 | +import tables_config |
| 24 | +import tables_client |
| 25 | +import tables_pipeline |
| 26 | + |
| 27 | + |
| 28 | +def parse_arguments(argv): |
| 29 | + """Parses command line arguments.""" |
| 30 | + |
| 31 | + parser = argparse.ArgumentParser(description='Args for Tables pipeline.') |
| 32 | + parser.add_argument( |
| 33 | + '--config_filename', |
| 34 | + required=True, |
| 35 | + type=str, |
| 36 | + help='The filepath for the YAML configuration file.') |
| 37 | + parser.add_argument( |
| 38 | + '--log_dir', |
| 39 | + required=False, |
| 40 | + type=str, |
| 41 | + help='The directory to generate a session log file in.') |
| 42 | + parser.add_argument( |
| 43 | + '--service_account_filename', |
| 44 | + required=False, |
| 45 | + type=str, |
| 46 | + help='The filepath for the json key for oath.') |
| 47 | + parser.add_argument( |
| 48 | + '--console_log_level', |
| 49 | + required=False, |
| 50 | + type=str, |
| 51 | + default=logging.WARN, |
| 52 | + help='Controls the log level for the console display.') |
| 53 | + parser.add_argument( |
| 54 | + '--file_log_level', |
| 55 | + required=False, |
| 56 | + type=str, |
| 57 | + default=logging.INFO, |
| 58 | + help='Controls the log level to write to file. Set to logging.DEBUG for' |
| 59 | + 'to write out the full AutoML service responses (very verbose).') |
| 60 | + args, _ = parser.parse_known_args(args=argv[1:]) |
| 61 | + |
| 62 | + # Parser for parameters to pass to TablesConfig |
| 63 | + param_parser = argparse.ArgumentParser(description='Args for config params.') |
| 64 | + |
| 65 | + # Resource parameters |
| 66 | + param_parser.add_argument( |
| 67 | + '--project', |
| 68 | + required=False, |
| 69 | + type=str, |
| 70 | + help='GCP project ID to run AutoML Tables on.') |
| 71 | + param_parser.add_argument( |
| 72 | + '--location', |
| 73 | + required=False, |
| 74 | + default='us-central1', |
| 75 | + type=str, |
| 76 | + help='GCP location to run AutoML Tables in.') |
| 77 | + |
| 78 | + # Runtime parameters |
| 79 | + param_parser.add_argument( |
| 80 | + '--build_dataset', |
| 81 | + action='store_const', |
| 82 | + const=True, |
| 83 | + help='Builds a new dataset, loads an old dataset otherwise.') |
| 84 | + param_parser.add_argument( |
| 85 | + '--build_model', |
| 86 | + action='store_const', |
| 87 | + const=True, |
| 88 | + help='Builds a new model, loads an old model otherwise.') |
| 89 | + param_parser.add_argument( |
| 90 | + '--make_prediction', |
| 91 | + action='store_const', |
| 92 | + const=True, |
| 93 | + help='Makes a batch prediction.') |
| 94 | + |
| 95 | + # Dataset parameters |
| 96 | + # Note that columns_dtype and columns_nullable must be set in YAML config. |
| 97 | + param_parser.add_argument( |
| 98 | + '--dataset_display_name', |
| 99 | + required=False, |
| 100 | + type=str, |
| 101 | + help='Name of the Tables Dataset (32 character max).') |
| 102 | + param_parser.add_argument( |
| 103 | + '--dataset_input_path', |
| 104 | + required=False, |
| 105 | + type=str, |
| 106 | + help=('Path to import the training data from, one of' |
| 107 | + 'bq://project.dataset.table or gs://path/to/csv')) |
| 108 | + param_parser.add_argument( |
| 109 | + '--label_column', |
| 110 | + required=False, |
| 111 | + type=str, |
| 112 | + help='Label to to train model on, for regression or classification.') |
| 113 | + param_parser.add_argument( |
| 114 | + '--split_column', |
| 115 | + required=False, |
| 116 | + type=str, |
| 117 | + help='Explicitly defines "TRAIN"/"VALIDATION"/"TEST" split.') |
| 118 | + param_parser.add_argument( |
| 119 | + '--weight_column', |
| 120 | + required=False, |
| 121 | + type=str, |
| 122 | + help='Weights loss and metrics.') |
| 123 | + param_parser.add_argument( |
| 124 | + '--time_column', |
| 125 | + required=False, |
| 126 | + type=str, |
| 127 | + help='Date/timestamp to automatically split data on.') |
| 128 | + |
| 129 | + # Model parameters |
| 130 | + # Note that ignore columns must be set in YAML config. |
| 131 | + param_parser.add_argument( |
| 132 | + '--model_display_name', |
| 133 | + required=False, |
| 134 | + type=str, |
| 135 | + help='Name of the Tables Model (32 character max).') |
| 136 | + param_parser.add_argument( |
| 137 | + '--train_hours', |
| 138 | + required=False, |
| 139 | + type=float, |
| 140 | + help='The number of hours to train the model for.') |
| 141 | + param_parser.add_argument( |
| 142 | + '--optimization_objective', |
| 143 | + required=False, |
| 144 | + type=str, |
| 145 | + help='Metric to optimize for in training.') |
| 146 | + |
| 147 | + # Predict parameters |
| 148 | + param_parser.add_argument( |
| 149 | + '--predict_input_path', |
| 150 | + required=False, |
| 151 | + type=str, |
| 152 | + help=('Path to import the batch prediction data from, one of' |
| 153 | + 'bq://project.dataset.table or gs://path/to/csv')) |
| 154 | + param_parser.add_argument( |
| 155 | + '--predict_output_path', |
| 156 | + required=False, |
| 157 | + type=str, |
| 158 | + help=('Path to export batch predictions to, one of' |
| 159 | + 'bq://project or gs://path')) |
| 160 | + params, _ = param_parser.parse_known_args(args=argv[1:]) |
| 161 | + return args, params |
| 162 | + |
| 163 | + |
| 164 | +def main(): |
| 165 | + args, params = parse_arguments(sys.argv) |
| 166 | + config = tables_config.TablesConfig(args.config_filename, vars(params)) |
| 167 | + client = tables_client.TablesClient(args.service_account_filename) |
| 168 | + pipeline = tables_pipeline.TablesPipeline( |
| 169 | + tables_config=config, |
| 170 | + tables_client=client, |
| 171 | + log_dir=args.log_dir, |
| 172 | + console_log_level=args.console_log_level, |
| 173 | + file_log_level=args.file_log_level) |
| 174 | + pipeline.run() |
| 175 | + |
| 176 | + |
| 177 | +if __name__ == '__main__': |
| 178 | + main() |
0 commit comments