Use pytest conventions consistently (#6337)
* Do not derive from unittest.TestCase (not needed for pytest) * assertRaises -> pytest.raises * Simplify test_empty_dmatrix with test parametrization * setUpClass -> setup_class, tearDownClass -> teardown_class * Don't import unittest; import pytest * Use plain assert * Use parametrized tests in more places * Fix test_gpu_with_sklearn.py * Put back run_empty_dmatrix_reg / run_empty_dmatrix_cls * Fix test_eta_decay_gpu_hist * Add parametrized tests for monotone constraints * Fix test names * Remove test parametrization * Revise test_slice to be not flaky
This commit is contained in:
committed by
GitHub
parent
c763b50dd0
commit
9c9070aea2
@@ -1,5 +1,4 @@
|
||||
import xgboost as xgb
|
||||
import unittest
|
||||
import pytest
|
||||
import os
|
||||
import testing as tm
|
||||
@@ -9,9 +8,9 @@ import tempfile
|
||||
pytestmark = pytest.mark.skipif(**tm.no_sklearn())
|
||||
|
||||
|
||||
class TestCallbacks(unittest.TestCase):
|
||||
class TestCallbacks:
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
def setup_class(cls):
|
||||
from sklearn.datasets import load_breast_cancer
|
||||
X, y = load_breast_cancer(return_X_y=True)
|
||||
cls.X = X
|
||||
@@ -148,9 +147,9 @@ class TestCallbacks(unittest.TestCase):
|
||||
early_stop = xgb.callback.EarlyStopping(rounds=early_stopping_rounds,
|
||||
save_best=True)
|
||||
cls = xgb.XGBClassifier(booster='gblinear', n_estimators=10)
|
||||
self.assertRaises(ValueError, lambda: cls.fit(X, y, eval_set=[(X, y)],
|
||||
eval_metric=tm.eval_error_metric,
|
||||
callbacks=[early_stop]))
|
||||
with pytest.raises(ValueError):
|
||||
cls.fit(X, y, eval_set=[(X, y)], eval_metric=tm.eval_error_metric,
|
||||
callbacks=[early_stop])
|
||||
|
||||
# No error
|
||||
early_stop = xgb.callback.EarlyStopping(rounds=early_stopping_rounds,
|
||||
@@ -172,17 +171,20 @@ class TestCallbacks(unittest.TestCase):
|
||||
watchlist = [(dtest, 'eval'), (dtrain, 'train')]
|
||||
num_round = 4
|
||||
|
||||
warning_check = pytest.warns(UserWarning) if deprecated_callback else tm.noop_context()
|
||||
|
||||
# learning_rates as a list
|
||||
# init eta with 0 to check whether learning_rates work
|
||||
param = {'max_depth': 2, 'eta': 0, 'verbosity': 0,
|
||||
'objective': 'binary:logistic', 'eval_metric': 'error',
|
||||
'tree_method': tree_method}
|
||||
evals_result = {}
|
||||
bst = xgb.train(param, dtrain, num_round, watchlist,
|
||||
callbacks=[scheduler([
|
||||
0.8, 0.7, 0.6, 0.5
|
||||
])],
|
||||
evals_result=evals_result)
|
||||
with warning_check:
|
||||
bst = xgb.train(param, dtrain, num_round, watchlist,
|
||||
callbacks=[scheduler([
|
||||
0.8, 0.7, 0.6, 0.5
|
||||
])],
|
||||
evals_result=evals_result)
|
||||
eval_errors_0 = list(map(float, evals_result['eval']['error']))
|
||||
assert isinstance(bst, xgb.core.Booster)
|
||||
# validation error should decrease, if eta > 0
|
||||
@@ -193,10 +195,11 @@ class TestCallbacks(unittest.TestCase):
|
||||
'objective': 'binary:logistic', 'eval_metric': 'error',
|
||||
'tree_method': tree_method}
|
||||
evals_result = {}
|
||||
bst = xgb.train(param, dtrain, num_round, watchlist,
|
||||
callbacks=[scheduler(
|
||||
[0.8, 0.7, 0.6, 0.5])],
|
||||
evals_result=evals_result)
|
||||
with warning_check:
|
||||
bst = xgb.train(param, dtrain, num_round, watchlist,
|
||||
callbacks=[scheduler(
|
||||
[0.8, 0.7, 0.6, 0.5])],
|
||||
evals_result=evals_result)
|
||||
eval_errors_1 = list(map(float, evals_result['eval']['error']))
|
||||
assert isinstance(bst, xgb.core.Booster)
|
||||
# validation error should decrease, if learning_rate > 0
|
||||
@@ -208,11 +211,12 @@ class TestCallbacks(unittest.TestCase):
|
||||
'eval_metric': 'error', 'tree_method': tree_method
|
||||
}
|
||||
evals_result = {}
|
||||
bst = xgb.train(param, dtrain, num_round, watchlist,
|
||||
callbacks=[scheduler(
|
||||
[0, 0, 0, 0]
|
||||
)],
|
||||
evals_result=evals_result)
|
||||
with warning_check:
|
||||
bst = xgb.train(param, dtrain, num_round, watchlist,
|
||||
callbacks=[scheduler(
|
||||
[0, 0, 0, 0]
|
||||
)],
|
||||
evals_result=evals_result)
|
||||
eval_errors_2 = list(map(float, evals_result['eval']['error']))
|
||||
assert isinstance(bst, xgb.core.Booster)
|
||||
# validation error should not decrease, if eta/learning_rate = 0
|
||||
@@ -223,11 +227,12 @@ class TestCallbacks(unittest.TestCase):
|
||||
return num_boost_round / (ithround + 1)
|
||||
|
||||
evals_result = {}
|
||||
bst = xgb.train(param, dtrain, num_round, watchlist,
|
||||
callbacks=[
|
||||
scheduler(eta_decay)
|
||||
],
|
||||
evals_result=evals_result)
|
||||
with warning_check:
|
||||
bst = xgb.train(param, dtrain, num_round, watchlist,
|
||||
callbacks=[
|
||||
scheduler(eta_decay)
|
||||
],
|
||||
evals_result=evals_result)
|
||||
eval_errors_3 = list(map(float, evals_result['eval']['error']))
|
||||
|
||||
assert isinstance(bst, xgb.core.Booster)
|
||||
@@ -238,18 +243,15 @@ class TestCallbacks(unittest.TestCase):
|
||||
assert eval_errors_3[i] != eval_errors_2[i]
|
||||
|
||||
def test_eta_decay_hist(self):
|
||||
with pytest.warns(UserWarning):
|
||||
self.run_eta_decay('hist', True)
|
||||
self.run_eta_decay('hist', True)
|
||||
self.run_eta_decay('hist', False)
|
||||
|
||||
def test_eta_decay_approx(self):
|
||||
with pytest.warns(UserWarning):
|
||||
self.run_eta_decay('approx', True)
|
||||
self.run_eta_decay('approx', True)
|
||||
self.run_eta_decay('approx', False)
|
||||
|
||||
def test_eta_decay_exact(self):
|
||||
with pytest.warns(UserWarning):
|
||||
self.run_eta_decay('exact', True)
|
||||
self.run_eta_decay('exact', True)
|
||||
self.run_eta_decay('exact', False)
|
||||
|
||||
def test_check_point(self):
|
||||
|
||||
Reference in New Issue
Block a user