|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +import sys |
| 4 | +import unittest |
| 5 | +import numpy |
| 6 | +import pandas |
| 7 | +from pyspark.ml.feature import CountVectorizer, CountVectorizerModel |
| 8 | +from onnx.defs import onnx_opset_version |
| 9 | +from onnxconverter_common.onnx_ex import DEFAULT_OPSET_NUMBER |
| 10 | +from onnxmltools import convert_sparkml |
| 11 | +from onnxmltools.convert.common.data_types import StringTensorType |
| 12 | +from tests.sparkml.sparkml_test_utils import save_data_models, run_onnx_model, compare_results |
| 13 | +from tests.sparkml import SparkMlTestCase |
| 14 | + |
| 15 | +TARGET_OPSET = min(DEFAULT_OPSET_NUMBER, onnx_opset_version()) |
| 16 | + |
| 17 | +class TestSparkmlCountVectorizer(SparkMlTestCase): |
| 18 | + |
| 19 | + @unittest.skipIf(sys.version_info < (3, 8), |
| 20 | + reason="pickle fails on python 3.7") |
| 21 | + def test_count_vectorizer_default(self): |
| 22 | + data = self.spark.createDataFrame([ |
| 23 | + ("A B C".split(" "), ), |
| 24 | + ("A B B C A".split(" "), ), |
| 25 | + ], ["text"]) |
| 26 | + count_vec = CountVectorizer(inputCol="text", outputCol="result", minTF=1.0, binary=False) |
| 27 | + model: CountVectorizerModel = count_vec.fit(data) |
| 28 | + result = model.transform(data) |
| 29 | + |
| 30 | + model_onnx = convert_sparkml(model, 'Sparkml CountVectorizer', [('text', StringTensorType([None, None]))], target_opset=TARGET_OPSET) |
| 31 | + self.assertTrue(model_onnx is not None) |
| 32 | + |
| 33 | + data_pd = data.toPandas() |
| 34 | + data_np = { |
| 35 | + "text": data_pd.text.apply(lambda x: pandas.Series(x)).values.astype(str), |
| 36 | + } |
| 37 | + |
| 38 | + expected = { |
| 39 | + "prediction_result": numpy.asarray(result.toPandas().result.apply(lambda x: pandas.Series(x.toArray())).values.astype(numpy.float32)), |
| 40 | + } |
| 41 | + |
| 42 | + paths = save_data_models(data_np, expected, model, model_onnx, basename="SparkmlCountVectorizerModel_Default") |
| 43 | + onnx_model_path = paths[-1] |
| 44 | + |
| 45 | + output_names = ['result'] |
| 46 | + output, output_shapes = run_onnx_model(output_names, data_np, onnx_model_path) |
| 47 | + actual_output = dict(zip(output_names, output)) |
| 48 | + |
| 49 | + assert output_shapes[0] == [None, 3] |
| 50 | + compare_results(expected["prediction_result"], actual_output["result"], decimal=5) |
| 51 | + |
| 52 | + @unittest.skipIf(sys.version_info < (3, 8), |
| 53 | + reason="pickle fails on python 3.7") |
| 54 | + def test_count_vectorizer_binary(self): |
| 55 | + data = self.spark.createDataFrame([ |
| 56 | + ("A B C".split(" "), ), |
| 57 | + ("A B B C A".split(" "), ), |
| 58 | + ("B B B D".split(" "), ), |
| 59 | + ], ["text"]) |
| 60 | + count_vec = CountVectorizer(inputCol="text", outputCol="result", minTF=2.0, binary=True) |
| 61 | + model: CountVectorizerModel = count_vec.fit(data) |
| 62 | + result = model.transform(data) |
| 63 | + |
| 64 | + model_onnx = convert_sparkml(model, 'Sparkml CountVectorizer', [('text', StringTensorType([None, None]))], target_opset=TARGET_OPSET) |
| 65 | + self.assertTrue(model_onnx is not None) |
| 66 | + |
| 67 | + data_pd = data.toPandas() |
| 68 | + data_np = { |
| 69 | + "text": data_pd.text.apply(lambda x: pandas.Series(x)).values.astype(str), |
| 70 | + } |
| 71 | + |
| 72 | + expected = { |
| 73 | + "prediction_result": numpy.asarray(result.toPandas().result.apply(lambda x: pandas.Series(x.toArray())).values.astype(numpy.float32)), |
| 74 | + } |
| 75 | + |
| 76 | + paths = save_data_models(data_np, expected, model, model_onnx, basename="SparkmlCountVectorizerModel_Binary") |
| 77 | + onnx_model_path = paths[-1] |
| 78 | + |
| 79 | + output_names = ['result'] |
| 80 | + output, output_shapes = run_onnx_model(output_names, data_np, onnx_model_path) |
| 81 | + actual_output = dict(zip(output_names, output)) |
| 82 | + |
| 83 | + assert output_shapes[0] == [None, 4] |
| 84 | + compare_results(expected["prediction_result"], actual_output["result"], decimal=5) |
| 85 | + |
| 86 | +if __name__ == "__main__": |
| 87 | + unittest.main() |
0 commit comments