Refactor Python tests. (#3897)

* Deprecate nose tests.
* Format python tests.
This commit is contained in:
Jiaming Yuan
2018-11-15 13:56:33 +13:00
committed by GitHub
parent c76d993681
commit 2ea0f887c1
23 changed files with 302 additions and 225 deletions

View File

@@ -1,18 +1,19 @@
import sys
import pytest
import unittest
sys.path.append('tests/python/')
import test_linear
import testing as tm
import unittest
class TestGPULinear(unittest.TestCase):
datasets = ["Boston", "Digits", "Cancer", "Sparse regression",
"Boston External Memory"]
@pytest.mark.skipif(**tm.no_sklearn())
def test_gpu_coordinate(self):
tm._skip_if_no_sklearn()
variable_param = {
'booster': ['gblinear'],
'updater': ['coord_descent'],

View File

@@ -1,15 +1,14 @@
from __future__ import print_function
import numpy as np
import sys
import unittest
import xgboost as xgb
from nose.plugins.attrib import attr
import pytest
rng = np.random.RandomState(1994)
@attr('gpu')
@pytest.mark.gpu
class TestGPUPredict(unittest.TestCase):
def test_predict(self):
iterations = 10
@@ -18,9 +17,12 @@ class TestGPUPredict(unittest.TestCase):
test_num_cols = [10, 50, 500]
for num_rows in test_num_rows:
for num_cols in test_num_cols:
dtrain = xgb.DMatrix(np.random.randn(num_rows, num_cols), label=[0, 1] * int(num_rows / 2))
dval = xgb.DMatrix(np.random.randn(num_rows, num_cols), label=[0, 1] * int(num_rows / 2))
dtest = xgb.DMatrix(np.random.randn(num_rows, num_cols), label=[0, 1] * int(num_rows / 2))
dtrain = xgb.DMatrix(np.random.randn(num_rows, num_cols),
label=[0, 1] * int(num_rows / 2))
dval = xgb.DMatrix(np.random.randn(num_rows, num_cols),
label=[0, 1] * int(num_rows / 2))
dtest = xgb.DMatrix(np.random.randn(num_rows, num_cols),
label=[0, 1] * int(num_rows / 2))
watchlist = [(dtrain, 'train'), (dval, 'validation')]
res = {}
param = {
@@ -28,7 +30,8 @@ class TestGPUPredict(unittest.TestCase):
"predictor": "gpu_predictor",
'eval_metric': 'auc',
}
bst = xgb.train(param, dtrain, iterations, evals=watchlist, evals_result=res)
bst = xgb.train(param, dtrain, iterations, evals=watchlist,
evals_result=res)
assert self.non_decreasing(res["train"]["auc"])
gpu_pred_train = bst.predict(dtrain, output_margin=True)
gpu_pred_test = bst.predict(dtest, output_margin=True)
@@ -39,21 +42,26 @@ class TestGPUPredict(unittest.TestCase):
cpu_pred_train = bst_cpu.predict(dtrain, output_margin=True)
cpu_pred_test = bst_cpu.predict(dtest, output_margin=True)
cpu_pred_val = bst_cpu.predict(dval, output_margin=True)
np.testing.assert_allclose(cpu_pred_train, gpu_pred_train, rtol=1e-5)
np.testing.assert_allclose(cpu_pred_val, gpu_pred_val, rtol=1e-5)
np.testing.assert_allclose(cpu_pred_test, gpu_pred_test, rtol=1e-5)
np.testing.assert_allclose(cpu_pred_train, gpu_pred_train,
rtol=1e-5)
np.testing.assert_allclose(cpu_pred_val, gpu_pred_val,
rtol=1e-5)
np.testing.assert_allclose(cpu_pred_test, gpu_pred_test,
rtol=1e-5)
def non_decreasing(self, L):
return all((x - y) < 0.001 for x, y in zip(L, L[1:]))
# Test case for a bug where multiple batch predictions made on a test set produce incorrect results
# Test case for a bug where multiple batch predictions made on a
# test set produce incorrect results
def test_multi_predict(self):
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
n = 1000
X, y = make_regression(n, random_state=rng)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=123)
X_train, X_test, y_train, y_test = train_test_split(X, y,
random_state=123)
dtrain = xgb.DMatrix(X_train, label=y_train)
dtest = xgb.DMatrix(X_test)
@@ -85,8 +93,7 @@ class TestGPUPredict(unittest.TestCase):
params = {'tree_method': 'gpu_hist',
'predictor': 'cpu_predictor',
'n_jobs': -1,
'seed': 123
}
'seed': 123}
m = xgb.XGBRegressor(**params).fit(X_train, y_train)
cpu_train_score = m.score(X_train, y_train)
cpu_test_score = m.score(X_test, y_test)

