blob: 39d5e233239fb6c79470c65aa0205d4542690399 [file]
/* ----------------------------------------------------------------------- *//**
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*
*//* ----------------------------------------------------------------------- */
/**
* @brief Compares the madlib prediction output with pypmml prediction output.
* Assumes that the caller has already run madlib's predict function on the test table
* Creates a dictionary of the madlib results by querying the madlib_predict_table
* Calls the pmml function on the trained madlib table
* Loops through all the rows of the test table and FOR EACH ROW:
** Gets the madlib prediction from the dict created above
** Runs pypmml predict
** Compares madlib and pypmml results
** Throws an error if the values don't match
* @param test_table The test table which was used for madlib prediction. This table will be used for pmml prediction
* @param madlib_train_table The trained model table
* @param madlib_predict_table The madlib prediction table
* @param id_col id column in the test table used for uniquely identifying the rows for comparison
* @param madlib_prediction_metric Name of the column in madlib prediction table that contains the value to compare
* @param pypmml_prediction_metric Name of the key in pmml prediction dict that contains the value to compare
* @param grouping_col grouping col used during training
* @param name_spec name_spec to be passed to the madlib pmml function
* @returns
*
*/
CREATE OR REPLACE FUNCTION test_pmml_output(test_table TEXT, madlib_train_table TEXT, madlib_predict_table TEXT,
id_col TEXT, madlib_prediction_metric TEXT, pypmml_prediction_metric TEXT, grouping_col TEXT, name_spec TEXT) returns VOID as $$
# This function returns a key so that we can compare the results from the madlib prediction table with the pmml
# result that gets run on the test table
# The key of this dict is either just the 'id' or a combination of 'id' and all the grouping cold
def get_unique_key(input):
unique_key = [str(input[id_col])]
if grouping_col != '':
cols = grouping_col.split(',')
for col in cols:
unique_key.append(str(input[col]))
return ','.join(unique_key)
def are_results_different(madlib_result, pypmml_result):
from math import isnan, isinf
if isinstance(madlib_result, str):
return str(pypmml_result) != str(madlib_result)
elif isinstance(madlib_result, float) or isinstance(madlib_result, int):
if isinf(float(madlib_result)) or isnan(float(madlib_result)):
return True
if isinf(float(pypmml_result)) or isnan(float(pypmml_result)):
return True
tol = 1e-6
return abs(pypmml_result - madlib_result) > tol
return True
from pypmml import Model
madlib_predict_output_table = plpy.execute("SELECT * from {}".format(madlib_predict_table))
madlib_predict_output = {}
for madlib_pred in madlib_predict_output_table:
madlib_predict_output[get_unique_key(madlib_pred)] = madlib_pred[madlib_prediction_metric]
#get madlib pmml output string
if name_spec == '':
pmml_query = "SELECT pmml('{}')".format(madlib_train_table)
elif 'array' in name_spec.lower():
pmml_query = "SELECT pmml('{}',{})".format(madlib_train_table, name_spec)
else:
pmml_query = "SELECT pmml('{}','{}')".format(madlib_train_table, name_spec)
madlib_pmml_str = plpy.execute(pmml_query)[0]["pmml"]
# load pypmml model using madlib pmml string
pypmml_model = Model.fromString(madlib_pmml_str)
# load data and run pypmml predict
test_data = plpy.execute("SELECT * from {}".format(test_table))
for data in test_data:
madlib_result = madlib_predict_output[get_unique_key(data)]
pypmml_prediction = pypmml_model.predict(data)
if pypmml_prediction_metric not in pypmml_prediction:
plpy.error("Metric: '{}' does not exist in pypmml output: {}".format(pypmml_prediction_metric, pypmml_prediction))
pypmml_result = pypmml_prediction[pypmml_prediction_metric]
if are_results_different(madlib_result, pypmml_result):
plpy.info(madlib_pmml_str)
plpy.error("pmml comparison failed. input row: {}, metric to compare: {}, madlib result: {}, pypmml result: {}, detailed madlib output: {}, detailed pypmml output: {}".format(data, pypmml_prediction_metric, madlib_result, pypmml_result, madlib_predict_output, pypmml_prediction))
$$ language plpython3u;
CREATE OR REPLACE FUNCTION test_pmml_output(test_table TEXT, madlib_train_table TEXT, madlib_predict_table TEXT,
id_col TEXT, madlib_prediction_metric TEXT, pypmml_prediction_metric TEXT, grouping_col TEXT) returns VOID as $$
SELECT test_pmml_output(test_table, madlib_train_table, madlib_predict_table, id_col, madlib_prediction_metric, pypmml_prediction_metric, grouping_col, '')
$$ language sql;
CREATE OR REPLACE FUNCTION test_pmml_output(test_table TEXT, madlib_train_table TEXT, madlib_predict_table TEXT,
id_col TEXT, madlib_prediction_metric TEXT, pypmml_prediction_metric TEXT) returns VOID as $$
SELECT test_pmml_output(test_table, madlib_train_table, madlib_predict_table, id_col, madlib_prediction_metric, pypmml_prediction_metric, '', '')
$$ language sql;
CREATE OR REPLACE FUNCTION count_expected_names(madlib_train_table varchar, formula varchar, expected varchar[])
RETURNS bigint AS $$
SELECT count(distinct result.name)
FROM
(SELECT text(unnest(xpath('/n:PMML/n:DataDictionary/n:DataField/@name',
pmml_col, ARRAY[ARRAY['n', 'http://www.dmg.org/PMML-4_1']]))) AS name
FROM (SELECT CASE WHEN $2 is NULL THEN pmml(madlib_train_table)
ELSE pmml(madlib_train_table, $2)
END AS pmml_col) pmml_output
) result,
(SELECT unnest($3) AS name) expected
WHERE expected.name = result.name;
$$ LANGUAGE sql;
CREATE OR REPLACE FUNCTION count_expected_names(madlib_train_table varchar, name_spec varchar[], expected varchar[])
RETURNS bigint AS $$
SELECT count(distinct result.name)
FROM
(SELECT text(unnest(xpath('/n:PMML/n:DataDictionary/n:DataField/@name',
pmml_col, ARRAY[ARRAY['n', 'http://www.dmg.org/PMML-4_1']]))) AS name
FROM (SELECT CASE WHEN $2 is NULL THEN pmml(madlib_train_table)
ELSE pmml(madlib_train_table, $2)
END AS pmml_col) pmml_output
) result,
(SELECT unnest($3) AS name) expected
WHERE expected.name = result.name;
$$ LANGUAGE sql;