"git@developer.sourcefind.cn:tianlh/lightgbm-dcu.git" did not exist on "fedf3971832201849c2bcdac334289cc97e46217"
Commit babf01c2 authored by Tsukasa OMOTO's avatar Tsukasa OMOTO Committed by Guolin Ke
Browse files

python: use pytest for tests (#498)

https://docs.pytest.org/
parent e2f3fc5e
......@@ -32,28 +32,28 @@ install:
- sudo apt-get install -y cmake
- 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 pep8 pytest
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 && pytest tests/c_api_test/test.py
- cd $TRAVIS_BUILD_DIR && pytest 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 && pytest tests/c_api_test/test.py
- cd $TRAVIS_BUILD_DIR && pytest 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 && pytest tests/c_api_test/test.py
- cd $TRAVIS_BUILD_DIR && pytest 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
......
......@@ -4,14 +4,15 @@ import ctypes
import os
import numpy as np
import pytest
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'))
@pytest.mark.skip
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
@pytest.mark.skip
def test_save_to_binary(handle, filename):
LIB.LGBM_DatasetSaveBinary(handle, c_str(filename))
@pytest.mark.skip
def test_load_from_csr(filename, reference):
data = []
label = []
......@@ -93,6 +97,7 @@ def test_load_from_csr(filename, reference):
return handle
@pytest.mark.skip
def test_load_from_csc(filename, reference):
data = []
label = []
......@@ -130,6 +135,7 @@ def test_load_from_csc(filename, reference):
return handle
@pytest.mark.skip
def test_load_from_mat(filename, reference):
data = []
label = []
......@@ -164,17 +170,18 @@ def test_load_from_mat(filename, reference):
return handle
@pytest.mark.skip
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()
# coding: utf-8
# pylint: skip-file
import os
import subprocess
import tempfile
import unittest
......@@ -51,9 +52,4 @@ class TestBasic(unittest.TestCase):
# 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'])
......@@ -194,8 +194,8 @@ class TestEngine(unittest.TestCase):
folds = tss.split(X_train)
lgb.cv(params_with_metric, lgb_train, num_boost_round=10, folds=folds, 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 @@ class TestEngine(unittest.TestCase):
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()
......@@ -104,8 +104,3 @@ class TestBasic(unittest.TestCase):
self.assertEqual(ax2.get_title(), '')
self.assertEqual(ax2.get_xlabel(), '')
self.assertEqual(ax2.get_ylabel(), '')
print("----------------------------------------------------------------------")
print("running test_plotting.py")
unittest.main()
# coding: utf-8
# pylint: skip-file
import math
import os
import unittest
import lightgbm as lgb
......@@ -52,10 +53,10 @@ class TestSklearn(unittest.TestCase):
self.assertAlmostEqual(ret, gbm.evals_result['valid_0']['multi_logloss'][gbm.best_iteration - 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], early_stopping_rounds=5, verbose=False,
......@@ -150,8 +151,3 @@ class TestSklearn(unittest.TestCase):
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()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment