From e3a240f257bfd4a2db4c82419f5be8f7cbe4a7e2 Mon Sep 17 00:00:00 2001 From: abdulelahsm Date: Tue, 9 Feb 2021 01:38:36 +0300 Subject: [PATCH 1/8] fix test_least_angle, test_omp, test_test_theil_sen --- .../linear_model/tests/test_least_angle.py | 10 +++---- sklearn/linear_model/tests/test_omp.py | 27 ++++++++++++------- sklearn/linear_model/tests/test_theil_sen.py | 16 ++++++----- 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/sklearn/linear_model/tests/test_least_angle.py b/sklearn/linear_model/tests/test_least_angle.py index 96c5a8fedbf14..e5d04cc15e97b 100644 --- a/sklearn/linear_model/tests/test_least_angle.py +++ b/sklearn/linear_model/tests/test_least_angle.py @@ -8,7 +8,6 @@ from sklearn.model_selection import train_test_split from sklearn.utils._testing import assert_allclose from sklearn.utils._testing import assert_array_almost_equal -from sklearn.utils._testing import assert_raises from sklearn.utils._testing import ignore_warnings from sklearn.utils._testing import assert_warns from sklearn.utils._testing import TempMemmap @@ -97,9 +96,8 @@ def test_lars_path_gram_equivalent(method, return_path): def test_x_none_gram_none_raises_value_error(): # Test that lars_path with no X and Gram raises exception Xy = np.dot(X.T, y) - assert_raises(ValueError, linear_model.lars_path, None, y, Gram=None, - Xy=Xy) - + with pytest.raises(ValueError): + linear_model.lars_path(None, Gram = None, Xy=Xy) def test_all_precomputed(): # Test that lars_path with precomputed Gram and Xy gives the right answer @@ -486,7 +484,9 @@ def test_lasso_lars_ic(): # test error on unknown IC lars_broken = linear_model.LassoLarsIC('') - assert_raises(ValueError, lars_broken.fit, X, y) + + with pytest.error(ValueError): + lars_broken.fit(X, y) def test_lars_path_readonly_data(): diff --git a/sklearn/linear_model/tests/test_omp.py b/sklearn/linear_model/tests/test_omp.py index f3f3080aebe66..bc79ab3b6c083 100644 --- a/sklearn/linear_model/tests/test_omp.py +++ b/sklearn/linear_model/tests/test_omp.py @@ -2,8 +2,6 @@ # License: BSD 3 clause import numpy as np - -from sklearn.utils._testing import assert_raises from sklearn.utils._testing import assert_array_equal from sklearn.utils._testing import assert_array_almost_equal from sklearn.utils._testing import assert_warns @@ -85,14 +83,23 @@ def test_unreachable_accuracy(): def test_bad_input(): - assert_raises(ValueError, orthogonal_mp, X, y, tol=-1) - assert_raises(ValueError, orthogonal_mp, X, y, n_nonzero_coefs=-1) - assert_raises(ValueError, orthogonal_mp, X, y, - n_nonzero_coefs=n_features + 1) - assert_raises(ValueError, orthogonal_mp_gram, G, Xy, tol=-1) - assert_raises(ValueError, orthogonal_mp_gram, G, Xy, n_nonzero_coefs=-1) - assert_raises(ValueError, orthogonal_mp_gram, G, Xy, - n_nonzero_coefs=n_features + 1) + with pytest.error(ValueError): + orthogonal_mp(X, y, tol=-1) + + with pytest.error(ValueError): + orthogonal_mp(X, y, n_nonzero_coefs = -1) + + with pytest.error(ValueError): + orthogonal_mp(X, y, n_nonzero_coefs= n_features + 1) + + with pytest.error(ValueError): + orthogonal_mp(G, Xy, tol=-1) + + with pytest.error(ValueError): + orthogonal_mp(G, Xy, n_nonzero_coefs=-1) + + with pytest.error(ValueError): + orthogonal_mp(G, Xy, n_nonzero_coefs = n_features+1) def test_perfect_signal_recovery(): diff --git a/sklearn/linear_model/tests/test_theil_sen.py b/sklearn/linear_model/tests/test_theil_sen.py index bd17298492ca0..334259dc020d6 100644 --- a/sklearn/linear_model/tests/test_theil_sen.py +++ b/sklearn/linear_model/tests/test_theil_sen.py @@ -16,7 +16,7 @@ from sklearn.linear_model import LinearRegression, TheilSenRegressor from sklearn.linear_model._theil_sen import _spatial_median, _breakdown_point from sklearn.linear_model._theil_sen import _modified_weiszfeld_step -from sklearn.utils._testing import assert_almost_equal, assert_raises +from sklearn.utils._testing import assert_almost_equal @contextmanager @@ -203,19 +203,22 @@ def test_calc_breakdown_point(): def test_checksubparams_negative_subpopulation(): X, y, w, c = gen_toy_problem_1d() theil_sen = TheilSenRegressor(max_subpopulation=-1, random_state=0) - assert_raises(ValueError, theil_sen.fit, X, y) - + + with pytest.error(ValueError): + theil_sen.fit(X, y) def test_checksubparams_too_few_subsamples(): X, y, w, c = gen_toy_problem_1d() theil_sen = TheilSenRegressor(n_subsamples=1, random_state=0) - assert_raises(ValueError, theil_sen.fit, X, y) + with pytest.error(ValueError): + theil_sen.fit(X, y) def test_checksubparams_too_many_subsamples(): X, y, w, c = gen_toy_problem_1d() theil_sen = TheilSenRegressor(n_subsamples=101, random_state=0) - assert_raises(ValueError, theil_sen.fit, X, y) + with pytest.error(ValueError): + theil_sen.fit(X, y) def test_checksubparams_n_subsamples_if_less_samples_than_features(): @@ -224,7 +227,8 @@ def test_checksubparams_n_subsamples_if_less_samples_than_features(): X = random_state.normal(size=(n_samples, n_features)) y = random_state.normal(size=n_samples) theil_sen = TheilSenRegressor(n_subsamples=9, random_state=0) - assert_raises(ValueError, theil_sen.fit, X, y) + with pytest.error(ValueError): + theil_sen.fit(X, y) def test_subpopulation(): From 10eaca6e5fd521d5b24633965445ec81b63efa5d Mon Sep 17 00:00:00 2001 From: abdulelahsm Date: Tue, 9 Feb 2021 16:06:57 +0300 Subject: [PATCH 2/8] pytest import fixes --- sklearn/linear_model/tests/test_least_angle.py | 3 +-- sklearn/linear_model/tests/test_omp.py | 3 ++- sklearn/linear_model/tests/test_theil_sen.py | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/sklearn/linear_model/tests/test_least_angle.py b/sklearn/linear_model/tests/test_least_angle.py index e5d04cc15e97b..b9449f5f88e8d 100644 --- a/sklearn/linear_model/tests/test_least_angle.py +++ b/sklearn/linear_model/tests/test_least_angle.py @@ -1,9 +1,8 @@ import warnings import numpy as np -import pytest from scipy import linalg - +import pytest from sklearn.base import clone from sklearn.model_selection import train_test_split from sklearn.utils._testing import assert_allclose diff --git a/sklearn/linear_model/tests/test_omp.py b/sklearn/linear_model/tests/test_omp.py index bc79ab3b6c083..2d9863612346d 100644 --- a/sklearn/linear_model/tests/test_omp.py +++ b/sklearn/linear_model/tests/test_omp.py @@ -2,6 +2,7 @@ # License: BSD 3 clause import numpy as np +import pytest from sklearn.utils._testing import assert_array_equal from sklearn.utils._testing import assert_array_almost_equal from sklearn.utils._testing import assert_warns @@ -90,7 +91,7 @@ def test_bad_input(): orthogonal_mp(X, y, n_nonzero_coefs = -1) with pytest.error(ValueError): - orthogonal_mp(X, y, n_nonzero_coefs= n_features + 1) + orthogonal_mp(X, y, n_nonzero_coefs= n_features + 1) with pytest.error(ValueError): orthogonal_mp(G, Xy, tol=-1) diff --git a/sklearn/linear_model/tests/test_theil_sen.py b/sklearn/linear_model/tests/test_theil_sen.py index 334259dc020d6..13e3c813c0260 100644 --- a/sklearn/linear_model/tests/test_theil_sen.py +++ b/sklearn/linear_model/tests/test_theil_sen.py @@ -8,6 +8,7 @@ import sys from contextlib import contextmanager import numpy as np +import pytest from numpy.testing import assert_array_equal, assert_array_less from numpy.testing import assert_array_almost_equal, assert_warns from scipy.linalg import norm @@ -203,7 +204,7 @@ def test_calc_breakdown_point(): def test_checksubparams_negative_subpopulation(): X, y, w, c = gen_toy_problem_1d() theil_sen = TheilSenRegressor(max_subpopulation=-1, random_state=0) - + with pytest.error(ValueError): theil_sen.fit(X, y) From b64bb2e58479e7c0a6f40aa3118194972cfe3ed7 Mon Sep 17 00:00:00 2001 From: abdulelahsm Date: Tue, 9 Feb 2021 16:30:42 +0300 Subject: [PATCH 3/8] linting fixes --- sklearn/linear_model/tests/test_least_angle.py | 2 +- sklearn/linear_model/tests/test_omp.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/sklearn/linear_model/tests/test_least_angle.py b/sklearn/linear_model/tests/test_least_angle.py index b9449f5f88e8d..d9bfba195d104 100644 --- a/sklearn/linear_model/tests/test_least_angle.py +++ b/sklearn/linear_model/tests/test_least_angle.py @@ -96,7 +96,7 @@ def test_x_none_gram_none_raises_value_error(): # Test that lars_path with no X and Gram raises exception Xy = np.dot(X.T, y) with pytest.raises(ValueError): - linear_model.lars_path(None, Gram = None, Xy=Xy) + linear_model.lars_path(None, Gram=None, Xy=Xy) def test_all_precomputed(): # Test that lars_path with precomputed Gram and Xy gives the right answer diff --git a/sklearn/linear_model/tests/test_omp.py b/sklearn/linear_model/tests/test_omp.py index 2d9863612346d..06f6e083a10fb 100644 --- a/sklearn/linear_model/tests/test_omp.py +++ b/sklearn/linear_model/tests/test_omp.py @@ -88,10 +88,10 @@ def test_bad_input(): orthogonal_mp(X, y, tol=-1) with pytest.error(ValueError): - orthogonal_mp(X, y, n_nonzero_coefs = -1) + orthogonal_mp(X, y, n_nonzero_coefs=-1) with pytest.error(ValueError): - orthogonal_mp(X, y, n_nonzero_coefs= n_features + 1) + orthogonal_mp(X, y, n_nonzero_coefs=n_features+1) with pytest.error(ValueError): orthogonal_mp(G, Xy, tol=-1) @@ -100,7 +100,7 @@ def test_bad_input(): orthogonal_mp(G, Xy, n_nonzero_coefs=-1) with pytest.error(ValueError): - orthogonal_mp(G, Xy, n_nonzero_coefs = n_features+1) + orthogonal_mp(G, Xy, n_nonzero_coefs=n_features+1) def test_perfect_signal_recovery(): From 68c9aa3563567eae459ea1e12bdce26b1bb155f8 Mon Sep 17 00:00:00 2001 From: abdulelahsm Date: Sat, 13 Mar 2021 21:14:14 +0300 Subject: [PATCH 4/8] pytest raises changes --- .../linear_model/tests/test_least_angle.py | 5 +-- sklearn/linear_model/tests/test_omp.py | 34 +++++++------------ sklearn/linear_model/tests/test_theil_sen.py | 9 ++--- 3 files changed, 20 insertions(+), 28 deletions(-) diff --git a/sklearn/linear_model/tests/test_least_angle.py b/sklearn/linear_model/tests/test_least_angle.py index d9bfba195d104..b29a6b4486a29 100644 --- a/sklearn/linear_model/tests/test_least_angle.py +++ b/sklearn/linear_model/tests/test_least_angle.py @@ -1,8 +1,8 @@ import warnings import numpy as np -from scipy import linalg import pytest +from scipy import linalg from sklearn.base import clone from sklearn.model_selection import train_test_split from sklearn.utils._testing import assert_allclose @@ -98,6 +98,7 @@ def test_x_none_gram_none_raises_value_error(): with pytest.raises(ValueError): linear_model.lars_path(None, Gram=None, Xy=Xy) + def test_all_precomputed(): # Test that lars_path with precomputed Gram and Xy gives the right answer G = np.dot(X.T, X) @@ -484,7 +485,7 @@ def test_lasso_lars_ic(): # test error on unknown IC lars_broken = linear_model.LassoLarsIC('') - with pytest.error(ValueError): + with pytest.raises(ValueError): lars_broken.fit(X, y) diff --git a/sklearn/linear_model/tests/test_omp.py b/sklearn/linear_model/tests/test_omp.py index 06f6e083a10fb..87aaa0b6a92d8 100644 --- a/sklearn/linear_model/tests/test_omp.py +++ b/sklearn/linear_model/tests/test_omp.py @@ -32,16 +32,16 @@ def test_correct_shapes(): assert (orthogonal_mp(X, y[:, 0], n_nonzero_coefs=5).shape == - (n_features,)) + (n_features,)) assert (orthogonal_mp(X, y, n_nonzero_coefs=5).shape == - (n_features, 3)) + (n_features, 3)) def test_correct_shapes_gram(): assert (orthogonal_mp_gram(G, Xy[:, 0], n_nonzero_coefs=5).shape == - (n_features,)) + (n_features,)) assert (orthogonal_mp_gram(G, Xy, n_nonzero_coefs=5).shape == - (n_features, 3)) + (n_features, 3)) def test_n_nonzero_coefs(): @@ -83,24 +83,14 @@ def test_unreachable_accuracy(): n_nonzero_coefs=n_features)) -def test_bad_input(): - with pytest.error(ValueError): - orthogonal_mp(X, y, tol=-1) - - with pytest.error(ValueError): - orthogonal_mp(X, y, n_nonzero_coefs=-1) - - with pytest.error(ValueError): - orthogonal_mp(X, y, n_nonzero_coefs=n_features+1) - - with pytest.error(ValueError): - orthogonal_mp(G, Xy, tol=-1) - - with pytest.error(ValueError): - orthogonal_mp(G, Xy, n_nonzero_coefs=-1) - - with pytest.error(ValueError): - orthogonal_mp(G, Xy, n_nonzero_coefs=n_features+1) +@pytest.mark.parametrize("positional_params", [(X, y), (G, Xy)]) +@pytest.mark.parametrize( + "keyword_params", + [{"tol": -1}, {"n_nonzero_coefs": -1}, {"n_nonzero_coefs": n_features + 1}] +) +def test_bad_input(positional_params, keyword_params): + with pytest.raises(ValueError): + orthogonal_mp(*positional_params, **keyword_params) def test_perfect_signal_recovery(): diff --git a/sklearn/linear_model/tests/test_theil_sen.py b/sklearn/linear_model/tests/test_theil_sen.py index 13e3c813c0260..0a3d065718c28 100644 --- a/sklearn/linear_model/tests/test_theil_sen.py +++ b/sklearn/linear_model/tests/test_theil_sen.py @@ -205,20 +205,21 @@ def test_checksubparams_negative_subpopulation(): X, y, w, c = gen_toy_problem_1d() theil_sen = TheilSenRegressor(max_subpopulation=-1, random_state=0) - with pytest.error(ValueError): + with pytest.raises(ValueError): theil_sen.fit(X, y) + def test_checksubparams_too_few_subsamples(): X, y, w, c = gen_toy_problem_1d() theil_sen = TheilSenRegressor(n_subsamples=1, random_state=0) - with pytest.error(ValueError): + with pytest.raises(ValueError): theil_sen.fit(X, y) def test_checksubparams_too_many_subsamples(): X, y, w, c = gen_toy_problem_1d() theil_sen = TheilSenRegressor(n_subsamples=101, random_state=0) - with pytest.error(ValueError): + with pytest.raises(ValueError): theil_sen.fit(X, y) @@ -228,7 +229,7 @@ def test_checksubparams_n_subsamples_if_less_samples_than_features(): X = random_state.normal(size=(n_samples, n_features)) y = random_state.normal(size=n_samples) theil_sen = TheilSenRegressor(n_subsamples=9, random_state=0) - with pytest.error(ValueError): + with pytest.raises(ValueError): theil_sen.fit(X, y) From b2a4ef68599d21698cf3f68409dcea5baef46b9a Mon Sep 17 00:00:00 2001 From: abdulelahsm Date: Sat, 13 Mar 2021 21:44:49 +0300 Subject: [PATCH 5/8] resolving conflicts --- sklearn/linear_model/tests/test_omp.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sklearn/linear_model/tests/test_omp.py b/sklearn/linear_model/tests/test_omp.py index 87aaa0b6a92d8..50b3a85976652 100644 --- a/sklearn/linear_model/tests/test_omp.py +++ b/sklearn/linear_model/tests/test_omp.py @@ -83,6 +83,7 @@ def test_unreachable_accuracy(): n_nonzero_coefs=n_features)) +# Parametrizing positional params and keyword params to enhance the test @pytest.mark.parametrize("positional_params", [(X, y), (G, Xy)]) @pytest.mark.parametrize( "keyword_params", From 957ae4b9f6eef1cd59a1b78afafddf5caac9de3c Mon Sep 17 00:00:00 2001 From: abdulelahsm Date: Sun, 14 Mar 2021 00:29:00 +0300 Subject: [PATCH 6/8] fix merge conflict --- sklearn/linear_model/tests/test_omp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sklearn/linear_model/tests/test_omp.py b/sklearn/linear_model/tests/test_omp.py index 50b3a85976652..e9d24bfa3b11c 100644 --- a/sklearn/linear_model/tests/test_omp.py +++ b/sklearn/linear_model/tests/test_omp.py @@ -83,7 +83,7 @@ def test_unreachable_accuracy(): n_nonzero_coefs=n_features)) -# Parametrizing positional params and keyword params to enhance the test +# Parametrizing the decorator to enhance the test @pytest.mark.parametrize("positional_params", [(X, y), (G, Xy)]) @pytest.mark.parametrize( "keyword_params", From d90362532b21d697af858945cc95c8e43b0c74f6 Mon Sep 17 00:00:00 2001 From: "Abdulelah S. Al Mesfer" <28743265+abdulelahsm@users.noreply.github.com> Date: Wed, 31 Mar 2021 09:50:34 +0300 Subject: [PATCH 7/8] Update sklearn/linear_model/tests/test_omp.py Co-authored-by: Olivier Grisel --- sklearn/linear_model/tests/test_omp.py | 1 - 1 file changed, 1 deletion(-) diff --git a/sklearn/linear_model/tests/test_omp.py b/sklearn/linear_model/tests/test_omp.py index 852adaf378531..1d2eb6a239786 100644 --- a/sklearn/linear_model/tests/test_omp.py +++ b/sklearn/linear_model/tests/test_omp.py @@ -87,7 +87,6 @@ def test_unreachable_accuracy(): n_nonzero_coefs=n_features)) -# Parametrizing the decorator to enhance the test @pytest.mark.parametrize("positional_params", [(X, y), (G, Xy)]) @pytest.mark.parametrize( "keyword_params", From 08190c2e7df6558c6d33434f886cdb6efe0f6f32 Mon Sep 17 00:00:00 2001 From: Guillaume Lemaitre Date: Fri, 9 Apr 2021 11:31:59 +0200 Subject: [PATCH 8/8] Update sklearn/linear_model/tests/test_least_angle.py Co-authored-by: Chiara Marmo --- sklearn/linear_model/tests/test_least_angle.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sklearn/linear_model/tests/test_least_angle.py b/sklearn/linear_model/tests/test_least_angle.py index 808162e0212ef..4321c39b45e92 100644 --- a/sklearn/linear_model/tests/test_least_angle.py +++ b/sklearn/linear_model/tests/test_least_angle.py @@ -95,7 +95,7 @@ def test_x_none_gram_none_raises_value_error(): # Test that lars_path with no X and Gram raises exception Xy = np.dot(X.T, y) with pytest.raises(ValueError): - linear_model.lars_path(None, Gram=None, Xy=Xy) + linear_model.lars_path(None, y, Gram=None, Xy=Xy) def test_all_precomputed():