Skip to content

Commit

Permalink
TST Changes assert to pytest style in svm, manifold, linear_model, fe…
Browse files Browse the repository at this point in the history
…ature_extraction, decomposition (scikit-learn#19999)

Changed the assert_raises, assert_raise_message, assert_warns in the following files:

* test_factor_analysis.py
* test_text.py
* test_bayes.py
* test_ransac.py
* test_sag.py
* test_locally_linear.py
* test_bounds.py
* test_sparse.py
* test_svm.py
  • Loading branch information
azihna authored May 22, 2021
1 parent 5081c2f commit aa898de
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 61 deletions.
12 changes: 6 additions & 6 deletions sklearn/decomposition/tests/test_factor_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import numpy as np
import pytest

from sklearn.utils._testing import assert_warns
from sklearn.utils._testing import assert_raises
from sklearn.utils._testing import assert_almost_equal
from sklearn.utils._testing import assert_array_almost_equal
from sklearn.exceptions import ConvergenceWarning
Expand Down Expand Up @@ -69,14 +67,16 @@ def test_factor_analysis():
with pytest.raises(ValueError):
fa.fit(X[:, :2])

f = lambda x, y: np.abs(getattr(x, y)) # sign will not be equal
def f(x, y):
return np.abs(getattr(x, y)) # sign will not be equal
fa1, fa2 = fas
for attr in ['loglike_', 'components_', 'noise_variance_']:
assert_almost_equal(f(fa1, attr), f(fa2, attr))

fa1.max_iter = 1
fa1.verbose = True
assert_warns(ConvergenceWarning, fa1.fit, X)
with pytest.warns(ConvergenceWarning):
fa1.fit(X)

# Test get_covariance and get_precision with n_components == n_features
# with n_components < n_features and with n_components == 0
Expand All @@ -101,8 +101,8 @@ def test_factor_analysis():
assert not np.allclose(results[rot1], results[rot2])
assert np.allclose(projections[rot1], projections[rot2], atol=3)

assert_raises(ValueError,
FactorAnalysis(rotation='not_implemented').fit_transform, X)
with pytest.raises(ValueError):
FactorAnalysis(rotation="not_implemented").fit_transform(X)

# test against R's psych::principal with rotate="varimax"
# (i.e., the values below stem from rotating the components in R)
Expand Down
37 changes: 22 additions & 15 deletions sklearn/feature_extraction/tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from numpy.testing import assert_array_equal
from sklearn.utils import IS_PYPY
from sklearn.utils._testing import (assert_almost_equal,
assert_raise_message,
fails_if_pypy,
assert_allclose_dense_sparse,
skip_if_32bit)
Expand Down Expand Up @@ -1093,7 +1092,8 @@ def func():
hv = HashingVectorizer()
hv.fit_transform(['hello world', np.nan, 'hello hello'])

assert_raise_message(exception, message, func)
with pytest.raises(exception, match=message):
func()


def test_tfidfvectorizer_binary():
Expand Down Expand Up @@ -1127,11 +1127,16 @@ def test_vectorizer_string_object_as_input(Vectorizer):
message = ("Iterable over raw text documents expected, "
"string object received.")
vec = Vectorizer()
assert_raise_message(
ValueError, message, vec.fit_transform, "hello world!")
assert_raise_message(ValueError, message, vec.fit, "hello world!")

with pytest.raises(ValueError, match=message):
vec.fit_transform("hello world!")

with pytest.raises(ValueError, match=message):
vec.fit("hello world!")
vec.fit(["some text", "some other text"])
assert_raise_message(ValueError, message, vec.transform, "hello world!")

with pytest.raises(ValueError, match=message):
vec.transform("hello world!")


@pytest.mark.parametrize("X_dtype", [np.float32, np.float64])
Expand Down Expand Up @@ -1186,20 +1191,22 @@ def test_vectorizers_invalid_ngram_range(vec):
# vectorizers could be initialized with invalid ngram range
# test for raising error message
invalid_range = vec.ngram_range
message = ("Invalid value for ngram_range=%s "
"lower boundary larger than the upper boundary."
% str(invalid_range))
message = re.escape(
f"Invalid value for ngram_range={invalid_range} "
"lower boundary larger than the upper boundary."
)
if isinstance(vec, HashingVectorizer) and IS_PYPY:
pytest.xfail(reason='HashingVectorizer is not supported on PyPy')

assert_raise_message(
ValueError, message, vec.fit, ["good news everyone"])
assert_raise_message(
ValueError, message, vec.fit_transform, ["good news everyone"])
with pytest.raises(ValueError, match=message):
vec.fit(['good news everyone'])

with pytest.raises(ValueError, match=message):
vec.fit_transform(['good news everyone'])

if isinstance(vec, HashingVectorizer):
assert_raise_message(
ValueError, message, vec.transform, ["good news everyone"])
with pytest.raises(ValueError, match=message):
vec.transform(['good news everyone'])


def _check_stop_words_consistency(estimator):
Expand Down
4 changes: 2 additions & 2 deletions sklearn/linear_model/tests/test_bayes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from sklearn.utils._testing import assert_array_almost_equal
from sklearn.utils._testing import assert_almost_equal
from sklearn.utils._testing import assert_array_less
from sklearn.utils._testing import assert_raise_message
from sklearn.utils import check_random_state
from sklearn.linear_model import BayesianRidge, ARDRegression
from sklearn.linear_model import Ridge
Expand All @@ -29,7 +28,8 @@ def test_n_iter():
y = np.array([1, 2, 6, 8, 10])
clf = BayesianRidge(n_iter=0)
msg = "n_iter should be greater than or equal to 1."
assert_raise_message(ValueError, msg, clf.fit, X, y)
with pytest.raises(ValueError, match=msg):
clf.fit(X, y)


def test_bayesian_ridge_scores():
Expand Down
13 changes: 8 additions & 5 deletions sklearn/linear_model/tests/test_ransac.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from numpy.testing import assert_array_equal

from sklearn.utils import check_random_state
from sklearn.utils._testing import assert_raises_regexp
from sklearn.utils._testing import assert_allclose
from sklearn.datasets import make_regression
from sklearn.linear_model import LinearRegression, RANSACRegressor
Expand Down Expand Up @@ -159,7 +158,8 @@ def test_ransac_resid_thresh_no_inliers():
max_trials=5)

