Skip to content

TST replace assert_warns* by pytest.warns in ensemble/tests #19425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 28 additions & 21 deletions sklearn/ensemble/tests/test_bagging.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
from sklearn.utils._testing import assert_array_equal
from sklearn.utils._testing import assert_array_almost_equal
from sklearn.utils._testing import assert_raises
from sklearn.utils._testing import assert_warns
from sklearn.utils._testing import assert_warns_message
from sklearn.utils._testing import assert_raise_message

from sklearn.dummy import DummyClassifier, DummyRegressor
Expand Down Expand Up @@ -348,14 +346,19 @@ def test_oob_score_classification():
assert abs(test_score - clf.oob_score_) < 0.1

# Test with few estimators
assert_warns(UserWarning,
BaggingClassifier(base_estimator=base_estimator,
n_estimators=1,
bootstrap=True,
oob_score=True,
random_state=rng).fit,
X_train,
y_train)
warn_msg = (
"Some inputs do not have OOB scores. This probably means too few "
"estimators were used to compute any reliable oob estimates."
)
with pytest.warns(UserWarning, match=warn_msg):
clf = BaggingClassifier(
base_estimator=base_estimator,
n_estimators=1,
bootstrap=True,
oob_score=True,
random_state=rng,
)
clf.fit(X_train, y_train)


def test_oob_score_regression():
Expand All @@ -377,14 +380,18 @@ def test_oob_score_regression():
assert abs(test_score - clf.oob_score_) < 0.1

# Test with few estimators
assert_warns(UserWarning,
BaggingRegressor(base_estimator=DecisionTreeRegressor(),
n_estimators=1,
bootstrap=True,
oob_score=True,
random_state=rng).fit,
X_train,
y_train)
warn_msg = (
"Some inputs do not have OOB scores. This probably means too few "
"estimators were used to compute any reliable oob estimates."
)
with pytest.warns(UserWarning, match=warn_msg):
regr = BaggingRegressor(
base_estimator=DecisionTreeRegressor(),
n_estimators=1,
bootstrap=True,
oob_score=True,
random_state=rng)
regr.fit(X_train, y_train)


def test_single_estimator():
Expand Down Expand Up @@ -654,9 +661,9 @@ def test_warm_start_equal_n_estimators():
# modify X to nonsense values, this should not change anything
X_train += 1.

assert_warns_message(UserWarning,
"Warm-start fitting without increasing n_estimators does not",
clf.fit, X_train, y_train)
warn_msg = "Warm-start fitting without increasing n_estimators does not"
with pytest.warns(UserWarning, match=warn_msg):
clf.fit(X_train, y_train)
assert_array_equal(y_pred, clf.predict(X_test))


Expand Down
24 changes: 16 additions & 8 deletions sklearn/ensemble/tests/test_forest.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
from sklearn.utils._testing import assert_array_almost_equal
from sklearn.utils._testing import assert_array_equal
from sklearn.utils._testing import assert_raises
from sklearn.utils._testing import assert_warns
from sklearn.utils._testing import assert_warns_message
from sklearn.utils._testing import _convert_container
from sklearn.utils._testing import ignore_warnings
from sklearn.utils._testing import skip_if_no_parallel
Expand Down Expand Up @@ -1128,8 +1126,14 @@ def check_class_weight_errors(name):
# Warning warm_start with preset
clf = ForestClassifier(class_weight='balanced', warm_start=True,
random_state=0)
assert_warns(UserWarning, clf.fit, X, y)
assert_warns(UserWarning, clf.fit, X, _y)
clf.fit(X, y)

warn_msg = (
"Warm-start fitting without increasing n_estimators does not fit new "
"trees."
)
with pytest.warns(UserWarning, match=warn_msg):
clf.fit(X, _y)

# Not a list or preset for multi-output
clf = ForestClassifier(class_weight=1, random_state=0)
Expand Down Expand Up @@ -1229,7 +1233,12 @@ def check_warm_start_equal_n_estimators(name):
# Now est_2 equals est.

