From 6c3f434cda17913028ef6260ec173ff1ea41d2fd Mon Sep 17 00:00:00 2001 From: "Thomas J. Fan" Date: Fri, 15 Apr 2022 10:03:29 -0400 Subject: [PATCH 1/3] MNT Replace pytest.warns(None) in test_neighbors --- sklearn/neighbors/tests/test_neighbors.py | 45 ++++++++++++++++------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/sklearn/neighbors/tests/test_neighbors.py b/sklearn/neighbors/tests/test_neighbors.py index a5d95dde70cb1..d7c5dbda86649 100644 --- a/sklearn/neighbors/tests/test_neighbors.py +++ b/sklearn/neighbors/tests/test_neighbors.py @@ -1,4 +1,5 @@ from itertools import product +import warnings import pytest import re @@ -1529,16 +1530,17 @@ def test_neighbors_metrics( neigh.fit(X_train) # wminkoski is deprecated in SciPy 1.6.0 and removed in 1.8.0 - ExceptionToAssert = None if ( metric == "wminkowski" and algorithm == "brute" and sp_version >= parse_version("1.6.0") ): - ExceptionToAssert = FutureWarning - - with pytest.warns(ExceptionToAssert): - results[algorithm] = neigh.kneighbors(X_test, return_distance=True) + with pytest.warns(FutureWarning): + results[algorithm] = neigh.kneighbors(X_test, return_distance=True) + else: + with warnings.catch_warnings(): + warnings.simplefilter("error", FutureWarning) + results[algorithm] = neigh.kneighbors(X_test, return_distance=True) brute_dst, brute_idx = results["brute"] ball_tree_dst, ball_tree_idx = results["ball_tree"] @@ -1597,7 +1599,8 @@ def test_kneighbors_brute_backend( ) neigh.fit(X_train) - with pytest.warns(ExceptionToAssert): + + def assert_legacy_backend_same_as_cython_backend(): with config_context(enable_cython_pairwise_dist=False): # Use the legacy backend for brute legacy_brute_dst, legacy_brute_idx = neigh.kneighbors( @@ -1608,9 +1611,17 @@ def test_kneighbors_brute_backend( pdr_brute_dst, pdr_brute_idx = neigh.kneighbors( X_test, return_distance=True ) - - assert_allclose(legacy_brute_dst, pdr_brute_dst) - assert_array_equal(legacy_brute_idx, pdr_brute_idx) + assert_allclose(legacy_brute_dst, pdr_brute_dst) + assert_array_equal(legacy_brute_idx, pdr_brute_idx) + + if ExceptionToAssert is None: + with warnings.catch_warnings(): + warnings.simplefilter("error", FutureWarning) + warnings.simplefilter("error", DeprecationWarning) + assert_legacy_backend_same_as_cython_backend() + else: + with pytest.warns(ExceptionToAssert): + assert_legacy_backend_same_as_cython_backend() def test_callable_metric(): @@ -2119,7 +2130,8 @@ def test_radius_neighbors_brute_backend( ) neigh.fit(X_train) - with pytest.warns(ExceptionToAssert): + + def assert_legacy_backend_same_as_cython_backend(): with config_context(enable_cython_pairwise_dist=False): # Use the legacy backend for brute legacy_brute_dst, legacy_brute_idx = neigh.radius_neighbors( @@ -2130,10 +2142,17 @@ def test_radius_neighbors_brute_backend( pdr_brute_dst, pdr_brute_idx = neigh.radius_neighbors( X_test, return_distance=True ) + assert_radius_neighborhood_results_equality( + legacy_brute_dst, pdr_brute_dst, legacy_brute_idx, pdr_brute_idx + ) - assert_radius_neighborhood_results_equality( - legacy_brute_dst, pdr_brute_dst, legacy_brute_idx, pdr_brute_idx - ) + if ExceptionToAssert is None: + with warnings.catch_warnings(): + warnings.simplefilter("error", FutureWarning) + assert_legacy_backend_same_as_cython_backend() + else: + with pytest.warns(ExceptionToAssert): + assert_legacy_backend_same_as_cython_backend() def test_valid_metrics_has_no_duplicate(): From dc2cae8b89f01e717dd9f4bbdb02a98385053afd Mon Sep 17 00:00:00 2001 From: "Thomas J. Fan" Date: Fri, 15 Apr 2022 13:56:56 -0400 Subject: [PATCH 2/3] FIX Fixes CI issue --- sklearn/neighbors/tests/test_neighbors.py | 39 ++++++----------------- 1 file changed, 10 insertions(+), 29 deletions(-) diff --git a/sklearn/neighbors/tests/test_neighbors.py b/sklearn/neighbors/tests/test_neighbors.py index d7c5dbda86649..333146b58bbd2 100644 --- a/sklearn/neighbors/tests/test_neighbors.py +++ b/sklearn/neighbors/tests/test_neighbors.py @@ -1,5 +1,5 @@ from itertools import product -import warnings +from contextlib import nullcontext import pytest import re @@ -1538,9 +1538,7 @@ def test_neighbors_metrics( with pytest.warns(FutureWarning): results[algorithm] = neigh.kneighbors(X_test, return_distance=True) else: - with warnings.catch_warnings(): - warnings.simplefilter("error", FutureWarning) - results[algorithm] = neigh.kneighbors(X_test, return_distance=True) + results[algorithm] = neigh.kneighbors(X_test, return_distance=True) brute_dst, brute_idx = results["brute"] ball_tree_dst, ball_tree_idx = results["ball_tree"] @@ -1558,7 +1556,7 @@ def test_neighbors_metrics( # TODO: Remove filterwarnings in 1.3 when wminkowski is removed -@pytest.mark.filterwarnings("ignore:WMinkowskiDistance:FutureWarning:sklearn") +@pytest.mark.filterwarnings("ignore:WMinkowskiDistance:FutureWarning:sklearn*") @pytest.mark.parametrize( "metric", sorted(set(neighbors.VALID_METRICS["brute"]) - set(["precomputed"])) ) @@ -1578,14 +1576,14 @@ def test_kneighbors_brute_backend( metric_params_list = _generate_test_params_for(metric, n_features) # wminkoski is deprecated in SciPy 1.6.0 and removed in 1.8.0 - ExceptionToAssert = None + warn_context_manager = nullcontext() if metric == "wminkowski" and sp_version >= parse_version("1.6.0"): if global_dtype == np.float64: # Warning from sklearn.metrics._dist_metrics.WMinkowskiDistance - ExceptionToAssert = FutureWarning + warn_context_manager = pytest.warns(FutureWarning) if global_dtype == np.float32: # Warning from Scipy - ExceptionToAssert = DeprecationWarning + warn_context_manager = pytest.warns(DeprecationWarning) for metric_params in metric_params_list: p = metric_params.pop("p", 2) @@ -1600,7 +1598,7 @@ def test_kneighbors_brute_backend( neigh.fit(X_train) - def assert_legacy_backend_same_as_cython_backend(): + with warn_context_manager: with config_context(enable_cython_pairwise_dist=False): # Use the legacy backend for brute legacy_brute_dst, legacy_brute_idx = neigh.kneighbors( @@ -1614,15 +1612,6 @@ def assert_legacy_backend_same_as_cython_backend(): assert_allclose(legacy_brute_dst, pdr_brute_dst) assert_array_equal(legacy_brute_idx, pdr_brute_idx) - if ExceptionToAssert is None: - with warnings.catch_warnings(): - warnings.simplefilter("error", FutureWarning) - warnings.simplefilter("error", DeprecationWarning) - assert_legacy_backend_same_as_cython_backend() - else: - with pytest.warns(ExceptionToAssert): - assert_legacy_backend_same_as_cython_backend() - def test_callable_metric(): def custom_metric(x1, x2): @@ -2114,9 +2103,9 @@ def test_radius_neighbors_brute_backend( metric_params_list = _generate_test_params_for(metric, n_features) # wminkoski is deprecated in SciPy 1.6.0 and removed in 1.8.0 - ExceptionToAssert = None + warn_context_manager = nullcontext() if metric == "wminkowski" and sp_version >= parse_version("1.6.0"): - ExceptionToAssert = FutureWarning + warn_context_manager = pytest.warns(FutureWarning) for metric_params in metric_params_list: p = metric_params.pop("p", 2) @@ -2131,7 +2120,7 @@ def test_radius_neighbors_brute_backend( neigh.fit(X_train) - def assert_legacy_backend_same_as_cython_backend(): + with warn_context_manager: with config_context(enable_cython_pairwise_dist=False): # Use the legacy backend for brute legacy_brute_dst, legacy_brute_idx = neigh.radius_neighbors( @@ -2146,14 +2135,6 @@ def assert_legacy_backend_same_as_cython_backend(): legacy_brute_dst, pdr_brute_dst, legacy_brute_idx, pdr_brute_idx ) - if ExceptionToAssert is None: - with warnings.catch_warnings(): - warnings.simplefilter("error", FutureWarning) - assert_legacy_backend_same_as_cython_backend() - else: - with pytest.warns(ExceptionToAssert): - assert_legacy_backend_same_as_cython_backend() - def test_valid_metrics_has_no_duplicate(): for val in neighbors.VALID_METRICS.values(): From 36d8b84509d8cd37db978e81fef075f9fbf8d4dd Mon Sep 17 00:00:00 2001 From: "Thomas J. Fan" Date: Fri, 15 Apr 2022 14:02:03 -0400 Subject: [PATCH 3/3] CLN Reduce diff --- sklearn/neighbors/tests/test_neighbors.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/sklearn/neighbors/tests/test_neighbors.py b/sklearn/neighbors/tests/test_neighbors.py index 333146b58bbd2..951829a3cfca4 100644 --- a/sklearn/neighbors/tests/test_neighbors.py +++ b/sklearn/neighbors/tests/test_neighbors.py @@ -1556,7 +1556,7 @@ def test_neighbors_metrics( # TODO: Remove filterwarnings in 1.3 when wminkowski is removed -@pytest.mark.filterwarnings("ignore:WMinkowskiDistance:FutureWarning:sklearn*") +@pytest.mark.filterwarnings("ignore:WMinkowskiDistance:FutureWarning:sklearn") @pytest.mark.parametrize( "metric", sorted(set(neighbors.VALID_METRICS["brute"]) - set(["precomputed"])) ) @@ -1597,7 +1597,6 @@ def test_kneighbors_brute_backend( ) neigh.fit(X_train) - with warn_context_manager: with config_context(enable_cython_pairwise_dist=False): # Use the legacy backend for brute @@ -1609,8 +1608,9 @@ def test_kneighbors_brute_backend( pdr_brute_dst, pdr_brute_idx = neigh.kneighbors( X_test, return_distance=True ) - assert_allclose(legacy_brute_dst, pdr_brute_dst) - assert_array_equal(legacy_brute_idx, pdr_brute_idx) + + assert_allclose(legacy_brute_dst, pdr_brute_dst) + assert_array_equal(legacy_brute_idx, pdr_brute_idx) def test_callable_metric(): @@ -2119,7 +2119,6 @@ def test_radius_neighbors_brute_backend( ) neigh.fit(X_train) - with warn_context_manager: with config_context(enable_cython_pairwise_dist=False): # Use the legacy backend for brute @@ -2131,9 +2130,10 @@ def test_radius_neighbors_brute_backend( pdr_brute_dst, pdr_brute_idx = neigh.radius_neighbors( X_test, return_distance=True ) - assert_radius_neighborhood_results_equality( - legacy_brute_dst, pdr_brute_dst, legacy_brute_idx, pdr_brute_idx - ) + + assert_radius_neighborhood_results_equality( + legacy_brute_dst, pdr_brute_dst, legacy_brute_idx, pdr_brute_idx + ) def test_valid_metrics_has_no_duplicate():