msg = ("RANSAC could not find a valid consensus set")
assert_raises_regexp(ValueError, msg, ransac_estimator.fit, X, y)
with pytest.raises(ValueError, match=msg):
ransac_estimator.fit(X, y)
assert ransac_estimator.n_skips_no_inliers_ == 5
assert ransac_estimator.n_skips_invalid_data_ == 0
assert ransac_estimator.n_skips_invalid_model_ == 0
Expand All @@ -175,7 +175,8 @@ def is_data_valid(X, y):
max_trials=5)

msg = ("RANSAC could not find a valid consensus set")
assert_raises_regexp(ValueError, msg, ransac_estimator.fit, X, y)
with pytest.raises(ValueError, match=msg):
ransac_estimator.fit(X, y)
assert ransac_estimator.n_skips_no_inliers_ == 0
assert ransac_estimator.n_skips_invalid_data_ == 5
assert ransac_estimator.n_skips_invalid_model_ == 0
Expand All @@ -191,7 +192,8 @@ def is_model_valid(estimator, X, y):
max_trials=5)

msg = ("RANSAC could not find a valid consensus set")
assert_raises_regexp(ValueError, msg, ransac_estimator.fit, X, y)
with pytest.raises(ValueError, match=msg):
ransac_estimator.fit(X, y)
assert ransac_estimator.n_skips_no_inliers_ == 0
assert ransac_estimator.n_skips_invalid_data_ == 0
assert ransac_estimator.n_skips_invalid_model_ == 5
Expand All @@ -208,7 +210,8 @@ def is_data_valid(X, y):
max_skips=3)

msg = ("RANSAC skipped more iterations than `max_skips`")
assert_raises_regexp(ValueError, msg, ransac_estimator.fit, X, y)
with pytest.raises(ValueError, match=msg):
ransac_estimator.fit(X, y)
assert ransac_estimator.n_skips_no_inliers_ == 0
assert ransac_estimator.n_skips_invalid_data_ == 4
assert ransac_estimator.n_skips_invalid_model_ == 0
Expand Down
26 changes: 14 additions & 12 deletions sklearn/linear_model/tests/test_sag.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# License: BSD 3 clause

