From 4532ab2bb19b2c731463fc9971cf468a8ae197b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xavier=20Dupr=C3=A9?= Date: Wed, 4 Sep 2024 17:18:00 +0200 Subject: [PATCH] Update CI with onnxruntime==1.19.0 (#1125) * Update CI with onnxruntime==1.19.0 Signed-off-by: xadupre * ruff Signed-off-by: xadupre * black Signed-off-by: xadupre * fix missing import Signed-off-by: xadupre * replace warnings.warning Signed-off-by: xadupre * fix fix Signed-off-by: xadupre * add missing import Signed-off-by: xadupre * fix one test Signed-off-by: xadupre --------- Signed-off-by: xadupre --- .github/workflows/linux-ci.yml | 4 +- .github/workflows/windows-macos-ci.yml | 8 +- .../bench_plot_onnxruntime_decision_tree.py | 5 +- benchmarks/bench_plot_onnxruntime_hgb.py | 9 +- benchmarks/bench_plot_onnxruntime_linreg.py | 18 +-- benchmarks/bench_plot_onnxruntime_logreg.py | 5 +- .../bench_plot_onnxruntime_random_forest.py | 5 +- ...ench_plot_onnxruntime_random_forest_reg.py | 7 +- benchmarks/bench_plot_onnxruntime_svm_reg.py | 7 +- benchmarks/post_graph.py | 2 +- docs/conf.py | 4 +- docs/examples/plot_backend.py | 1 + docs/examples/plot_benchmark_cdist.py | 1 + docs/examples/plot_benchmark_pipeline.py | 3 +- docs/examples/plot_black_op.py | 5 +- docs/examples/plot_cast_transformer.py | 5 +- docs/examples/plot_complex_pipeline.py | 1 + .../plot_convert_decision_function.py | 1 + docs/examples/plot_convert_syntax.py | 13 ++- docs/examples/plot_convert_zipmap.py | 1 + docs/examples/plot_custom_model.py | 9 +- docs/examples/plot_custom_parser.py | 1 + .../plot_custom_parser_alternative.py | 1 + docs/examples/plot_errors_onnxruntime.py | 1 + docs/examples/plot_gpr.py | 1 + docs/examples/plot_intermediate_outputs.py | 1 + docs/examples/plot_investigate_pipeline.py | 3 +- docs/examples/plot_onnx_operators.py | 13 ++- docs/examples/plot_pipeline_lightgbm.py | 5 +- docs/examples/plot_pipeline_xgboost.py | 5 +- docs/exts/sphinx_skl2onnx_extension.py | 3 +- docs/tests/test_documentation_examples.py | 6 +- docs/tutorial/plot_abegin_convert_pipeline.py | 1 + docs/tutorial/plot_bbegin_measure_time.py | 1 + docs/tutorial/plot_catwoe_transformer.py | 3 +- docs/tutorial/plot_cbegin_opset.py | 1 + docs/tutorial/plot_dbegin_options_list.py | 1 + docs/tutorial/plot_dbegin_options_zipmap.py | 1 + docs/tutorial/plot_ebegin_float_double.py | 1 + docs/tutorial/plot_fbegin_investigate.py | 1 + docs/tutorial/plot_gbegin_cst.py | 1 + docs/tutorial/plot_gbegin_dataframe.py | 1 + docs/tutorial/plot_gconverting.py | 1 + docs/tutorial/plot_gexternal_catboost.py | 3 +- docs/tutorial/plot_gexternal_lightgbm.py | 5 +- docs/tutorial/plot_gexternal_lightgbm_reg.py | 12 +- docs/tutorial/plot_gexternal_xgboost.py | 1 + docs/tutorial/plot_icustom_converter.py | 1 + docs/tutorial/plot_jcustom_syntax.py | 1 + .../plot_kcustom_converter_wrapper.py | 1 + docs/tutorial/plot_lcustom_options.py | 1 + docs/tutorial/plot_mcustom_parser.py | 1 + docs/tutorial/plot_ngrams.py | 2 +- docs/tutorial/plot_usparse_xgboost.py | 1 + docs/tutorial/plot_weird_pandas_and_hash.py | 1 + docs/tutorial/plot_wext_pyod_forest.py | 1 + docs/tutorial/plot_woe_transformer.py | 3 +- pyproject.toml | 44 +++++++- skl2onnx/__init__.py | 15 +-- skl2onnx/_parse.py | 17 +-- skl2onnx/_supported_operators.py | 17 ++- skl2onnx/algebra/__init__.py | 2 +- skl2onnx/algebra/automation.py | 4 +- skl2onnx/algebra/graph_state.py | 67 ++++++------ skl2onnx/algebra/onnx_operator.py | 66 ++++++----- skl2onnx/algebra/onnx_operator_mixin.py | 1 + skl2onnx/algebra/onnx_ops.py | 103 +++++++++--------- .../algebra/onnx_subgraph_operator_mixin.py | 2 - skl2onnx/algebra/sklearn_ops.py | 1 + skl2onnx/common/__init__.py | 2 +- skl2onnx/common/_apply_operation.py | 6 +- skl2onnx/common/_container.py | 31 +++--- skl2onnx/common/_onnx_optimisation_common.py | 5 +- skl2onnx/common/_registration.py | 13 +-- skl2onnx/common/_topology.py | 37 +++---- skl2onnx/common/data_types.py | 29 ++--- skl2onnx/common/exceptions.py | 1 - skl2onnx/common/onnx_optimisation_identity.py | 25 ++--- skl2onnx/common/shape_calculator.py | 1 + skl2onnx/common/tree_ensemble.py | 1 + skl2onnx/common/utils.py | 19 ++-- skl2onnx/common/utils_checking.py | 11 +- skl2onnx/common/utils_sklearn.py | 4 +- skl2onnx/convert.py | 9 +- skl2onnx/helpers/__init__.py | 6 +- skl2onnx/helpers/integration.py | 2 +- skl2onnx/helpers/investigate.py | 35 +++--- skl2onnx/helpers/onnx_helper.py | 16 +-- skl2onnx/operator_converters/_gp_kernels.py | 6 +- skl2onnx/operator_converters/ada_boost.py | 4 +- .../calibrated_classifier_cv.py | 2 +- skl2onnx/operator_converters/class_labels.py | 2 +- skl2onnx/operator_converters/common.py | 4 +- skl2onnx/operator_converters/decision_tree.py | 6 +- .../operator_converters/dict_vectoriser.py | 6 +- .../operator_converters/feature_hasher.py | 5 +- .../operator_converters/gamma_regressor.py | 2 +- .../operator_converters/gaussian_mixture.py | 13 +-- .../operator_converters/gaussian_process.py | 4 +- .../operator_converters/gradient_boosting.py | 4 +- .../operator_converters/grid_search_cv.py | 2 +- skl2onnx/operator_converters/imputer_op.py | 8 +- .../operator_converters/isolation_forest.py | 2 +- skl2onnx/operator_converters/kernel_pca.py | 2 +- .../operator_converters/label_binariser.py | 2 +- skl2onnx/operator_converters/label_encoder.py | 2 +- .../operator_converters/linear_classifier.py | 20 ++-- .../operator_converters/linear_regressor.py | 2 +- .../local_outlier_factor.py | 7 +- .../operator_converters/nearest_neighbours.py | 20 ++-- .../operator_converters/one_hot_encoder.py | 2 +- .../operator_converters/ordinal_encoder.py | 2 +- .../polynomial_features.py | 4 +- .../operator_converters/power_transformer.py | 8 +- skl2onnx/operator_converters/random_forest.py | 8 +- skl2onnx/operator_converters/scaler_op.py | 6 +- .../support_vector_machines.py | 8 +- .../operator_converters/text_vectoriser.py | 9 +- .../operator_converters/tfidf_transformer.py | 4 +- skl2onnx/operator_converters/zip_map.py | 4 +- skl2onnx/proto/__init__.py | 11 +- .../shape_calculators/gaussian_process.py | 4 +- skl2onnx/shape_calculators/grid_search_cv.py | 2 +- skl2onnx/shape_calculators/imputer.py | 2 +- skl2onnx/shape_calculators/multioutput.py | 2 +- skl2onnx/shape_calculators/scaler.py | 2 +- skl2onnx/sklapi/__init__.py | 10 +- skl2onnx/sklapi/cast_regressor.py | 10 +- skl2onnx/sklapi/cast_transformer.py | 8 +- skl2onnx/sklapi/replace_transformer.py | 6 +- skl2onnx/sklapi/sklearn_text.py | 5 +- skl2onnx/sklapi/woe_transformer.py | 6 +- skl2onnx/sklapi/woe_transformer_onnx.py | 17 +-- skl2onnx/tutorial/__init__.py | 2 +- skl2onnx/tutorial/benchmark.py | 1 + skl2onnx/tutorial/imagenet_classes.py | 20 ++-- tests/benchmark.py | 2 +- tests/test_algebra_cascade.py | 10 +- tests/test_algebra_custom_model.py | 1 + ...test_algebra_custom_model_sub_estimator.py | 1 + tests/test_algebra_deprecation.py | 2 +- tests/test_algebra_onnx_operators.py | 11 +- tests/test_algebra_onnx_operators_opset.py | 2 +- tests/test_algebra_symbolic.py | 2 +- tests/test_algebra_test_helper.py | 2 +- tests/test_convert_options.py | 2 +- tests/test_custom_transformer_ordwoe.py | 3 +- tests/test_custom_transformer_tsne.py | 5 +- tests/test_investigate.py | 2 +- tests/test_onnx_helper.py | 3 +- tests/test_onnx_rare_helper.py | 1 + tests/test_onnxruntime.py | 2 +- tests/test_options.py | 1 + .../test_other_converter_library_pipelines.py | 1 + tests/test_scikit_pandas.py | 5 +- tests/test_sklearn_cast_regressor.py | 1 + tests/test_sklearn_cast_transformer.py | 1 + ...test_sklearn_count_vectorizer_converter.py | 1 + ..._sklearn_count_vectorizer_converter_bug.py | 1 + .../test_sklearn_decision_tree_converters.py | 2 +- tests/test_sklearn_documentation.py | 1 + tests/test_sklearn_double_tensor_type_cls.py | 5 +- tests/test_sklearn_double_tensor_type_reg.py | 6 +- tests/test_sklearn_double_tensor_type_tr.py | 6 +- tests/test_sklearn_feature_hasher.py | 7 +- ...st_sklearn_feature_selection_converters.py | 1 + ..._sklearn_function_transformer_converter.py | 1 + ...est_sklearn_gaussian_process_classifier.py | 5 +- ...test_sklearn_gaussian_process_regressor.py | 4 +- .../test_sklearn_grid_search_cv_converter.py | 4 +- tests/test_sklearn_imputer_converter.py | 1 + tests/test_sklearn_isolation_forest.py | 1 + tests/test_sklearn_kernel_pca_converter.py | 6 +- tests/test_sklearn_local_outlier_factor.py | 1 + tests/test_sklearn_mlp_converter.py | 1 + tests/test_sklearn_multi_output.py | 1 - ...est_sklearn_nearest_neighbour_converter.py | 3 +- tests/test_sklearn_normalizer_converter.py | 1 + .../test_sklearn_one_hot_encoder_converter.py | 1 + tests/test_sklearn_ordinal_encoder.py | 1 + ...passive_aggressive_classifier_converter.py | 1 + tests/test_sklearn_pipeline.py | 7 +- .../test_sklearn_pipeline_within_pipeline.py | 1 + ...t_sklearn_polynomial_features_converter.py | 1 + tests/test_sklearn_power_transformer.py | 1 + tests/test_sklearn_replace_transformer.py | 1 + tests/test_sklearn_scaler_converter.py | 1 + tests/test_sklearn_svm_converters.py | 1 + tests/test_sklearn_text.py | 2 - ...est_sklearn_tfidf_transformer_converter.py | 1 + ...earn_tfidf_transformer_converter_sparse.py | 1 + ...test_sklearn_tfidf_vectorizer_converter.py | 3 +- ...sklearn_tfidf_vectorizer_converter_char.py | 1 + ...earn_tfidf_vectorizer_converter_dataset.py | 1 + ...arn_tfidf_vectorizer_converter_pipeline.py | 1 + ...klearn_tfidf_vectorizer_converter_regex.py | 5 +- tests/test_sklearn_woe_transformer.py | 3 +- tests/test_topology_prune.py | 1 + tests/test_utils/__init__.py | 6 +- .../reference_implementation_afe.py | 6 +- .../reference_implementation_helper.py | 2 +- .../test_utils/reference_implementation_ml.py | 4 +- .../reference_implementation_svm.py | 3 +- .../reference_implementation_text.py | 6 +- .../reference_implementation_tree.py | 3 +- .../reference_implementation_zipmap.py | 2 +- tests/test_utils/tests_helper.py | 28 ++--- tests/test_utils/utils_backend.py | 12 +- tests/test_utils/utils_backend_onnx.py | 29 ++--- tests/test_utils/utils_backend_onnxruntime.py | 24 ++-- tests/test_utils_sklearn.py | 1 + tests/test_variable_names.py | 1 - tests_onnxmltools/test_columns.py | 10 +- tests_onnxmltools/test_lightgbm.py | 9 +- tests_onnxmltools/test_xgboost_converters.py | 6 +- 215 files changed, 750 insertions(+), 637 deletions(-) diff --git a/.github/workflows/linux-ci.yml b/.github/workflows/linux-ci.yml index 56fa27060..23116adf7 100644 --- a/.github/workflows/linux-ci.yml +++ b/.github/workflows/linux-ci.yml @@ -14,14 +14,14 @@ jobs: numpy_version: '>=1.21.1,<2.0' scipy_version: '>=1.7.0' onnx_version: 'onnx==1.16.0' - onnxrt_version: 'onnxruntime==1.18.0' + onnxrt_version: 'onnxruntime==1.19.0' python_version: '3.12' - python_version: '3.12' documentation: 0 numpy_version: '>=1.21.1,<2.0' scipy_version: '>=1.7.0' onnx_version: 'onnx==1.16.0' - onnxrt_version: 'onnxruntime==1.18.0' + onnxrt_version: 'onnxruntime==1.18.1' sklearn_version: '==1.4.2' - python_version: '3.11' documentation: 1 diff --git a/.github/workflows/windows-macos-ci.yml b/.github/workflows/windows-macos-ci.yml index 78690e62a..92219ff33 100644 --- a/.github/workflows/windows-macos-ci.yml +++ b/.github/workflows/windows-macos-ci.yml @@ -9,12 +9,18 @@ jobs: os: [windows-latest, macos-latest] sklearn_version: ['==1.5.0', '==1.4.2', '==1.3.2', '==1.2.2', '==1.1.3'] include: + - sklearn_version: '==1.5.0' + python_version: '3.12' + numpy_version: '>=1.21.1,<2.0' + scipy_version: '>=1.7.0' + onnx_version: 'onnx==1.16.0' + onnxrt_version: 'onnxruntime<1.20.0' - sklearn_version: '==1.5.0' python_version: '3.11' numpy_version: '>=1.21.1,<2.0' scipy_version: '>=1.7.0' onnx_version: 'onnx==1.16.0' - onnxrt_version: 'onnxruntime==1.18.0' + onnxrt_version: 'onnxruntime<1.19.0' - python_version: '3.11' numpy_version: '>=1.21.1,<2.0' scipy_version: '>=1.7.0' diff --git a/benchmarks/bench_plot_onnxruntime_decision_tree.py b/benchmarks/bench_plot_onnxruntime_decision_tree.py index 8b08aca8b..f0f4cd26f 100644 --- a/benchmarks/bench_plot_onnxruntime_decision_tree.py +++ b/benchmarks/bench_plot_onnxruntime_decision_tree.py @@ -4,6 +4,7 @@ """ Benchmark of onnxruntime on DecisionTree. """ + # Authors: Xavier Dupré (benchmark) from io import BytesIO from time import perf_counter as time @@ -101,7 +102,7 @@ def bench(n_obs, n_features, max_depths, methods, repeat=10, verbose=False): # creates different inputs to avoid caching in any ways Xs = [] - for r in range(repeat): + for _r in range(repeat): x = np.empty((n, nfeat)) x[:, :] = rand(n, nfeat)[:, :] Xs.append(x.astype(np.float32)) @@ -123,7 +124,7 @@ def bench(n_obs, n_features, max_depths, methods, repeat=10, verbose=False): r2 = 0 for X in Xs: p2 = fct2(X) - r2 += 1 + r2 += 1 # noqa: SIM113 if r2 >= repeated: break end = time() diff --git a/benchmarks/bench_plot_onnxruntime_hgb.py b/benchmarks/bench_plot_onnxruntime_hgb.py index 5c2d0f23d..7968fb4a8 100644 --- a/benchmarks/bench_plot_onnxruntime_hgb.py +++ b/benchmarks/bench_plot_onnxruntime_hgb.py @@ -4,6 +4,7 @@ """ Benchmark of onnxruntime on RandomForestRegressor. """ + import warnings from io import BytesIO from time import perf_counter as time @@ -100,7 +101,7 @@ def bench( # creates different inputs to avoid caching in any ways Xs = [] - for r in range(repeat): + for _r in range(repeat): x = np.empty((n, nfeat)) x[:, :] = rand(n, nfeat)[:, :] Xs.append(x.astype(np.float32)) @@ -122,7 +123,7 @@ def bench( r2 = 0 for X in Xs: p2 = fct2(X) - r2 += 1 + r2 += 1 # noqa: SIM113 if r2 >= repeated: break end = time() @@ -140,7 +141,7 @@ def bench( try: assert_almost_equal(p1.ravel(), p2.ravel(), decimal=5) except AssertionError as e: - warnings.warn(str(e)) + warnings.warn(str(e), stacklevel=0) return res @@ -158,7 +159,7 @@ def plot_results(df, verbose=False): for n_obs in sorted(set(df.n_obs)): for max_depth in sorted(set(df.max_depth)): pos = 0 - for n_jobs in [1]: + for _n_jobs in [1]: a = ax[row, pos] if row == ax.shape[0] - 1: a.set_xlabel("N features", fontsize="x-small") diff --git a/benchmarks/bench_plot_onnxruntime_linreg.py b/benchmarks/bench_plot_onnxruntime_linreg.py index 82490c22d..3a9f94982 100644 --- a/benchmarks/bench_plot_onnxruntime_linreg.py +++ b/benchmarks/bench_plot_onnxruntime_linreg.py @@ -4,6 +4,7 @@ """ Benchmark of onnxruntime on LinearRegression. """ + import warnings from io import BytesIO from time import perf_counter as time @@ -100,7 +101,7 @@ def bench(n_obs, n_features, fit_intercepts, methods, repeat=10, verbose=False): # creates different inputs to avoid caching in any ways Xs = [] - for r in range(loop_repeat): + for _r in range(loop_repeat): x = np.empty((n, nfeat)) x[:, :] = rand(n, nfeat)[:, :] Xs.append(x.astype(np.float32)) @@ -120,7 +121,7 @@ def bench(n_obs, n_features, fit_intercepts, methods, repeat=10, verbose=False): r2 = 0 for X in Xs: p2 = fct2(X) - r2 += 1 + r2 += 1 # noqa: SIM113 end = time() obs["time_ort"] = (end - st) / repeated res.append(obs) @@ -128,13 +129,12 @@ def bench(n_obs, n_features, fit_intercepts, methods, repeat=10, verbose=False): print("bench", len(res), ":", obs) # checks that both produce the same outputs - if n <= 10000: - if len(p1.shape) == 1 and len(p2.shape) == 2: - p2 = p2.ravel() - try: - assert_almost_equal(p1.ravel(), p2.ravel(), decimal=5) - except AssertionError as e: - warnings.warn(str(e)) + if n <= 10000 and len(p1.shape) == 1 and len(p2.shape) == 2: + p2 = p2.ravel() + try: + assert_almost_equal(p1.ravel(), p2.ravel(), decimal=5) + except AssertionError as e: + warnings.warn(str(e), stacklevel=0) return res diff --git a/benchmarks/bench_plot_onnxruntime_logreg.py b/benchmarks/bench_plot_onnxruntime_logreg.py index dc2f1ec37..4656ffb59 100644 --- a/benchmarks/bench_plot_onnxruntime_logreg.py +++ b/benchmarks/bench_plot_onnxruntime_logreg.py @@ -4,6 +4,7 @@ """ Benchmark of onnxruntime on LogisticRegression. """ + # Authors: Xavier Dupré (benchmark) from io import BytesIO from time import perf_counter as time @@ -113,7 +114,7 @@ def bench(n_obs, n_features, fit_intercepts, methods, repeat=10, verbose=False): # creates different inputs to avoid caching in any ways Xs = [] - for r in range(repeat): + for _r in range(repeat): x = np.empty((n, nfeat)) x[:, :] = rand(n, nfeat)[:, :] Xs.append(x.astype(np.float32)) @@ -133,7 +134,7 @@ def bench(n_obs, n_features, fit_intercepts, methods, repeat=10, verbose=False): r2 = 0 for X in Xs: p2 = fct2(X) - r2 += 1 + r2 += 1 # noqa: SIM113 end = time() obs["time_ort"] = (end - st) / repeated res.append(obs) diff --git a/benchmarks/bench_plot_onnxruntime_random_forest.py b/benchmarks/bench_plot_onnxruntime_random_forest.py index c968b5457..b18f9cff5 100644 --- a/benchmarks/bench_plot_onnxruntime_random_forest.py +++ b/benchmarks/bench_plot_onnxruntime_random_forest.py @@ -4,6 +4,7 @@ """ Benchmark of onnxruntime on RandomForest. """ + # Authors: Xavier Dupré (benchmark) from io import BytesIO from time import perf_counter as time @@ -118,7 +119,7 @@ def bench( # creates different inputs to avoid caching in any ways Xs = [] - for r in range(repeat): + for _r in range(repeat): x = np.empty((n, nfeat)) x[:, :] = rand(n, nfeat)[:, :] Xs.append(x) @@ -140,7 +141,7 @@ def bench( r2 = 0 for X in Xs: p2 = fct2(X) - r2 += 1 + r2 += 1 # noqa: SIM113 if r2 >= repeated: break end = time() diff --git a/benchmarks/bench_plot_onnxruntime_random_forest_reg.py b/benchmarks/bench_plot_onnxruntime_random_forest_reg.py index c942e72e5..5223c0647 100644 --- a/benchmarks/bench_plot_onnxruntime_random_forest_reg.py +++ b/benchmarks/bench_plot_onnxruntime_random_forest_reg.py @@ -4,6 +4,7 @@ """ Benchmark of onnxruntime on RandomForestRegressor. """ + import sys import warnings from io import BytesIO @@ -137,7 +138,7 @@ def bench( # creates different inputs to avoid caching # in any ways Xs = [] - for r in range(repeat): + for _r in range(repeat): x = np.empty((n, nfeat)) x[:, :] = rand(n, nfeat)[:, :] Xs.append(x.astype(np.float32)) @@ -159,7 +160,7 @@ def bench( r2 = 0 for X in Xs: p2 = fct2(X) - r2 += 1 + r2 += 1 # noqa: SIM113 if r2 >= repeated: break end = time() @@ -191,7 +192,7 @@ def bench( p1.ravel(), p2.ravel(), decimal=5 ) except AssertionError as e: - warnings.warn(str(e)) + warnings.warn(str(e), stacklevel=0) return res diff --git a/benchmarks/bench_plot_onnxruntime_svm_reg.py b/benchmarks/bench_plot_onnxruntime_svm_reg.py index 2f38010c6..f4e817a2d 100644 --- a/benchmarks/bench_plot_onnxruntime_svm_reg.py +++ b/benchmarks/bench_plot_onnxruntime_svm_reg.py @@ -4,6 +4,7 @@ """ Benchmark of onnxruntime on SVM. """ + # Authors: Xavier Dupré (benchmark) import warnings from io import BytesIO @@ -86,7 +87,7 @@ def bench(n_obs, n_features, kernels, methods, repeat=10, verbose=False): # creates different inputs to avoid caching in any ways Xs = [] - for r in range(repeat): + for _r in range(repeat): x = np.empty((n, nfeat)) x[:, :] = rand(n, nfeat)[:, :] Xs.append(x.astype(np.float32)) @@ -108,7 +109,7 @@ def bench(n_obs, n_features, kernels, methods, repeat=10, verbose=False): r2 = 0 for X in Xs: p2 = fct2(X) - r2 += 1 + r2 += 1 # noqa: SIM113 if r2 >= repeated: break end = time() @@ -126,7 +127,7 @@ def bench(n_obs, n_features, kernels, methods, repeat=10, verbose=False): try: assert_almost_equal(p1.ravel(), p2.ravel(), decimal=3) except AssertionError as e: - warnings.warn(str(e)) + warnings.warn(str(e), stacklevel=0) return res diff --git a/benchmarks/post_graph.py b/benchmarks/post_graph.py index 2d3276e72..3d755f187 100644 --- a/benchmarks/post_graph.py +++ b/benchmarks/post_graph.py @@ -103,7 +103,7 @@ def svm_models(): tick.label.set_fontsize(8) for tick in ax.yaxis.get_major_ticks(): tick.label.set_fontsize(8) - pos += 1 + pos += 1 # noqa: SIM113 fig.tight_layout() fig.savefig("svm_model.png", dpi=1000) diff --git a/docs/conf.py b/docs/conf.py index 41613d290..d0f019d4e 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -1,6 +1,4 @@ # SPDX-License-Identifier: Apache-2.0 -# -*- coding: utf-8 -*- -# # Configuration file for the Sphinx documentation builder. import os @@ -10,7 +8,7 @@ import skl2onnx sys.path.append(os.path.abspath("exts")) -from github_link import make_linkcode_resolve # noqa +from github_link import make_linkcode_resolve # -- Project information ----------------------------------------------------- diff --git a/docs/examples/plot_backend.py b/docs/examples/plot_backend.py index 3b7643b33..4d5084920 100644 --- a/docs/examples/plot_backend.py +++ b/docs/examples/plot_backend.py @@ -17,6 +17,7 @@ Let's use the API to compute the prediction of a simple logistic regression model. """ + import skl2onnx import onnxruntime import onnx diff --git a/docs/examples/plot_benchmark_cdist.py b/docs/examples/plot_benchmark_cdist.py index 9247d7a93..0af7b5c81 100644 --- a/docs/examples/plot_benchmark_cdist.py +++ b/docs/examples/plot_benchmark_cdist.py @@ -18,6 +18,7 @@ generated/scipy.spatial.distance.cdist.html>`_ function computes pairwise distances. """ + from pprint import pprint from timeit import Timer import numpy as np diff --git a/docs/examples/plot_benchmark_pipeline.py b/docs/examples/plot_benchmark_pipeline.py index 4428d2ea7..7f3f13f9f 100644 --- a/docs/examples/plot_benchmark_pipeline.py +++ b/docs/examples/plot_benchmark_pipeline.py @@ -20,6 +20,7 @@ does not handle string type. This cannot be part of the final ONNX pipeline and must be removed. Look for comment starting with ``---`` below. """ + import skl2onnx import onnx import sklearn @@ -105,7 +106,7 @@ pipe.predict_proba(X_digits[:2]) -for i, step in enumerate(steps): +for _i, step in enumerate(steps): onnx_step = step["onnx_step"] sess = rt.InferenceSession( onnx_step.SerializeToString(), providers=["CPUExecutionProvider"] diff --git a/docs/examples/plot_black_op.py b/docs/examples/plot_black_op.py index d5c46d639..f52299ec7 100644 --- a/docs/examples/plot_black_op.py +++ b/docs/examples/plot_black_op.py @@ -19,6 +19,7 @@ The first converter to change its behaviour depending on a black list of operators is for model *GaussianMixture*. """ + import onnxruntime import onnx import numpy @@ -167,11 +168,11 @@ ################################# # **Versions used for this example** -import sklearn # noqa +import sklearn print("numpy:", numpy.__version__) print("scikit-learn:", sklearn.__version__) -import skl2onnx # noqa +import skl2onnx print("onnx: ", onnx.__version__) print("onnxruntime: ", onnxruntime.__version__) diff --git a/docs/examples/plot_cast_transformer.py b/docs/examples/plot_cast_transformer.py index 66439329b..93e4c9443 100644 --- a/docs/examples/plot_cast_transformer.py +++ b/docs/examples/plot_cast_transformer.py @@ -28,6 +28,7 @@ based on the assumption ``(x / y)`` is usually different from ``x * ( 1 / y)`` on a computer. """ + import onnxruntime import onnx import os @@ -169,11 +170,11 @@ def maxdiff(a1, a2): ################################# # **Versions used for this example** -import sklearn # noqa +import sklearn print("numpy:", np.__version__) print("scikit-learn:", sklearn.__version__) -import skl2onnx # noqa +import skl2onnx print("onnx: ", onnx.__version__) print("onnxruntime: ", onnxruntime.__version__) diff --git a/docs/examples/plot_complex_pipeline.py b/docs/examples/plot_complex_pipeline.py index 8457bdf11..ca60eb100 100644 --- a/docs/examples/plot_complex_pipeline.py +++ b/docs/examples/plot_complex_pipeline.py @@ -29,6 +29,7 @@ does not handle string type. This cannot be part of the final ONNX pipeline and must be removed. Look for comment starting with ``---`` below. """ + import os import pprint import pandas as pd diff --git a/docs/examples/plot_convert_decision_function.py b/docs/examples/plot_convert_decision_function.py index 886501325..db5381c15 100644 --- a/docs/examples/plot_convert_decision_function.py +++ b/docs/examples/plot_convert_decision_function.py @@ -19,6 +19,7 @@ ++++++++++++++++++++++++++++ """ + import numpy import sklearn from sklearn.datasets import load_iris diff --git a/docs/examples/plot_convert_syntax.py b/docs/examples/plot_convert_syntax.py index d6f16ddbe..819b28528 100644 --- a/docs/examples/plot_convert_syntax.py +++ b/docs/examples/plot_convert_syntax.py @@ -16,6 +16,7 @@ Simple function to check the converted model works fine. """ + import onnxruntime import onnx import numpy @@ -121,7 +122,7 @@ def to_onnx_operator( self, inputs=None, outputs=("Y",), target_opset=None, **kwargs ): if inputs is None: - raise RuntimeError("Parameter inputs should contain at least " "one name.") + raise RuntimeError("Parameter inputs should contain at least one name.") opv = target_opset or self.op_version i0 = self.get_inputs(inputs, 0) W = self.W_.astype(np.float32) @@ -185,7 +186,7 @@ def to_onnx_operator( # # Finally, let's see the graph converted with *sklearn-onnx*. -from onnx.tools.net_drawer import GetPydotGraph, GetOpNodeProducer # noqa +from onnx.tools.net_drawer import GetPydotGraph, GetOpNodeProducer pydot_graph = GetPydotGraph( onx.graph, @@ -197,11 +198,11 @@ def to_onnx_operator( ) pydot_graph.write_dot("pipeline_onnx_mixin.dot") -import os # noqa +import os os.system("dot -O -Gdpi=300 -Tpng pipeline_onnx_mixin.dot") -import matplotlib.pyplot as plt # noqa +import matplotlib.pyplot as plt image = plt.imread("pipeline_onnx_mixin.dot.png") fig, ax = plt.subplots(figsize=(40, 20)) @@ -211,11 +212,11 @@ def to_onnx_operator( ################################# # **Versions used for this example** -import sklearn # noqa +import sklearn print("numpy:", numpy.__version__) print("scikit-learn:", sklearn.__version__) -import skl2onnx # noqa +import skl2onnx print("onnx: ", onnx.__version__) print("onnxruntime: ", onnxruntime.__version__) diff --git a/docs/examples/plot_convert_zipmap.py b/docs/examples/plot_convert_zipmap.py index e2a1f5385..a1d882606 100644 --- a/docs/examples/plot_convert_zipmap.py +++ b/docs/examples/plot_convert_zipmap.py @@ -19,6 +19,7 @@ ++++++++++++++++++++++++++++ """ + from timeit import repeat import numpy import sklearn diff --git a/docs/examples/plot_custom_model.py b/docs/examples/plot_custom_model.py index 94e7faf19..0ea230202 100644 --- a/docs/examples/plot_custom_model.py +++ b/docs/examples/plot_custom_model.py @@ -40,6 +40,7 @@ * k nearest neightbours, :math:`f(X') \\rightarrow X'_3` * final normalization, simple scaling :math:`X'_3 \\rightarrow X'_4` """ + import inspect import os import numpy @@ -69,7 +70,7 @@ def __init__( estimator=None, normalize=True, keep_tsne_outputs=False, - **kwargs + **kwargs, ): """ :param transformer: `TSNE` by default @@ -280,7 +281,7 @@ def plot_embedding(Xp, y, imgs, title=None, figsize=(12, 4)): X_train_tsne2, y_train, imgs_train, - "Predictable t-SNE of the digits\n" "StandardScaler+KNeighborsRegressor", + "Predictable t-SNE of the digits\nStandardScaler+KNeighborsRegressor", ) ################################ @@ -292,7 +293,7 @@ def plot_embedding(Xp, y, imgs, title=None, figsize=(12, 4)): X_test_tsne2, y_test, imgs_test, - "Predictable t-SNE of the digits\n" "StandardScaler+KNeighborsRegressor", + "Predictable t-SNE of the digits\nStandardScaler+KNeighborsRegressor", ) ####################################### @@ -369,7 +370,7 @@ def predictable_tsne_converter(scope, operator, container): [knn_output.onnx_name], [output.full_name], op_domain="ai.onnx.ml", - **attrs + **attrs, ) diff --git a/docs/examples/plot_custom_parser.py b/docs/examples/plot_custom_parser.py index 64102ab79..2c1db46f7 100644 --- a/docs/examples/plot_custom_parser.py +++ b/docs/examples/plot_custom_parser.py @@ -20,6 +20,7 @@ A new class is created, it trains any classifier and implements the method *validate* mentioned above. """ + import inspect import numpy as np import skl2onnx diff --git a/docs/examples/plot_custom_parser_alternative.py b/docs/examples/plot_custom_parser_alternative.py index 9a28f5103..a10722e9f 100644 --- a/docs/examples/plot_custom_parser_alternative.py +++ b/docs/examples/plot_custom_parser_alternative.py @@ -25,6 +25,7 @@ A new class is created, it trains any classifier and implements the method *validate* mentioned above. """ + import inspect import numpy as np import skl2onnx diff --git a/docs/examples/plot_errors_onnxruntime.py b/docs/examples/plot_errors_onnxruntime.py index 0d364d5d9..4bb7e873e 100644 --- a/docs/examples/plot_errors_onnxruntime.py +++ b/docs/examples/plot_errors_onnxruntime.py @@ -17,6 +17,7 @@ trained on *Iris* datasets. The model takes a vector of dimension 2 and returns a class among three. """ + import skl2onnx import onnx import sklearn diff --git a/docs/examples/plot_gpr.py b/docs/examples/plot_gpr.py index c97064d21..05ea4e396 100644 --- a/docs/examples/plot_gpr.py +++ b/docs/examples/plot_gpr.py @@ -21,6 +21,7 @@ A very basic example using *GaussianProcessRegressor* on the Boston dataset. """ + import pprint import numpy import sklearn diff --git a/docs/examples/plot_intermediate_outputs.py b/docs/examples/plot_intermediate_outputs.py index 2408ca659..9e4930998 100644 --- a/docs/examples/plot_intermediate_outputs.py +++ b/docs/examples/plot_intermediate_outputs.py @@ -25,6 +25,7 @@ does not handle string type. This cannot be part of the final ONNX pipeline and must be removed. Look for comment starting with ``---`` below. """ + import skl2onnx import onnx import sklearn diff --git a/docs/examples/plot_investigate_pipeline.py b/docs/examples/plot_investigate_pipeline.py index 1ca787e27..8c8a36c85 100644 --- a/docs/examples/plot_investigate_pipeline.py +++ b/docs/examples/plot_investigate_pipeline.py @@ -22,6 +22,7 @@ does not handle string type. This cannot be part of the final ONNX pipeline and must be removed. Look for comment starting with ``---`` below. """ + import skl2onnx import onnx import sklearn @@ -82,7 +83,7 @@ pipe.predict_proba(X_digits[:2]) -for i, step in enumerate(steps): +for _i, step in enumerate(steps): onnx_step = step["onnx_step"] sess = rt.InferenceSession( onnx_step.SerializeToString(), providers=["CPUExecutionProvider"] diff --git a/docs/examples/plot_onnx_operators.py b/docs/examples/plot_onnx_operators.py index 48fa0fbc9..74fca3213 100644 --- a/docs/examples/plot_onnx_operators.py +++ b/docs/examples/plot_onnx_operators.py @@ -35,6 +35,7 @@ on github `onnx.proto `_. """ + import onnxruntime import numpy import os @@ -85,7 +86,7 @@ # The list is dynamically created based on the installed # onnx package. -from skl2onnx.algebra.onnx_ops import OnnxPad # noqa +from skl2onnx.algebra.onnx_ops import OnnxPad pad = OnnxPad( "X", @@ -135,7 +136,7 @@ ##################################### # Which we translate into: -from skl2onnx.algebra.onnx_ops import OnnxTranspose # noqa +from skl2onnx.algebra.onnx_ops import OnnxTranspose node = OnnxTranspose( OnnxTranspose("X", perm=[1, 0, 2], op_version=12), perm=[1, 0, 2], op_version=12 @@ -157,10 +158,10 @@ def predict_with_onnxruntime(model_def, *inputs): model_def.SerializeToString(), providers=["CPUExecutionProvider"] ) names = [i.name for i in sess.get_inputs()] - dinputs = {name: input for name, input in zip(names, inputs)} + dinputs = dict(zip(names, inputs)) res = sess.run(None, dinputs) names = [o.name for o in sess.get_outputs()] - return {name: output for name, output in zip(names, res)} + return dict(zip(names, res)) Y = predict_with_onnxruntime(model_def, X) @@ -190,11 +191,11 @@ def predict_with_onnxruntime(model_def, *inputs): ################################# # **Versions used for this example** -import sklearn # noqa +import sklearn print("numpy:", numpy.__version__) print("scikit-learn:", sklearn.__version__) -import skl2onnx # noqa +import skl2onnx print("onnx: ", onnx.__version__) print("onnxruntime: ", onnxruntime.__version__) diff --git a/docs/examples/plot_pipeline_lightgbm.py b/docs/examples/plot_pipeline_lightgbm.py index 63b8df391..e8763f45a 100644 --- a/docs/examples/plot_pipeline_lightgbm.py +++ b/docs/examples/plot_pipeline_lightgbm.py @@ -19,6 +19,7 @@ Train a LightGBM classifier +++++++++++++++++++++++++++ """ + import lightgbm import onnxmltools import skl2onnx @@ -32,10 +33,10 @@ from skl2onnx import convert_sklearn, update_registered_converter from skl2onnx.common.shape_calculator import ( calculate_linear_classifier_output_shapes, -) # noqa +) from onnxmltools.convert.lightgbm.operator_converters.LightGbm import ( convert_lightgbm, -) # noqa +) import onnxmltools.convert.common.data_types from skl2onnx.common.data_types import FloatTensorType import numpy diff --git a/docs/examples/plot_pipeline_xgboost.py b/docs/examples/plot_pipeline_xgboost.py index 720373aa3..28933e97a 100644 --- a/docs/examples/plot_pipeline_xgboost.py +++ b/docs/examples/plot_pipeline_xgboost.py @@ -17,6 +17,7 @@ Train a XGBoost classifier ++++++++++++++++++++++++++ """ + import os import numpy import matplotlib.pyplot as plt @@ -34,11 +35,11 @@ from skl2onnx import convert_sklearn, update_registered_converter from skl2onnx.common.shape_calculator import ( calculate_linear_classifier_output_shapes, -) # noqa +) import onnxmltools from onnxmltools.convert.xgboost.operator_converters.XGBoost import ( convert_xgboost, -) # noqa +) import onnxmltools.convert.common.data_types data = load_iris() diff --git a/docs/exts/sphinx_skl2onnx_extension.py b/docs/exts/sphinx_skl2onnx_extension.py index d05f97110..c6b5d3bdd 100644 --- a/docs/exts/sphinx_skl2onnx_extension.py +++ b/docs/exts/sphinx_skl2onnx_extension.py @@ -3,6 +3,7 @@ """ Extension for sphinx. """ + from importlib import import_module import sphinx from docutils import nodes @@ -34,7 +35,7 @@ def skl2onnx_version_role( version = "v" + onnxruntime.__version__ else: raise RuntimeError( - "skl2onnx_version_role cannot interpret content '{0}'." "".format(text) + "skl2onnx_version_role cannot interpret content '{0}'.".format(text) ) node = nodes.literal(version) return [node], [] diff --git a/docs/tests/test_documentation_examples.py b/docs/tests/test_documentation_examples.py index ae61d33cb..4f53cb268 100644 --- a/docs/tests/test_documentation_examples.py +++ b/docs/tests/test_documentation_examples.py @@ -50,7 +50,7 @@ def run_test(self, fold: str, name: str, verbose=0) -> int: if verbose: print(f"failed: {name!r} due to missing dot.") return 0 - raise AssertionError( + raise AssertionError( # noqa: B904 "Example '{}' (cmd: {} - exec_prefix='{}') " "failed due to\n{}" "".format(name, cmds, sys.exec_prefix, st) @@ -79,13 +79,13 @@ def add_test_methods(cls): if reason: @unittest.skip(reason) - def _test_(self, name=name): + def _test_(self, name=name, fold=fold): res = self.run_test(fold, name, verbose=VERBOSE) self.assertTrue(res) else: - def _test_(self, name=name): + def _test_(self, name=name, fold=fold): res = self.run_test(fold, name, verbose=VERBOSE) self.assertTrue(res) diff --git a/docs/tutorial/plot_abegin_convert_pipeline.py b/docs/tutorial/plot_abegin_convert_pipeline.py index fae79bc22..2a5a34617 100644 --- a/docs/tutorial/plot_abegin_convert_pipeline.py +++ b/docs/tutorial/plot_abegin_convert_pipeline.py @@ -17,6 +17,7 @@ Training a pipeline +++++++++++++++++++ """ + import numpy from onnxruntime import InferenceSession from sklearn.datasets import load_diabetes diff --git a/docs/tutorial/plot_bbegin_measure_time.py b/docs/tutorial/plot_bbegin_measure_time.py index 3b6b83382..5d8950d42 100644 --- a/docs/tutorial/plot_bbegin_measure_time.py +++ b/docs/tutorial/plot_bbegin_measure_time.py @@ -14,6 +14,7 @@ Training a pipeline +++++++++++++++++++ """ + import numpy from pandas import DataFrame from tqdm import tqdm diff --git a/docs/tutorial/plot_catwoe_transformer.py b/docs/tutorial/plot_catwoe_transformer.py index aca734bd0..ddfe418bc 100644 --- a/docs/tutorial/plot_catwoe_transformer.py +++ b/docs/tutorial/plot_catwoe_transformer.py @@ -24,6 +24,7 @@ `_. Every feature is converter into integer. """ + import numpy as np from onnxruntime import InferenceSession from sklearn.datasets import load_iris @@ -35,7 +36,7 @@ from skl2onnx.algebra.onnx_ops import OnnxCast from skl2onnx.algebra.onnx_operator import OnnxSubEstimator from skl2onnx.sklapi import WOETransformer -import skl2onnx.sklapi.register # noqa +import skl2onnx.sklapi.register # noqa: F401 data = load_iris() X, y = data.data, data.target diff --git a/docs/tutorial/plot_cbegin_opset.py b/docs/tutorial/plot_cbegin_opset.py index b5341a7bc..3d7ee620c 100644 --- a/docs/tutorial/plot_cbegin_opset.py +++ b/docs/tutorial/plot_cbegin_opset.py @@ -33,6 +33,7 @@ A simple example. """ + from onnx.defs import onnx_opset_version from skl2onnx import to_onnx import numpy diff --git a/docs/tutorial/plot_dbegin_options_list.py b/docs/tutorial/plot_dbegin_options_list.py index 16dedc475..e65439817 100644 --- a/docs/tutorial/plot_dbegin_options_list.py +++ b/docs/tutorial/plot_dbegin_options_list.py @@ -19,6 +19,7 @@ The first converter to change its behaviour depending on a black list of operators is for model *GaussianMixture*. """ + from timeit import timeit import numpy from onnxruntime import InferenceSession diff --git a/docs/tutorial/plot_dbegin_options_zipmap.py b/docs/tutorial/plot_dbegin_options_zipmap.py index 2b71569c1..b8e173174 100644 --- a/docs/tutorial/plot_dbegin_options_zipmap.py +++ b/docs/tutorial/plot_dbegin_options_zipmap.py @@ -17,6 +17,7 @@ ++++++++++++++++++++++++++++ """ + from timeit import repeat import numpy import sklearn diff --git a/docs/tutorial/plot_ebegin_float_double.py b/docs/tutorial/plot_ebegin_float_double.py index e3fc22dd0..763bbc6f0 100644 --- a/docs/tutorial/plot_ebegin_float_double.py +++ b/docs/tutorial/plot_ebegin_float_double.py @@ -49,6 +49,7 @@ different results is not null. The following graph shows the discord areas. """ + from skl2onnx.sklapi import CastTransformer from skl2onnx import to_onnx from onnxruntime import InferenceSession diff --git a/docs/tutorial/plot_fbegin_investigate.py b/docs/tutorial/plot_fbegin_investigate.py index daecc4ca8..793519193 100644 --- a/docs/tutorial/plot_fbegin_investigate.py +++ b/docs/tutorial/plot_fbegin_investigate.py @@ -27,6 +27,7 @@ has *n* steps, it converts the pipeline with step 1, then the pipeline with steps 1, 2, then 1, 2, 3... """ + import numpy from onnx.reference import ReferenceEvaluator from onnxruntime import InferenceSession diff --git a/docs/tutorial/plot_gbegin_cst.py b/docs/tutorial/plot_gbegin_cst.py index 1f3a47902..9f8d0fa0e 100644 --- a/docs/tutorial/plot_gbegin_cst.py +++ b/docs/tutorial/plot_gbegin_cst.py @@ -17,6 +17,7 @@ We download one model from the :epkg:`ONNX Zoo` but the model could be trained and produced by another converter library. """ + import pprint import numpy from onnx import load diff --git a/docs/tutorial/plot_gbegin_dataframe.py b/docs/tutorial/plot_gbegin_dataframe.py index 3ac6228aa..ae0679d87 100644 --- a/docs/tutorial/plot_gbegin_dataframe.py +++ b/docs/tutorial/plot_gbegin_dataframe.py @@ -15,6 +15,7 @@ +++++++++++++++++++++++++ """ + import numpy import pprint from onnxruntime import InferenceSession diff --git a/docs/tutorial/plot_gconverting.py b/docs/tutorial/plot_gconverting.py index bf0cc91f6..e185b8c5f 100644 --- a/docs/tutorial/plot_gconverting.py +++ b/docs/tutorial/plot_gconverting.py @@ -11,6 +11,7 @@ +++++++++++++ """ + import numpy from onnxruntime import InferenceSession from sklearn.datasets import load_iris diff --git a/docs/tutorial/plot_gexternal_catboost.py b/docs/tutorial/plot_gexternal_catboost.py index a9c6bd48f..ed283a896 100644 --- a/docs/tutorial/plot_gexternal_catboost.py +++ b/docs/tutorial/plot_gexternal_catboost.py @@ -18,6 +18,7 @@ Train a CatBoostClassifier ++++++++++++++++++++++++++ """ + import numpy from onnx.helper import get_attribute_value from sklearn.datasets import load_iris @@ -27,7 +28,7 @@ from skl2onnx import convert_sklearn, update_registered_converter from skl2onnx.common.shape_calculator import ( calculate_linear_classifier_output_shapes, -) # noqa +) from skl2onnx.common.data_types import ( FloatTensorType, Int64TensorType, diff --git a/docs/tutorial/plot_gexternal_lightgbm.py b/docs/tutorial/plot_gexternal_lightgbm.py index afdfc0eda..5b93c3f0f 100644 --- a/docs/tutorial/plot_gexternal_lightgbm.py +++ b/docs/tutorial/plot_gexternal_lightgbm.py @@ -19,14 +19,15 @@ Train a LightGBM classifier +++++++++++++++++++++++++++ """ + import onnxruntime as rt from skl2onnx import convert_sklearn, update_registered_converter from skl2onnx.common.shape_calculator import ( calculate_linear_classifier_output_shapes, -) # noqa +) from onnxmltools.convert.lightgbm.operator_converters.LightGbm import ( convert_lightgbm, -) # noqa +) from skl2onnx.common.data_types import FloatTensorType import numpy from sklearn.datasets import load_iris diff --git a/docs/tutorial/plot_gexternal_lightgbm_reg.py b/docs/tutorial/plot_gexternal_lightgbm_reg.py index 000958584..329a37b25 100644 --- a/docs/tutorial/plot_gexternal_lightgbm_reg.py +++ b/docs/tutorial/plot_gexternal_lightgbm_reg.py @@ -35,6 +35,7 @@ Train a LGBMRegressor +++++++++++++++++++++ """ + import packaging.version as pv import warnings import timeit @@ -47,11 +48,11 @@ from skl2onnx import to_onnx, update_registered_converter from skl2onnx.common.shape_calculator import ( calculate_linear_regressor_output_shapes, -) # noqa +) from onnxmltools import __version__ as oml_version from onnxmltools.convert.lightgbm.operator_converters.LightGbm import ( convert_lightgbm, -) # noqa +) N = 1000 @@ -83,7 +84,8 @@ def skl2onnx_convert_lightgbm(scope, operator, container): if pv.Version(oml_version) < pv.Version("1.9.2"): warnings.warn( "Option split was released in version 1.9.2 but %s is " - "installed. It will be ignored." % oml_version + "installed. It will be ignored." % oml_version, + stacklevel=0, ) operator.split = options["split"] else: @@ -172,7 +174,7 @@ def skl2onnx_convert_lightgbm(scope, operator, container): # the parameter *split*. res = [] -for i in tqdm(list(range(20, 170, 20)) + [200, 300, 400, 500]): +for i in tqdm([*range(20, 170, 20), 200, 300, 400, 500]): model_onnx_split = to_onnx( reg, X[:1].astype(numpy.float32), @@ -194,7 +196,7 @@ def skl2onnx_convert_lightgbm(scope, operator, container): # Graph. _, ax = plt.subplots(1, 1) df.plot( - title="Sum of discrepancies against split\n" "split = number of tree per node", + title="Sum of discrepancies against split\nsplit = number of tree per node", ax=ax, ) diff --git a/docs/tutorial/plot_gexternal_xgboost.py b/docs/tutorial/plot_gexternal_xgboost.py index d05d2d158..284c18982 100644 --- a/docs/tutorial/plot_gexternal_xgboost.py +++ b/docs/tutorial/plot_gexternal_xgboost.py @@ -18,6 +18,7 @@ Train a XGBoost classifier ++++++++++++++++++++++++++ """ + import numpy import onnxruntime as rt from sklearn.datasets import load_iris, load_diabetes, make_classification diff --git a/docs/tutorial/plot_icustom_converter.py b/docs/tutorial/plot_icustom_converter.py index 7a82d94b8..72c19e0e0 100644 --- a/docs/tutorial/plot_icustom_converter.py +++ b/docs/tutorial/plot_icustom_converter.py @@ -31,6 +31,7 @@ If *X* is a matrix of features, :math:`V=\\frac{1}{n}X'X` is the covariance matrix. We compute :math:`X V^{1/2}`. """ + import pickle from io import BytesIO import numpy diff --git a/docs/tutorial/plot_jcustom_syntax.py b/docs/tutorial/plot_jcustom_syntax.py index 595526c77..8878eaffb 100644 --- a/docs/tutorial/plot_jcustom_syntax.py +++ b/docs/tutorial/plot_jcustom_syntax.py @@ -24,6 +24,7 @@ It basically copies what is in example `:ref:`l-plot-custom-converter`. """ + from skl2onnx.common.data_types import guess_proto_type from onnxconverter_common.onnx_ops import apply_sub from onnxruntime import InferenceSession diff --git a/docs/tutorial/plot_kcustom_converter_wrapper.py b/docs/tutorial/plot_kcustom_converter_wrapper.py index f93c35ab9..c9a1403e6 100644 --- a/docs/tutorial/plot_kcustom_converter_wrapper.py +++ b/docs/tutorial/plot_kcustom_converter_wrapper.py @@ -25,6 +25,7 @@ If *X* is a matrix of features, :math:`V=\\frac{1}{n}X'X` is the covariance matrix. We compute :math:`X V^{1/2}`. """ + import pickle from io import BytesIO import numpy diff --git a/docs/tutorial/plot_lcustom_options.py b/docs/tutorial/plot_lcustom_options.py index 805b26139..fd906899e 100644 --- a/docs/tutorial/plot_lcustom_options.py +++ b/docs/tutorial/plot_lcustom_options.py @@ -22,6 +22,7 @@ ++++++++++++ """ + from pandas import DataFrame from skl2onnx.tutorial import measure_time import numpy diff --git a/docs/tutorial/plot_mcustom_parser.py b/docs/tutorial/plot_mcustom_parser.py index 4ef837a75..856796f0a 100644 --- a/docs/tutorial/plot_mcustom_parser.py +++ b/docs/tutorial/plot_mcustom_parser.py @@ -24,6 +24,7 @@ A new transformer +++++++++++++++++ """ + import numpy from onnxruntime import InferenceSession from sklearn.base import TransformerMixin, BaseEstimator diff --git a/docs/tutorial/plot_ngrams.py b/docs/tutorial/plot_ngrams.py index e6ecb79bc..086b3cbc5 100644 --- a/docs/tutorial/plot_ngrams.py +++ b/docs/tutorial/plot_ngrams.py @@ -22,7 +22,7 @@ from sklearn.feature_extraction.text import TfidfVectorizer from skl2onnx import to_onnx from skl2onnx.sklapi import TraceableTfidfVectorizer -import skl2onnx.sklapi.register # noqa +import skl2onnx.sklapi.register # noqa: F401 corpus = numpy.array( [ diff --git a/docs/tutorial/plot_usparse_xgboost.py b/docs/tutorial/plot_usparse_xgboost.py index 03159e8ef..a9114ca1a 100644 --- a/docs/tutorial/plot_usparse_xgboost.py +++ b/docs/tutorial/plot_usparse_xgboost.py @@ -23,6 +23,7 @@ All imports. It also registered onnx converters for :epkg:`xgboost` and *lightgbm*. """ + import warnings import numpy import pandas diff --git a/docs/tutorial/plot_weird_pandas_and_hash.py b/docs/tutorial/plot_weird_pandas_and_hash.py index 5c7526dc3..a3d08da3a 100644 --- a/docs/tutorial/plot_weird_pandas_and_hash.py +++ b/docs/tutorial/plot_weird_pandas_and_hash.py @@ -10,6 +10,7 @@ Initial example +++++++++++++++ """ + import logging import numpy as np from pandas import DataFrame diff --git a/docs/tutorial/plot_wext_pyod_forest.py b/docs/tutorial/plot_wext_pyod_forest.py index fbfbb89fe..5c024984d 100644 --- a/docs/tutorial/plot_wext_pyod_forest.py +++ b/docs/tutorial/plot_wext_pyod_forest.py @@ -21,6 +21,7 @@ All imports. It also registered onnx converters for :epkg:`xgboost` and *lightgbm*. """ + import numpy as np import pandas as pd from onnxruntime import InferenceSession diff --git a/docs/tutorial/plot_woe_transformer.py b/docs/tutorial/plot_woe_transformer.py index ce93ee2ee..11d8ff998 100644 --- a/docs/tutorial/plot_woe_transformer.py +++ b/docs/tutorial/plot_woe_transformer.py @@ -22,6 +22,7 @@ (left-right-closed). The first interval is associated to weight 55 and and the second one to 107. """ + import os import numpy as np import pandas as pd @@ -32,7 +33,7 @@ from skl2onnx.sklapi import WOETransformer # automatically registers the converter for WOETransformer -import skl2onnx.sklapi.register # noqa +import skl2onnx.sklapi.register # noqa: F401 X = np.arange(10).astype(np.float32).reshape((-1, 1)) diff --git a/pyproject.toml b/pyproject.toml index 86f0d9b0a..c7cc432d3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,11 +7,47 @@ exclude = [ "dist", ] -# Same as Black. -line-length = 88 +line-length = 93 -[tool.ruff.lint.mccabe] -max-complexity = 10 +[tool.ruff.lint] +select = [ + "B", # flake8-bugbear + "C4", # flake8-comprehensions + #"D", # pydocstyle + "E", # pycodestyle + "F", # Pyflakes + "G", # flake8-logging-format + #"I", # isort + "ISC", # flake8-implicit-str-concat + "LOG", # flake8-logging + #"N", # pep8-naming + #"NPY", # modern numpy + #"PERF", # Perflint + "PIE", # flake8-pie + "PYI", # flake8-pyi + "RUF", # Ruff-specific rules + "SIM", # flake8-simplify + "SLOT", # flake8-slot + "T10", # flake8-debugger + #"TID", # Disallow relative imports + #"TRY", # flake8-try-except-raise + "UP", # pyupgrade + "W", # pycodestyle + "YTT", # flake8-2020 +] [tool.ruff.lint.per-file-ignores] +"**" = ["C413", "C408", "C417", "E731", "PIE808", "RUF012", "RUF015", "SIM103", "SIM108", "SIM114", "SIM910", "UP008", "UP015", "UP028", "UP030", "UP031", "UP032"] +"**/__init__.py" = ["F401"] +"docs/**" = ["B018", "E402"] "skl2onnx/algebra/onnx_ops.py" = ["F821"] +"skl2onnx/common/_apply_operation.py" = ["F403", "F405"] +"tests/**" = [ + "B007", "B019", "B028", "B904", + "C401", "C403", "C405", "C406", "C408", "C413", "C416", "C417", + "PIE808", "PIE810", + "RUF005", "RUF012", "RUF010", "RUF015", + "SIM102", "SIM105", "SIM113", "SIM114", "SIM118", "SIM300", + "UP015", "UP018", "UP028", "UP030", "UP031", "UP032" +] +"tests_onnxmltools/**" = ["B028", "B904", "C403", "C408", "C413", "C417", "PIE808", "PIE810", "RUF010", "RUF015", "SIM102", "SIM105", "SIM118", "UP015", "UP028", "UP030", "UP031", "UP032"] diff --git a/skl2onnx/__init__.py b/skl2onnx/__init__.py index 431380c70..a3fadf515 100644 --- a/skl2onnx/__init__.py +++ b/skl2onnx/__init__.py @@ -3,6 +3,7 @@ """ Main entry point to the converter from the *scikit-learn* to *onnx*. """ + __version__ = "1.18.0" __author__ = "Microsoft" __producer__ = "skl2onnx" @@ -12,10 +13,10 @@ __max_supported_opset__ = 21 # Converters are tested up to this version. -from .convert import convert_sklearn, to_onnx, wrap_as_onnx_mixin # noqa -from ._supported_operators import update_registered_converter, get_model_alias # noqa -from ._parse import update_registered_parser # noqa -from .proto import get_latest_tested_opset_version # noqa +from .convert import convert_sklearn, to_onnx, wrap_as_onnx_mixin +from ._supported_operators import update_registered_converter, get_model_alias +from ._parse import update_registered_parser +from .proto import get_latest_tested_opset_version def supported_converters(from_sklearn=False): @@ -32,11 +33,11 @@ def supported_converters(from_sklearn=False): whose name is prefixed by ``'Sklearn'`` :return: list of supported models as string """ - from .common._registration import _converter_pool # noqa + from .common._registration import _converter_pool # The two following lines populates the list of supported converters. - from . import shape_calculators # noqa - from . import operator_converters # noqa + from . import shape_calculators + from . import operator_converters names = sorted(_converter_pool.keys()) if from_sklearn: diff --git a/skl2onnx/_parse.py b/skl2onnx/_parse.py index b3c158fab..cf2a685bf 100644 --- a/skl2onnx/_parse.py +++ b/skl2onnx/_parse.py @@ -76,11 +76,11 @@ def _fetch_input_slice(scope, inputs, column_indices): raise TypeError("Parameter inputs must be a list.") if len(inputs) == 0: raise RuntimeError( - "Operator ArrayFeatureExtractor requires at " "least one inputs." + "Operator ArrayFeatureExtractor requires at least one inputs." ) if len(inputs) != 1: raise RuntimeError( - "Operator ArrayFeatureExtractor does not support " "multiple input tensors." + "Operator ArrayFeatureExtractor does not support multiple input tensors." ) if ( isinstance(inputs[0].type, TensorType) @@ -117,11 +117,11 @@ def _parse_sklearn_simple_model(scope, model, inputs, custom_parsers=None, alias # alias can be None if isinstance(model, str): raise RuntimeError( - "Parameter model must be an object not a " "string '{0}'.".format(model) + "Parameter model must be an object not a string '{0}'.".format(model) ) if any(not isinstance(i, Variable) for i in inputs): raise TypeError( - "One input is not a Variable for model %r - %r." "" % (model, inputs) + "One input is not a Variable for model %r - %r." % (model, inputs) ) if alias is None: alias = _get_sklearn_operator_name(type(model)) @@ -140,6 +140,7 @@ def _parse_sklearn_simple_model(scope, model, inputs, custom_parsers=None, alias "The parser signature should parser(scope=None, " "inputs=None)." % (parser_names, e, type(model)), DeprecationWarning, + stacklevel=0, ) names = parser_names() if names is not None: @@ -565,6 +566,7 @@ def _parse_sklearn_multi_output_classifier(scope, model, inputs, custom_parsers= "Set option zipmap to False to " "remove this message." % type(model), UserWarning, + stacklevel=0, ) alias = _get_sklearn_operator_name(type(model)) this_operator = scope.declare_local_operator(alias, model) @@ -574,10 +576,9 @@ def _parse_sklearn_multi_output_classifier(scope, model, inputs, custom_parsers= classes = model.classes_ else: classes = [get_label_classes(scope, m) for m in model.estimators_] - if len(set(cl.dtype for cl in classes)) != 1: + if len({cl.dtype for cl in classes}) != 1: raise RuntimeError( - "Class labels may have only one type %r." - "" % set(cl.dtype for cl in classes) + "Class labels may have only one type %r." % {cl.dtype for cl in classes} ) if classes[0].dtype in (np.int32, np.int64, np.bool_): ctype = Int64TensorType @@ -600,7 +601,7 @@ def _parse_sklearn_multi_output_classifier(scope, model, inputs, custom_parsers= "class_labels", SequenceType(ctype()) ) clout.outputs.append(class_labels) - return list(this_operator.outputs) + [class_labels] + return [*this_operator.outputs, class_labels] return this_operator.outputs diff --git a/skl2onnx/_supported_operators.py b/skl2onnx/_supported_operators.py index 34f017cc3..7343bb1e8 100644 --- a/skl2onnx/_supported_operators.py +++ b/skl2onnx/_supported_operators.py @@ -276,7 +276,7 @@ # Second verification as these models still require # manual activation. try: - from sklearn.ensemble._hist_gradient_boosting.gradient_boosting import ( # noqa + from sklearn.ensemble._hist_gradient_boosting.gradient_boosting import ( HistGradientBoostingClassifier, HistGradientBoostingRegressor, ) @@ -543,8 +543,12 @@ def update_registered_converter( :: - from skl2onnx.common.shape_calculator import calculate_linear_classifier_output_shapes - from skl2onnx.operator_converters.RandomForest import convert_sklearn_random_forest_classifier + from skl2onnx.common.shape_calculator import ( + calculate_linear_classifier_output_shapes, + ) + from skl2onnx.operator_converters.RandomForest import ( + convert_sklearn_random_forest_classifier, + ) from skl2onnx import update_registered_converter update_registered_converter( SGDClassifier, 'SklearnLinearClassifier', @@ -558,7 +562,7 @@ def update_registered_converter( option `'zipmap'` is added to the list. Every classifier must declare this option to let the default parser automatically handle that option. - """ # noqa + """ if ( not overwrite and model in sklearn_operator_name_map @@ -566,7 +570,8 @@ def update_registered_converter( ): warnings.warn( "Model '{0}' was already registered under alias " - "'{1}'.".format(model, sklearn_operator_name_map[model]) + "'{1}'.".format(model, sklearn_operator_name_map[model]), + stacklevel=0, ) sklearn_operator_name_map[model] = alias register_converter(alias, convert_fct, overwrite=overwrite, options=options) @@ -592,7 +597,7 @@ def _get_sklearn_operator_name(model_type): :return: A string which stands for the type of the input model in our conversion framework """ - if model_type not in sklearn_operator_name_map: + if model_type not in sklearn_operator_name_map: # noqa: SIM401 # No proper operator name found, it means a local operator. alias = None else: diff --git a/skl2onnx/algebra/__init__.py b/skl2onnx/algebra/__init__.py index 12bfd17f9..345130211 100644 --- a/skl2onnx/algebra/__init__.py +++ b/skl2onnx/algebra/__init__.py @@ -1,4 +1,4 @@ # SPDX-License-Identifier: Apache-2.0 -from .onnx_operator import OnnxOperator # noqa +from .onnx_operator import OnnxOperator diff --git a/skl2onnx/algebra/automation.py b/skl2onnx/algebra/automation.py index a96c8a951..3ed93da0f 100644 --- a/skl2onnx/algebra/automation.py +++ b/skl2onnx/algebra/automation.py @@ -2,7 +2,7 @@ import textwrap import onnx -import onnx.defs # noqa +import onnx.defs def _get_doc_template(): @@ -182,7 +182,7 @@ def format_doc(cl): classes = dynamic_class_creation_sklearn() tmpl = _template_operator_sklearn - values = [(k, v) for k, v in sorted(classes.items())] + values = list(sorted(classes.items())) values = [_[1] for _ in values] docs = tmpl.render(len=len, classes=values, format_doc=format_doc) return docs diff --git a/skl2onnx/algebra/graph_state.py b/skl2onnx/algebra/graph_state.py index e2d434ab3..552deed9a 100644 --- a/skl2onnx/algebra/graph_state.py +++ b/skl2onnx/algebra/graph_state.py @@ -44,7 +44,7 @@ def __init__( operator=None, run_converters=False, input_types=None, - **attrs + **attrs, ): logger.debug( "[State] +%s n_inputs=%r n_outputs=%r", @@ -100,9 +100,7 @@ def __init__( if v is None: continue if not isinstance(v, list): - raise TypeError( - "Attribute %r must be a list not %r." "" % (att, type(v)) - ) + raise TypeError("Attribute %r must be a list not %r." % (att, type(v))) for i, vi in enumerate(v): if hasattr(vi, "state") or hasattr(vi, "onx_op"): continue @@ -216,7 +214,7 @@ def __fct__(var, operator): except ValueError: pass raise RuntimeError( - "Unexpected type for parameter 'var': {0}." "".format(type(var)) + "Unexpected type for parameter 'var': {0}.".format(type(var)) ) try: @@ -266,7 +264,7 @@ def _ty_astype(cst): ty = guess_proto_type(_guess_numpy_type(cst.dtype, cst.shape)) except NotImplementedError as e: st = str(astype).lower() - if st.startswith("u") or st.startswith(" self.input_range[1] - ): - raise RuntimeError( - "Operator '{}' expects a number of inputs " - "in [{}, {}] not {} (expected opset={}, " - "class opset={})".format( - self.operator_name, - *self.input_range, - len(self.inputs), - op_version, - self.op_version - ) + if self.inputs is not None and ( + len(self.inputs) < self.input_range[0] + or len(self.inputs) > self.input_range[1] + ): + raise RuntimeError( + "Operator '{}' expects a number of inputs " + "in [{}, {}] not {} (expected opset={}, " + "class opset={})".format( + self.operator_name, + *self.input_range, + len(self.inputs), + op_version, + self.op_version, ) + ) # global context if global_context is None: self.global_context = None @@ -501,7 +500,7 @@ def __init__( inp = (inp, None) elif hasattr(inp, "add_to"): # OnnxOperator - existing = set(_[0] for _ in self.expected_inputs) + existing = {_[0] for _ in self.expected_inputs} i = 10 name = "input%d" % (10 + i) while name in existing: @@ -541,7 +540,7 @@ def _post_process_attributes(self, clear_subgraph_inputs=False): self.graph_algebra = graph_algebra if clear_subgraph_inputs: - for k, v in self.kwargs.items(): + for _k, v in self.kwargs.items(): if isinstance(v, GraphProto): del v.input[:] @@ -640,9 +639,7 @@ def get_output(self, i, scope=None): if self.output_names_ is not None: res = self.output_names_[i] if not isinstance(res, (tuple, Variable)): - raise RuntimeError( - "Unable to retrieve output %r from %r." "" % (i, self) - ) + raise RuntimeError("Unable to retrieve output %r from %r." % (i, self)) return res def _set_output_names_(self, scope, operator): @@ -715,7 +712,7 @@ def _add_to_inputs(self, operator): map(lambda o: "'%s'" % o.onnx_name, operator.inputs) ) raise RuntimeError( - "Unable to find variable " "{} in {}.".format(input, vars) + "Unable to find variable {} in {}.".format(input, vars) ) else: inputs.append(input) @@ -776,7 +773,7 @@ def add_to(self, scope, container, operator=None, run_converters=False): output_range=self.output_range, operator=operator, run_converters=run_converters, - **kwargs + **kwargs, ) self.state.run() self._verify_add_to_() @@ -843,7 +840,7 @@ def get_output_type_inference(self, input_shapes=None): continue given[i] = dt.type rev = {} - for i, (name, v) in enumerate(expected_inputs): + for i, (_name, v) in enumerate(expected_inputs): if v in rev: rev[v].append(i) else: @@ -964,7 +961,7 @@ def to_onnx( if inputs is None: raise NotImplementedError("inputs must be specified.") if isinstance(inputs, dict): - inputs = [(k, v) for k, v in inputs.items()] + inputs = list(inputs.items()) new_inputs = [] for obj in inputs: if isinstance(obj, Variable): @@ -1038,7 +1035,7 @@ def to_onnx( # infer shapes if outputs: if isinstance(outputs, dict): - outputs = [(k, v) for k, v in outputs.items()] + outputs = list(outputs.items()) shapes = [] for o in outputs: if isinstance(o, Variable): @@ -1050,7 +1047,7 @@ def to_onnx( type_shape = o[1] shapes.append(Variable(o[0], o[0], None, type_shape)) else: - raise TypeError("Outputs must be Variable or " "tuple(name, type).") + raise TypeError("Outputs must be Variable or tuple(name, type).") logger.debug( "[Ops.to_onnx] %s id=%d outputs=%r", self.__class__.__name__, @@ -1067,10 +1064,10 @@ def to_onnx( target_opset=target_opset, ) if self.output_names: - set_names = set( + set_names = { v.onnx_name if hasattr(v, "onnx_name") else v for v in self.output_names - ) + } shapes = [shape for shape in shapes if shape.onnx_name in set_names] logger.debug( @@ -1190,7 +1187,7 @@ def __init__( domain=None, options=None, input_types=None, - **kwargs + **kwargs, ): OnnxOperator.__init__( self, @@ -1198,7 +1195,7 @@ def __init__( op_version=op_version, output_names=output_names, domain=domain, - **kwargs + **kwargs, ) self.operator_instance = skl_op self.options = options @@ -1288,7 +1285,7 @@ def add_to(self, scope, container, operator=None, run_converters=False): map(lambda o: "'%s'" % o.onnx_name, operator.inputs) ) raise RuntimeError( - "Unable to find variable " "{} in {}.".format(input, vars) + "Unable to find variable {} in {}.".format(input, vars) ) elif isinstance(input, tuple) and len(input) == 2: if scope is not None and input[0] in scope.variables: @@ -1315,7 +1312,7 @@ def add_to(self, scope, container, operator=None, run_converters=False): options=self.options, run_converters=run_converters, input_types=self.input_types, - **kwargs + **kwargs, ) self.state.run() @@ -1341,4 +1338,5 @@ def __init__(self, *args, **kwargs): "It should be replaced by OnnxSubEstimator." ), DeprecationWarning, + stacklevel=0, ) diff --git a/skl2onnx/algebra/onnx_operator_mixin.py b/skl2onnx/algebra/onnx_operator_mixin.py index 8fab57d66..03abb6e08 100644 --- a/skl2onnx/algebra/onnx_operator_mixin.py +++ b/skl2onnx/algebra/onnx_operator_mixin.py @@ -218,6 +218,7 @@ def converter( "This will be the case in version 1.11, class=%r." "" % type(self), DeprecationWarning, + stacklevel=0, ) try: if inputs: diff --git a/skl2onnx/algebra/onnx_ops.py b/skl2onnx/algebra/onnx_ops.py index fb270a7af..eb6c00032 100644 --- a/skl2onnx/algebra/onnx_ops.py +++ b/skl2onnx/algebra/onnx_ops.py @@ -3,6 +3,7 @@ """ Place holder for all ONNX operators. """ + import sys import numpy as np @@ -54,7 +55,7 @@ def __init__(self, *args, **kwargs): try: op_version = min(op_version, op_version_class) except TypeError: - raise TypeError( + raise TypeError( # noqa: B904 "Could not compare versions {} ? {} for " "class '{}' since_version {}. Parameter 'op_version' " "is probably missing when the class " @@ -193,7 +194,7 @@ def _c(obj, label, i): schema = res[name] inputs = [_c(o, "I", i) for i, o in enumerate(schema.inputs)] outputs = [_c(o, "O", i) for i, o in enumerate(schema.outputs)] - args = [p for p in schema.attributes] + args = list(schema.attributes) class_name = "Onnx" + (name if "_" in name else schema.name) doc = f"See `{name} `_." @@ -247,10 +248,10 @@ def OnnxReduceSumApi11(*x, axes=None, keepdims=1, op_version=None, output_names= raise RuntimeError("op_version must be specified.") if op_version is None or op_version >= 13: if axes is None: - return OnnxReduceSum( # noqa + return OnnxReduceSum( *x, keepdims=keepdims, op_version=op_version, output_names=output_names ) - return OnnxReduceSum( # noqa + return OnnxReduceSum( *x, np.array(axes, dtype=np.int64), keepdims=keepdims, @@ -259,10 +260,10 @@ def OnnxReduceSumApi11(*x, axes=None, keepdims=1, op_version=None, output_names= ) if op_version >= 11: if axes is None: - return OnnxReduceSum_11( # noqa + return OnnxReduceSum_11( *x, keepdims=keepdims, op_version=op_version, output_names=output_names ) - return OnnxReduceSum_11( # noqa + return OnnxReduceSum_11( *x, axes=axes, keepdims=keepdims, @@ -272,14 +273,14 @@ def OnnxReduceSumApi11(*x, axes=None, keepdims=1, op_version=None, output_names= if axes is None: return OnnxReduceSum_1( *x, - keepdims=keepdims, # noqa + keepdims=keepdims, op_version=op_version, output_names=output_names, ) return OnnxReduceSum_1( *x, axes=axes, - keepdims=keepdims, # noqa + keepdims=keepdims, op_version=op_version, output_names=output_names, ) @@ -295,10 +296,10 @@ def OnnxReduceAnyApi18( raise RuntimeError("op_version must be specified.") if op_version is None or op_version >= 18: if axes is None: - return cl18( # noqa + return cl18( *x, keepdims=keepdims, op_version=op_version, output_names=output_names ) - return cl18( # noqa + return cl18( *x, np.array(axes, dtype=np.int64), keepdims=keepdims, @@ -309,14 +310,14 @@ def OnnxReduceAnyApi18( if axes is None: return cl13( *x, - keepdims=keepdims, # noqa + keepdims=keepdims, op_version=op_version, output_names=output_names, ) return cl13( *x, axes=axes, - keepdims=keepdims, # noqa + keepdims=keepdims, op_version=op_version, output_names=output_names, ) @@ -324,28 +325,28 @@ def OnnxReduceAnyApi18( if axes is None: return cl11( *x, - keepdims=keepdims, # noqa + keepdims=keepdims, op_version=op_version, output_names=output_names, ) return cl11( *x, axes=axes, - keepdims=keepdims, # noqa + keepdims=keepdims, op_version=op_version, output_names=output_names, ) if axes is None: return cl1( *x, - keepdims=keepdims, # noqa + keepdims=keepdims, op_version=op_version, output_names=output_names, ) return cl1( *x, axes=axes, - keepdims=keepdims, # noqa + keepdims=keepdims, op_version=op_version, output_names=output_names, ) @@ -361,9 +362,9 @@ def OnnxReduceSumSquareApi18( raise TypeError(f"axes must be a list or an array not {type(axes)}.") return OnnxReduceAnyApi18( OnnxReduceSumSquare, - OnnxReduceSumSquare_13, # noqa + OnnxReduceSumSquare_13, OnnxReduceSumSquare_11, - OnnxReduceSumSquare_1, # noqa + OnnxReduceSumSquare_1, *x, axes=axes, keepdims=keepdims, @@ -378,9 +379,9 @@ def OnnxReduceMeanApi18(*x, axes=None, keepdims=1, op_version=None, output_names """ return OnnxReduceAnyApi18( OnnxReduceMean, - OnnxReduceMean_13, # noqa + OnnxReduceMean_13, OnnxReduceMean_11, - OnnxReduceMean_1, # noqa + OnnxReduceMean_1, *x, axes=axes, keepdims=keepdims, @@ -395,9 +396,9 @@ def OnnxReduceMaxApi18(*x, axes=None, keepdims=1, op_version=None, output_names= """ return OnnxReduceAnyApi18( OnnxReduceMax, - OnnxReduceMax_13, # noqa + OnnxReduceMax_13, OnnxReduceMax_11, - OnnxReduceMax_1, # noqa + OnnxReduceMax_1, *x, axes=axes, keepdims=keepdims, @@ -414,9 +415,9 @@ def OnnxReduceLogSumExpApi18( """ return OnnxReduceAnyApi18( OnnxReduceLogSumExp, - OnnxReduceLogSumExp_13, # noqa + OnnxReduceLogSumExp_13, OnnxReduceLogSumExp_11, - OnnxReduceLogSumExp_1, # noqa + OnnxReduceLogSumExp_1, *x, axes=axes, keepdims=keepdims, @@ -431,9 +432,9 @@ def OnnxReduceL2Api18(*x, axes=None, keepdims=1, op_version=None, output_names=N """ return OnnxReduceAnyApi18( OnnxReduceL2, - OnnxReduceL2_13, # noqa + OnnxReduceL2_13, OnnxReduceL2_11, - OnnxReduceL2_1, # noqa + OnnxReduceL2_1, *x, axes=axes, keepdims=keepdims, @@ -461,7 +462,7 @@ def OnnxSplitApi18( num_outputs = len(output_names) if num_outputs is None: raise AttributeError("num_outputs cannot be None for Split-18.") - return OnnxSplit_18( # noqa + return OnnxSplit_18( *x, axis=axis, op_version=op_version, @@ -469,14 +470,14 @@ def OnnxSplitApi18( output_names=output_names, ) if num_outputs is None: - return OnnxSplit_18( # noqa + return OnnxSplit_18( *x, np.array(split, dtype=np.int64), axis=axis, op_version=op_version, output_names=output_names, ) - return OnnxSplit_18( # noqa + return OnnxSplit_18( *x, np.array(split, dtype=np.int64), axis=axis, @@ -486,10 +487,10 @@ def OnnxSplitApi18( ) if op_version >= 13: if split is None: - return OnnxSplit_13( # noqa + return OnnxSplit_13( *x, axis=axis, op_version=op_version, output_names=output_names ) - return OnnxSplit_13( # noqa + return OnnxSplit_13( *x, np.array(split, dtype=np.int64), axis=axis, @@ -498,20 +499,20 @@ def OnnxSplitApi18( ) if op_version >= 11: if split is None: - return OnnxSplit_11( # noqa + return OnnxSplit_11( *x, axis=axis, op_version=op_version, output_names=output_names ) - return OnnxSplit_11( # noqa + return OnnxSplit_11( *x, split=split, axis=axis, op_version=op_version, output_names=output_names ) if split is None: - return OnnxSplit_2( # noqa + return OnnxSplit_2( *x, axis=axis, op_version=op_version, output_names=output_names ) return OnnxSplit_2( *x, split=split, - axis=axis, # noqa + axis=axis, op_version=op_version, output_names=output_names, ) @@ -524,18 +525,21 @@ def OnnxSqueezeApi11(*x, axes=None, op_version=None, output_names=None): if op_version is None: raise RuntimeError("op_version must be specified.") if op_version is None or op_version >= 13: - return OnnxSqueeze( # noqa + return OnnxSqueeze( *x, np.array(axes, dtype=np.int64), op_version=op_version, output_names=output_names, ) if op_version >= 11: - return OnnxSqueeze_11( # noqa + return OnnxSqueeze_11( *x, axes=axes, op_version=op_version, output_names=output_names ) return OnnxSqueeze_1( - *x, axes=axes, op_version=op_version, output_names=output_names # noqa + *x, + axes=axes, + op_version=op_version, + output_names=output_names, ) @@ -546,18 +550,21 @@ def OnnxUnsqueezeApi11(*x, axes=None, op_version=None, output_names=None): if op_version is None: raise RuntimeError("op_version must be specified.") if op_version is None or op_version >= 13: - return OnnxUnsqueeze( # noqa + return OnnxUnsqueeze( *x, np.array(axes, dtype=np.int64), op_version=op_version, output_names=output_names, ) if op_version >= 11: - return OnnxUnsqueeze_11( # noqa + return OnnxUnsqueeze_11( *x, axes=axes, op_version=op_version, output_names=output_names ) return OnnxUnsqueeze_1( - *x, axes=axes, op_version=op_version, output_names=output_names # noqa + *x, + axes=axes, + op_version=op_version, + output_names=output_names, ) @@ -568,16 +575,16 @@ def OnnxReduceL2_typed( Adds operator ReduceL2 for float or double. """ if dtype == np.float32: - return OnnxReduceL2Api18( # noqa + return OnnxReduceL2Api18( x, axes=axes, keepdims=keepdims, op_version=op_version, output_names=output_names, ) - x2 = OnnxMul(x, x, op_version=op_version) # noqa + x2 = OnnxMul(x, x, op_version=op_version) red = OnnxReduceSumApi11(x2, axes=[1], keepdims=1, op_version=op_version) - return OnnxSqrt(red, op_version=op_version, output_names=output_names) # noqa + return OnnxSqrt(red, op_version=op_version, output_names=output_names) def OnnxReshapeApi13(*x, allowzero=0, op_version=None, output_names=None): @@ -587,11 +594,9 @@ def OnnxReshapeApi13(*x, allowzero=0, op_version=None, output_names=None): if op_version is None: raise RuntimeError("op_version must be specified.") if op_version is None or op_version >= 14: - return OnnxReshape( # noqa + return OnnxReshape( *x, allowzero=allowzero, op_version=op_version, output_names=output_names ) if op_version >= 13: - return OnnxReshape_13( # noqa - *x, op_version=op_version, output_names=output_names - ) - return OnnxReshape_5(*x, op_version=op_version, output_names=output_names) # noqa + return OnnxReshape_13(*x, op_version=op_version, output_names=output_names) + return OnnxReshape_5(*x, op_version=op_version, output_names=output_names) diff --git a/skl2onnx/algebra/onnx_subgraph_operator_mixin.py b/skl2onnx/algebra/onnx_subgraph_operator_mixin.py index e265b7198..4dba571a3 100644 --- a/skl2onnx/algebra/onnx_subgraph_operator_mixin.py +++ b/skl2onnx/algebra/onnx_subgraph_operator_mixin.py @@ -7,5 +7,3 @@ class OnnxSubGraphOperatorMixin(OnnxOperatorMixin): """ :class:`OnnxOperatorMixin` for converters. """ - - pass diff --git a/skl2onnx/algebra/sklearn_ops.py b/skl2onnx/algebra/sklearn_ops.py index 320053c18..6bb8cedd9 100644 --- a/skl2onnx/algebra/sklearn_ops.py +++ b/skl2onnx/algebra/sklearn_ops.py @@ -3,6 +3,7 @@ """ Place holder for all ONNX operators. """ + import sys import textwrap from sklearn.pipeline import Pipeline, FeatureUnion diff --git a/skl2onnx/common/__init__.py b/skl2onnx/common/__init__.py index e3075a040..a1a29ba99 100644 --- a/skl2onnx/common/__init__.py +++ b/skl2onnx/common/__init__.py @@ -3,4 +3,4 @@ # skl2onnx common code has been refactored into onnxconverter-common. -from .exceptions import MissingShapeCalculator, MissingConverter # noqa +from .exceptions import MissingShapeCalculator, MissingConverter diff --git a/skl2onnx/common/_apply_operation.py b/skl2onnx/common/_apply_operation.py index 786624495..b655ac6d0 100644 --- a/skl2onnx/common/_apply_operation.py +++ b/skl2onnx/common/_apply_operation.py @@ -11,7 +11,7 @@ def np_dtype_to_tensor_dtype(dtype): return NP_TYPE_TO_TENSOR_TYPE[dtype] -from onnxconverter_common.onnx_ops import * # noqa +from onnxconverter_common.onnx_ops import * from ..proto import onnx_proto @@ -64,7 +64,7 @@ def apply_normalizer(scope, inputs, outputs, container, norm, use_float): keepdims=1, name=scope.get_unique_operator_name("ReduceSum"), ) - apply_div( # noqa + apply_div( scope, [input, norm], output, @@ -98,7 +98,7 @@ def apply_normalizer(scope, inputs, outputs, container, norm, use_float): container.add_node( "Sqrt", [norm], norm2, name=scope.get_unique_operator_name("Sqrt") ) - apply_div( # noqa + apply_div( scope, [input, norm2], output, diff --git a/skl2onnx/common/_container.py b/skl2onnx/common/_container.py index 7245612e2..c344c7806 100644 --- a/skl2onnx/common/_container.py +++ b/skl2onnx/common/_container.py @@ -36,9 +36,9 @@ def _get_operation_list(use_shortlist=True): else: shortlist = None regs = [ - re.compile("container.add_node[(]'([A-Z][a-zA-Z0-9]*)', " "\\[?input_name"), - re.compile("container.add_node[(]'([A-Z][a-zA-Z0-9]*)', " "\\[\\]"), - re.compile("container.add_node[(]'([A-Z][a-zA-Z0-9]*)', " "inputs"), + re.compile("container.add_node[(]'([A-Z][a-zA-Z0-9]*)', \\[?input_name"), + re.compile("container.add_node[(]'([A-Z][a-zA-Z0-9]*)', \\[\\]"), + re.compile("container.add_node[(]'([A-Z][a-zA-Z0-9]*)', inputs"), re.compile("scope, '([A-Z][a-zA-Z0-9]*)', \\[?input_name"), re.compile("op_type = '([A-Z][a-zA-Z0-9]*)'"), ] @@ -118,14 +118,10 @@ def check_white_black_list(self, node_type): Checks a node type is allowed according to white and black lists. """ - if self._white_op: - if node_type not in self._white_op: - raise RuntimeError( - "Operator '{}' is not white listed.".format(node_type) - ) - if self._black_op: - if node_type in self._black_op: - raise RuntimeError("Operator '{}' is black listed.".format(node_type)) + if self._white_op and node_type not in self._white_op: + raise RuntimeError("Operator '{}' is not white listed.".format(node_type)) + if self._black_op and node_type in self._black_op: + raise RuntimeError("Operator '{}' is black listed.".format(node_type)) def debug(self, *args, **kwargs): """ @@ -342,7 +338,7 @@ def swap_names(self, old_name, new_name): if modified: if node.op_type in exc_list: raise NotImplementedError( - "Unable to handle subgraphs for node type %r." "" % node.op_type + "Unable to handle subgraphs for node type %r." % node.op_type ) node.input[:] = new_input[:] node.output[:] = new_output[:] @@ -633,7 +629,7 @@ def add_node( ) if name is None or not isinstance(name, str) or name == "": name = f"N{len(self.nodes)}" - existing_names = set(n.name for n in self.nodes) + existing_names = {n.name for n in self.nodes} if name in existing_names: name += f"-N{len(self.nodes)}" @@ -668,12 +664,12 @@ def add_node( "with name '{}'.".format(common, op_type, name) ) if not isinstance(inputs, list) or not all(isinstance(s, str) for s in inputs): - type_list = ",".join(list(str(type(s)) for s in inputs)) + type_list = ",".join([str(type(s)) for s in inputs]) raise ValueError("Inputs must be a list of string but get [%s]" % type_list) if not isinstance(outputs, list) or not all( isinstance(s, str) for s in outputs ): - type_list = ",".join(list(str(type(s)) for s in outputs)) + type_list = ",".join([str(type(s)) for s in outputs]) raise ValueError( "Outputs must be a list of string but get [%s]" % type_list ) @@ -736,7 +732,7 @@ def add_node( def target_opset_any_domain(self, domain): target_opset = self.target_opset_all if isinstance(target_opset, dict): - if domain in target_opset: + if domain in target_opset: # noqa: SIM401 to = target_opset[domain] else: to = None @@ -773,7 +769,8 @@ def _get_op_version(self, domain, op_type): else: warnings.warn( "Unable to find operator '{}' in domain '{}' in ONNX, " - "op_version is forced to 1.".format(op_type, domain) + "op_version is forced to 1.".format(op_type, domain), + stacklevel=0, ) vers = [1] highest = self.target_opset_any_domain(domain) diff --git a/skl2onnx/common/_onnx_optimisation_common.py b/skl2onnx/common/_onnx_optimisation_common.py index e45753f9c..d790b3fc2 100644 --- a/skl2onnx/common/_onnx_optimisation_common.py +++ b/skl2onnx/common/_onnx_optimisation_common.py @@ -4,6 +4,7 @@ Common functions to reduce the number of nodes of an :epkg:`ONNX` graphs. """ + from onnx.helper import make_graph, ValueInfoProto, make_model from onnx import AttributeProto, NodeProto from onnx.helper import make_attribute @@ -24,7 +25,7 @@ def _apply_optimisation_on_graph( return: new onnx model """ if hasattr(onnx_model, "graph"): - graph = fct(onnx_model.graph, debug_info=debug_info + ["GRAPH"], **kwargs) + graph = fct(onnx_model.graph, debug_info=[*debug_info, "GRAPH"], **kwargs) new_model = make_model(graph) new_model.ir_version = onnx_model.ir_version new_model.producer_name = onnx_model.producer_name @@ -62,7 +63,7 @@ def _apply_remove_node_fct_node(fct, node, recursive, debug_info): for att in node.attribute: if att.name == "body": new_body = fct( - att.g, recursive=recursive, debug_info=debug_info + [att.name] + att.g, recursive=recursive, debug_info=[*debug_info, att.name] ) new_atts.append(_make_att_graph(att.name, new_body)) modified += 1 diff --git a/skl2onnx/common/_registration.py b/skl2onnx/common/_registration.py index 08070450e..aa7a0a246 100644 --- a/skl2onnx/common/_registration.py +++ b/skl2onnx/common/_registration.py @@ -20,10 +20,9 @@ def __call__(self, *args): len(args) == 3 and hasattr(args[2], "_get_allowed_options") and hasattr(args[1], "raw_operator") - ): + ) and args[1].raw_operator is not None: # Checks that the user did not specify a wrong option. - if args[1].raw_operator is not None: - args[2]._get_allowed_options(args[1].raw_operator) + args[2]._get_allowed_options(args[1].raw_operator) return self._fct(*args) def get_allowed_options(self): @@ -56,7 +55,7 @@ def register_converter( if conversion_function is None: raise ValueError("A converter cannot be None for %r." % operator_name) if not overwrite and operator_name in _converter_pool: - raise ValueError("We do not overwrite registered converter " "by default") + raise ValueError("We do not overwrite registered converter by default") if len(_converter_pool) > 0: key = next(iter(_converter_pool)) check_signature( @@ -89,9 +88,7 @@ def register_shape_calculator(operator_name, calculator_function, overwrite=Fals if calculator_function is None: raise ValueError("A shape calculator cannot be None for %r." % operator_name) if not overwrite and operator_name in _shape_calculator_pool: - raise ValueError( - "We do not overwrite registrated shape calculator " "by default" - ) + raise ValueError("We do not overwrite registrated shape calculator by default") if calculator_function is not None and len(_shape_calculator_pool) > 0: key = next(iter(_shape_calculator_pool)) check_signature( @@ -102,6 +99,6 @@ def register_shape_calculator(operator_name, calculator_function, overwrite=Fals def get_shape_calculator(operator_name): if operator_name not in _shape_calculator_pool: - msg = "Unsupported shape calculator for operator " "'%s'." % operator_name + msg = "Unsupported shape calculator for operator '%s'." % operator_name raise ValueError(msg) return _shape_calculator_pool[operator_name] diff --git a/skl2onnx/common/_topology.py b/skl2onnx/common/_topology.py index 4518ac1aa..5d44d3cf5 100644 --- a/skl2onnx/common/_topology.py +++ b/skl2onnx/common/_topology.py @@ -9,21 +9,15 @@ import numpy as np from onnx import onnx_pb as onnx_proto from onnx.helper import make_graph, make_model, make_tensor_value_info -from onnxconverter_common.data_types import ( # noqa +from onnxconverter_common.data_types import ( DataType, TensorType, - FloatType, - Int64Type, - StringType, - DictionaryType, - FloatTensorType, # noqa + FloatTensorType, Int64TensorType, - SequenceType, # noqa StringTensorType, DoubleTensorType, Int32TensorType, BooleanTensorType, - DoubleTensorType, ) try: @@ -172,7 +166,7 @@ def __init__(self, raw_name, onnx_name, scope, type=None): try: shape = list(shape) except TypeError: - raise TypeError( + raise TypeError( # noqa: B904 "shape must be a tuple or a list not " "{}.".format(type_fct(shape)) ) @@ -198,7 +192,7 @@ def _check(self): continue if not isinstance(k, (int, np.integer)): raise ValueError( - "Unexpected type %r for shape %r." "" % (type(k), self) + "Unexpected type %r for shape %r." % (type(k), self) ) @property @@ -365,7 +359,7 @@ def get_shape(tt): ) else: raise NotImplementedError( - "Unsupported type '{}' as " "a string ({}).".format(type(obj), obj) + "Unsupported type '{}' as a string ({}).".format(type(obj), obj) ) return Variable(name, name, None, ty) @@ -396,7 +390,7 @@ def empty_shape(shape): if self.type is None: if other_type is None: return - elif other_type is not None: + elif other_type is not None: # noqa: SIM102 if isinstance(self.type, type(other_type)): if self.type.shape == other_type.shape: return @@ -423,7 +417,7 @@ def raw_name(self): def onnx_name(self): if self._onnx_name.startswith("u("): raise RuntimeError( - "Variable should be renamed as onnx_name=%r." "" % self._onnx_name + "Variable should be renamed as onnx_name=%r." % self._onnx_name ) return self._onnx_name @@ -445,7 +439,7 @@ def __eq__(self, second): def append(self, v): if not isinstance(v, Variable): raise TypeError( - "Input and output must be of type Variable not %r." "" % type(v) + "Input and output must be of type Variable not %r." % type(v) ) if self.kind == "Out": v.set_parent(self.parent) @@ -636,7 +630,7 @@ def infer_types(self): try: shape_calc = _registration.get_shape_calculator(self.type) except ValueError: - raise MissingShapeCalculator( + raise MissingShapeCalculator( # noqa: B904 "Unable to find a shape calculator for alias '{}' " "and type '{}'.".format(self.type, type(self.raw_operator)) ) @@ -760,7 +754,7 @@ def get_unique_variable_name(self, seed, rename=True): """ if not isinstance(seed, str): raise TypeError( - "Parameter seed must be a string not {}." "".format(type(seed)) + "Parameter seed must be a string not {}.".format(type(seed)) ) if rename: name = self._naming(seed, self.onnx_variable_names) @@ -1115,7 +1109,7 @@ def call_converter(self, operator, container, verbose=0): try: conv = _registration.get_converter(operator.type) except ValueError: - raise MissingConverter( + raise MissingConverter( # noqa: B904 "Unable to find converter for alias '{}' type " "'{}'. You may raise an issue at " "https://github.com/onnx/sklearn-onnx/issues." @@ -1171,7 +1165,7 @@ def _initialize_graph_status_for_traversing(self): """ if len(self.scopes) != 1: raise RuntimeError("Only one scope is allowed not %d." % len(self.scopes)) - input_names = set(v.onnx_name for v in self.scopes[0].input_variables) + input_names = {v.onnx_name for v in self.scopes[0].input_variables} if len(input_names) == 0: raise RuntimeError("No detected inputs.") for variable in self.unordered_variable_iterator(): @@ -1198,7 +1192,7 @@ def _propagate_status(self, operator, container, fed_variables, verbose=0): if verbose > 1: print( "[_propagate_status] newly fed=%r" - % list(v.onnx_name for v in operator.outputs if v.is_fed) + % [v.onnx_name for v in operator.outputs if v.is_fed] ) stack = list(fed_variables) scope = self.scopes[0] @@ -1255,7 +1249,7 @@ def _check_operator_(operator): def _check_variable_in_(variable, operator): idop = id(operator) - ids = set(id(op) for op in variable.operators_inputs_) + ids = {id(op) for op in variable.operators_inputs_} if idop not in ids: raise RuntimeError( "Operator %r not registered in the list of operators " @@ -1516,7 +1510,8 @@ def convert_topology( warnings.warn( "Parameter target_opset {} > {} is higher than the " "the latest tested version" - ".".format(onnx_target_opset, get_latest_tested_opset_version()) + ".".format(onnx_target_opset, get_latest_tested_opset_version()), + stacklevel=0, ) container = ModelComponentContainer( diff --git a/skl2onnx/common/data_types.py b/skl2onnx/common/data_types.py index a09971a6f..00532a732 100644 --- a/skl2onnx/common/data_types.py +++ b/skl2onnx/common/data_types.py @@ -1,24 +1,24 @@ # SPDX-License-Identifier: Apache-2.0 import numpy as np -from onnxconverter_common.data_types import ( # noqa +from onnxconverter_common.data_types import ( # noqa: F401 DataType, Int64Type, - FloatType, # noqa + FloatType, StringType, - TensorType, # noqa + TensorType, Int64TensorType, Int32TensorType, - BooleanTensorType, # noqa + BooleanTensorType, FloatTensorType, StringTensorType, - DoubleTensorType, # noqa + DoubleTensorType, DictionaryType, SequenceType, -) # noqa +) try: - from onnxconverter_common.data_types import ( # noqa + from onnxconverter_common.data_types import ( Complex64TensorType, Complex128TensorType, ) @@ -271,11 +271,11 @@ def _guess_numpy_type(data_type, dims): data_type in (np.str_, str, object) or str(data_type) in ("")] else: - debug_info = debug_info + [str(type(onnx_model)).split(".")[-1].strip("'>")] + debug_info = [*debug_info, str(type(onnx_model)).split(".")[-1].strip("'>")] if hasattr(onnx_model, "graph"): return _apply_optimisation_on_graph( @@ -47,8 +48,8 @@ def onnx_remove_node_identity(onnx_model, recursive=True, debug_info=None): graph = onnx_model - inputs = set(i.name for i in graph.input) - outputs = set(o.name for o in graph.output) + inputs = {i.name for i in graph.input} + outputs = {o.name for o in graph.output} def retrieve_idnodes(graph, existing_nodes): idnodes = [] @@ -63,7 +64,7 @@ def retrieve_idnodes(graph, existing_nodes): def retrieve_local_variables_subgraphs(graph): local = set() - existing = set(i.name for i in graph.input) + existing = {i.name for i in graph.input} for node in graph.node: for i in node.input: if i not in existing: @@ -110,13 +111,11 @@ def retrieve_local_variables_nodes(nodes): continue if out in nodes[j].input: nodes[j] = _rename_node_input(nodes[j], out, inp) - logger.debug( - "[VarId-a] rename node input %r into %r" % (out, inp) - ) + logger.debug("[VarId-a] rename node input %r into %r", out, inp) rem += 1 if nodes[j].op_type == "Identity": restart = True - logger.debug("[NodeId-a] remove %r" % nodes[i]) + logger.debug("[NodeId-a] remove %r", nodes[i]) nodes[i] = None rem += 1 continue @@ -132,19 +131,17 @@ def retrieve_local_variables_nodes(nodes): continue if inp in nodes[j].output: nodes[j] = _rename_node_output(nodes[j], inp, out) - logger.debug("[Var] rename node output %r into %r" % (out, inp)) + logger.debug("[Var] rename node output %r into %r", out, inp) rem += 1 if nodes[j].op_type == "Identity": restart = True if inp in nodes[j].input: nodes[j] = _rename_node_input(nodes[j], inp, out) - logger.debug( - "[VarId-b] rename node input %r into %r" % (out, inp) - ) + logger.debug("[VarId-b] rename node input %r into %r", out, inp) rem += 1 if nodes[j].op_type == "Identity": restart = True - logger.debug("[NodeId-b] remove %r" % nodes[i]) + logger.debug("[NodeId-b] remove %r", nodes[i]) nodes[i] = None rem += 1 @@ -158,7 +155,7 @@ def retrieve_local_variables_nodes(nodes): onnx_remove_node_identity, node, recursive=True, - debug_info=debug_info + [node.name], + debug_info=[*debug_info, node.name], ) # Finally create the new graph. diff --git a/skl2onnx/common/shape_calculator.py b/skl2onnx/common/shape_calculator.py index 2fe75e920..ae37a318c 100644 --- a/skl2onnx/common/shape_calculator.py +++ b/skl2onnx/common/shape_calculator.py @@ -4,6 +4,7 @@ Functions to calculate output shapes of linear classifiers and regressors. """ + import numbers import numpy as np from .data_types import ( diff --git a/skl2onnx/common/tree_ensemble.py b/skl2onnx/common/tree_ensemble.py index 5e8b3893a..233f2e003 100644 --- a/skl2onnx/common/tree_ensemble.py +++ b/skl2onnx/common/tree_ensemble.py @@ -3,6 +3,7 @@ """ Common functions to convert any learner based on trees. """ + import numpy as np diff --git a/skl2onnx/common/utils.py b/skl2onnx/common/utils.py index 3aaf1a4e8..cc4c7230f 100644 --- a/skl2onnx/common/utils.py +++ b/skl2onnx/common/utils.py @@ -5,13 +5,18 @@ import hashlib import numpy as np from onnx.numpy_helper import from_array -from onnxconverter_common.utils import sklearn_installed, skl2onnx_installed # noqa -from onnxconverter_common.utils import is_numeric_type, is_string_type # noqa -from onnxconverter_common.utils import cast_list, convert_to_python_value # noqa -from onnxconverter_common.utils import convert_to_python_default_value # noqa -from onnxconverter_common.utils import convert_to_list # noqa -from onnxconverter_common.utils import check_input_and_output_numbers # noqa -from onnxconverter_common.utils import check_input_and_output_types # noqa +from onnxconverter_common.utils import ( # noqa: F401 + sklearn_installed, + skl2onnx_installed, + is_numeric_type, + is_string_type, + cast_list, + convert_to_python_value, + convert_to_python_default_value, + convert_to_list, + check_input_and_output_numbers, + check_input_and_output_types, +) from .data_types import TensorType _unique_index = {"subgraph": 0} diff --git a/skl2onnx/common/utils_checking.py b/skl2onnx/common/utils_checking.py index 34086f438..b87621a51 100644 --- a/skl2onnx/common/utils_checking.py +++ b/skl2onnx/common/utils_checking.py @@ -15,10 +15,13 @@ def check_signature(fct, reference, skip=None): def select_parameters(pars): new_pars = OrderedDict() for i, (name, p) in enumerate(pars.items()): - if i >= 3 and name in ("op_type", "op_domain", "op_version"): - if p.default is not None: - # Parameters op_type and op_domain are skipped. - continue + if ( + i >= 3 + and name in ("op_type", "op_domain", "op_version") + and p.default is not None + ): + # Parameters op_type and op_domain are skipped. + continue new_pars[name] = p return new_pars diff --git a/skl2onnx/common/utils_sklearn.py b/skl2onnx/common/utils_sklearn.py index 4c86573b9..6453350fb 100644 --- a/skl2onnx/common/utils_sklearn.py +++ b/skl2onnx/common/utils_sklearn.py @@ -14,7 +14,7 @@ def enumerate_model_names(model, prefix="", short=True): """ if isinstance(model, (list, tuple)): if all(map(lambda x: isinstance(x, tuple) and len(x) in (2, 3), model)): - for i, named_mod in enumerate(model): + for _i, named_mod in enumerate(model): name, mod = named_mod[:2] p = name if short and prefix == "" else "{}__{}".format(prefix, name) for t in enumerate_model_names(mod, p, short=short): @@ -131,7 +131,7 @@ def _process_pipeline_options(model, options): """ new_options = None names = dict(enumerate_model_names(model)) - for k, v in names.items(): + for _k, v in names.items(): if id(v) in options and isinstance(v, Pipeline): opts = options[id(v)] last = v.steps[-1][1] diff --git a/skl2onnx/convert.py b/skl2onnx/convert.py index 38ac6385c..6ef138cce 100644 --- a/skl2onnx/convert.py +++ b/skl2onnx/convert.py @@ -11,8 +11,8 @@ from ._parse import parse_sklearn_model # Invoke the registration of all our converters and shape calculators. -from . import shape_calculators # noqa -from . import operator_converters # noqa +from . import shape_calculators # noqa: F401 +from . import operator_converters # noqa: F401 def convert_sklearn( @@ -179,8 +179,9 @@ def convert_sklearn( name = str(uuid4().hex) if dtype is not None: warnings.warn( - "Parameter dtype is no longer supported. " "It will be removed in 1.9.0.", + "Parameter dtype is no longer supported. It will be removed in 1.9.0.", DeprecationWarning, + stacklevel=0, ) target_opset = target_opset if target_opset else get_latest_tested_opset_version() @@ -248,7 +249,7 @@ def to_onnx( black_op: Optional[Set[str]] = None, final_types: Optional[List[Tuple[str, Sequence[Optional[Union[int, str]]]]]] = None, dtype: Optional[np.dtype] = None, - naming: Callable = None, + naming: Optional[Callable] = None, model_optim: bool = True, verbose: int = 0, ): diff --git a/skl2onnx/helpers/__init__.py b/skl2onnx/helpers/__init__.py index 81f5f5e33..fe51b4bc3 100644 --- a/skl2onnx/helpers/__init__.py +++ b/skl2onnx/helpers/__init__.py @@ -1,6 +1,6 @@ # SPDX-License-Identifier: Apache-2.0 -from .investigate import collect_intermediate_steps, compare_objects # noqa -from .investigate import enumerate_pipeline_models # noqa -from .integration import add_onnx_graph # noqa +from .investigate import collect_intermediate_steps, compare_objects +from .investigate import enumerate_pipeline_models +from .integration import add_onnx_graph diff --git a/skl2onnx/helpers/integration.py b/skl2onnx/helpers/integration.py index 8d4c1d1da..038c0dafe 100644 --- a/skl2onnx/helpers/integration.py +++ b/skl2onnx/helpers/integration.py @@ -22,7 +22,7 @@ def get_tensor_shape(obj: Union[ValueInfoProto, TypeProto]) -> Tuple[int, ...]: if len(shape) == 0: shape = None else: - shape = list(None if s == 0 else s for s in shape) + shape = [None if s == 0 else s for s in shape] return shape diff --git a/skl2onnx/helpers/investigate.py b/skl2onnx/helpers/investigate.py index d7aaa6063..27c6cf802 100644 --- a/skl2onnx/helpers/investigate.py +++ b/skl2onnx/helpers/investigate.py @@ -36,7 +36,7 @@ def enumerate_pipeline_models(pipe, coor=None, vs=None): raise NotImplementedError("Unable to handle this specific case.") elif hasattr(pipe, "mapper") and pipe.mapper: # azureml DataTransformer - for couple in enumerate_pipeline_models(pipe.mapper, coor + (0,)): + for couple in enumerate_pipeline_models(pipe.mapper, (*coor, 0)): yield couple elif hasattr(pipe, "built_features"): # sklearn_pandas.dataframe_mapper.DataFrameMapper @@ -44,25 +44,25 @@ def enumerate_pipeline_models(pipe, coor=None, vs=None): if isinstance(columns, str): columns = (columns,) if transformers is None: - yield (coor + (i,)), None, columns + yield ((*coor, i)), None, columns else: for couple in enumerate_pipeline_models( - transformers, coor + (i,), columns + transformers, (*coor, i), columns ): yield couple elif isinstance(pipe, Pipeline): for i, (_, model) in enumerate(pipe.steps): - for couple in enumerate_pipeline_models(model, coor + (i,)): + for couple in enumerate_pipeline_models(model, (*coor, i)): yield couple elif ColumnTransformer is not None and isinstance(pipe, ColumnTransformer): for i, (_, fitted_transformer, column) in enumerate(pipe.transformers): for couple in enumerate_pipeline_models( - fitted_transformer, coor + (i,), column + fitted_transformer, (*coor, i), column ): yield couple elif isinstance(pipe, FeatureUnion): for i, (_, model) in enumerate(pipe.transformer_list): - for couple in enumerate_pipeline_models(model, coor + (i,)): + for couple in enumerate_pipeline_models(model, (*coor, i)): yield couple elif TransformedTargetRegressor is not None and isinstance( pipe, TransformedTargetRegressor @@ -103,10 +103,8 @@ def __init__(self, model): self.methods["predict_proba"] = lambda model, X: model._debug_predict_proba( X ) - if hasattr(model, "decision_function") and callable( - model.decision_function - ): # noqa - model._debug_decision_function = model.decision_function # noqa + if hasattr(model, "decision_function") and callable(model.decision_function): + model._debug_decision_function = model.decision_function self.methods["decision_function"] = ( lambda model, X: model._debug_decision_function(X) ) @@ -211,7 +209,8 @@ def decision_function(self, X, *args, **kwargs): except AttributeError: warnings.warn( "Unable to overwrite method '{}' for class " - "{}.".format(k, type(model)) + "{}.".format(k, type(model)), + stacklevel=0, ) else: skl_model._debug = BaseEstimatorDebugInformation(skl_model) @@ -221,7 +220,8 @@ def decision_function(self, X, *args, **kwargs): except AttributeError: warnings.warn( "Unable to overwrite method '{}' for class " - "{}.".format(k, type(skl_model)) + "{}.".format(k, type(skl_model)), + stacklevel=0, ) @@ -289,12 +289,11 @@ def compare_objects(o1, o2, decimal=4): """ def convert(o): - if isinstance(o, list) and len(o) == 1: - if isinstance(o[0], numpy.ndarray): - if o[0].dtype in (numpy.str_, object, str): - o = list(o[0]) - else: - o = o[0] + if isinstance(o, list) and len(o) == 1 and isinstance(o[0], numpy.ndarray): + if o[0].dtype in (numpy.str_, object, str): + o = list(o[0]) + else: + o = o[0] # Following line avoid importing pandas and taking # dependency on pandas. if o.__class__.__name__ == "Series": diff --git a/skl2onnx/helpers/onnx_helper.py b/skl2onnx/helpers/onnx_helper.py index 93f13461f..5cf49c37c 100644 --- a/skl2onnx/helpers/onnx_helper.py +++ b/skl2onnx/helpers/onnx_helper.py @@ -3,7 +3,7 @@ from logging import getLogger from io import BytesIO import numpy as np -import onnx # noqa +import onnx from onnx import shape_inference, TensorProto, ValueInfoProto from onnx.numpy_helper import from_array, to_array from onnx.helper import ( @@ -65,7 +65,7 @@ def enumerate_model_node_outputs(model, add_node=False): """ if not hasattr(model, "graph"): raise TypeError( - "Parameter model is not an ONNX model but " "{}".format(type(model)) + "Parameter model is not an ONNX model but {}".format(type(model)) ) for node in model.graph.node: for out in node.output: @@ -234,7 +234,7 @@ def infer_outputs( onnx_inputs.append(inp) elif isinstance(input, onnx.TensorProto): v = make_tensor_value_info( - input.name, input.data_type.real, list(d for d in input.dims) + input.name, input.data_type.real, list(input.dims) ) onnx_inputs.append(v) elif isinstance(input, onnx.AttributeProto): @@ -252,7 +252,7 @@ def infer_outputs( domains = {} for n in node: domains[n.domain] = max(domains.get(n.domain, 1), getattr(n, "op_version", 1)) - for i, (k, v) in enumerate(domains.items()): + for i, (k, _v) in enumerate(domains.items()): if i == 0 and len(original_model.opset_import) == 1: op_set = original_model.opset_import[0] else: @@ -385,19 +385,19 @@ def add_output_initializer(model_onnx, name, value, suffix="_init"): for name, value in zip(name_list, value_list): name_output = name name_init = name + suffix - names = set(i.name for i in model_onnx.graph.initializer) + names = {i.name for i in model_onnx.graph.initializer} if name_output in names or name_init in names: raise ValueError( "Names %r or %r is already taken by an initializer: %r." % (name_output, name_init, ", ".join(sorted(names))) ) - names = set(i.name for i in model_onnx.graph.output) + names = {i.name for i in model_onnx.graph.output} if name_output in names or name_init in names: raise ValueError( "Names %r or %r is already taken by an output: %r." % (name_output, name_init, ", ".join(sorted(names))) ) - names = set(i.name for i in model_onnx.graph.input) + names = {i.name for i in model_onnx.graph.input} if name_output in names or name_init in names: raise ValueError( "Names %r or %r is already taken by an output: %r." @@ -408,7 +408,7 @@ def add_output_initializer(model_onnx, name, value, suffix="_init"): cst = from_array(value, name=name_init) except RuntimeError as e: st = str(value.dtype).lower() - if st.startswith("u") or st.startswith(" 2: # See https://scikit-learn.org/dev/modules/generated/ - # sklearn.linear_model.LogisticRegression.html#sklearn.linear_model.LogisticRegression + # sklearn.linear_model.LogisticRegression.html# + # sklearn.linear_model.LogisticRegression # multi_class attribute is deprecated. # OVR is not supported anymore. multi_class = 2 @@ -76,7 +77,8 @@ def convert_sklearn_linear_classifier( ) else: # See https://scikit-learn.org/dev/modules/generated/ - # sklearn.linear_model.LogisticRegression.html#sklearn.linear_model.LogisticRegression + # sklearn.linear_model.LogisticRegression.html + # #sklearn.linear_model.LogisticRegression # multi_class attribute is deprecated. # OVR is not supported anymore. if number_of_classes > 2: @@ -127,7 +129,7 @@ def convert_sklearn_linear_classifier( class_labels = [int(i) for i in classes] classifier_attrs["classlabels_ints"] = class_labels else: - raise RuntimeError("Label vector must be a string or a integer " "tensor.") + raise RuntimeError("Label vector must be a string or a integer tensor.") if not use_linear_op or type(operator.inputs[0].type) in (DoubleTensorType,): # Double -> double parameters not supported in ONNX LinearClassifier @@ -222,7 +224,7 @@ def convert_sklearn_linear_classifier( input_name, [label_name, operator.outputs[1].full_name], op_domain="ai.onnx.ml", - **classifier_attrs + **classifier_attrs, ) elif ( isinstance(op, (LinearSVC, RidgeClassifier, RidgeClassifierCV)) @@ -249,7 +251,7 @@ def convert_sklearn_linear_classifier( input_name, [y_pred_name, raw_scores_tensor_name], op_domain="ai.onnx.ml", - **classifier_attrs + **classifier_attrs, ) container.add_node( "Binarizer", @@ -270,7 +272,7 @@ def convert_sklearn_linear_classifier( input_name, [label_name, raw_scores_tensor_name], op_domain="ai.onnx.ml", - **classifier_attrs + **classifier_attrs, ) container.add_node( "ArrayFeatureExtractor", @@ -292,7 +294,7 @@ def convert_sklearn_linear_classifier( input_name, [label_name, probability_tensor_name], op_domain="ai.onnx.ml", - **classifier_attrs + **classifier_attrs, ) use_float = type(operator.inputs[0].type) not in (DoubleTensorType,) apply_normalizer( @@ -315,7 +317,7 @@ def convert_sklearn_linear_classifier( input_name, [y_pred_name, operator.outputs[1].full_name], op_domain="ai.onnx.ml", - **classifier_attrs + **classifier_attrs, ) container.add_node( "Binarizer", @@ -336,7 +338,7 @@ def convert_sklearn_linear_classifier( input_name, [label_name, operator.outputs[1].full_name], op_domain="ai.onnx.ml", - **classifier_attrs + **classifier_attrs, ) diff --git a/skl2onnx/operator_converters/linear_regressor.py b/skl2onnx/operator_converters/linear_regressor.py index 3c99c99f8..d77720ef4 100644 --- a/skl2onnx/operator_converters/linear_regressor.py +++ b/skl2onnx/operator_converters/linear_regressor.py @@ -247,7 +247,7 @@ def convert_sklearn_poisson_regressor( Y = OnnxSigmoid(eta, op_version=opv) else: raise RuntimeError( - f"Unexpected type of link for {loss!r} loss " "in operator type {op!r}." + f"Unexpected type of link for {loss!r} loss in operator type {op!r}." ) last_dim = 1 if len(op.coef_.shape) == 1 else op.coef_.shape[-1] diff --git a/skl2onnx/operator_converters/local_outlier_factor.py b/skl2onnx/operator_converters/local_outlier_factor.py index 5a5beda8d..949166e8b 100644 --- a/skl2onnx/operator_converters/local_outlier_factor.py +++ b/skl2onnx/operator_converters/local_outlier_factor.py @@ -35,7 +35,7 @@ def convert_sklearn_local_outlier_factor( op = operator.raw_operator if not op.novelty: raise RuntimeError( - "The converter only converts the model %r is novelty is True." "" % type(op) + "The converter only converts the model %r is novelty is True." % type(op) ) outputs = operator.outputs opv = container.target_opset @@ -56,7 +56,8 @@ def convert_sklearn_local_outlier_factor( warnings.warn( "Option p=%r may not be compatible with the runtime. " "See https://github.com/microsoft/onnxruntime/blob/main/" - "docs/ContribOperators.md#com.microsoft.CDist." + "docs/ContribOperators.md#com.microsoft.CDist.", + stacklevel=0, ) kwargs["p"] = op.p @@ -69,7 +70,7 @@ def convert_sklearn_local_outlier_factor( op_version=opv, keep_distances=True, optim=options.get("optim", None), - **kwargs + **kwargs, ) # dist_k = self._distances_fit_X_[neighbors_indices, self.n_neighbors_ - 1] diff --git a/skl2onnx/operator_converters/nearest_neighbours.py b/skl2onnx/operator_converters/nearest_neighbours.py index 2bc79ad78..4c104f264 100644 --- a/skl2onnx/operator_converters/nearest_neighbours.py +++ b/skl2onnx/operator_converters/nearest_neighbours.py @@ -74,7 +74,7 @@ def onnx_nearest_neighbors_indices_k( op_version=None, keep_distances=False, optim=None, - **kwargs + **kwargs, ): """ Retrieves the nearest neigbours *ONNX*. @@ -107,7 +107,7 @@ def onnx_nearest_neighbors_indices_k( op_version=op_version, dim_in=dim_in, dim_out=dim_out, - **kwargs_dist + **kwargs_dist, ) else: raise ValueError("Unknown optimisation '{}'.".format(optim)) @@ -126,7 +126,7 @@ def onnx_nearest_neighbors_indices_k( largest=0, sorted=1, op_version=11, - **kwargs_topk + **kwargs_topk, ) if keep_distances: return ( @@ -148,7 +148,7 @@ def onnx_nearest_neighbors_indices_radius( keep_distances=False, optim=None, proto_dtype=None, - **kwargs + **kwargs, ): """ Retrieves the nearest neigbours *ONNX*. @@ -182,7 +182,7 @@ def onnx_nearest_neighbors_indices_radius( op_version=op_version, dim_in=dim_in, dim_out=dim_out, - **kwargs + **kwargs, ) else: raise ValueError("Unknown optimisation '{}'.".format(optim)) @@ -305,7 +305,7 @@ def _convert_nearest_neighbors(operator, container, k=None, radius=None): dtype=dtype, op_version=opv, optim=options.get("optim", None), - **distance_kwargs + **distance_kwargs, ) top_distances = None elif radius is not None: @@ -319,7 +319,7 @@ def _convert_nearest_neighbors(operator, container, k=None, radius=None): keep_distances=True, proto_dtype=proto_type, optim=options.get("optim", None), - **distance_kwargs + **distance_kwargs, ) top_indices, top_distances, binary = three elif weights == "distance": @@ -332,7 +332,7 @@ def _convert_nearest_neighbors(operator, container, k=None, radius=None): op_version=opv, keep_distances=True, optim=options.get("optim", None), - **distance_kwargs + **distance_kwargs, ) else: raise RuntimeError( @@ -775,7 +775,7 @@ def _nearest_neighbours( neg_dist, np.array([model.n_neighbors], dtype=np.int64), op_version=10, - **kwargs + **kwargs, ) else: node = OnnxTopK_11( @@ -784,7 +784,7 @@ def _nearest_neighbours( largest=0, sorted=1, op_version=11, - **kwargs + **kwargs, ) return node[1], missing_input_name diff --git a/skl2onnx/operator_converters/one_hot_encoder.py b/skl2onnx/operator_converters/one_hot_encoder.py index dca82f540..7d20f0fd7 100644 --- a/skl2onnx/operator_converters/one_hot_encoder.py +++ b/skl2onnx/operator_converters/one_hot_encoder.py @@ -153,7 +153,7 @@ def convert_sklearn_one_hot_encoder( try: ci = int(c) except TypeError: - raise RuntimeError( + raise RuntimeError( # noqa: B904 "Category '{}' cannot be casted into int.".format(c) ) if ci != c: diff --git a/skl2onnx/operator_converters/ordinal_encoder.py b/skl2onnx/operator_converters/ordinal_encoder.py index c17e5cc5f..1f4c4757c 100644 --- a/skl2onnx/operator_converters/ordinal_encoder.py +++ b/skl2onnx/operator_converters/ordinal_encoder.py @@ -105,7 +105,7 @@ def convert_sklearn_ordinal_encoder( label_encoder_output, op_domain="ai.onnx.ml", op_version=2, - **attrs + **attrs, ) apply_reshape( scope, diff --git a/skl2onnx/operator_converters/polynomial_features.py b/skl2onnx/operator_converters/polynomial_features.py index 27b0c9533..1fd4885cd 100644 --- a/skl2onnx/operator_converters/polynomial_features.py +++ b/skl2onnx/operator_converters/polynomial_features.py @@ -107,7 +107,7 @@ def convert_sklearn_polynomial_features( apply_concat( scope, - [t for t in transformed_columns], + list(transformed_columns), concat_result_name, container, axis=1, @@ -122,7 +122,7 @@ def convert_sklearn_polynomial_features( else: apply_concat( scope, - [t for t in transformed_columns], + list(transformed_columns), operator.outputs[0].full_name, container, axis=1, diff --git a/skl2onnx/operator_converters/power_transformer.py b/skl2onnx/operator_converters/power_transformer.py index 461900cbe..28440a275 100644 --- a/skl2onnx/operator_converters/power_transformer.py +++ b/skl2onnx/operator_converters/power_transformer.py @@ -42,14 +42,10 @@ def convert_powertransformer( # logical masks for input less_than_zero = OnnxLess(op_in, zeros_, op_version=opv) - less_mask = OnnxCast( - less_than_zero, to=getattr(TensorProto, "FLOAT"), op_version=opv - ) + less_mask = OnnxCast(less_than_zero, to=TensorProto.FLOAT, op_version=opv) greater_than_zero = OnnxNot(less_than_zero, op_version=opv) - greater_mask = OnnxCast( - greater_than_zero, to=getattr(TensorProto, "FLOAT"), op_version=opv - ) + greater_mask = OnnxCast(greater_than_zero, to=TensorProto.FLOAT, op_version=opv) # logical masks for lambdas lambda_zero_mask = np.float32(lambdas == 0) diff --git a/skl2onnx/operator_converters/random_forest.py b/skl2onnx/operator_converters/random_forest.py index d04c22ac4..6cd4ec0ab 100644 --- a/skl2onnx/operator_converters/random_forest.py +++ b/skl2onnx/operator_converters/random_forest.py @@ -127,7 +127,7 @@ def convert_sklearn_random_forest_classifier( options = container.get_options(op, dict(raw_scores=False)) else: raise NotImplementedError( - "Model should have attribute 'n_outputs_' or " "'n_trees_per_iteration_'." + "Model should have attribute 'n_outputs_' or 'n_trees_per_iteration_'." ) use_raw_scores = options["raw_scores"] @@ -270,7 +270,7 @@ def convert_sklearn_random_forest_classifier( [operator.outputs[0].full_name, operator.outputs[1].full_name], op_domain=op_domain, op_version=op_version, - **attr_pairs + **attr_pairs, ) if not options.get("decision_path", False) and not options.get( @@ -457,7 +457,7 @@ def convert_sklearn_random_forest_regressor_converter( attrs["n_targets"] = op.n_trees_per_iteration_ else: raise NotImplementedError( - "Model should have attribute 'n_outputs_' or " "'n_trees_per_iteration_'." + "Model should have attribute 'n_outputs_' or 'n_trees_per_iteration_'." ) if hasattr(op, "estimators_"): @@ -528,7 +528,7 @@ def convert_sklearn_random_forest_regressor_converter( operator.outputs[0].full_name, op_domain=op_domain, op_version=op_version, - **attrs + **attrs, ) if hasattr(op, "n_trees_per_iteration_"): diff --git a/skl2onnx/operator_converters/scaler_op.py b/skl2onnx/operator_converters/scaler_op.py index 14d1b7e4b..675db7438 100644 --- a/skl2onnx/operator_converters/scaler_op.py +++ b/skl2onnx/operator_converters/scaler_op.py @@ -43,7 +43,7 @@ def convert_sklearn_scaler( # Identity container.add_node("Identity", feature_name, operator.outputs[0].full_name) return - if C is not None and C != model_C: + if C is not None and C != model_C: # noqa: SIM300 raise RuntimeError( "Unable Mismatch between expected shape %r and model (., %r)" " in operator %r." % (operator.outputs[0].type.shape, model_C, operator) @@ -66,7 +66,7 @@ def convert_sklearn_scaler( # Identity container.add_node("Identity", feature_name, operator.outputs[0].full_name) return - if C is not None and C != model_C: + if C is not None and C != model_C: # noqa: SIM300 raise RuntimeError( "Unable Mismatch between expected shape %r and model (., %r)" " in operator %r." % (operator.outputs[0].type.shape, model_C, operator) @@ -91,7 +91,7 @@ def convert_sklearn_scaler( # Identity container.add_node("Identity", feature_name, operator.outputs[0].full_name) return - if C is not None and C != model_C: + if C is not None and C != model_C: # noqa: SIM300 raise RuntimeError( "Unable Mismatch between expected shape %r and model (., %r)" " in operator %r." % (operator.outputs[0].type.shape, model_C, operator) diff --git a/skl2onnx/operator_converters/support_vector_machines.py b/skl2onnx/operator_converters/support_vector_machines.py index ea5160123..4e4316dec 100644 --- a/skl2onnx/operator_converters/support_vector_machines.py +++ b/skl2onnx/operator_converters/support_vector_machines.py @@ -98,7 +98,7 @@ def convert_sklearn_svm_regressor( svm_out, op_domain=op_domain, op_version=op_version, - **svm_attrs + **svm_attrs, ) apply_cast( scope, svm_out, operator.output_full_names, container, to=proto_dtype @@ -126,7 +126,7 @@ def convert_sklearn_svm_regressor( svm_out0, op_domain=op_domain, op_version=op_version, - **svm_attrs + **svm_attrs, ) svm_out = operator.output_full_names[1] @@ -259,7 +259,7 @@ def convert_sklearn_svm_classifier( [label_name, svm_out], op_domain=op_domain, op_version=op_version, - **svm_attrs + **svm_attrs, ) apply_cast(scope, svm_out, probability_tensor_name, container, to=proto_dtype) if len(op.classes_) == 2 and use_raw_scores: @@ -291,7 +291,7 @@ def convert_sklearn_svm_classifier( if apply_less is None: raise RuntimeError( - "Function apply_less is missing. " "onnxconverter-common is too old." + "Function apply_less is missing. onnxconverter-common is too old." ) cst0 = scope.get_unique_variable_name("cst0") diff --git a/skl2onnx/operator_converters/text_vectoriser.py b/skl2onnx/operator_converters/text_vectoriser.py index e00c819a8..19a5492b0 100644 --- a/skl2onnx/operator_converters/text_vectoriser.py +++ b/skl2onnx/operator_converters/text_vectoriser.py @@ -54,7 +54,7 @@ def _intelligent_split(text, op, tokenizer, existing): # Usually, ' ' is not part of any token. pass elif len(spl) == 3: - stok = (all([s in op.vocabulary_ for s in spl]), spl) + stok = (all(s in op.vocabulary_ for s in spl), spl) spl12 = ( spl[2] in op.vocabulary_ and (spl[0] + " " + spl[1]) in op.vocabulary_, @@ -183,7 +183,7 @@ def convert_sklearn_text_vectorizer( Regular expression ``[^\\\\\\\\n]`` is used to split a sentance into character (and not works) if ``analyser=='char'``. The mode ``analyser=='char_wb'`` is not implemented. - """ # noqa + """ op = operator.raw_operator if container.target_opset is not None and container.target_opset < 9: @@ -206,6 +206,7 @@ def convert_sklearn_text_vectorizer( "You may raise an issue at " "https://github.com/onnx/sklearn-onnx/issues.", UserWarning, + stacklevel=0, ) if op.strip_accents is not None: raise NotImplementedError( @@ -250,9 +251,7 @@ def convert_sklearn_text_vectorizer( default_separators = options["separators"] else: if options["separators"] != "DEFAULT": - raise RuntimeError( - "Option separators has no effect " "if analyser != 'word'." - ) + raise RuntimeError("Option separators has no effect if analyser != 'word'.") regex = options["tokenexp"] if options["tokenexp"] else "." default_separators = None diff --git a/skl2onnx/operator_converters/tfidf_transformer.py b/skl2onnx/operator_converters/tfidf_transformer.py index a57288223..b7086dab6 100644 --- a/skl2onnx/operator_converters/tfidf_transformer.py +++ b/skl2onnx/operator_converters/tfidf_transformer.py @@ -41,7 +41,7 @@ def convert_sklearn_tfidf_transformer( ones = scope.get_unique_variable_name("ones") cst = np.ones((C,), dtype=float_type) container.add_initializer(ones, proto_dtype, [C], cst.flatten()) - apply_add(scope, data + [ones], plus1, container, broadcast=1) + apply_add(scope, [*data, ones], plus1, container, broadcast=1) plus1logged = scope.get_unique_variable_name("plus1logged") apply_log(scope, plus1, plus1logged, container) data = [plus1logged] @@ -54,7 +54,7 @@ def convert_sklearn_tfidf_transformer( shape = [len(cst)] idfcst = scope.get_unique_variable_name("idfcst") container.add_initializer(idfcst, proto_dtype, shape, cst) - apply_mul(scope, data + [idfcst], output_name, container, broadcast=1) + apply_mul(scope, [*data, idfcst], output_name, container, broadcast=1) else: output_name = data[0] diff --git a/skl2onnx/operator_converters/zip_map.py b/skl2onnx/operator_converters/zip_map.py index 914ea0529..db20114d5 100644 --- a/skl2onnx/operator_converters/zip_map.py +++ b/skl2onnx/operator_converters/zip_map.py @@ -52,7 +52,7 @@ def convert_sklearn_zipmap( operator.inputs[1].full_name, operator.outputs[1].full_name, op_domain="ai.onnx.ml", - **zipmap_attrs + **zipmap_attrs, ) return @@ -70,7 +70,7 @@ def convert_sklearn_zipmap( operator.inputs[0].full_name, operator.outputs[0].full_name, op_domain="ai.onnx.ml", - **zipmap_attrs + **zipmap_attrs, ) diff --git a/skl2onnx/proto/__init__.py b/skl2onnx/proto/__init__.py index 443dfa451..ab89e3e5e 100644 --- a/skl2onnx/proto/__init__.py +++ b/skl2onnx/proto/__init__.py @@ -5,19 +5,20 @@ # we import ONNX protobuf definition here so that we can conduct quick # fixes by overwriting ONNX functions without changing any lines # elsewhere. -from onnx import onnx_pb as onnx_proto # noqa -from onnx import defs # noqa +from onnx import onnx_pb as onnx_proto +from onnx import defs # Overwrite the make_tensor defined in onnx.helper because of a bug # (string tensor get assigned twice) from onnx import mapping -from onnx.onnx_pb import TensorProto, ValueInfoProto # noqa +from onnx.onnx_pb import TensorProto, ValueInfoProto -try: - from onnx.onnx_pb import SparseTensorProto # noqa +try: # noqa: SIM105 + from onnx.onnx_pb import SparseTensorProto except ImportError: # onnx is too old. pass + from onnx.helper import split_complex_to_pairs diff --git a/skl2onnx/shape_calculators/gaussian_process.py b/skl2onnx/shape_calculators/gaussian_process.py index 3a4083d54..b3550594f 100644 --- a/skl2onnx/shape_calculators/gaussian_process.py +++ b/skl2onnx/shape_calculators/gaussian_process.py @@ -15,10 +15,10 @@ def calculate_sklearn_gaussian_process_regressor_shape(operator): ) if len(operator.inputs) != 1: raise RuntimeError( - "Only one input vector is allowed for " "GaussianProcessRegressor." + "Only one input vector is allowed for GaussianProcessRegressor." ) if len(operator.outputs) not in (1, 2): - raise RuntimeError("One output is expected for " "GaussianProcessRegressor.") + raise RuntimeError("One output is expected for GaussianProcessRegressor.") variable = operator.inputs[0] diff --git a/skl2onnx/shape_calculators/grid_search_cv.py b/skl2onnx/shape_calculators/grid_search_cv.py index bde460fce..ba43ed6c5 100644 --- a/skl2onnx/shape_calculators/grid_search_cv.py +++ b/skl2onnx/shape_calculators/grid_search_cv.py @@ -11,7 +11,7 @@ def convert_sklearn_grid_search_cv(operator): name = sklearn_operator_name_map.get(type(best_estimator), None) if name is None: logger = logging.getLogger("skl2onnx") - logger.warn( + logger.warning( "[convert_sklearn_grid_search_cv] failed to find alias " "to model type %r.", type(best_estimator), diff --git a/skl2onnx/shape_calculators/imputer.py b/skl2onnx/shape_calculators/imputer.py index ff6e68307..f43804a60 100644 --- a/skl2onnx/shape_calculators/imputer.py +++ b/skl2onnx/shape_calculators/imputer.py @@ -35,7 +35,7 @@ def calculate_sklearn_imputer_output_shapes(operator): ) output = operator.outputs[0] for variable in operator.inputs: - if not isinstance(variable.type, type(output.type)): # noqa + if not isinstance(variable.type, type(output.type)): raise RuntimeError( "Inputs and outputs should have the same type " "%r != %r." % (type(variable.type), type(output.type)) diff --git a/skl2onnx/shape_calculators/multioutput.py b/skl2onnx/shape_calculators/multioutput.py index fe275dfb4..a8d221484 100644 --- a/skl2onnx/shape_calculators/multioutput.py +++ b/skl2onnx/shape_calculators/multioutput.py @@ -23,7 +23,7 @@ def multioutput_classifier_shape_calculator(operator): check_input_and_output_numbers(operator, input_count_range=1, output_count_range=2) if not isinstance(operator.outputs[1].type, SequenceType): raise RuntimeError( - "Probabilites should be a sequence not %r." "" % operator.outputs[1].type + "Probabilites should be a sequence not %r." % operator.outputs[1].type ) i = operator.inputs[0] outputs = operator.outputs diff --git a/skl2onnx/shape_calculators/scaler.py b/skl2onnx/shape_calculators/scaler.py index 12f7077e3..3ba9aa5ee 100644 --- a/skl2onnx/shape_calculators/scaler.py +++ b/skl2onnx/shape_calculators/scaler.py @@ -27,7 +27,7 @@ def calculate_sklearn_scaler_output_shapes(operator): # Inputs: multiple float- and integer-tensors # Output: one float tensor for variable in operator.inputs: - if len(set(variable.get_first_dimension() for variable in operator.inputs)) > 1: + if len({variable.get_first_dimension() for variable in operator.inputs}) > 1: raise RuntimeError("Batch size must be identical across inputs.") N = operator.inputs[0].get_first_dimension() diff --git a/skl2onnx/sklapi/__init__.py b/skl2onnx/sklapi/__init__.py index 02de3d89c..419cc83b5 100644 --- a/skl2onnx/sklapi/__init__.py +++ b/skl2onnx/sklapi/__init__.py @@ -1,8 +1,8 @@ # SPDX-License-Identifier: Apache-2.0 -from .cast_transformer import CastTransformer # noqa -from .cast_regressor import CastRegressor # noqa -from .replace_transformer import ReplaceTransformer # noqa -from .sklearn_text import TraceableCountVectorizer, TraceableTfidfVectorizer # noqa -from .woe_transformer import WOETransformer # noqa +from .cast_transformer import CastTransformer +from .cast_regressor import CastRegressor +from .replace_transformer import ReplaceTransformer +from .sklearn_text import TraceableCountVectorizer, TraceableTfidfVectorizer +from .woe_transformer import WOETransformer diff --git a/skl2onnx/sklapi/cast_regressor.py b/skl2onnx/sklapi/cast_regressor.py index 9e7274f65..a4056accd 100644 --- a/skl2onnx/sklapi/cast_regressor.py +++ b/skl2onnx/sklapi/cast_regressor.py @@ -8,10 +8,10 @@ except ImportError: def _deprecate_positional_args(x): - return x # noqa + return x -class CastRegressor(RegressorMixin, BaseEstimator): # noqa +class CastRegressor(RegressorMixin, BaseEstimator): """ Cast predictions into a specific types. This should be used to minimize the conversion @@ -24,7 +24,7 @@ class CastRegressor(RegressorMixin, BaseEstimator): # noqa wrapped regressor dtype : numpy type, output are cast into that type - """ # noqa + """ @_deprecate_positional_args def __init__(self, estimator, *, dtype=np.float32): @@ -34,10 +34,10 @@ def __init__(self, estimator, *, dtype=np.float32): def _cast(self, a, name): try: a2 = a.astype(self.dtype) - except ValueError: + except ValueError as e: raise ValueError( "Unable to cast {} from {} into {}.".format(name, a.dtype, self.dtype) - ) + ) from e return a2 def fit(self, X, y=None, sample_weight=None): diff --git a/skl2onnx/sklapi/cast_transformer.py b/skl2onnx/sklapi/cast_transformer.py index 8416366b9..464093d43 100644 --- a/skl2onnx/sklapi/cast_transformer.py +++ b/skl2onnx/sklapi/cast_transformer.py @@ -8,7 +8,7 @@ except ImportError: def _deprecate_positional_args(x): - return x # noqa + return x class CastTransformer(TransformerMixin, BaseEstimator): @@ -21,7 +21,7 @@ class CastTransformer(TransformerMixin, BaseEstimator): ---------- dtype : numpy type, output are cast into that type - """ # noqa + """ @_deprecate_positional_args def __init__(self, *, dtype=np.float32): @@ -36,10 +36,10 @@ def _cast(self, a, name): raise TypeError("{} must be a numpy array or a dataframe.".format(name)) try: a2 = a.astype(self.dtype) - except ValueError: + except ValueError as e: raise ValueError( "Unable to cast {} from {} into {}.".format(name, a.dtype, self.dtype) - ) + ) from e return a2 def fit(self, X, y=None, sample_weight=None): diff --git a/skl2onnx/sklapi/replace_transformer.py b/skl2onnx/sklapi/replace_transformer.py index bc084d83e..21716f1b8 100644 --- a/skl2onnx/sklapi/replace_transformer.py +++ b/skl2onnx/sklapi/replace_transformer.py @@ -8,7 +8,7 @@ except ImportError: def _deprecate_positional_args(x): - return x # noqa + return x class ReplaceTransformer(TransformerMixin, BaseEstimator): @@ -21,7 +21,7 @@ class ReplaceTransformer(TransformerMixin, BaseEstimator): from_value : value to replace to_value : new value dtype: dtype of replaced values - """ # noqa + """ @_deprecate_positional_args def __init__(self, *, from_value=0, to_value=np.nan, dtype=np.float32): @@ -36,7 +36,7 @@ def _replace(self, a): # implicit return a raise RuntimeError( - "Unable to replace 0 by nan one value by another " "in sparse matrix." + "Unable to replace 0 by nan one value by another in sparse matrix." ) return np.where(a == self.from_value, self.to_value, a) diff --git a/skl2onnx/sklapi/sklearn_text.py b/skl2onnx/sklapi/sklearn_text.py index b6b1b90fe..784576975 100644 --- a/skl2onnx/sklapi/sklearn_text.py +++ b/skl2onnx/sklapi/sklearn_text.py @@ -86,9 +86,8 @@ def _fix_vocabulary(expected, new_voc): if not isinstance(w, tuple): raise TypeError(f"Tuple is expected for a token not {type(w)}.") s = " ".join(w) - if s in expected: - if expected[s] != wid: - update[w] = wid + if s in expected and expected[s] != wid: + update[w] = wid if update: new_voc.update(update) duplicates = {} diff --git a/skl2onnx/sklapi/woe_transformer.py b/skl2onnx/sklapi/woe_transformer.py index 9e045becf..d5e9864ec 100644 --- a/skl2onnx/sklapi/woe_transformer.py +++ b/skl2onnx/sklapi/woe_transformer.py @@ -8,7 +8,7 @@ except ImportError: def _deprecate_positional_args(x): - return x # noqa + return x class WOETransformer(TransformerMixin, BaseEstimator): @@ -80,7 +80,7 @@ def fit(self, X, y=None, sample_weight=None): continue if not isinstance(intervals, list): raise TypeError( - "Intervals for column %d must be a list not %r." "" % (i, intervals) + "Intervals for column %d must be a list not %r." % (i, intervals) ) inlist = [] inweight = [] @@ -91,7 +91,7 @@ def fit(self, X, y=None, sample_weight=None): ) if len(interval) < 2: raise ValueError( - "Interval %d should have at least two values " "%r." % interval + "Interval %d should have at least two values %r." % interval ) res = [] for j in range(0, 2): diff --git a/skl2onnx/sklapi/woe_transformer_onnx.py b/skl2onnx/sklapi/woe_transformer_onnx.py index 231e22ade..bb1b21a86 100644 --- a/skl2onnx/sklapi/woe_transformer_onnx.py +++ b/skl2onnx/sklapi/woe_transformer_onnx.py @@ -1,7 +1,7 @@ # SPDX-License-Identifier: Apache-2.0 import warnings -from typing import List +from typing import List, Optional import numpy as np from onnx.helper import ( make_node, @@ -49,7 +49,7 @@ def woe_parser( scope: Scope, model: BaseEstimator, inputs: List[Variable], - custom_parsers: dict = None, + custom_parsers: Optional[dict] = None, ): "ONNX parser for WOETransformer: defines the output type." alias = _get_sklearn_operator_name(type(model)) @@ -234,7 +234,7 @@ def onnx_attributes(self): nodes_featureids=[n.feature for n in self.nodes], nodes_missing_value_tracks_true=[0 for n in self.nodes], nodes_modes=[n.onnx_mode for n in self.nodes], - nodes_nodeids=[i for i in range(len(self.nodes))], + nodes_nodeids=list(range(len(self.nodes))), nodes_treeids=[0 for n in self.nodes], nodes_values=[float(n.onnx_threshold) for n in self.nodes], post_transform="NONE", @@ -267,7 +267,10 @@ def onnx_attributes(self): ) ) if len(atts["target_weights"]) != len(set(atts["target_weights"])): - warnings.warn("All targets should be unique %r." % atts["target_weights"]) + warnings.warn( + "All targets should be unique %r." % atts["target_weights"], + stacklevel=0, + ) return atts def mapping(self, intervals): @@ -321,7 +324,7 @@ def process(node, intervals): if hasattr(node, at): delattr(node, at) - d_intervals = {i: t for i, t in enumerate(intervals)} + d_intervals = dict(enumerate(intervals)) changes = 1 while changes > 0: changes = 0 @@ -501,7 +504,7 @@ def woe_converter(scope: Scope, operator: Operator, container: ModelComponentCon node = OnnxTreeEnsembleRegressor_1( X, op_version=1, domain="ai.onnx.ml", **atts ) - cats = list(sorted(set(int(n.onnx_value) for n in tree.nodes if n.is_leaf))) + cats = list(sorted({int(n.onnx_value) for n in tree.nodes if n.is_leaf})) mat_mapping = _mapping2matrix(mapping, cats, op.weights_[i], dtype) if verbose > 1: print("[woe_converter] mapping=%r" % mapping) @@ -599,7 +602,7 @@ def woe_transformer_to_onnx(op, opset=None): **atts ) ) - cats = list(sorted(set(int(n.onnx_value) for n in tree.nodes if n.is_leaf))) + cats = list(sorted({int(n.onnx_value) for n in tree.nodes if n.is_leaf})) mat_mapping = _mapping2matrix(mapping, cats, op.weights_[i], np.float32) nodes.append( make_node("Reshape", ["rf%d" % i, "vector_shape"], ["resh%d" % i]) diff --git a/skl2onnx/tutorial/__init__.py b/skl2onnx/tutorial/__init__.py index e0ac6c472..f87885b8f 100644 --- a/skl2onnx/tutorial/__init__.py +++ b/skl2onnx/tutorial/__init__.py @@ -4,4 +4,4 @@ Shortcuts to *tutorial*. """ -from .benchmark import measure_time # noqa +from .benchmark import measure_time diff --git a/skl2onnx/tutorial/benchmark.py b/skl2onnx/tutorial/benchmark.py index 411342bc9..061adcf17 100644 --- a/skl2onnx/tutorial/benchmark.py +++ b/skl2onnx/tutorial/benchmark.py @@ -3,6 +3,7 @@ """ Tools to help benchmarking. """ + from timeit import Timer import numpy diff --git a/skl2onnx/tutorial/imagenet_classes.py b/skl2onnx/tutorial/imagenet_classes.py index bdeb97969..07976135c 100644 --- a/skl2onnx/tutorial/imagenet_classes.py +++ b/skl2onnx/tutorial/imagenet_classes.py @@ -40,7 +40,7 @@ 31: "tree frog, tree-frog", 32: "tailed frog, bell toad, ribbed toad, tailed toad, Ascaphus trui", 33: "loggerhead, loggerhead turtle, Caretta caretta", - 34: "leatherback turtle, leatherback, leathery turtle, " "Dermochelys coriacea", + 34: "leatherback turtle, leatherback, leathery turtle, Dermochelys coriacea", 35: "mud turtle", 36: "terrapin", 37: "box turtle, box tortoise", @@ -113,7 +113,7 @@ 103: "platypus, duckbill, duckbilled platypus, duck-billed " "platypus, Ornithorhynchus anatinus", 104: "wallaby, brush kangaroo", - 105: "koala, koala bear, kangaroo bear, native bear, " "Phascolarctos cinereus", + 105: "koala, koala bear, kangaroo bear, native bear, Phascolarctos cinereus", 106: "wombat", 107: "jellyfish", 108: "sea anemone, anemone", @@ -131,8 +131,8 @@ 120: "fiddler crab", 121: "king crab, Alaska crab, Alaskan king crab, Alaska king " "crab, Paralithodes camtschatica", - 122: "American lobster, Northern lobster, Maine lobster, " "Homarus americanus", - 123: "spiny lobster, langouste, rock lobster, crawfish, " "crayfish, sea crawfish", + 122: "American lobster, Northern lobster, Maine lobster, Homarus americanus", + 123: "spiny lobster, langouste, rock lobster, crawfish, crayfish, sea crawfish", 124: "crayfish, crawfish, crawdad, crawdaddy", 125: "hermit crab", 126: "isopod", @@ -297,7 +297,7 @@ 283: "Persian cat", 284: "Siamese cat, Siamese", 285: "Egyptian cat", - 286: "cougar, puma, catamount, mountain lion, painter, " "panther, Felis concolor", + 286: "cougar, puma, catamount, mountain lion, painter, panther, Felis concolor", 287: "lynx, catamount", 288: "leopard, Panthera pardus", 289: "snow leopard, ounce, Panthera uncia", @@ -306,7 +306,7 @@ 292: "tiger, Panthera tigris", 293: "cheetah, chetah, Acinonyx jubatus", 294: "brown bear, bruin, Ursus arctos", - 295: "American black bear, black bear, Ursus americanus, " "Euarctos americanus", + 295: "American black bear, black bear, Ursus americanus, Euarctos americanus", 296: "ice bear, polar bear, Ursus Maritimus, Thalarctos maritimus", 297: "sloth bear, Melursus ursinus, Ursus ursinus", 298: "mongoose", @@ -405,7 +405,7 @@ 388: "giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca", 389: "barracouta, snoek", 390: "eel", - 391: "coho, cohoe, coho salmon, blue jack, silver salmon, " "Oncorhynchus kisutch", + 391: "coho, cohoe, coho salmon, blue jack, silver salmon, Oncorhynchus kisutch", 392: "rock beauty, Holocanthus tricolor", 393: "anemone fish", 394: "sturgeon", @@ -651,7 +651,7 @@ 629: "lipstick, lip rouge", 630: "Loafer", 631: "lotion", - 632: "loudspeaker, speaker, speaker unit, loudspeaker " "system, speaker system", + 632: "loudspeaker, speaker, speaker unit, loudspeaker system, speaker system", 633: "loupe, jeweler's loupe", 634: "lumbermill, sawmill", 635: "magnetic compass", @@ -753,7 +753,7 @@ 731: "plunger, plumber's helper", 732: "Polaroid camera, Polaroid Land camera", 733: "pole", - 734: "police van, police wagon, paddy wagon, patrol wagon, " "wagon, black Maria", + 734: "police van, police wagon, paddy wagon, patrol wagon, wagon, black Maria", 735: "poncho", 736: "pool table, billiard table, snooker table", 737: "pop bottle, soda bottle", @@ -1017,7 +1017,7 @@ 993: "gyromitra", 994: "stinkhorn, carrion fungus", 995: "earthstar", - 996: "hen-of-the-woods, hen of the woods, Polyporus frondosus, " "Grifola frondosa", + 996: "hen-of-the-woods, hen of the woods, Polyporus frondosus, Grifola frondosa", 997: "bolete", 998: "ear, spike, capitulum", 999: "toilet tissue, toilet paper, bathroom tissue", diff --git a/tests/benchmark.py b/tests/benchmark.py index fc93dd6b2..58f04e5a1 100644 --- a/tests/benchmark.py +++ b/tests/benchmark.py @@ -35,7 +35,7 @@ def run_all_tests(folder=None, verbose=True): print("[benchmark] look into '{0}'".format(folder)) try: - import onnxmltools # noqa + import onnxmltools # noqa: F401 except ImportError: warnings.warn("Cannot import onnxmltools. Some tests won't work.") diff --git a/tests/test_algebra_cascade.py b/tests/test_algebra_cascade.py index 0ba83580b..46a280842 100644 --- a/tests/test_algebra_cascade.py +++ b/tests/test_algebra_cascade.py @@ -108,7 +108,7 @@ def test_cascade_scaler(self): def generate_onnx_graph(dim, nbnode, input_name="X1", opv=1): i1 = input_name scale = list(np.ones((1, dim)).ravel()) - for i in range(nbnode - 1): + for _i in range(nbnode - 1): i2 = list(map(float, np.ones((1, dim)).astype(np.float32).ravel())) node = OnnxScaler(i1, offset=i2, scale=scale, op_version=opv) i1 = node @@ -173,7 +173,7 @@ def test_scaler_converted(self): onx = to_onnx(st, X.astype(np.float32), target_opset=opv) except RuntimeError as e: if ( - "is higher than the number of the " "installed onnx package" + "is higher than the number of the installed onnx package" ) in str(e): continue raise e @@ -233,7 +233,7 @@ def test_model_mlp_regressor_default(self): ) except RuntimeError as e: if ( - "is higher than the number of the " "installed onnx package" + "is higher than the number of the installed onnx package" ) in str(e): continue raise e @@ -247,9 +247,7 @@ def test_model_mlp_regressor_default(self): continue if opv >= onnx_opset_version(): continue - if ("No suitable kernel definition found for " "op Cast(9)") in str( - e - ): + if ("No suitable kernel definition found for op Cast(9)") in str(e): # too old onnxruntime continue raise AssertionError( diff --git a/tests/test_algebra_custom_model.py b/tests/test_algebra_custom_model.py index c6f78f8ba..4a06b2cdf 100644 --- a/tests/test_algebra_custom_model.py +++ b/tests/test_algebra_custom_model.py @@ -3,6 +3,7 @@ """ Tests scikit-learn's binarizer converter. """ + import unittest import numpy as np import onnx diff --git a/tests/test_algebra_custom_model_sub_estimator.py b/tests/test_algebra_custom_model_sub_estimator.py index cea2fd1ae..f4058eb24 100644 --- a/tests/test_algebra_custom_model_sub_estimator.py +++ b/tests/test_algebra_custom_model_sub_estimator.py @@ -3,6 +3,7 @@ """ Tests scikit-learn's binarizer converter. """ + import unittest import warnings import numpy as np diff --git a/tests/test_algebra_deprecation.py b/tests/test_algebra_deprecation.py index bf95741c4..2c3249e4f 100644 --- a/tests/test_algebra_deprecation.py +++ b/tests/test_algebra_deprecation.py @@ -92,7 +92,7 @@ def test_sub_operator(self): mes = None for w in ws: if ( - w.category == DeprecationWarning + w.category is DeprecationWarning and "numpy" not in str(w.message).lower() ): mes = w.message diff --git a/tests/test_algebra_onnx_operators.py b/tests/test_algebra_onnx_operators.py index ed52d72b8..68ce078fb 100644 --- a/tests/test_algebra_onnx_operators.py +++ b/tests/test_algebra_onnx_operators.py @@ -282,11 +282,12 @@ def test_constant_of_shape(self): for opset in [TARGET_OPSET]: for value in [5, np.float32(5)]: - with self.subTest(opset=opset, value=value): - with self.assertRaises(TypeError): - OnnxConstantOfShape( - "X", value=value, op_version=opset, output_names=["Y"] - ) + with self.subTest(opset=opset, value=value), self.assertRaises( + TypeError + ): + OnnxConstantOfShape( + "X", value=value, op_version=opset, output_names=["Y"] + ) @unittest.skipIf(TARGET_OPSET < 10, reason="not available") def test_onnx_reversed_order(self): diff --git a/tests/test_algebra_onnx_operators_opset.py b/tests/test_algebra_onnx_operators_opset.py index 719509753..2f1391c53 100644 --- a/tests/test_algebra_onnx_operators_opset.py +++ b/tests/test_algebra_onnx_operators_opset.py @@ -5,7 +5,7 @@ from numpy.testing import assert_almost_equal import onnx import onnxruntime as ort -from skl2onnx.algebra.onnx_ops import OnnxPad # noqa +from skl2onnx.algebra.onnx_ops import OnnxPad class TestOnnxOperatorsOpset(unittest.TestCase): diff --git a/tests/test_algebra_symbolic.py b/tests/test_algebra_symbolic.py index f84a35974..71540b51a 100644 --- a/tests/test_algebra_symbolic.py +++ b/tests/test_algebra_symbolic.py @@ -167,7 +167,7 @@ def generate_onnx_graph(dim, nbnode, input_name="X1"): matrices = [] scale = list(numpy.ones((1, dim)).ravel()) i1 = input_name - for i in range(nbnode - 1): + for _i in range(nbnode - 1): i2 = list(rand(1, dim).ravel()) matrices.append(i2) node = OnnxScaler(i1, offset=i2, scale=scale) diff --git a/tests/test_algebra_test_helper.py b/tests/test_algebra_test_helper.py index e01e75e69..41d6e31a4 100644 --- a/tests/test_algebra_test_helper.py +++ b/tests/test_algebra_test_helper.py @@ -159,7 +159,7 @@ def test_tensor_type(self): dtypes.append( (np.complex128, Complex128TensorType, onnx_proto.TensorProto.COMPLEX128) ) - for dtype, exp, pt in dtypes: + for _dtype, exp, _pt in dtypes: nt2 = guess_tensor_type(exp([None, 1])) self.assertEqual(nt2.__class__, exp) diff --git a/tests/test_convert_options.py b/tests/test_convert_options.py index 77c6ca04e..d00de6fd9 100644 --- a/tests/test_convert_options.py +++ b/tests/test_convert_options.py @@ -227,7 +227,7 @@ def almost_equal_multi( assert_almost_equal(row.ravel(), pr.ravel(), decimal=decimal) elif zipmap: - for expected, proba in zip(expected_proba, probas): + for _expected, proba in zip(expected_proba, probas): assert_almost_equal( expected_proba, TestConvertOptions.dict_to_array(proba), diff --git a/tests/test_custom_transformer_ordwoe.py b/tests/test_custom_transformer_ordwoe.py index ea98ba79d..f1d29f6ee 100644 --- a/tests/test_custom_transformer_ordwoe.py +++ b/tests/test_custom_transformer_ordwoe.py @@ -3,6 +3,7 @@ """ Tests scikit-learn's binarizer converter. """ + import unittest import numpy as np from numpy.testing import assert_almost_equal @@ -16,8 +17,8 @@ from skl2onnx.algebra.onnx_ops import OnnxCast, OnnxIdentity from skl2onnx.algebra.onnx_operator import OnnxSubEstimator from skl2onnx.sklapi import WOETransformer -import skl2onnx.sklapi.register # noqa from test_utils import TARGET_OPSET, InferenceSessionEx as InferenceSession +import skl2onnx.sklapi.register # noqa: F401 class OrdinalWOETransformer(BaseEstimator, TransformerMixin): diff --git a/tests/test_custom_transformer_tsne.py b/tests/test_custom_transformer_tsne.py index 34ea5ade2..9492ff1d3 100644 --- a/tests/test_custom_transformer_tsne.py +++ b/tests/test_custom_transformer_tsne.py @@ -3,6 +3,7 @@ """ Tests scikit-learn's binarizer converter. """ + import unittest import inspect import numpy @@ -31,7 +32,7 @@ def __init__( estimator=None, normalize=True, keep_tsne_outputs=False, - **kwargs + **kwargs, ): TransformerMixin.__init__(self) BaseEstimator.__init__(self) @@ -147,7 +148,7 @@ def predictable_tsne_converter(scope, operator, container): [knn_output.onnx_name], [output.full_name], op_domain="ai.onnx.ml", - **attrs + **attrs, ) diff --git a/tests/test_investigate.py b/tests/test_investigate.py index 691153976..42e98074f 100644 --- a/tests/test_investigate.py +++ b/tests/test_investigate.py @@ -102,7 +102,7 @@ def test_missing_converter(self): model.transform(data) all_models = list(enumerate_pipeline_models(model)) - for ind, step, last in all_models: + for ind, step, _last in all_models: if ind == (0,): # whole pipeline continue diff --git a/tests/test_onnx_helper.py b/tests/test_onnx_helper.py index 7dc348a8e..7d0433055 100644 --- a/tests/test_onnx_helper.py +++ b/tests/test_onnx_helper.py @@ -3,6 +3,7 @@ """ Tests on functions in *onnx_helper*. """ + import unittest import packaging.version as pv import numpy @@ -40,7 +41,7 @@ def skl12(): class TestOnnxHelper(unittest.TestCase): def get_model(self, model): try: - import onnxruntime # noqa + import onnxruntime # noqa: F401 except ImportError: return None diff --git a/tests/test_onnx_rare_helper.py b/tests/test_onnx_rare_helper.py index 6afeeea6b..47621d0a2 100644 --- a/tests/test_onnx_rare_helper.py +++ b/tests/test_onnx_rare_helper.py @@ -3,6 +3,7 @@ """ Tests on functions in *onnx_helper*. """ + import unittest from sklearn.datasets import load_iris from sklearn.cluster import KMeans diff --git a/tests/test_onnxruntime.py b/tests/test_onnxruntime.py index c40b915f4..de121b9aa 100644 --- a/tests/test_onnxruntime.py +++ b/tests/test_onnxruntime.py @@ -7,7 +7,7 @@ from numpy.testing import assert_allclose try: - import onnx.reference # noqa + import onnx.reference # noqa: F401 from test_utils import ReferenceEvaluatorEx except ImportError: ReferenceEvaluatorEx = None diff --git a/tests/test_options.py b/tests/test_options.py index eed853cf7..5784fb9d2 100644 --- a/tests/test_options.py +++ b/tests/test_options.py @@ -3,6 +3,7 @@ """ Tests topology. """ + import unittest import numpy from sklearn.base import BaseEstimator, TransformerMixin diff --git a/tests/test_other_converter_library_pipelines.py b/tests/test_other_converter_library_pipelines.py index 13b4fe26d..00ff5295c 100644 --- a/tests/test_other_converter_library_pipelines.py +++ b/tests/test_other_converter_library_pipelines.py @@ -3,6 +3,7 @@ """ Tests scikit-learn's binarizer converter. """ + import unittest import numpy from sklearn.base import BaseEstimator, ClassifierMixin diff --git a/tests/test_scikit_pandas.py b/tests/test_scikit_pandas.py index 0abffee8f..8a2b059fe 100644 --- a/tests/test_scikit_pandas.py +++ b/tests/test_scikit_pandas.py @@ -3,6 +3,7 @@ """ Tests scikit-learn's binarizer converter. """ + import unittest import pandas from sklearn.preprocessing import StandardScaler, MinMaxScaler @@ -13,7 +14,7 @@ def has_scikit_pandas(): try: - import sklearn_pandas # noqa + import sklearn_pandas # noqa: F401 return True except ImportError: @@ -45,7 +46,7 @@ def test_scikit_pandas(self): ) try: - model_onnx = convert_sklearn( # noqa + convert_sklearn( mapper, "predictable_tsne", [("input", FloatTensorType([None, df.shape[1]]))], diff --git a/tests/test_sklearn_cast_regressor.py b/tests/test_sklearn_cast_regressor.py index a158f1a3f..4641818cf 100644 --- a/tests/test_sklearn_cast_regressor.py +++ b/tests/test_sklearn_cast_regressor.py @@ -3,6 +3,7 @@ """ Tests scikit-learn's cast transformer converter. """ + import unittest import math import packaging.version as pv diff --git a/tests/test_sklearn_cast_transformer.py b/tests/test_sklearn_cast_transformer.py index e90a376c1..10221c0e4 100644 --- a/tests/test_sklearn_cast_transformer.py +++ b/tests/test_sklearn_cast_transformer.py @@ -3,6 +3,7 @@ """ Tests scikit-learn's cast transformer converter. """ + import unittest import math import packaging.version as pv diff --git a/tests/test_sklearn_count_vectorizer_converter.py b/tests/test_sklearn_count_vectorizer_converter.py index 65bf03868..e9ae0e042 100644 --- a/tests/test_sklearn_count_vectorizer_converter.py +++ b/tests/test_sklearn_count_vectorizer_converter.py @@ -3,6 +3,7 @@ """ Tests scikit-learn's CountVectorizer converter. """ + import unittest import sys import packaging.version as pv diff --git a/tests/test_sklearn_count_vectorizer_converter_bug.py b/tests/test_sklearn_count_vectorizer_converter_bug.py index 90cbfa09b..074a22a1c 100644 --- a/tests/test_sklearn_count_vectorizer_converter_bug.py +++ b/tests/test_sklearn_count_vectorizer_converter_bug.py @@ -3,6 +3,7 @@ """ Tests scikit-learn's count vectorizer converter. """ + import unittest import packaging.version as pv import numpy diff --git a/tests/test_sklearn_decision_tree_converters.py b/tests/test_sklearn_decision_tree_converters.py index 4ecc9c582..757ca697d 100644 --- a/tests/test_sklearn_decision_tree_converters.py +++ b/tests/test_sklearn_decision_tree_converters.py @@ -43,7 +43,7 @@ class TestSklearnDecisionTreeModels(unittest.TestCase): @unittest.skipIf( pv.Version(ort_version) <= pv.Version("0.3.0"), - reason="No suitable kernel definition found " "for op Cast(9) (node Cast)", + reason="No suitable kernel definition found for op Cast(9) (node Cast)", ) def test_decisiontree_classifier1(self): model = DecisionTreeClassifier(max_depth=2) diff --git a/tests/test_sklearn_documentation.py b/tests/test_sklearn_documentation.py index 00acc45f7..81fe1941a 100644 --- a/tests/test_sklearn_documentation.py +++ b/tests/test_sklearn_documentation.py @@ -3,6 +3,7 @@ """ Tests examples from scikit-learn documentation. """ + import sys import unittest import urllib.error diff --git a/tests/test_sklearn_double_tensor_type_cls.py b/tests/test_sklearn_double_tensor_type_cls.py index 7da6f3354..da59f9168 100644 --- a/tests/test_sklearn_double_tensor_type_cls.py +++ b/tests/test_sklearn_double_tensor_type_cls.py @@ -73,10 +73,7 @@ def _common_classifier( verbose=False, ): for model_cls in model_cls_set: - if name_root is None: - name = model_cls.__name__ - else: - name = name_root + name = model_cls.__name__ if name_root is None else name_root for n_cl in [2, 3]: model, X = fit_classification_model( model_cls(), diff --git a/tests/test_sklearn_double_tensor_type_reg.py b/tests/test_sklearn_double_tensor_type_reg.py index e4aa67cbd..dc2ad9e51 100644 --- a/tests/test_sklearn_double_tensor_type_reg.py +++ b/tests/test_sklearn_double_tensor_type_reg.py @@ -81,7 +81,7 @@ def test_model_mlpregressor_64(self): @unittest.skipIf( pv.Version(ort_version) < pv.Version("1.7.0"), - reason="onnxruntime misses implementation for " "ReduceMean for double", + reason="onnxruntime misses implementation for ReduceMean for double", ) @ignore_warnings(category=warnings_to_skip) def test_bagging_regressor_sgd_64(self): @@ -146,7 +146,7 @@ def test_gpr_rbf_fitted_true_double(self): @unittest.skipIf( pv.Version(ort_version) < pv.Version("1.7.0"), - reason="onnxruntime misses implementation for " "TopK for double", + reason="onnxruntime misses implementation for TopK for double", ) @ignore_warnings(category=warnings_to_skip) def test_model_knn_regressor_double(self): @@ -169,7 +169,7 @@ def test_model_knn_regressor_double(self): @unittest.skipIf(VotingRegressor is None, reason="new in 0.21") @unittest.skipIf( pv.Version(ort_version) < pv.Version("1.7.0"), - reason="onnxruntime misses implementation for " "Sum for double", + reason="onnxruntime misses implementation for Sum for double", ) @ignore_warnings(category=warnings_to_skip) def test_model_voting_regression(self): diff --git a/tests/test_sklearn_double_tensor_type_tr.py b/tests/test_sklearn_double_tensor_type_tr.py index 78e2e7523..84d4111ef 100644 --- a/tests/test_sklearn_double_tensor_type_tr.py +++ b/tests/test_sklearn_double_tensor_type_tr.py @@ -44,11 +44,7 @@ class TestSklearnDoubleTensorTypeTransformer(unittest.TestCase): def _common_transform(self, model_cls_set, name_root=None, debug=False): for model_cls in model_cls_set: - if name_root is None: - name = model_cls.__name__ - else: - name = name_root - + name = model_cls.__name__ if name_root is None else name_root model = model_cls() X = np.random.randn(100, 4).astype(np.float64) model.fit(X) diff --git a/tests/test_sklearn_feature_hasher.py b/tests/test_sklearn_feature_hasher.py index aab1fb037..7b4b5c6ee 100644 --- a/tests/test_sklearn_feature_hasher.py +++ b/tests/test_sklearn_feature_hasher.py @@ -1,9 +1,10 @@ # SPDX-License-Identifier: Apache-2.0 -# coding: utf-8 """ Tests scikit-learn's feature selection converters """ + import unittest +from typing import Optional import packaging.version as pv import numpy as np from sklearn.utils._testing import assert_almost_equal @@ -540,7 +541,9 @@ def MurmurHash3_x86_32(data, seed): h1 = MurmurHash3.fmix(h1) return h1 - def _run(self, x, positive: int = None, seed: int = None): + def _run( + self, x, positive: Optional[int] = None, seed: Optional[int] = None + ): x2 = x.reshape((-1,)) y = np.empty(x2.shape, dtype=np.uint32) for i in range(y.shape[0]): diff --git a/tests/test_sklearn_feature_selection_converters.py b/tests/test_sklearn_feature_selection_converters.py index 63f3136d0..1feaa11e3 100644 --- a/tests/test_sklearn_feature_selection_converters.py +++ b/tests/test_sklearn_feature_selection_converters.py @@ -3,6 +3,7 @@ """ Tests scikit-learn's feature selection converters """ + import unittest import numpy as np from sklearn.datasets import load_breast_cancer diff --git a/tests/test_sklearn_function_transformer_converter.py b/tests/test_sklearn_function_transformer_converter.py index 41b67e367..c1f9f6cfa 100644 --- a/tests/test_sklearn_function_transformer_converter.py +++ b/tests/test_sklearn_function_transformer_converter.py @@ -3,6 +3,7 @@ """ Tests scikit-imputer converter. """ + import unittest import numpy as np import pandas diff --git a/tests/test_sklearn_gaussian_process_classifier.py b/tests/test_sklearn_gaussian_process_classifier.py index df3050248..57e6efe75 100644 --- a/tests/test_sklearn_gaussian_process_classifier.py +++ b/tests/test_sklearn_gaussian_process_classifier.py @@ -77,10 +77,7 @@ def common_test_gpc(self, dtype=np.float32, n_classes=2): gp, X = self.fit_classification_model(gp, n_classes=n_classes) # return_cov=False, return_std=False - if dtype == np.float32: - cls = FloatTensorType - else: - cls = DoubleTensorType + cls = FloatTensorType if dtype == np.float32 else DoubleTensorType model_onnx = to_onnx( gp, initial_types=[("X", cls([None, None]))], diff --git a/tests/test_sklearn_gaussian_process_regressor.py b/tests/test_sklearn_gaussian_process_regressor.py index 4bdc0ae73..a6c102bb7 100644 --- a/tests/test_sklearn_gaussian_process_regressor.py +++ b/tests/test_sklearn_gaussian_process_regressor.py @@ -186,7 +186,7 @@ def check_outputs( assert_almost_equal( self.remove_dim1(e), self.remove_dim1(g), decimal=decimal ) - except AssertionError as e: # noqa + except AssertionError as e: raise AssertionError( "Mismatch for output {} and attributes {}" ".".format(i, predict_attributes) @@ -1043,7 +1043,7 @@ def test_gpr_rbf_fitted_return_std_exp_sine_squared_double_true(self): except (AttributeError, TypeError) as e: # unstable issue fixed with scikit-learn>=0.24 warnings.warn( - "Training did not converge but fails at raising " "a warning: %r." % e + "Training did not converge but fails at raising a warning: %r." % e ) return diff --git a/tests/test_sklearn_grid_search_cv_converter.py b/tests/test_sklearn_grid_search_cv_converter.py index 3144d57fc..fec20d8ee 100644 --- a/tests/test_sklearn_grid_search_cv_converter.py +++ b/tests/test_sklearn_grid_search_cv_converter.py @@ -169,7 +169,7 @@ def test_grid_search_gaussian_regressor_float(self): X, model, model_onnx, - basename="SklearnGridSearchGaussianRegressionFloat" "-OneOffArray-Dec4", + basename="SklearnGridSearchGaussianRegressionFloat-OneOffArray-Dec4", ) @unittest.skipIf( @@ -191,7 +191,7 @@ def test_grid_search_gaussian_regressor_double(self): X.astype(np.float64), model, model_onnx, - basename="SklearnGridSearchGaussianRegressionDouble" "-OneOffArray-Dec4", + basename="SklearnGridSearchGaussianRegressionDouble-OneOffArray-Dec4", ) def test_grid_search_binary_float_nozipmap(self): diff --git a/tests/test_sklearn_imputer_converter.py b/tests/test_sklearn_imputer_converter.py index e1841b945..331698d65 100644 --- a/tests/test_sklearn_imputer_converter.py +++ b/tests/test_sklearn_imputer_converter.py @@ -3,6 +3,7 @@ """ Tests scikit-imputer converter. """ + import unittest import packaging.version as pv import numpy as np diff --git a/tests/test_sklearn_isolation_forest.py b/tests/test_sklearn_isolation_forest.py index dd072ed34..be0a1b8ee 100644 --- a/tests/test_sklearn_isolation_forest.py +++ b/tests/test_sklearn_isolation_forest.py @@ -3,6 +3,7 @@ """ Test scikit-learn's IsolationForest. """ + import logging import unittest import packaging.version as pv diff --git a/tests/test_sklearn_kernel_pca_converter.py b/tests/test_sklearn_kernel_pca_converter.py index c9b33df9f..6d7c9eb52 100644 --- a/tests/test_sklearn_kernel_pca_converter.py +++ b/tests/test_sklearn_kernel_pca_converter.py @@ -37,7 +37,11 @@ def _fit_model(self, model, dtype=np.float32): ) @ignore_warnings(category=(FutureWarning, DeprecationWarning)) def test_kernel_pca_default_float(self): - model, X_test = self._fit_model(KernelPCA(random_state=42)) + try: + model, X_test = self._fit_model(KernelPCA(random_state=42)) + except TypeError as e: + # not a converter issue + raise unittest.SkipTest(e) model_onnx = to_onnx(model, X_test, target_opset=TARGET_OPSET) dump_data_and_model(X_test, model, model_onnx, basename="SklearnKernelPCA32") diff --git a/tests/test_sklearn_local_outlier_factor.py b/tests/test_sklearn_local_outlier_factor.py index 574d1e0c8..1f68b2634 100644 --- a/tests/test_sklearn_local_outlier_factor.py +++ b/tests/test_sklearn_local_outlier_factor.py @@ -3,6 +3,7 @@ """ Test scikit-learn's LocalOutlierFactor. """ + import unittest import packaging.version as pv import numpy as np diff --git a/tests/test_sklearn_mlp_converter.py b/tests/test_sklearn_mlp_converter.py index 8aa18ed6d..607e88a2d 100644 --- a/tests/test_sklearn_mlp_converter.py +++ b/tests/test_sklearn_mlp_converter.py @@ -3,6 +3,7 @@ """ Tests scikit-learn's MLPClassifier and MLPRegressor converters. """ + import packaging.version as pv import unittest import numpy as np diff --git a/tests/test_sklearn_multi_output.py b/tests/test_sklearn_multi_output.py index a26cb58f2..e12b2403f 100644 --- a/tests/test_sklearn_multi_output.py +++ b/tests/test_sklearn_multi_output.py @@ -31,7 +31,6 @@ def setUp(self): log.disabled = True # log.setLevel(logging.DEBUG) # logging.basicConfig(level=logging.DEBUG) - pass @unittest.skipIf( pv.Version(ort_version) <= pv.Version("1.11.0"), diff --git a/tests/test_sklearn_nearest_neighbour_converter.py b/tests/test_sklearn_nearest_neighbour_converter.py index 9020097db..d9ae8a815 100644 --- a/tests/test_sklearn_nearest_neighbour_converter.py +++ b/tests/test_sklearn_nearest_neighbour_converter.py @@ -3,6 +3,7 @@ """ Tests scikit-learn's KNeighbours Classifier and Regressor converters. """ + import sys import warnings import unittest @@ -202,7 +203,7 @@ def test_model_knn_regressor_double(self): ) except OrtImpl as e: if ( - "Could not find an implementation for the node " "To_TopK:TopK(11)" + "Could not find an implementation for the node To_TopK:TopK(11)" ) in str(e): # onnxruntime does not declare TopK(11) for double return diff --git a/tests/test_sklearn_normalizer_converter.py b/tests/test_sklearn_normalizer_converter.py index 5975da08b..7fe97e88a 100644 --- a/tests/test_sklearn_normalizer_converter.py +++ b/tests/test_sklearn_normalizer_converter.py @@ -3,6 +3,7 @@ """ Tests scikit-normalizer converter. """ + import unittest import numpy from sklearn.preprocessing import Normalizer diff --git a/tests/test_sklearn_one_hot_encoder_converter.py b/tests/test_sklearn_one_hot_encoder_converter.py index 6696cf888..3dc2b5a13 100644 --- a/tests/test_sklearn_one_hot_encoder_converter.py +++ b/tests/test_sklearn_one_hot_encoder_converter.py @@ -1,6 +1,7 @@ # SPDX-License-Identifier: Apache-2.0 """Tests scikit-learn's OneHotEncoder converter.""" + import unittest import sys import packaging.version as pv diff --git a/tests/test_sklearn_ordinal_encoder.py b/tests/test_sklearn_ordinal_encoder.py index 608296ed2..584fd1273 100644 --- a/tests/test_sklearn_ordinal_encoder.py +++ b/tests/test_sklearn_ordinal_encoder.py @@ -1,6 +1,7 @@ # SPDX-License-Identifier: Apache-2.0 """Tests scikit-learn's OrdinalEncoder converter.""" + import unittest from numpy.testing import assert_almost_equal import packaging.version as pv diff --git a/tests/test_sklearn_passive_aggressive_classifier_converter.py b/tests/test_sklearn_passive_aggressive_classifier_converter.py index 45874a842..e5f699545 100644 --- a/tests/test_sklearn_passive_aggressive_classifier_converter.py +++ b/tests/test_sklearn_passive_aggressive_classifier_converter.py @@ -1,6 +1,7 @@ # SPDX-License-Identifier: Apache-2.0 """Tests scikit-learn's Passive Aggressive Classifier converter.""" + import unittest from sklearn.linear_model import PassiveAggressiveClassifier from skl2onnx import convert_sklearn diff --git a/tests/test_sklearn_pipeline.py b/tests/test_sklearn_pipeline.py index a731a5b73..928d90886 100644 --- a/tests/test_sklearn_pipeline.py +++ b/tests/test_sklearn_pipeline.py @@ -196,8 +196,6 @@ def test_combine_inputs_union_in_pipeline(self): basename="SklearnPipelineScaler11Union", ) - TARGET_OPSET - @unittest.skipIf(TARGET_OPSET < 15, reason="uses CastLike") @unittest.skipIf( pv.Version(ort_version) <= pv.Version("0.4.0"), reason="onnxruntime too old" @@ -823,7 +821,10 @@ def test_pipeline_pipeline_voting_tfidf_svc(self): ] ) y = numpy.array([0, 0, 1, 0, 1]) - voting.fit(data, y) + try: + voting.fit(data, y) + except AttributeError as e: + raise unittest.SkipTest(e) expected_label = voting.predict(data) expected_proba = voting.predict_proba(data) df = pandas.DataFrame(data) diff --git a/tests/test_sklearn_pipeline_within_pipeline.py b/tests/test_sklearn_pipeline_within_pipeline.py index 77e833d8f..031cfcecc 100644 --- a/tests/test_sklearn_pipeline_within_pipeline.py +++ b/tests/test_sklearn_pipeline_within_pipeline.py @@ -3,6 +3,7 @@ """ Tests pipeline within pipelines. """ + from textwrap import dedent import unittest from io import StringIO diff --git a/tests/test_sklearn_polynomial_features_converter.py b/tests/test_sklearn_polynomial_features_converter.py index af7f39129..cf6b87dde 100644 --- a/tests/test_sklearn_polynomial_features_converter.py +++ b/tests/test_sklearn_polynomial_features_converter.py @@ -3,6 +3,7 @@ """ Tests scikit-learn's polynomial features converter. """ + import unittest import numpy as np diff --git a/tests/test_sklearn_power_transformer.py b/tests/test_sklearn_power_transformer.py index c8177b22b..c59a8c30c 100644 --- a/tests/test_sklearn_power_transformer.py +++ b/tests/test_sklearn_power_transformer.py @@ -3,6 +3,7 @@ """ Test scikit-learn's PowerTransform """ + import unittest import packaging.version as pv diff --git a/tests/test_sklearn_replace_transformer.py b/tests/test_sklearn_replace_transformer.py index 9f8559d5b..b199849e5 100644 --- a/tests/test_sklearn_replace_transformer.py +++ b/tests/test_sklearn_replace_transformer.py @@ -3,6 +3,7 @@ """ Tests scikit-learn's cast transformer converter. """ + import unittest import numpy from sklearn.pipeline import Pipeline diff --git a/tests/test_sklearn_scaler_converter.py b/tests/test_sklearn_scaler_converter.py index 94128bac0..312a1e448 100644 --- a/tests/test_sklearn_scaler_converter.py +++ b/tests/test_sklearn_scaler_converter.py @@ -3,6 +3,7 @@ """ Tests scikit-learn's standard scaler converter. """ + import unittest import packaging.version as pv import numpy diff --git a/tests/test_sklearn_svm_converters.py b/tests/test_sklearn_svm_converters.py index da6db75eb..948e36177 100644 --- a/tests/test_sklearn_svm_converters.py +++ b/tests/test_sklearn_svm_converters.py @@ -3,6 +3,7 @@ """ Tests scikit-linear converter. """ + import unittest import packaging.version as pv import numpy diff --git a/tests/test_sklearn_text.py b/tests/test_sklearn_text.py index 9a8ee8603..256f5f635 100644 --- a/tests/test_sklearn_text.py +++ b/tests/test_sklearn_text.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - import unittest import packaging.version as pv import numpy diff --git a/tests/test_sklearn_tfidf_transformer_converter.py b/tests/test_sklearn_tfidf_transformer_converter.py index f848da48d..19e6b0ccb 100644 --- a/tests/test_sklearn_tfidf_transformer_converter.py +++ b/tests/test_sklearn_tfidf_transformer_converter.py @@ -4,6 +4,7 @@ """ Tests scikit-learn's TfidfTransformer converter. """ + import unittest import numpy from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer diff --git a/tests/test_sklearn_tfidf_transformer_converter_sparse.py b/tests/test_sklearn_tfidf_transformer_converter_sparse.py index 134faff7d..cf4523aed 100644 --- a/tests/test_sklearn_tfidf_transformer_converter_sparse.py +++ b/tests/test_sklearn_tfidf_transformer_converter_sparse.py @@ -2,6 +2,7 @@ """ Tests examples from scikit-learn's documentation. """ + import packaging.version as pv import unittest import sys diff --git a/tests/test_sklearn_tfidf_vectorizer_converter.py b/tests/test_sklearn_tfidf_vectorizer_converter.py index 117286488..e2437e000 100644 --- a/tests/test_sklearn_tfidf_vectorizer_converter.py +++ b/tests/test_sklearn_tfidf_vectorizer_converter.py @@ -3,6 +3,7 @@ """ Tests scikit-learn's tfidf converter. """ + import unittest import copy import sys @@ -181,7 +182,7 @@ def test_model_tfidf_vectorizer11_empty_string_case1(self): corpus[2:], vect, model_onnx, - basename="SklearnTfidfVectorizer11EmptyStringSepCase1-" "OneOff-SklCol", + basename="SklearnTfidfVectorizer11EmptyStringSepCase1-OneOff-SklCol", backend=BACKEND, ) diff --git a/tests/test_sklearn_tfidf_vectorizer_converter_char.py b/tests/test_sklearn_tfidf_vectorizer_converter_char.py index b5abf06b6..40f6eff8d 100644 --- a/tests/test_sklearn_tfidf_vectorizer_converter_char.py +++ b/tests/test_sklearn_tfidf_vectorizer_converter_char.py @@ -3,6 +3,7 @@ """ Tests scikit-learn's tfidf converter. """ + import unittest import packaging.version as pv import numpy diff --git a/tests/test_sklearn_tfidf_vectorizer_converter_dataset.py b/tests/test_sklearn_tfidf_vectorizer_converter_dataset.py index d34e05b5d..89f4e0b19 100644 --- a/tests/test_sklearn_tfidf_vectorizer_converter_dataset.py +++ b/tests/test_sklearn_tfidf_vectorizer_converter_dataset.py @@ -3,6 +3,7 @@ """ Tests scikit-learn's tfidf converter using downloaded data. """ + import unittest import packaging.version as pv import numpy as np diff --git a/tests/test_sklearn_tfidf_vectorizer_converter_pipeline.py b/tests/test_sklearn_tfidf_vectorizer_converter_pipeline.py index 65b2fbf33..796b9a45c 100644 --- a/tests/test_sklearn_tfidf_vectorizer_converter_pipeline.py +++ b/tests/test_sklearn_tfidf_vectorizer_converter_pipeline.py @@ -3,6 +3,7 @@ """ Tests scikit-learn's tfidf converter. """ + import unittest import packaging.version as pv import numpy diff --git a/tests/test_sklearn_tfidf_vectorizer_converter_regex.py b/tests/test_sklearn_tfidf_vectorizer_converter_regex.py index 184fd7ceb..aae50f75f 100644 --- a/tests/test_sklearn_tfidf_vectorizer_converter_regex.py +++ b/tests/test_sklearn_tfidf_vectorizer_converter_regex.py @@ -3,6 +3,7 @@ """ Tests scikit-learn's tfidf converter. """ + import unittest import packaging.version as pv import numpy @@ -380,7 +381,7 @@ def test_model_tfidf_vectorizer11parenthesis_class(self): corpus, vect, model_onnx, - basename="SklearnTfidfVectorizer11ParenthesisClassRegex-" "OneOff-SklCol", + basename="SklearnTfidfVectorizer11ParenthesisClassRegex-OneOff-SklCol", backend=BACKEND, ) @@ -434,7 +435,7 @@ def test_model_tfidf_vectorizer11_idparenthesis_id(self): corpus, vect, model_onnx, - basename="SklearnTfidfVectorizer11ParenthesisIdRegex-" "OneOff-SklCol", + basename="SklearnTfidfVectorizer11ParenthesisIdRegex-OneOff-SklCol", backend=BACKEND, ) diff --git a/tests/test_sklearn_woe_transformer.py b/tests/test_sklearn_woe_transformer.py index e54a9c5f6..b53d199b5 100644 --- a/tests/test_sklearn_woe_transformer.py +++ b/tests/test_sklearn_woe_transformer.py @@ -3,6 +3,7 @@ """ Tests scikit-learn's cast transformer converter. """ + import unittest import numpy from numpy.testing import assert_almost_equal @@ -17,9 +18,9 @@ ColumnTransformer = None from skl2onnx.sklapi import WOETransformer from skl2onnx.sklapi.woe_transformer_onnx import woe_transformer_to_onnx -import skl2onnx.sklapi.register # noqa from skl2onnx import to_onnx from test_utils import TARGET_OPSET, InferenceSessionEx as InferenceSession +import skl2onnx.sklapi.register # noqa: F401 class TestSklearnWOETransformerConverter(unittest.TestCase): diff --git a/tests/test_topology_prune.py b/tests/test_topology_prune.py index a5372dcfd..a985012d2 100644 --- a/tests/test_topology_prune.py +++ b/tests/test_topology_prune.py @@ -3,6 +3,7 @@ """ Tests topology. """ + import unittest import numpy from onnxruntime import InferenceSession diff --git a/tests/test_utils/__init__.py b/tests/test_utils/__init__.py index 3e9f67d09..7bb1aeafc 100644 --- a/tests/test_utils/__init__.py +++ b/tests/test_utils/__init__.py @@ -7,14 +7,14 @@ from onnxruntime import __version__ as ort_version from skl2onnx import __max_supported_opset__ as max_opset from skl2onnx.common._topology import OPSET_TO_IR_VERSION -from .tests_helper import dump_data_and_model # noqa -from .tests_helper import ( # noqa +from .tests_helper import dump_data_and_model +from .tests_helper import ( dump_one_class_classification, dump_binary_classification, dump_multilabel_classification, dump_multiple_classification, ) -from .tests_helper import ( # noqa +from .tests_helper import ( dump_multiple_regression, dump_single_regression, convert_model, diff --git a/tests/test_utils/reference_implementation_afe.py b/tests/test_utils/reference_implementation_afe.py index 499f4e399..228613e9b 100644 --- a/tests/test_utils/reference_implementation_afe.py +++ b/tests/test_utils/reference_implementation_afe.py @@ -2,6 +2,7 @@ """ Helpers to test runtimes. """ + from onnx.defs import onnx_opset_version @@ -21,10 +22,7 @@ def _array_feature_extrator(data, indices): for s in indices.shape: add *= s index = indices.ravel().tolist() - if len(data.shape) == 1: - new_shape = (1, add) - else: - new_shape = list(data.shape[:-1]) + [add] + new_shape = (1, add) if len(data.shape) == 1 else list(data.shape[:-1]) + [add] try: tem = data[..., index] except IndexError as e: diff --git a/tests/test_utils/reference_implementation_helper.py b/tests/test_utils/reference_implementation_helper.py index 676a3ffbe..a5ebca90c 100644 --- a/tests/test_utils/reference_implementation_helper.py +++ b/tests/test_utils/reference_implementation_helper.py @@ -69,7 +69,7 @@ def multiclass_probability(k, R): Q[t, t] += R[j, t] * R[j, t] Q[t, j] = -R[j, t] * R[t, j] - for it in range(max_iter): + for _it in range(max_iter): # stopping condition, recalculate QP,pQP for numerical accuracy pQp = 0 for t in range(0, k): diff --git a/tests/test_utils/reference_implementation_ml.py b/tests/test_utils/reference_implementation_ml.py index 7f6ffa799..9399c760a 100644 --- a/tests/test_utils/reference_implementation_ml.py +++ b/tests/test_utils/reference_implementation_ml.py @@ -51,7 +51,7 @@ def _run( _meth = FusedMatMul._fmatmul11 if transB else FusedMatMul._fmatmul10 else: _meth = FusedMatMul._fmatmul01 if transB else FusedMatMul._fmatmul00 - _meth = lambda a, b: _meth(a, b, alpha) # noqa + _meth = lambda a, b: _meth(a, b, alpha) # more recent versions of the operator if transBatchA is None: transBatchA = 0 @@ -220,7 +220,7 @@ def _run(self, x, cats_int64s=None, cats_strings=None, zeros=None): raise RuntimeError("No encoding was defined.") shape = x.shape - new_shape = shape + (len(classes_),) + new_shape = (*shape, len(classes_)) res = np.zeros(new_shape, dtype=np.float32) if len(x.shape) == 1: for i, v in enumerate(x): diff --git a/tests/test_utils/reference_implementation_svm.py b/tests/test_utils/reference_implementation_svm.py index 77692d8f4..e82bdebe0 100644 --- a/tests/test_utils/reference_implementation_svm.py +++ b/tests/test_utils/reference_implementation_svm.py @@ -2,8 +2,9 @@ """ Helpers to test runtimes. """ + import numpy as np -from onnx import numpy_helper # noqa +from onnx import numpy_helper from onnx.defs import onnx_opset_version diff --git a/tests/test_utils/reference_implementation_text.py b/tests/test_utils/reference_implementation_text.py index f5b4d84a8..11a27eedf 100644 --- a/tests/test_utils/reference_implementation_text.py +++ b/tests/test_utils/reference_implementation_text.py @@ -2,6 +2,7 @@ """ Helpers to test runtimes. """ + from typing import List import re import numpy as np @@ -322,10 +323,7 @@ def compute_impl( # type: ignore pool_strings=None, weights=None, ) -> None: - if len(X.shape) > 1: - X_flat = X[row_num] - else: - X_flat = X + X_flat = X[row_num] if len(X.shape) > 1 else X row_begin = 0 row_end = row_begin + row_size diff --git a/tests/test_utils/reference_implementation_tree.py b/tests/test_utils/reference_implementation_tree.py index d25b3e283..8b7457559 100644 --- a/tests/test_utils/reference_implementation_tree.py +++ b/tests/test_utils/reference_implementation_tree.py @@ -2,8 +2,9 @@ """ Helpers to test runtimes. """ + import numpy as np -from onnx import numpy_helper # noqa +from onnx import numpy_helper from onnx.defs import onnx_opset_version diff --git a/tests/test_utils/reference_implementation_zipmap.py b/tests/test_utils/reference_implementation_zipmap.py index 002328a4d..ed8e0ae87 100644 --- a/tests/test_utils/reference_implementation_zipmap.py +++ b/tests/test_utils/reference_implementation_zipmap.py @@ -2,6 +2,7 @@ """ Helpers to test runtimes. """ + import numpy from onnx.defs import onnx_opset_version @@ -74,7 +75,6 @@ def __getitem__(self, key): def __setitem__(self, pos, value): "unused but used by pickle" - pass def __len__(self): """ diff --git a/tests/test_utils/tests_helper.py b/tests/test_utils/tests_helper.py index d6b0e8230..240d7a67f 100644 --- a/tests/test_utils/tests_helper.py +++ b/tests/test_utils/tests_helper.py @@ -332,8 +332,8 @@ def _raw_score_binary_classification(model, X): for method in methods: if callable(method): - def call(X, model=model): - return method(model, X) # noqa + def call(X, model=model, method=method): + return method(model, X) else: try: @@ -350,8 +350,8 @@ def call(X, model=model): prediction.append(call(data)) # we only take the last one for benchmark - def lambda_original(): - return call(dataone) # noqa + def lambda_original(call=call, dataone=dataone): + return call(dataone) else: raise RuntimeError("Method '{0}' is not callable.".format(method)) @@ -362,7 +362,7 @@ def lambda_original(): prediction = [model.predict(data), model.predict_proba(data)] def lambda_original(): - return model.predict_proba(dataone) # noqa + return model.predict_proba(dataone) elif _has_decision_function(model): # Classifier without probabilities @@ -372,7 +372,7 @@ def lambda_original(): ] def lambda_original(): - return model.decision_function(dataone) # noqa + return model.decision_function(dataone) elif _has_transform_model(model): # clustering @@ -380,7 +380,7 @@ def lambda_original(): prediction = [model.predict(data), model.transform(data)] def lambda_original(): - return model.transform(dataone) # noqa + return model.transform(dataone) except ValueError as e: if "Buffer dtype mismatch" in str(e): @@ -393,7 +393,7 @@ def lambda_original(): dataone64 = dataone.astype(numpy.float64) def lambda_original(): - return model.transform(dataone64) # noqa + return model.transform(dataone64) else: raise e @@ -402,7 +402,7 @@ def lambda_original(): prediction = [model.predict(data)] def lambda_original(): - return model.predict(dataone) # noqa + return model.predict(dataone) elif hasattr(model, "transform"): options = extract_options(basename) @@ -411,13 +411,13 @@ def lambda_original(): prediction = model.transform(data.ravel()) def lambda_original(): - return model.transform(dataone.ravel()) # noqa + return model.transform(dataone.ravel()) else: prediction = model.transform(data) def lambda_original(): - return model.transform(dataone) # noqa + return model.transform(dataone) else: raise TypeError( @@ -937,9 +937,9 @@ def timeit_repeat(fct, number, repeat): is a function. """ res = [] - for r in range(0, repeat): + for _r in range(0, repeat): t1 = time.perf_counter() - for i in range(0, number): + for _i in range(0, number): fct() t2 = time.perf_counter() res.append(t2 - t1) @@ -1095,7 +1095,7 @@ def make_report_backend(folder, as_df=False, verbose=0): df = pandas.read_csv(fullname, sep=",") if model not in res: res[model] = {} - for index, row in df.iterrows(): + for _index, row in df.iterrows(): name = row["name"] ave = row["average"] std = row["deviation"] diff --git a/tests/test_utils/utils_backend.py b/tests/test_utils/utils_backend.py index 1e91c38b3..ef4a13c7f 100644 --- a/tests/test_utils/utils_backend.py +++ b/tests/test_utils/utils_backend.py @@ -3,10 +3,10 @@ """ Helpers to test runtimes. """ + import os import glob import pickle -import packaging.version as pv # noqa import numpy from numpy.testing import assert_array_almost_equal, assert_array_equal import onnx @@ -19,8 +19,6 @@ class ExpectedAssertionError(Exception): Expected failure. """ - pass - class OnnxRuntimeAssertionError(AssertionError): """ @@ -42,8 +40,6 @@ class OnnxRuntimeMissingNewOnnxOperatorException(OnnxRuntimeAssertionError): defined in the latest onnx. """ - pass - def evaluate_condition(backend, condition): """ @@ -51,7 +47,7 @@ def evaluate_condition(backend, condition): ``pv.Version(onnxruntime.__version__) <= pv.Version('0.1.3')`` """ if backend == "onnxruntime": - import onnxruntime # noqa + import onnxruntime # noqa: F401 return eval(condition) raise NotImplementedError( @@ -68,7 +64,7 @@ def is_backend_enabled(backend): """ if backend == "onnxruntime": try: - import onnxruntime # noqa + import onnxruntime # noqa: F401 return True except ImportError: @@ -169,7 +165,7 @@ def search_converted_models(root=None): expected = basename + ".expected.pkl" res = dict(onnx=onnx, data=data, expected=expected) ok = True - for k, v in res.items(): + for _k, v in res.items(): if not os.path.exists(v): ok = False if ok: diff --git a/tests/test_utils/utils_backend_onnx.py b/tests/test_utils/utils_backend_onnx.py index a58771036..914e98592 100644 --- a/tests/test_utils/utils_backend_onnx.py +++ b/tests/test_utils/utils_backend_onnx.py @@ -3,11 +3,11 @@ """ Helpers to test runtimes. """ + import io import contextlib import types import numpy as np -from scipy.special import expit # noqa import pandas import onnx from onnx import AttributeProto, numpy_helper @@ -303,7 +303,6 @@ def _extract_attribute_value(self, att, ref_att=None): ) else: - from onnx.reference.ops.op_scan import Scan as _Scan class Scan(_Scan): @@ -483,7 +482,6 @@ def replay_run(self, verbose=10): return "\n".join(classes) else: - ReferenceEvaluatorEx = None @@ -606,10 +604,7 @@ def compare_runtime( onx = test["onnx"] if options is None: - if isinstance(onx, str): - options = extract_options(onx) - else: - options = {} + options = extract_options(onx) if isinstance(onx, str) else {} elif not isinstance(options, dict): raise TypeError("options must be a dictionary.") @@ -630,9 +625,7 @@ def compare_runtime( smodel = "\nJSON ONNX\n" + str(model) else: smodel = "" - if "NOT_IMPLEMENTED : Could not find an implementation " "for the node" in str( - e - ): + if "NOT_IMPLEMENTED : Could not find an implementation for the node" in str(e): # onnxruntime does not implement a specific node yet. raise OnnxRuntimeMissingNewOnnxOperatorException( "ReferenceEvaluator does not implement a new operator " @@ -646,7 +639,7 @@ def compare_runtime( ) ) raise OnnxRuntimeAssertionError( - "Unable to load onnx '{0}'\nONNX\n{1}\n{2}" ".".format(onx, smodel, e) + "Unable to load onnx '{0}'\nONNX\n{1}\n{2}.".format(onx, smodel, e) ) input = load["data"] @@ -692,7 +685,7 @@ def compare_runtime( if shape == array_input.shape[1]: inputs = {} c = 0 - for i, n in enumerate(inp): + for _i, n in enumerate(inp): d = c + n.shape[1] inputs[n.name] = _create_column( [row[c:d] for row in input], n.type @@ -721,7 +714,7 @@ def compare_runtime( if shape == array_input.shape[1]: inputs = {} c = 0 - for i, n in enumerate(inp): + for _i, n in enumerate(inp): d = c + n.shape[1] inputs[n.name] = _create_column(input.iloc[:, c:d], n.type) c = d @@ -764,7 +757,7 @@ def compare_runtime( try: one = sess.run(None, {name: input}) if lambda_onnx is None: - lambda_onnx = lambda sess=sess, input=input: sess.run( # noqa + lambda_onnx = lambda sess=sess, input=input: sess.run( None, {name: input} ) if verbose: @@ -808,9 +801,7 @@ def to_array(vv): try: one = sess.run(None, iii) if lambda_onnx is None: - lambda_onnx = lambda sess=sess, iii=iii: sess.run( # noqa - None, iii - ) + lambda_onnx = lambda sess=sess, iii=iii: sess.run(None, iii) if verbose: import pprint @@ -863,7 +854,7 @@ def to_array(vv): output = sess.run(None, inputs) def lambda_onnx(): - return sess.run(None, inputs) # noqa + return sess.run(None, inputs) if verbose: import pprint @@ -876,7 +867,7 @@ def lambda_onnx(): _display_intermediate_steps(onx, inputs, disable_optimisation) if "-Fail" in onx: raise ExpectedAssertionError( - "onnxruntime cannot compute the " "prediction for '{0}'".format(onx) + "onnxruntime cannot compute the prediction for '{0}'".format(onx) ) else: if verbose: diff --git a/tests/test_utils/utils_backend_onnxruntime.py b/tests/test_utils/utils_backend_onnxruntime.py index 38150a6f3..5d7b0945c 100644 --- a/tests/test_utils/utils_backend_onnxruntime.py +++ b/tests/test_utils/utils_backend_onnxruntime.py @@ -3,6 +3,7 @@ """ Helpers to test runtimes. """ + import numpy import pandas import warnings @@ -115,10 +116,7 @@ def compare_runtime( onx = test["onnx"] if options is None: - if isinstance(onx, str): - options = extract_options(onx) - else: - options = {} + options = extract_options(onx) if isinstance(onx, str) else {} elif options is None: options = {} elif not isinstance(options, dict): @@ -230,7 +228,7 @@ def compare_runtime( if shape == array_input.shape[1]: inputs = {} c = 0 - for i, n in enumerate(inp): + for _i, n in enumerate(inp): d = c + n.shape[1] inputs[n.name] = _create_column( [row[c:d] for row in input], n.type @@ -255,7 +253,7 @@ def compare_runtime( if shape == array_input.shape[1]: inputs = {} c = 0 - for i, n in enumerate(inp): + for _i, n in enumerate(inp): d = c + n.shape[1] inputs[n.name] = _create_column(input.iloc[:, c:d], n.type) c = d @@ -296,8 +294,8 @@ def compare_runtime( one = sess.run(None, {name: input}) if lambda_onnx is None: - def lambda_onnx(): - return sess.run(None, {name: input}) # noqa + def lambda_onnx(input=input): + return sess.run(None, {name: input}) if verbose: import pprint @@ -329,14 +327,14 @@ def to_array(vv): t = list(inputs.items())[0] res = [] - for i in range(0, len(t[1])): + for i in range(len(t[1])): iii = {k: to_array(v[i]) for k, v in inputs.items()} try: one = sess.run(None, iii) if lambda_onnx is None: - def lambda_onnx(): - return sess.run(None, iii) # noqa + def lambda_onnx(iii=iii): + return sess.run(None, iii) if verbose: import pprint @@ -394,7 +392,7 @@ def lambda_onnx(): output = sess.run(None, inputs, run_options) def lambda_onnx(): - return sess.run(None, inputs) # noqa + return sess.run(None, inputs) if verbose: import pprint @@ -407,7 +405,7 @@ def lambda_onnx(): _display_intermediate_steps(onx, inputs, disable_optimisation) if "-Fail" in onx: raise ExpectedAssertionError( - "onnxruntime cannot compute the " "prediction for '{0}'".format(onx) + "onnxruntime cannot compute the prediction for '{0}'".format(onx) ) else: if verbose: diff --git a/tests/test_utils_sklearn.py b/tests/test_utils_sklearn.py index 4802c15d5..f29dda7a0 100644 --- a/tests/test_utils_sklearn.py +++ b/tests/test_utils_sklearn.py @@ -3,6 +3,7 @@ """ Tests on functions in *onnx_helper*. """ + import unittest import packaging.version as pv import numpy diff --git a/tests/test_variable_names.py b/tests/test_variable_names.py index ab4630be8..113f38a9e 100644 --- a/tests/test_variable_names.py +++ b/tests/test_variable_names.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: Apache-2.0 -# coding: utf-8 import unittest import copy diff --git a/tests_onnxmltools/test_columns.py b/tests_onnxmltools/test_columns.py index 6c7f28815..89df1c4fc 100644 --- a/tests_onnxmltools/test_columns.py +++ b/tests_onnxmltools/test_columns.py @@ -12,14 +12,10 @@ from skl2onnx import update_registered_converter, convert_sklearn from skl2onnx.common.shape_calculator import ( calculate_linear_classifier_output_shapes, -) # noqa +) from skl2onnx._parse import _parse_sklearn_classifier -from onnxmltools.convert.lightgbm.operator_converters.LightGbm import ( - convert_lightgbm, -) # noqa -from onnxmltools.convert.xgboost.operator_converters.XGBoost import ( - convert_xgboost, -) # noqa +from onnxmltools.convert.lightgbm.operator_converters.LightGbm import convert_lightgbm +from onnxmltools.convert.xgboost.operator_converters.XGBoost import convert_xgboost try: from test_utils import fit_classification_model diff --git a/tests_onnxmltools/test_lightgbm.py b/tests_onnxmltools/test_lightgbm.py index dc9e7a94e..35b1f3321 100644 --- a/tests_onnxmltools/test_lightgbm.py +++ b/tests_onnxmltools/test_lightgbm.py @@ -16,14 +16,14 @@ from skl2onnx import update_registered_converter from skl2onnx.common.data_types import FloatTensorType from skl2onnx.common.shape_calculator import ( - calculate_linear_classifier_output_shapes, # noqa + calculate_linear_classifier_output_shapes, calculate_linear_regressor_output_shapes, ) from onnxmltools.convert.lightgbm.operator_converters.LightGbm import ( - convert_lightgbm, # noqa + convert_lightgbm, ) import onnxmltools -from onnxmltools.convert.lightgbm._parse import WrappedBooster # noqa +from onnxmltools.convert.lightgbm._parse import WrappedBooster from skl2onnx import to_onnx from skl2onnx._parse import _parse_sklearn_classifier, _parse_sklearn_simple_model @@ -66,7 +66,7 @@ def calculate_lightgbm_output_shapes(operator): def lightgbm_parser(scope, model, inputs, custom_parsers=None): if hasattr(model, "fit"): raise TypeError( # pragma: no cover - "This converter does not apply on type '{}'." "".format(type(model)) + "This converter does not apply on type '{}'.".format(type(model)) ) if len(inputs) == 1: @@ -116,7 +116,6 @@ def __init__(self, wrapped): # pylint: disable=W0231 "booster_", "n_features_", "objective_", - "boosting_type", "n_features_in_", "n_features_out_", }: diff --git a/tests_onnxmltools/test_xgboost_converters.py b/tests_onnxmltools/test_xgboost_converters.py index c7d6d3d28..01212ca51 100644 --- a/tests_onnxmltools/test_xgboost_converters.py +++ b/tests_onnxmltools/test_xgboost_converters.py @@ -21,15 +21,13 @@ from skl2onnx import update_registered_converter, convert_sklearn from skl2onnx.common.data_types import FloatTensorType from skl2onnx.common.shape_calculator import ( - calculate_linear_classifier_output_shapes, # noqa + calculate_linear_classifier_output_shapes, calculate_linear_regressor_output_shapes, ) from skl2onnx._parse import _parse_sklearn_classifier from xgboost import XGBRegressor, XGBClassifier import onnxmltools -from onnxmltools.convert.xgboost.operator_converters.XGBoost import ( - convert_xgboost, # noqa -) +from onnxmltools.convert.xgboost.operator_converters.XGBoost import convert_xgboost try: from test_utils import dump_single_regression