View File

@@ -1,10 +1,9 @@
import numpy as np
import sys
import unittest
from nose.plugins.attrib import attr
import pytest
sys.path.append("tests/python")
import xgboost as xgb
from regression_test_utilities import run_suite, parameter_combinations, \
assert_results_non_increasing
@@ -45,7 +44,7 @@ class TestGPU(unittest.TestCase):
cpu_results = run_suite(param, select_datasets=datasets)
assert_gpu_results(cpu_results, gpu_results)
@attr('mgpu')
@pytest.mark.mgpu
def test_gpu_hist_mgpu(self):
variable_param = {'n_gpus': [-1], 'max_depth': [2, 10],
'max_leaves': [255, 4],
@@ -56,7 +55,7 @@ class TestGPU(unittest.TestCase):
gpu_results = run_suite(param, select_datasets=datasets)
assert_results_non_increasing(gpu_results, 1e-2)
@attr('mgpu')
@pytest.mark.mgpu
def test_specified_gpu_id_gpu_update(self):
variable_param = {'n_gpus': [1],
'gpu_id': [1],

View File

@@ -2,12 +2,12 @@ from __future__ import print_function
import sys
import time
import pytest
sys.path.append("../../tests/python")
import xgboost as xgb
import numpy as np
import unittest
from nose.plugins.attrib import attr
def eprint(*args, **kwargs):
@@ -16,9 +16,11 @@ def eprint(*args, **kwargs):
print(*args, file=sys.stdout, **kwargs)
sys.stdout.flush()
rng = np.random.RandomState(1994)
# "realistic" size based upon http://stat-computing.org/dataexpo/2009/ , which has been processed to one-hot encode categoricalsxsy
# "realistic" size based upon http://stat-computing.org/dataexpo/2009/
# , which has been processed to one-hot encode categoricalsxsy
cols = 31
# reduced to fit onto 1 gpu but still be large
rows3 = 5000 # small
@@ -28,7 +30,7 @@ rows1 = 42360032 # large
rowslist = [rows1, rows2, rows3]
@attr('slow')
@pytest.mark.slow
class TestGPU(unittest.TestCase):
def test_large(self):
for rows in rowslist:
@@ -47,15 +49,8 @@ class TestGPU(unittest.TestCase):
max_depth = 6
max_bin = 1024
# regression test --- hist must be same as exact on all-categorial data
ag_param = {'max_depth': max_depth,
'tree_method': 'exact',
'nthread': 0,
'eta': 1,
'silent': 0,
'debug_verbose': 5,
'objective': 'binary:logistic',
'eval_metric': 'auc'}
# regression test --- hist must be same as exact on
# all-categorial data
ag_paramb = {'max_depth': max_depth,
'tree_method': 'hist',
'nthread': 0,

View File

@@ -1,11 +1,13 @@
from __future__ import print_function
import numpy as np
import unittest
import xgboost as xgb
from nose.plugins.attrib import attr
from sklearn.datasets import make_regression
import unittest
import pytest
import xgboost as xgb
rng = np.random.RandomState(1994)
@@ -33,7 +35,7 @@ def assert_constraint(constraint, tree_method):
assert non_increasing(pred)
@attr('gpu')
@pytest.mark.gpu
class TestMonotonicConstraints(unittest.TestCase):
def test_exact(self):
assert_constraint(1, 'exact')