import math
import re
import pytest
import numpy as np
import scipy.sparse as sp
Expand All @@ -19,7 +20,6 @@
from sklearn.utils._testing import assert_almost_equal
from sklearn.utils._testing import assert_array_almost_equal
from sklearn.utils._testing import assert_allclose
from sklearn.utils._testing import assert_raise_message
from sklearn.utils import compute_class_weight
from sklearn.utils import check_random_state
from sklearn.preprocessing import LabelEncoder, LabelBinarizer
Expand Down Expand Up @@ -449,8 +449,8 @@ def test_get_auto_step_size():
assert_almost_equal(step_size_log, step_size_log_, decimal=4)

msg = 'Unknown loss function for SAG solver, got wrong instead of'
assert_raise_message(ValueError, msg, get_auto_step_size,
max_squared_sum_, alpha, "wrong", fit_intercept)
with pytest.raises(ValueError, match=msg):
get_auto_step_size(max_squared_sum_, alpha, "wrong", fit_intercept)


@pytest.mark.parametrize("seed", range(3)) # locally tested with 1000 seeds
Expand Down Expand Up @@ -737,27 +737,29 @@ def test_classifier_single_class():
X = [[1, 2], [3, 4]]
y = [1, 1]

assert_raise_message(ValueError,
"This solver needs samples of at least 2 classes "
"in the data",
LogisticRegression(solver='sag').fit,
X, y)
msg = "This solver needs samples of at least 2 classes in the data"
with pytest.raises(ValueError, match=msg):
LogisticRegression(solver='sag').fit(X, y)


def test_step_size_alpha_error():
X = [[0, 0], [0, 0]]
y = [1, -1]
fit_intercept = False
alpha = 1.
msg = ("Current sag implementation does not handle the case"
" step_size * alpha_scaled == 1")
msg = re.escape(
"Current sag implementation does not handle the case"
" step_size * alpha_scaled == 1"
)

clf1 = LogisticRegression(solver='sag', C=1. / alpha,
fit_intercept=fit_intercept)
assert_raise_message(ZeroDivisionError, msg, clf1.fit, X, y)
with pytest.raises(ZeroDivisionError, match=msg):
clf1.fit(X, y)

clf2 = Ridge(fit_intercept=fit_intercept, solver='sag', alpha=alpha)
assert_raise_message(ZeroDivisionError, msg, clf2.fit, X, y)
with pytest.raises(ZeroDivisionError, match=msg):
clf2.fit(X, y)


def test_multinomial_loss():
Expand Down
7 changes: 4 additions & 3 deletions sklearn/manifold/tests/test_locally_linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from sklearn import neighbors, manifold
from sklearn.manifold._locally_linear import barycenter_kneighbors_graph
from sklearn.utils._testing import ignore_warnings
from sklearn.utils._testing import assert_raise_message

eigen_solvers = ['dense', 'arpack']

Expand Down Expand Up @@ -106,11 +105,13 @@ def test_lle_init_parameters():

clf = manifold.LocallyLinearEmbedding(eigen_solver="error")
msg = "unrecognized eigen_solver 'error'"
assert_raise_message(ValueError, msg, clf.fit, X)
with pytest.raises(ValueError, match=msg):
clf.fit(X)

clf = manifold.LocallyLinearEmbedding(method="error")
msg = "unrecognized method 'error'"
assert_raise_message(ValueError, msg, clf.fit, X)
with pytest.raises(ValueError, match=msg):
clf.fit(X)


def test_pipeline():
Expand Down
7 changes: 3 additions & 4 deletions sklearn/svm/tests/test_bounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
from sklearn.linear_model import LogisticRegression
from sklearn.svm._newrand import set_seed_wrap, bounded_rand_int_wrap

from sklearn.utils._testing import assert_raise_message


dense_X = [[-1, 0], [0, 1], [1, 1], [1, 1]]
sparse_X = sp.csr_matrix(dense_X)
Expand Down Expand Up @@ -38,8 +36,9 @@ def test_l1_min_c(loss, X_label, Y_label, intercept_label):

def test_l1_min_c_l2_loss():
# loss='l2' should raise ValueError
assert_raise_message(ValueError, "loss type not in",
l1_min_c, dense_X, Y1, loss="l2")
msg = 'loss type not in'
with pytest.raises(ValueError, match=msg):
l1_min_c(dense_X, Y1, loss="l2")


