diff --git a/tests/python/test_basic.py b/tests/python/test_basic.py index dad7ddc9d..acfb6db56 100644 --- a/tests/python/test_basic.py +++ b/tests/python/test_basic.py @@ -110,16 +110,19 @@ class TestBasic(unittest.TestCase): # error must be smaller than 10% assert err < 0.1 - # save dmatrix into binary buffer - dtest.save_binary('dtest.buffer') - # save model - bst.save_model('xgb.model') - # load model and data in - bst2 = xgb.Booster(model_file='xgb.model') - dtest2 = xgb.DMatrix('dtest.buffer') - preds2 = bst2.predict(dtest2) - # assert they are the same - assert np.sum(np.abs(preds2 - preds)) == 0 + with tempfile.TemporaryDirectory() as tmpdir: + dtest_path = os.path.join(tmpdir, 'dtest.buffer') + model_path = os.path.join(tmpdir, 'xgb.model') + # save dmatrix into binary buffer + dtest.save_binary(dtest_path) + # save model + bst.save_model(model_path) + # load model and data in + bst2 = xgb.Booster(model_file=model_path) + dtest2 = xgb.DMatrix(dtest_path) + preds2 = bst2.predict(dtest2) + # assert they are the same + assert np.sum(np.abs(preds2 - preds)) == 0 def test_dump(self): data = np.random.randn(100, 2) diff --git a/tests/python/test_basic_models.py b/tests/python/test_basic_models.py index 3eafdf71d..529f7784c 100644 --- a/tests/python/test_basic_models.py +++ b/tests/python/test_basic_models.py @@ -6,6 +6,7 @@ import json import testing as tm import pytest import locale +import tempfile dpath = 'demo/data/' dtrain = xgb.DMatrix(dpath + 'agaricus.txt.train') @@ -60,15 +61,20 @@ class TestModels(unittest.TestCase): # error must be smaller than 10% assert err < 0.1 - # save dmatrix into binary buffer - dtest.save_binary('dtest.buffer') - model_path = 'xgb.model.dart' - # save model - bst.save_model(model_path) - # load model and data in - bst2 = xgb.Booster(params=param, model_file='xgb.model.dart') - dtest2 = xgb.DMatrix('dtest.buffer') + with tempfile.TemporaryDirectory() as tmpdir: + dtest_path = os.path.join(tmpdir, 'dtest.dmatrix') + model_path = os.path.join(tmpdir, 'xgboost.model.dart') + # save dmatrix into binary buffer + dtest.save_binary(dtest_path) + model_path = model_path + # save model + bst.save_model(model_path) + # load model and data in + bst2 = xgb.Booster(params=param, model_file=model_path) + dtest2 = xgb.DMatrix(dtest_path) + preds2 = bst2.predict(dtest2, ntree_limit=num_round) + # assert they are the same assert np.sum(np.abs(preds2 - preds)) == 0 @@ -103,7 +109,6 @@ class TestModels(unittest.TestCase): for ii in range(len(preds_list)): for jj in range(ii + 1, len(preds_list)): assert np.sum(np.abs(preds_list[ii] - preds_list[jj])) > 0 - os.remove(model_path) def run_eta_decay(self, tree_method): watchlist = [(dtest, 'eval'), (dtrain, 'train')] diff --git a/tests/python/test_plotting.py b/tests/python/test_plotting.py index 18b0b83c7..e5e3a96e1 100644 --- a/tests/python/test_plotting.py +++ b/tests/python/test_plotting.py @@ -14,27 +14,27 @@ try: except ImportError: pass +pytestmark = pytest.mark.skipif(**tm.no_multiple(tm.no_matplotlib(), + tm.no_graphviz())) -pytestmark = pytest.mark.skipif(**tm.no_multiple(tm.no_matplotlib(), tm.no_graphviz())) - - -dpath = 'demo/data/' -rng = np.random.RandomState(1994) +dpath = 'demo/data/agaricus.txt.train' class TestPlotting(unittest.TestCase): - def test_plotting(self): - bst2 = xgb.Booster(model_file='xgb.model') + m = xgb.DMatrix(dpath) + booster = xgb.train({'max_depth': 2, 'eta': 1, + 'objective': 'binary:logistic'}, m, + num_boost_round=2) - ax = xgb.plot_importance(bst2) + ax = xgb.plot_importance(booster) assert isinstance(ax, Axes) assert ax.get_title() == 'Feature importance' assert ax.get_xlabel() == 'F score' assert ax.get_ylabel() == 'Features' assert len(ax.patches) == 4 - ax = xgb.plot_importance(bst2, color='r', + ax = xgb.plot_importance(booster, color='r', title='t', xlabel='x', ylabel='y') assert isinstance(ax, Axes) assert ax.get_title() == 't' @@ -44,7 +44,7 @@ class TestPlotting(unittest.TestCase): for p in ax.patches: assert p.get_facecolor() == (1.0, 0, 0, 1.0) # red - ax = xgb.plot_importance(bst2, color=['r', 'r', 'b', 'b'], + ax = xgb.plot_importance(booster, color=['r', 'r', 'b', 'b'], title=None, xlabel=None, ylabel=None) assert isinstance(ax, Axes) assert ax.get_title() == '' @@ -56,10 +56,10 @@ class TestPlotting(unittest.TestCase): assert ax.patches[2].get_facecolor() == (0, 0, 1.0, 1.0) # blue assert ax.patches[3].get_facecolor() == (0, 0, 1.0, 1.0) # blue - g = xgb.to_graphviz(bst2, num_trees=0) + g = xgb.to_graphviz(booster, num_trees=0) assert isinstance(g, Source) - ax = xgb.plot_tree(bst2, num_trees=0) + ax = xgb.plot_tree(booster, num_trees=0) assert isinstance(ax, Axes) def test_importance_plot_lim(self):