est_2.set_params(random_state=2)
assert_warns(UserWarning, est_2.fit, X, y)
warn_msg = (
"Warm-start fitting without increasing n_estimators does not fit "
"new trees."
)
with pytest.warns(UserWarning, match=warn_msg):
est_2.fit(X, y)
# If we had fit the trees again we would have got a different forest as we
# changed the random state.
assert_array_equal(est.apply(X), est_2.apply(X))
Expand Down Expand Up @@ -1324,9 +1333,8 @@ def test_min_impurity_split():

for Estimator in all_estimators:
est = Estimator(min_impurity_split=0.1)
est = assert_warns_message(FutureWarning,
"min_impurity_decrease",
est.fit, X, y)
with pytest.warns(FutureWarning, match="min_impurity_decrease"):
est = est.fit(X, y)
for tree in est.estimators_:
assert tree.min_impurity_split == 0.1

Expand Down
15 changes: 9 additions & 6 deletions sklearn/ensemble/tests/test_gradient_boosting.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
from sklearn.utils._testing import assert_array_equal
from sklearn.utils._testing import assert_raises
from sklearn.utils._testing import assert_raise_message
from sklearn.utils._testing import assert_warns
from sklearn.utils._testing import assert_warns_message
from sklearn.utils._testing import skip_if_32bit
from sklearn.exceptions import DataConversionWarning
from sklearn.exceptions import NotFittedError
Expand Down Expand Up @@ -560,7 +558,13 @@ def test_shape_y():
# This will raise a DataConversionWarning that we want to
# "always" raise, elsewhere the warnings gets ignored in the
# later tests, and the tests that check for this warning fail
assert_warns(DataConversionWarning, clf.fit, X, y_)
warn_msg = (
"A column-vector y was passed when a 1d array was expected. "
"Please change the shape of y to \\(n_samples, \\), for "
"example using ravel()."
)
with pytest.warns(DataConversionWarning, match=warn_msg):
clf.fit(X, y_)
assert_array_equal(clf.predict(T), true_result)
assert 100 == len(clf.estimators_)

Expand Down Expand Up @@ -1000,9 +1004,8 @@ def test_min_impurity_split(GBEstimator):
X, y = datasets.make_hastie_10_2(n_samples=100, random_state=1)

est = GBEstimator(min_impurity_split=0.1)
est = assert_warns_message(FutureWarning,
"min_impurity_decrease",
est.fit, X, y)
with pytest.warns(FutureWarning, match="min_impurity_decrease"):
est = est.fit(X, y)
for tree in est.estimators_.flat:
assert tree.min_impurity_split == 0.1

Expand Down
13 changes: 6 additions & 7 deletions sklearn/ensemble/tests/test_iforest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from sklearn.utils._testing import assert_array_equal
from sklearn.utils._testing import assert_array_almost_equal
from sklearn.utils._testing import assert_raises
from sklearn.utils._testing import assert_warns_message
from sklearn.utils._testing import ignore_warnings
from sklearn.utils._testing import assert_allclose

Expand Down Expand Up @@ -101,9 +100,9 @@ def test_iforest_error():
# The dataset has less than 256 samples, explicitly setting
# max_samples > n_samples should result in a warning. If not set
# explicitly there should be no warning
assert_warns_message(UserWarning,
"max_samples will be set to n_samples for estimation",
IsolationForest(max_samples=1000).fit, X)
warn_msg = "max_samples will be set to n_samples for estimation"
with pytest.warns(UserWarning, match=warn_msg):
IsolationForest(max_samples=1000).fit(X)
# note that assert_no_warnings does not apply since it enables a
# PendingDeprecationWarning triggered by scipy.sparse's use of
# np.matrix. See issue #11251.
Expand Down Expand Up @@ -139,9 +138,9 @@ def test_max_samples_attribute():
assert clf.max_samples_ == X.shape[0]

clf = IsolationForest(max_samples=500)
assert_warns_message(UserWarning,
"max_samples will be set to n_samples for estimation",
clf.fit, X)
warn_msg = "max_samples will be set to n_samples for estimation"
with pytest.warns(UserWarning, match=warn_msg):
clf.fit(X)
assert clf.max_samples_ == X.shape[0]

clf = IsolationForest(max_samples=0.4).fit(X)
Expand Down