2011-03-20 00:52:54 +08:00
|
|
|
import os
|
|
|
|
|
import shutil
|
|
|
|
|
import tempfile
|
2013-01-06 02:49:16 +08:00
|
|
|
import warnings
|
2012-12-30 11:44:11 +08:00
|
|
|
import nose
|
2013-01-06 00:12:54 +08:00
|
|
|
import numpy
|
2015-04-17 03:40:01 +08:00
|
|
|
from pickle import loads
|
|
|
|
|
from pickle import dumps
|
2011-03-20 00:52:54 +08:00
|
|
|
|
2012-12-31 11:12:42 +08:00
|
|
|
from sklearn.datasets import get_data_home
|
|
|
|
|
from sklearn.datasets import clear_data_home
|
|
|
|
|
from sklearn.datasets import load_files
|
2013-01-02 14:22:33 +08:00
|
|
|
from sklearn.datasets import load_sample_images
|
|
|
|
|
from sklearn.datasets import load_sample_image
|
|
|
|
|
from sklearn.datasets import load_digits
|
|
|
|
|
from sklearn.datasets import load_diabetes
|
|
|
|
|
from sklearn.datasets import load_linnerud
|
2013-01-06 00:12:54 +08:00
|
|
|
from sklearn.datasets import load_iris
|
2015-09-28 13:53:47 +08:00
|
|
|
from sklearn.datasets import load_breast_cancer
|
2013-01-06 00:12:54 +08:00
|
|
|
from sklearn.datasets import load_boston
|
2015-04-17 03:40:01 +08:00
|
|
|
from sklearn.datasets.base import Bunch
|
2012-12-31 11:12:42 +08:00
|
|
|
|
2013-03-19 06:29:34 +08:00
|
|
|
from sklearn.externals.six import b, u
|
2013-03-19 05:04:47 +08:00
|
|
|
|
2012-10-25 23:09:46 +08:00
|
|
|
from sklearn.utils.testing import assert_false
|
|
|
|
|
from sklearn.utils.testing import assert_true
|
|
|
|
|
from sklearn.utils.testing import assert_equal
|
2013-01-02 14:22:33 +08:00
|
|
|
from sklearn.utils.testing import assert_raises
|
2011-03-20 00:52:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
DATA_HOME = tempfile.mkdtemp(prefix="scikit_learn_data_home_test_")
|
2012-12-30 11:44:11 +08:00
|
|
|
LOAD_FILES_ROOT = tempfile.mkdtemp(prefix="scikit_learn_load_files_test_")
|
|
|
|
|
TEST_CATEGORY_DIR1 = ""
|
|
|
|
|
TEST_CATEGORY_DIR2 = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _remove_dir(path):
|
|
|
|
|
if os.path.isdir(path):
|
|
|
|
|
shutil.rmtree(path)
|
2011-03-20 00:52:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def teardown_module():
|
|
|
|
|
"""Test fixture (clean up) run once after all tests of this module"""
|
2012-12-30 11:44:11 +08:00
|
|
|
for path in [DATA_HOME, LOAD_FILES_ROOT]:
|
|
|
|
|
_remove_dir(path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def setup_load_files():
|
|
|
|
|
global TEST_CATEGORY_DIR1
|
|
|
|
|
global TEST_CATEGORY_DIR2
|
|
|
|
|
TEST_CATEGORY_DIR1 = tempfile.mkdtemp(dir=LOAD_FILES_ROOT)
|
|
|
|
|
TEST_CATEGORY_DIR2 = tempfile.mkdtemp(dir=LOAD_FILES_ROOT)
|
|
|
|
|
sample_file = tempfile.NamedTemporaryFile(dir=TEST_CATEGORY_DIR1,
|
|
|
|
|
delete=False)
|
2013-03-19 05:04:47 +08:00
|
|
|
sample_file.write(b("Hello World!\n"))
|
2012-12-30 11:44:11 +08:00
|
|
|
sample_file.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def teardown_load_files():
|
|
|
|
|
_remove_dir(TEST_CATEGORY_DIR1)
|
|
|
|
|
_remove_dir(TEST_CATEGORY_DIR2)
|
2011-03-20 00:52:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_data_home():
|
|
|
|
|
# get_data_home will point to a pre-existing folder
|
|
|
|
|
data_home = get_data_home(data_home=DATA_HOME)
|
2012-10-25 23:09:46 +08:00
|
|
|
assert_equal(data_home, DATA_HOME)
|
2011-03-20 00:52:54 +08:00
|
|
|
assert_true(os.path.exists(data_home))
|
|
|
|
|
|
|
|
|
|
# clear_data_home will delete both the content and the folder it-self
|
|
|
|
|
clear_data_home(data_home=data_home)
|
|
|
|
|
assert_false(os.path.exists(data_home))
|
|
|
|
|
|
|
|
|
|
# if the folder is missing it will be created again
|
|
|
|
|
data_home = get_data_home(data_home=DATA_HOME)
|
|
|
|
|
assert_true(os.path.exists(data_home))
|
2012-12-30 11:44:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_default_empty_load_files():
|
|
|
|
|
res = load_files(LOAD_FILES_ROOT)
|
|
|
|
|
assert_equal(len(res.filenames), 0)
|
|
|
|
|
assert_equal(len(res.target_names), 0)
|
|
|
|
|
assert_equal(res.DESCR, None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@nose.tools.with_setup(setup_load_files, teardown_load_files)
|
|
|
|
|
def test_default_load_files():
|
|
|
|
|
res = load_files(LOAD_FILES_ROOT)
|
|
|
|
|
assert_equal(len(res.filenames), 1)
|
2012-12-30 11:44:11 +08:00
|
|
|
assert_equal(len(res.target_names), 2)
|
2012-12-30 11:44:11 +08:00
|
|
|
assert_equal(res.DESCR, None)
|
2013-03-19 06:29:34 +08:00
|
|
|
assert_equal(res.data, [b("Hello World!\n")])
|
2012-12-30 11:44:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@nose.tools.with_setup(setup_load_files, teardown_load_files)
|
2013-07-03 19:46:11 +08:00
|
|
|
def test_load_files_w_categories_desc_and_encoding():
|
2012-12-30 11:44:11 +08:00
|
|
|
category = os.path.abspath(TEST_CATEGORY_DIR1).split('/').pop()
|
|
|
|
|
res = load_files(LOAD_FILES_ROOT, description="test",
|
2013-07-03 19:46:11 +08:00
|
|
|
categories=category, encoding="utf-8")
|
2012-12-30 11:44:11 +08:00
|
|
|
assert_equal(len(res.filenames), 1)
|
2012-12-30 11:44:11 +08:00
|
|
|
assert_equal(len(res.target_names), 1)
|
2012-12-30 11:44:11 +08:00
|
|
|
assert_equal(res.DESCR, "test")
|
2013-03-19 06:29:34 +08:00
|
|
|
assert_equal(res.data, [u("Hello World!\n")])
|
2012-12-30 11:44:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@nose.tools.with_setup(setup_load_files, teardown_load_files)
|
|
|
|
|
def test_load_files_wo_load_content():
|
|
|
|
|
res = load_files(LOAD_FILES_ROOT, load_content=False)
|
|
|
|
|
assert_equal(len(res.filenames), 1)
|
2012-12-30 11:44:11 +08:00
|
|
|
assert_equal(len(res.target_names), 2)
|
2012-12-30 11:44:11 +08:00
|
|
|
assert_equal(res.DESCR, None)
|
|
|
|
|
assert_equal(res.get('data'), None)
|
2013-01-02 14:22:33 +08:00
|
|
|
|
|
|
|
|
|
2013-01-06 00:12:54 +08:00
|
|
|
def test_load_sample_images():
|
2013-01-06 02:49:16 +08:00
|
|
|
try:
|
|
|
|
|
res = load_sample_images()
|
|
|
|
|
assert_equal(len(res.images), 2)
|
|
|
|
|
assert_equal(len(res.filenames), 2)
|
|
|
|
|
assert_true(res.DESCR)
|
|
|
|
|
except ImportError:
|
|
|
|
|
warnings.warn("Could not load sample images, PIL is not available.")
|
2013-01-02 14:22:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_load_digits():
|
|
|
|
|
digits = load_digits()
|
2013-01-06 00:12:54 +08:00
|
|
|
assert_equal(digits.data.shape, (1797, 64))
|
|
|
|
|
assert_equal(numpy.unique(digits.target).size, 10)
|
2013-01-02 14:22:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_load_digits_n_class_lt_10():
|
|
|
|
|
digits = load_digits(9)
|
2013-01-06 00:12:54 +08:00
|
|
|
assert_equal(digits.data.shape, (1617, 64))
|
|
|
|
|
assert_equal(numpy.unique(digits.target).size, 9)
|
2013-01-02 14:22:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_load_sample_image():
|
2013-01-06 02:49:16 +08:00
|
|
|
try:
|
|
|
|
|
china = load_sample_image('china.jpg')
|
|
|
|
|
assert_equal(china.dtype, 'uint8')
|
|
|
|
|
assert_equal(china.shape, (427, 640, 3))
|
|
|
|
|
except ImportError:
|
|
|
|
|
warnings.warn("Could not load sample images, PIL is not available.")
|
2013-01-02 14:22:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_load_missing_sample_image_error():
|
2013-01-06 02:49:16 +08:00
|
|
|
have_PIL = True
|
|
|
|
|
try:
|
|
|
|
|
try:
|
|
|
|
|
from scipy.misc import imread
|
|
|
|
|
except ImportError:
|
|
|
|
|
from scipy.misc.pilutil import imread
|
|
|
|
|
except ImportError:
|
|
|
|
|
have_PIL = False
|
|
|
|
|
if have_PIL:
|
|
|
|
|
assert_raises(AttributeError, load_sample_image,
|
|
|
|
|
'blop.jpg')
|
|
|
|
|
else:
|
|
|
|
|
warnings.warn("Could not load sample images, PIL is not available.")
|
2013-01-02 14:22:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_load_diabetes():
|
|
|
|
|
res = load_diabetes()
|
2013-01-06 00:12:54 +08:00
|
|
|
assert_equal(res.data.shape, (442, 10))
|
|
|
|
|
assert_true(res.target.size, 442)
|
2013-01-02 14:22:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_load_linnerud():
|
|
|
|
|
res = load_linnerud()
|
2013-01-06 00:12:54 +08:00
|
|
|
assert_equal(res.data.shape, (20, 3))
|
|
|
|
|
assert_equal(res.target.shape, (20, 3))
|
|
|
|
|
assert_equal(len(res.target_names), 3)
|
|
|
|
|
assert_true(res.DESCR)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_load_iris():
|
|
|
|
|
res = load_iris()
|
|
|
|
|
assert_equal(res.data.shape, (150, 4))
|
|
|
|
|
assert_equal(res.target.size, 150)
|
|
|
|
|
assert_equal(res.target_names.size, 3)
|
|
|
|
|
assert_true(res.DESCR)
|
|
|
|
|
|
|
|
|
|
|
2015-09-28 13:53:47 +08:00
|
|
|
def test_load_breast_cancer():
|
|
|
|
|
res = load_breast_cancer()
|
|
|
|
|
assert_equal(res.data.shape, (569, 30))
|
|
|
|
|
assert_equal(res.target.size, 569)
|
|
|
|
|
assert_equal(res.target_names.size, 2)
|
|
|
|
|
assert_true(res.DESCR)
|
|
|
|
|
|
|
|
|
|
|
2013-01-06 00:12:54 +08:00
|
|
|
def test_load_boston():
|
|
|
|
|
res = load_boston()
|
|
|
|
|
assert_equal(res.data.shape, (506, 13))
|
|
|
|
|
assert_equal(res.target.size, 506)
|
2014-02-16 20:38:31 +08:00
|
|
|
assert_equal(res.feature_names.size, 13)
|
2013-01-02 14:22:33 +08:00
|
|
|
assert_true(res.DESCR)
|
2015-04-17 03:40:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_loads_dumps_bunch():
|
|
|
|
|
bunch = Bunch(x="x")
|
|
|
|
|
bunch_from_pkl = loads(dumps(bunch))
|
|
|
|
|
bunch_from_pkl.x = "y"
|
|
|
|
|
assert_equal(bunch_from_pkl['x'], bunch_from_pkl.x)
|