|
| 1 | +# Copyright 2020 The SQLFlow Authors. All rights reserved. |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | + |
| 14 | +from runtime import db |
| 15 | +from runtime.diagnostics import SQLFlowDiagnostic |
| 16 | +from runtime.model import EstimatorType |
| 17 | +from runtime.pai import table_ops |
| 18 | + |
| 19 | + |
| 20 | +def create_predict_result_table(datasource, select, result_table, label_column, |
| 21 | + train_label_column, model_type): |
| 22 | + """Create predict result table with given name and label column |
| 23 | +
|
| 24 | + Args: |
| 25 | + datasource: current datasource |
| 26 | + select: sql statement to get prediction data set |
| 27 | + result_table: the table name to save result |
| 28 | + label_column: name of the label column, if not exist in select |
| 29 | + result, we will add a int column in the result table |
| 30 | + train_label_column: name of the label column when training |
| 31 | + model_type: type of model defined in runtime.model.oss |
| 32 | + """ |
| 33 | + conn = db.connect_with_data_source(datasource) |
| 34 | + conn.execute("DROP TABLE IF EXISTS %s" % result_table) |
| 35 | + # PAI ml will create result table itself |
| 36 | + if model_type == EstimatorType.PAIML: |
| 37 | + return |
| 38 | + |
| 39 | + create_table_sql = "CREATE TABLE %s AS SELECT * FROM %s LIMIT 0" % ( |
| 40 | + result_table, select) |
| 41 | + conn.execute(create_table_sql) |
| 42 | + |
| 43 | + # if label is not in data table, add a int column for it |
| 44 | + schema = db.get_table_schema(conn, result_table) |
| 45 | + col_type = "INT" |
| 46 | + for (name, ctype) in schema: |
| 47 | + if name == train_label_column or name == label_column: |
| 48 | + col_type = ctype |
| 49 | + break |
| 50 | + col_names = [col[0] for col in schema] |
| 51 | + if label_column not in col_names: |
| 52 | + conn.execute( |
| 53 | + conn, "ALTER TABLE %s ADD %s %s" % |
| 54 | + (result_table, label_column, col_type)) |
| 55 | + if train_label_column != label_column and train_label_column in col_names: |
| 56 | + conn.execute( |
| 57 | + conn, "ALTER TABLE %s DROP COLUMN %s" % |
| 58 | + (result_table, train_label_column)) |
| 59 | + |
| 60 | + |
| 61 | +# (TODO: lhw) This function is a common tool for prediction |
| 62 | +# on all platforms, we need to move it to a new file |
| 63 | +def create_explain_result_table(datasource, data_table, result_table, |
| 64 | + model_type, estimator, label_column): |
| 65 | + """Create explain result table from given datasource |
| 66 | +
|
| 67 | + Args: |
| 68 | + datasource: current datasource |
| 69 | + data_table: input data table name |
| 70 | + result_table: table name to store the result |
| 71 | + model_type: type of the model to use |
| 72 | + estimator: estimator class if the model is TensorFlow estimator |
| 73 | + label_column: column name of the predict label |
| 74 | + """ |
| 75 | + conn = db.connect_with_data_source(datasource) |
| 76 | + drop_stmt = "DROP TABLE IF EXISTS %s" % result_table |
| 77 | + conn.execute(drop_stmt) |
| 78 | + |
| 79 | + create_stmt = "" |
| 80 | + if model_type == EstimatorType.PAIML: |
| 81 | + return |
| 82 | + elif model_type == EstimatorType.TENSORFLOW: |
| 83 | + if estimator.startswith("BoostedTrees"): |
| 84 | + column_def = "" |
| 85 | + if conn.driver == "mysql": |
| 86 | + column_def = "(feature VARCHAR(255), dfc FLOAT, gain FLOAT)" |
| 87 | + else: |
| 88 | + # Hive & MaxCompute |
| 89 | + column_def = "(feature STRING, dfc STRING, gain STRING)" |
| 90 | + create_stmt = "CREATE TABLE IF NOT EXISTS %s %s;" % (result_table, |
| 91 | + column_def) |
| 92 | + else: |
| 93 | + if not label_column: |
| 94 | + raise SQLFlowDiagnostic( |
| 95 | + "need to specify WITH label_col=lable_col_name " |
| 96 | + "when explaining deep models") |
| 97 | + create_stmt = get_create_shap_result_sql(conn, data_table, |
| 98 | + result_table, |
| 99 | + label_column) |
| 100 | + elif model_type == EstimatorType.XGBOOST: |
| 101 | + if not label_column: |
| 102 | + raise SQLFlowDiagnostic( |
| 103 | + "need to specify WITH label_col=lable_col_name " |
| 104 | + "when explaining xgboost models") |
| 105 | + create_stmt = get_create_shap_result_sql(conn, data_table, |
| 106 | + result_table, label_column) |
| 107 | + else: |
| 108 | + raise SQLFlowDiagnostic( |
| 109 | + "not supported modelType %d for creating Explain result table" % |
| 110 | + model_type) |
| 111 | + |
| 112 | + if not conn.execute(create_stmt): |
| 113 | + raise SQLFlowDiagnostic("Can't create explain result table") |
| 114 | + |
| 115 | + |
| 116 | +def get_create_shap_result_sql(conn, data_table, result_table, label_column): |
| 117 | + """Get a sql statement which create a result table for SHAP |
| 118 | +
|
| 119 | + Args: |
| 120 | + conn: a database connection |
| 121 | + data_table: table name to read data from |
| 122 | + result_table: result table name |
| 123 | + label_column: column name of label |
| 124 | +
|
| 125 | + Returns: |
| 126 | + a sql statement to create SHAP result table |
| 127 | + """ |
| 128 | + schema = db.get_table_schema(conn, data_table) |
| 129 | + fields = ["%s STRING" % f[0] for f in schema if f[0] != label_column] |
| 130 | + return "CREATE TABLE IF NOT EXISTS %s (%s)" % (result_table, |
| 131 | + ",".join(fields)) |
| 132 | + |
| 133 | + |
| 134 | +def create_evaluate_result_table(datasource, result_table, metrics): |
| 135 | + """Create a table to hold the evaluation result |
| 136 | +
|
| 137 | + Args: |
| 138 | + datasource: current datasource |
| 139 | + result_table: the table name to save result |
| 140 | + metrics: list of evaluation metrics names |
| 141 | + """ |
| 142 | + table_ops.drop_tables([result_table], datasource) |
| 143 | + # Always add loss |
| 144 | + ext_metrics = ["loss"] |
| 145 | + if isinstance(metrics, list): |
| 146 | + ext_metrics.extend(metrics) |
| 147 | + fields = ["%s STRING" % m for m in ext_metrics] |
| 148 | + sql = "CREATE TABLE IF NOT EXISTS %s (%s);" % (result_table, |
| 149 | + ",".join(fields)) |
| 150 | + conn = db.connect_with_data_source(datasource) |
| 151 | + conn.execute(sql) |
0 commit comments