Skip to content

Commit

Permalink
python: use nose for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
henry0312 committed May 5, 2017
1 parent b54f60f commit b41f814
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 47 deletions.
13 changes: 7 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions python-package/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
nose
rednose
Cython
numpy
scipy
pandas
scikit-learn
31 changes: 17 additions & 14 deletions tests/c_api_test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand All @@ -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 = []
Expand Down Expand Up @@ -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 = []
Expand Down Expand Up @@ -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 = []
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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()
8 changes: 2 additions & 6 deletions tests/python_package_test/test_basic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# coding: utf-8
# pylint: skip-file
import os
import subprocess
import tempfile
import unittest

Expand Down Expand Up @@ -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'])
9 changes: 2 additions & 7 deletions tests/python_package_test/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
5 changes: 0 additions & 5 deletions tests/python_package_test/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
14 changes: 5 additions & 9 deletions tests/python_package_test/test_sklearn.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# coding: utf-8
# pylint: skip-file
import math
import os
import unittest

import lightgbm as lgb
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()

0 comments on commit b41f814

Please sign in to comment.