From b41f814dfb99b8f048aef957b20fb7c4e52c80a9 Mon Sep 17 00:00:00 2001 From: Tsukasa OMOTO Date: Tue, 2 May 2017 16:55:30 +0900 Subject: [PATCH] python: use nose for tests http://nose.readthedocs.io/en/latest/index.html --- .travis.yml | 13 ++++----- python-package/requirements.txt | 7 +++++ tests/c_api_test/test.py | 31 ++++++++++++---------- tests/python_package_test/test_basic.py | 8 ++---- tests/python_package_test/test_engine.py | 9 ++----- tests/python_package_test/test_plotting.py | 5 ---- tests/python_package_test/test_sklearn.py | 14 ++++------ 7 files changed, 40 insertions(+), 47 deletions(-) create mode 100644 python-package/requirements.txt diff --git a/.travis.yml b/.travis.yml index 6e4f9123c564..6d29c36ac3e0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -33,27 +33,28 @@ install: - conda install --yes atlas numpy scipy scikit-learn pandas matplotlib - conda install --yes -c conda-forge boost=1.63.0 - pip install pep8 +- pip install -r $TRAVIS_BUILD_DIR/python-package/requirements.txt script: - cd $TRAVIS_BUILD_DIR - mkdir build && cd build && cmake -DUSE_MPI=ON ..&& make -- cd $TRAVIS_BUILD_DIR/tests/c_api_test && python test.py - cd $TRAVIS_BUILD_DIR/python-package && python setup.py install -- cd $TRAVIS_BUILD_DIR/tests/python_package_test && python test_basic.py && python test_engine.py && python test_sklearn.py && python test_plotting.py +- cd $TRAVIS_BUILD_DIR && nosetests --rednose tests/c_api_test +- cd $TRAVIS_BUILD_DIR && nosetests --rednose tests/python_package_test - cd $TRAVIS_BUILD_DIR && pep8 --ignore=E501 --exclude=./compute,./docs . - cd $TRAVIS_BUILD_DIR - rm -rf build && mkdir build && cd build && cmake -DUSE_GPU=ON -DBOOST_ROOT="$HOME/miniconda/" -DOpenCL_INCLUDE_DIR=$AMDAPPSDK/include/ .. - sed -i 's/std::string device_type = "cpu";/std::string device_type = "gpu";/' ../include/LightGBM/config.h - make - sed -i 's/std::string device_type = "gpu";/std::string device_type = "cpu";/' ../include/LightGBM/config.h -- cd $TRAVIS_BUILD_DIR/tests/c_api_test && python test.py - cd $TRAVIS_BUILD_DIR/python-package && python setup.py install -- cd $TRAVIS_BUILD_DIR/tests/python_package_test && python test_basic.py && python test_engine.py && python test_sklearn.py && python test_plotting.py +- cd $TRAVIS_BUILD_DIR && nosetests --rednose tests/c_api_test +- cd $TRAVIS_BUILD_DIR && nosetests --rednose tests/python_package_test - cd $TRAVIS_BUILD_DIR - rm -rf build && mkdir build && cd build && cmake .. && make -- cd $TRAVIS_BUILD_DIR/tests/c_api_test && python test.py - cd $TRAVIS_BUILD_DIR/python-package && python setup.py install -- cd $TRAVIS_BUILD_DIR/tests/python_package_test && python test_basic.py && python test_engine.py && python test_sklearn.py && python test_plotting.py +- cd $TRAVIS_BUILD_DIR && nosetests --rednose tests/c_api_test +- cd $TRAVIS_BUILD_DIR && nosetests --rednose tests/python_package_test - cd $TRAVIS_BUILD_DIR/tests/cpp_test && ../../lightgbm config=train.conf && ../../lightgbm config=predict.conf output_result=origin.pred - cd $TRAVIS_BUILD_DIR/build && make - cd $TRAVIS_BUILD_DIR/tests/cpp_test && ../../lightgbm config=predict.conf output_result=ifelse.pred && python test.py diff --git a/python-package/requirements.txt b/python-package/requirements.txt new file mode 100644 index 000000000000..2d5b139e6b3a --- /dev/null +++ b/python-package/requirements.txt @@ -0,0 +1,7 @@ +nose +rednose +Cython +numpy +scipy +pandas +scikit-learn diff --git a/tests/c_api_test/test.py b/tests/c_api_test/test.py index 9656b2bb3b17..ff547bc2ff95 100644 --- a/tests/c_api_test/test.py +++ b/tests/c_api_test/test.py @@ -3,15 +3,16 @@ import ctypes import os +import nose.tools import numpy as np from scipy import sparse def LoadDll(): if os.name == 'nt': - lib_path = '../../windows/x64/DLL/lib_lightgbm.dll' + lib_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../windows/x64/DLL/lib_lightgbm.dll') else: - lib_path = '../../lib_lightgbm.so' + lib_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../lib_lightgbm.so') lib = ctypes.cdll.LoadLibrary(lib_path) return lib @@ -34,6 +35,7 @@ def c_str(string): return ctypes.c_char_p(string.encode('ascii')) +@nose.tools.nottest def test_load_from_file(filename, reference): ref = None if reference is not None: @@ -52,10 +54,12 @@ def test_load_from_file(filename, reference): return handle +@nose.tools.nottest def test_save_to_binary(handle, filename): LIB.LGBM_DatasetSaveBinary(handle, c_str(filename)) +@nose.tools.nottest def test_load_from_csr(filename, reference): data = [] label = [] @@ -93,6 +97,7 @@ def test_load_from_csr(filename, reference): return handle +@nose.tools.nottest def test_load_from_csc(filename, reference): data = [] label = [] @@ -130,6 +135,7 @@ def test_load_from_csc(filename, reference): return handle +@nose.tools.nottest def test_load_from_mat(filename, reference): data = [] label = [] @@ -164,17 +170,18 @@ def test_load_from_mat(filename, reference): return handle +@nose.tools.nottest def test_free_dataset(handle): LIB.LGBM_DatasetFree(handle) def test_dataset(): - train = test_load_from_file('../../examples/binary_classification/binary.train', None) - test = test_load_from_mat('../../examples/binary_classification/binary.test', train) + train = test_load_from_file(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../examples/binary_classification/binary.train'), None) + test = test_load_from_mat(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../examples/binary_classification/binary.test'), train) test_free_dataset(test) - test = test_load_from_csr('../../examples/binary_classification/binary.test', train) + test = test_load_from_csr(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../examples/binary_classification/binary.test'), train) test_free_dataset(test) - test = test_load_from_csc('../../examples/binary_classification/binary.test', train) + test = test_load_from_csc(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../examples/binary_classification/binary.test'), train) test_free_dataset(test) test_save_to_binary(train, 'train.binary.bin') test_free_dataset(train) @@ -183,8 +190,8 @@ def test_dataset(): def test_booster(): - train = test_load_from_mat('../../examples/binary_classification/binary.train', None) - test = test_load_from_mat('../../examples/binary_classification/binary.test', train) + train = test_load_from_mat(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../examples/binary_classification/binary.train'), None) + test = test_load_from_mat(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../examples/binary_classification/binary.test'), train) booster = ctypes.c_void_p() LIB.LGBM_BoosterCreate(train, c_str("app=binary metric=auc num_leaves=31 verbose=0"), ctypes.byref(booster)) LIB.LGBM_BoosterAddValidData(booster, test) @@ -204,7 +211,7 @@ def test_booster(): num_total_model = ctypes.c_long() LIB.LGBM_BoosterCreateFromModelfile(c_str('model.txt'), ctypes.byref(num_total_model), ctypes.byref(booster2)) data = [] - inp = open('../../examples/binary_classification/binary.test', 'r') + inp = open(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../examples/binary_classification/binary.test'), 'r') for line in inp.readlines(): data.append([float(x) for x in line.split('\t')[1:]]) inp.close() @@ -223,9 +230,5 @@ def test_booster(): 50, ctypes.byref(num_preb), preb.ctypes.data_as(ctypes.POINTER(ctypes.c_double))) - LIB.LGBM_BoosterPredictForFile(booster2, c_str('../../examples/binary_classification/binary.test'), 0, 0, 50, c_str('preb.txt')) + LIB.LGBM_BoosterPredictForFile(booster2, c_str(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../examples/binary_classification/binary.test')), 0, 0, 50, c_str('preb.txt')) LIB.LGBM_BoosterFree(booster2) - - -test_dataset() -test_booster() diff --git a/tests/python_package_test/test_basic.py b/tests/python_package_test/test_basic.py index 4e4a1e14a434..9f63056ee329 100644 --- a/tests/python_package_test/test_basic.py +++ b/tests/python_package_test/test_basic.py @@ -1,6 +1,7 @@ # coding: utf-8 # pylint: skip-file import os +import subprocess import tempfile import unittest @@ -51,9 +52,4 @@ def test(self): # we need to check the consistency of model file here, so test for exact equal self.assertEqual(*preds) # check pmml - os.system('python ../../pmml/pmml.py model.txt') - - -print("----------------------------------------------------------------------") -print("running test_basic.py") -unittest.main() + subprocess.call(['python', os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../pmml/pmml.py'), 'model.txt']) diff --git a/tests/python_package_test/test_engine.py b/tests/python_package_test/test_engine.py index dfbc3beea436..9ca50c401ce4 100644 --- a/tests/python_package_test/test_engine.py +++ b/tests/python_package_test/test_engine.py @@ -194,8 +194,8 @@ def test_cv(self): lgb.cv(params, lgb_train, num_boost_round=10, data_splitter=tss, nfold=5, # test if wrong nfold is ignored metrics='l2', verbose_eval=False) # lambdarank - X_train, y_train = load_svmlight_file('../../examples/lambdarank/rank.train') - q_train = np.loadtxt('../../examples/lambdarank/rank.train.query') + X_train, y_train = load_svmlight_file(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../examples/lambdarank/rank.train')) + q_train = np.loadtxt(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../examples/lambdarank/rank.train.query')) params_lambdarank = {'objective': 'lambdarank', 'verbose': -1} lgb_train = lgb.Dataset(X_train, y_train, group=q_train) lgb.cv(params_lambdarank, lgb_train, num_boost_round=10, nfold=3, metrics='l2', verbose_eval=False) @@ -286,8 +286,3 @@ def test_pandas_categorical(self): np.testing.assert_almost_equal(pred0, pred2) np.testing.assert_almost_equal(pred0, pred3) np.testing.assert_almost_equal(pred0, pred4) - - -print("----------------------------------------------------------------------") -print("running test_engine.py") -unittest.main() diff --git a/tests/python_package_test/test_plotting.py b/tests/python_package_test/test_plotting.py index 72c4a72ebebc..4671e44ef813 100644 --- a/tests/python_package_test/test_plotting.py +++ b/tests/python_package_test/test_plotting.py @@ -104,8 +104,3 @@ def test_plot_metrics(self): self.assertEqual(ax2.get_title(), '') self.assertEqual(ax2.get_xlabel(), '') self.assertEqual(ax2.get_ylabel(), '') - - -print("----------------------------------------------------------------------") -print("running test_plotting.py") -unittest.main() diff --git a/tests/python_package_test/test_sklearn.py b/tests/python_package_test/test_sklearn.py index 25dab43981a6..1dda6443d692 100644 --- a/tests/python_package_test/test_sklearn.py +++ b/tests/python_package_test/test_sklearn.py @@ -1,6 +1,7 @@ # coding: utf-8 # pylint: skip-file import math +import os import unittest import lightgbm as lgb @@ -52,10 +53,10 @@ def test_multiclass(self): self.assertAlmostEqual(ret, gbm.evals_result['valid_0']['multi_logloss'][-1], places=5) def test_lambdarank(self): - X_train, y_train = load_svmlight_file('../../examples/lambdarank/rank.train') - X_test, y_test = load_svmlight_file('../../examples/lambdarank/rank.test') - q_train = np.loadtxt('../../examples/lambdarank/rank.train.query') - q_test = np.loadtxt('../../examples/lambdarank/rank.test.query') + X_train, y_train = load_svmlight_file(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../examples/lambdarank/rank.train')) + X_test, y_test = load_svmlight_file(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../examples/lambdarank/rank.test')) + q_train = np.loadtxt(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../examples/lambdarank/rank.train.query')) + q_test = np.loadtxt(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../examples/lambdarank/rank.test.query')) gbm = lgb.LGBMRanker() gbm.fit(X_train, y_train, group=q_train, eval_set=[(X_test, y_test)], eval_group=[q_test], eval_at=[1, 3], verbose=False, @@ -150,8 +151,3 @@ def test_joblib(self): self.assertEqual(len(pred_origin), len(pred_pickle)) for preds in zip(pred_origin, pred_pickle): self.assertAlmostEqual(*preds, places=5) - - -print("----------------------------------------------------------------------") -print("running test_sklearn.py") -unittest.main()