From 19d24b2bf87455776f08ddb8615ff242012d465f Mon Sep 17 00:00:00 2001 From: Xavier Dupre Date: Tue, 23 Jan 2024 16:23:52 +0100 Subject: [PATCH] fix many tiny bugs Signed-off-by: Xavier Dupre --- .azure-pipelines/linux-conda-CI.yml | 2 +- docs/conf.py | 14 ++++----- docs/requirements.txt | 2 +- docs/tests/test_documentation_examples.py | 2 ++ docs/tutorial/plot_gbegin_dataframe.py | 29 ------------------- skl2onnx/algebra/type_helper.py | 2 +- tests/test_onnx_helper.py | 8 +++++ tests/test_sklearn_array_feature_extractor.py | 8 +++++ ...earn_calibrated_classifier_cv_converter.py | 14 +++++++++ 9 files changed, 41 insertions(+), 40 deletions(-) diff --git a/.azure-pipelines/linux-conda-CI.yml b/.azure-pipelines/linux-conda-CI.yml index 063056abf..e461c4be1 100644 --- a/.azure-pipelines/linux-conda-CI.yml +++ b/.azure-pipelines/linux-conda-CI.yml @@ -54,7 +54,7 @@ jobs: pandas.version: '' lgbm.version: '' onnxcc.version: '>=1.8.1' - run.example: '1' + run.example: '0' Py311-Onnx140-Rt151-Skl130: do.bench: '0' diff --git a/docs/conf.py b/docs/conf.py index bda240463..f0f9f1f31 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -35,14 +35,12 @@ "sphinx.ext.graphviz", "sphinx_skl2onnx_extension", "matplotlib.sphinxext.plot_directive", - "pyquickhelper.sphinxext.sphinx_cmdref_extension", - "pyquickhelper.sphinxext.sphinx_collapse_extension", - "pyquickhelper.sphinxext.sphinx_docassert_extension", - "pyquickhelper.sphinxext.sphinx_epkg_extension", - "pyquickhelper.sphinxext.sphinx_exref_extension", - "pyquickhelper.sphinxext.sphinx_faqref_extension", - "pyquickhelper.sphinxext.sphinx_gdot_extension", - "pyquickhelper.sphinxext.sphinx_runpython_extension", + "sphinx_runpython.blocdefs.sphinx_exref_extension", + "sphinx_runpython.blocdefs.sphinx_faqref_extension", + "sphinx_runpython.blocdefs.sphinx_mathdef_extension", + "sphinx_runpython.epkg", + "sphinx_runpython.gdot", + "sphinx_runpython.runpython", "sphinxcontrib.blockdiag", ] diff --git a/docs/requirements.txt b/docs/requirements.txt index 3d63c68bd..58c0069ce 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -18,7 +18,6 @@ pandas pydot pyinstrument pyod -pyquickhelper>=1.11.3762 pytest pytest-cov scikit-learn>=1.1 @@ -26,6 +25,7 @@ skl2onnx sphinx sphinxcontrib-blockdiag sphinx-gallery +sphinx-runpython tabulate tqdm wheel diff --git a/docs/tests/test_documentation_examples.py b/docs/tests/test_documentation_examples.py index 8c2f2cf4d..ae61d33cb 100644 --- a/docs/tests/test_documentation_examples.py +++ b/docs/tests/test_documentation_examples.py @@ -73,6 +73,8 @@ def add_test_methods(cls): if not name.endswith(".py") or not name.startswith("plot_"): continue reason = None + if name in {"plot_woe_transformer.py"}: + reason = "dot not available" if reason: diff --git a/docs/tutorial/plot_gbegin_dataframe.py b/docs/tutorial/plot_gbegin_dataframe.py index 73008dab3..3ac6228aa 100644 --- a/docs/tutorial/plot_gbegin_dataframe.py +++ b/docs/tutorial/plot_gbegin_dataframe.py @@ -15,17 +15,14 @@ +++++++++++++++++++++++++ """ -from mlinsights.plotting import pipeline2dot import numpy import pprint -from onnx.reference import ReferenceEvaluator from onnxruntime import InferenceSession from pandas import DataFrame from sklearn.pipeline import Pipeline from sklearn.compose import ColumnTransformer from sklearn.preprocessing import OneHotEncoder from sklearn.ensemble import RandomForestClassifier -from pyquickhelper.helpgen.graphviz_helper import plot_graphviz from skl2onnx import to_onnx from skl2onnx.algebra.type_helper import guess_initial_types @@ -54,14 +51,6 @@ pipe = Pipeline([("preprocess", preprocessor), ("rf", RandomForestClassifier())]) pipe.fit(train_data, data["y"]) -##################################### -# Display. - -dot = pipeline2dot(pipe, train_data) -ax = plot_graphviz(dot) -ax.get_xaxis().set_visible(False) -ax.get_yaxis().set_visible(False) - ####################################### # Conversion to ONNX # ++++++++++++++++++ @@ -84,24 +73,6 @@ except Exception as e: print(e) -########################### -# Let's use a shortcut - -oinf = ReferenceEvaluator(onx) -got = oinf.run(None, train_data) -print(pipe.predict(train_data)) -print(got["label"]) - -################################# -# And probilities. - -print(pipe.predict_proba(train_data)) -print(got["probabilities"]) - -###################################### -# It looks ok. Let's dig into the details to -# directly use *onnxruntime*. -# # Unhide conversion logic with a dataframe # ++++++++++++++++++++++++++++++++++++++++ # diff --git a/skl2onnx/algebra/type_helper.py b/skl2onnx/algebra/type_helper.py index 8e5346db0..c10c9e27f 100644 --- a/skl2onnx/algebra/type_helper.py +++ b/skl2onnx/algebra/type_helper.py @@ -90,7 +90,7 @@ def _guess_dim(value): ) -def guess_initial_types(X, initial_types): +def guess_initial_types(X, initial_types=None): if X is None and initial_types is None: raise NotImplementedError("Initial types must be specified.") if initial_types is None: diff --git a/tests/test_onnx_helper.py b/tests/test_onnx_helper.py index bf37ed3ad..7dc348a8e 100644 --- a/tests/test_onnx_helper.py +++ b/tests/test_onnx_helper.py @@ -31,6 +31,12 @@ def one_hot_encoder_supports_string(): return pv.Version(vers) >= pv.Version("0.20.0") +def skl12(): + # pv.Version does not work with development versions + vers = ".".join(sklearn_version.split(".")[:2]) + return pv.Version(vers) >= pv.Version("1.2") + + class TestOnnxHelper(unittest.TestCase): def get_model(self, model): try: @@ -73,6 +79,7 @@ def test_onnx_helper_load_save(self): not one_hot_encoder_supports_string(), reason="OneHotEncoder did not have categories_ before 0.20", ) + @unittest.skipIf(not skl12(), reason="sparse_output") def test_onnx_helper_load_save_init(self): model = make_pipeline( Binarizer(), @@ -105,6 +112,7 @@ def test_onnx_helper_load_save_init(self): not one_hot_encoder_supports_string(), reason="OneHotEncoder did not have categories_ before 0.20", ) + @unittest.skipIf(not skl12(), reason="sparse_output") def test_onnx_helper_load_save_init_meta(self): model = make_pipeline( Binarizer(), OneHotEncoder(sparse_output=False), StandardScaler() diff --git a/tests/test_sklearn_array_feature_extractor.py b/tests/test_sklearn_array_feature_extractor.py index b8b57fe5b..b2c611945 100644 --- a/tests/test_sklearn_array_feature_extractor.py +++ b/tests/test_sklearn_array_feature_extractor.py @@ -6,6 +6,7 @@ import pandas as pd import numpy as np from onnxruntime import __version__ as ort_version +from sklearn import __version__ as sklearn_version from sklearn.mixture import GaussianMixture from sklearn.preprocessing import OneHotEncoder @@ -20,11 +21,18 @@ from test_utils import dump_data_and_model, TARGET_OPSET +def skl12(): + # pv.Version does not work with development versions + vers = ".".join(sklearn_version.split(".")[:2]) + return pv.Version(vers) >= pv.Version("1.2") + + class TestSklearnArrayFeatureExtractor(unittest.TestCase): @unittest.skipIf( ColumnTransformer is None or pv.Version(ort_version) <= pv.Version("0.4.0"), reason="onnxruntime too old", ) + @unittest.skipIf(not skl12(), reason="sparse_output") def test_array_feature_extractor(self): data_to_cluster = pd.DataFrame( [[1, 2, 3.5, 4.5], [1, 2, 1.7, 4.0], [2, 4, 2.4, 4.3], [2, 4, 2.5, 4.0]], diff --git a/tests/test_sklearn_calibrated_classifier_cv_converter.py b/tests/test_sklearn_calibrated_classifier_cv_converter.py index 1e87232f1..038e15d02 100644 --- a/tests/test_sklearn_calibrated_classifier_cv_converter.py +++ b/tests/test_sklearn_calibrated_classifier_cv_converter.py @@ -9,6 +9,7 @@ import numpy as np from numpy.testing import assert_almost_equal from onnxruntime import __version__ as ort_version +from sklearn import __version__ as sklearn_version from sklearn.calibration import CalibratedClassifierCV from sklearn.datasets import load_digits, load_iris from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier @@ -49,6 +50,12 @@ ort_version = ort_version.split("+")[0] +def skl12(): + # pv.Version does not work with development versions + vers = ".".join(sklearn_version.split(".")[:2]) + return pv.Version(vers) >= pv.Version("1.2") + + class TestSklearnCalibratedClassifierCVConverters(unittest.TestCase): @ignore_warnings(category=(FutureWarning, ConvergenceWarning, DeprecationWarning)) def test_model_calibrated_classifier_cv_float(self): @@ -186,6 +193,7 @@ def test_model_calibrated_classifier_cv_isotonic_binary_knn(self): pv.Version(ort_version) < pv.Version("0.5.0"), reason="not available" ) @ignore_warnings(category=(FutureWarning, ConvergenceWarning, DeprecationWarning)) + @unittest.skipIf(not skl12(), reason="base_estimator") def test_model_calibrated_classifier_cv_logistic_regression(self): data = load_iris() X, y = data.data, data.target @@ -210,6 +218,7 @@ def test_model_calibrated_classifier_cv_logistic_regression(self): pv.Version(ort_version) < pv.Version("0.5.0"), reason="not available" ) @ignore_warnings(category=(FutureWarning, ConvergenceWarning, DeprecationWarning)) + @unittest.skipIf(not skl12(), reason="base_estimator") def test_model_calibrated_classifier_cv_rf(self): data = load_iris() X, y = data.data, data.target @@ -234,6 +243,7 @@ def test_model_calibrated_classifier_cv_rf(self): pv.Version(ort_version) < pv.Version("0.5.0"), reason="not available" ) @ignore_warnings(category=(FutureWarning, ConvergenceWarning, DeprecationWarning)) + @unittest.skipIf(not skl12(), reason="base_estimator") def test_model_calibrated_classifier_cv_gbt(self): data = load_iris() X, y = data.data, data.target @@ -259,6 +269,7 @@ def test_model_calibrated_classifier_cv_gbt(self): pv.Version(ort_version) < pv.Version("0.5.0"), reason="not available" ) @ignore_warnings(category=(FutureWarning, ConvergenceWarning, DeprecationWarning)) + @unittest.skipIf(not skl12(), reason="base_estimator") def test_model_calibrated_classifier_cv_hgbt(self): data = load_iris() X, y = data.data, data.target @@ -308,6 +319,7 @@ def test_model_calibrated_classifier_cv_tree(self): ) @unittest.skipIf(apply_less is None, reason="onnxconverter-common old") @ignore_warnings(category=(FutureWarning, ConvergenceWarning, DeprecationWarning)) + @unittest.skipIf(not skl12(), reason="base_estimator") def test_model_calibrated_classifier_cv_svc(self): data = load_iris() X, y = data.data, data.target @@ -330,6 +342,7 @@ def test_model_calibrated_classifier_cv_svc(self): ) @unittest.skipIf(apply_less is None, reason="onnxconverter-common old") @ignore_warnings(category=(FutureWarning, ConvergenceWarning, DeprecationWarning)) + @unittest.skipIf(not skl12(), reason="base_estimator") def test_model_calibrated_classifier_cv_linearsvc(self): data = load_iris() X, y = data.data, data.target @@ -354,6 +367,7 @@ def test_model_calibrated_classifier_cv_linearsvc(self): ) @unittest.skipIf(apply_less is None, reason="onnxconverter-common old") @ignore_warnings(category=(FutureWarning, ConvergenceWarning, DeprecationWarning)) + @unittest.skipIf(not skl12(), reason="base_estimator") def test_model_calibrated_classifier_cv_linearsvc2(self): data = load_iris() X, y = data.data, data.target