def check_l1_min_c(X, y, loss, fit_intercept=True, intercept_scaling=None):
Expand Down
8 changes: 4 additions & 4 deletions sklearn/svm/tests/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
from sklearn.svm.tests import test_svm
from sklearn.exceptions import ConvergenceWarning
from sklearn.utils.extmath import safe_sparse_dot
from sklearn.utils._testing import (assert_raise_message, ignore_warnings,
skip_if_32bit)
from sklearn.utils._testing import ignore_warnings, skip_if_32bit


# test sample 1
Expand Down Expand Up @@ -69,7 +68,8 @@ def check_svm_model_equal(dense_svm, sparse_svm, X_train, y_train, X_test):
sparse_svm.predict_proba(X_test), 4)
msg = "cannot use sparse input in 'SVC' trained on dense data"
if sparse.isspmatrix(X_test):
assert_raise_message(ValueError, msg, dense_svm.predict, X_test)
with pytest.raises(ValueError, match=msg):
dense_svm.predict(X_test)


@skip_if_32bit
Expand Down Expand Up @@ -148,7 +148,7 @@ def test_svc_iris():
for k in ('linear', 'poly', 'rbf'):
sp_clf = svm.SVC(kernel=k).fit(iris.data, iris.target)
clf = svm.SVC(kernel=k).fit(iris.data.toarray(),
iris.target)
iris.target)

assert_array_almost_equal(clf.support_vectors_,
sp_clf.support_vectors_.toarray())
Expand Down
27 changes: 17 additions & 10 deletions sklearn/svm/tests/test_svm.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from sklearn.metrics import f1_score
from sklearn.metrics.pairwise import rbf_kernel
from sklearn.utils import check_random_state
from sklearn.utils._testing import assert_raise_message
from sklearn.utils._testing import ignore_warnings
from sklearn.utils.validation import _num_samples
from sklearn.utils import shuffle
Expand Down Expand Up @@ -123,7 +122,8 @@ def test_precomputed():
# same as before, but using a callable function instead of the kernel
# matrix. kernel is just a linear kernel

kfunc = lambda x, y: np.dot(x, y.T)
def kfunc(x, y):
return np.dot(x, y.T)
clf = svm.SVC(kernel=kfunc)
clf.fit(np.array(X), Y)
pred = clf.predict(T)
Expand Down Expand Up @@ -739,13 +739,16 @@ def test_linear_svx_uppercase_loss_penality_raises_error():

X, y = [[0.0], [1.0]], [0, 1]

assert_raise_message(ValueError, "loss='SQuared_hinge' is not supported",
svm.LinearSVC(loss="SQuared_hinge").fit, X, y)
msg = "loss='SQuared_hinge' is not supported"
with pytest.raises(ValueError, match=msg):
svm.LinearSVC(loss="SQuared_hinge").fit(X, y)

assert_raise_message(ValueError,
("The combination of penalty='L2'"
" and loss='squared_hinge' is not supported"),
svm.LinearSVC(penalty="L2").fit, X, y)
msg = (
"The combination of penalty='L2'"
" and loss='squared_hinge' is not supported"
)
with pytest.raises(ValueError, match=msg):
svm.LinearSVC(penalty="L2").fit(X, y)


def test_linearsvc():
Expand Down Expand Up @@ -1043,10 +1046,12 @@ def test_linear_svc_intercept_scaling():

for i in [-1, 0]:
lsvc = svm.LinearSVC(intercept_scaling=i)

msg = ('Intercept scaling is %r but needs to be greater than 0.'
' To disable fitting an intercept,'
' set fit_intercept=False.' % lsvc.intercept_scaling)
assert_raise_message(ValueError, msg, lsvc.fit, X, Y)
with pytest.raises(ValueError, match=msg):
lsvc.fit(X, Y)


def test_lsvc_intercept_scaling_zero():
Expand Down Expand Up @@ -1076,7 +1081,9 @@ def test_hasattr_predict_proba():
G.probability = True
assert hasattr(G, 'predict_proba')
msg = "predict_proba is not available when fitted with probability=False"
assert_raise_message(NotFittedError, msg, G.predict_proba, iris.data)

with pytest.raises(NotFittedError, match=msg):
G.predict_proba(iris.data)


def test_decision_function_shape_two_class():
Expand Down

0 comments on commit aa898de

Please sign in to comment.