2013-05-28 19:48:57 +08:00
|
|
|
import warnings
|
|
|
|
|
import unittest
|
|
|
|
|
import sys
|
2018-04-23 20:58:40 +08:00
|
|
|
import os
|
|
|
|
|
import atexit
|
|
|
|
|
|
2017-06-09 22:33:57 +08:00
|
|
|
import numpy as np
|
2018-04-23 20:58:40 +08:00
|
|
|
|
2017-06-09 22:33:57 +08:00
|
|
|
from scipy import sparse
|
2013-05-28 19:48:57 +08:00
|
|
|
|
2018-07-18 04:50:32 +08:00
|
|
|
import pytest
|
|
|
|
|
|
2017-07-12 00:42:10 +08:00
|
|
|
from sklearn.utils.deprecation import deprecated
|
|
|
|
|
from sklearn.utils.metaestimators import if_delegate_has_method
|
2012-12-21 18:46:14 +08:00
|
|
|
from sklearn.utils.testing import (
|
2016-10-08 00:46:52 +08:00
|
|
|
assert_raises,
|
2016-11-24 06:11:04 +08:00
|
|
|
assert_less,
|
|
|
|
|
assert_greater,
|
2014-06-30 09:02:54 +08:00
|
|
|
assert_less_equal,
|
|
|
|
|
assert_greater_equal,
|
2013-05-28 19:48:57 +08:00
|
|
|
assert_warns,
|
|
|
|
|
assert_no_warnings,
|
2012-12-21 18:46:14 +08:00
|
|
|
assert_equal,
|
|
|
|
|
set_random_state,
|
2016-04-21 00:28:36 +08:00
|
|
|
assert_raise_message,
|
2017-07-12 00:42:10 +08:00
|
|
|
ignore_warnings,
|
|
|
|
|
check_docstring_parameters,
|
2017-08-17 07:05:24 +08:00
|
|
|
assert_allclose_dense_sparse,
|
2018-04-23 20:58:40 +08:00
|
|
|
assert_raises_regex,
|
|
|
|
|
TempMemmap,
|
|
|
|
|
create_memmap_backed_data,
|
|
|
|
|
_delete_folder)
|
2012-12-21 18:46:14 +08:00
|
|
|
|
2017-07-12 00:42:10 +08:00
|
|
|
from sklearn.utils.testing import SkipTest
|
2012-11-02 17:23:15 +08:00
|
|
|
from sklearn.tree import DecisionTreeClassifier
|
2015-03-20 11:11:33 +08:00
|
|
|
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
|
2012-05-06 23:10:55 +08:00
|
|
|
|
|
|
|
|
|
2016-11-24 06:11:04 +08:00
|
|
|
def test_assert_less():
|
|
|
|
|
assert_less(0, 1)
|
|
|
|
|
assert_raises(AssertionError, assert_less, 1, 0)
|
2012-05-06 23:10:55 +08:00
|
|
|
|
|
|
|
|
|
2016-11-24 06:11:04 +08:00
|
|
|
def test_assert_greater():
|
|
|
|
|
assert_greater(1, 0)
|
|
|
|
|
assert_raises(AssertionError, assert_greater, 0, 1)
|
2012-11-02 17:23:15 +08:00
|
|
|
|
|
|
|
|
|
2014-06-30 09:02:54 +08:00
|
|
|
def test_assert_less_equal():
|
|
|
|
|
assert_less_equal(0, 1)
|
|
|
|
|
assert_less_equal(1, 1)
|
|
|
|
|
assert_raises(AssertionError, assert_less_equal, 1, 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_assert_greater_equal():
|
|
|
|
|
assert_greater_equal(1, 0)
|
|
|
|
|
assert_greater_equal(1, 1)
|
|
|
|
|
assert_raises(AssertionError, assert_greater_equal, 0, 1)
|
|
|
|
|
|
|
|
|
|
|
2012-11-02 17:23:15 +08:00
|
|
|
def test_set_random_state():
|
2015-03-20 11:11:33 +08:00
|
|
|
lda = LinearDiscriminantAnalysis()
|
2012-11-02 17:23:15 +08:00
|
|
|
tree = DecisionTreeClassifier()
|
2015-03-20 11:11:33 +08:00
|
|
|
# Linear Discriminant Analysis doesn't have random state: smoke test
|
2012-11-02 17:23:15 +08:00
|
|
|
set_random_state(lda, 3)
|
|
|
|
|
set_random_state(tree, 3)
|
|
|
|
|
assert_equal(tree.random_state, 3)
|
2012-12-21 18:46:14 +08:00
|
|
|
|
|
|
|
|
|
2017-06-09 22:33:57 +08:00
|
|
|
def test_assert_allclose_dense_sparse():
|
|
|
|
|
x = np.arange(9).reshape(3, 3)
|
|
|
|
|
msg = "Not equal to tolerance "
|
|
|
|
|
y = sparse.csc_matrix(x)
|
|
|
|
|
for X in [x, y]:
|
|
|
|
|
# basic compare
|
|
|
|
|
assert_raise_message(AssertionError, msg, assert_allclose_dense_sparse,
|
|
|
|
|
X, X * 2)
|
|
|
|
|
assert_allclose_dense_sparse(X, X)
|
|
|
|
|
|
|
|
|
|
assert_raise_message(ValueError, "Can only compare two sparse",
|
|
|
|
|
assert_allclose_dense_sparse, x, y)
|
|
|
|
|
|
|
|
|
|
A = sparse.diags(np.ones(5), offsets=0).tocsr()
|
|
|
|
|
B = sparse.csr_matrix(np.ones((1, 5)))
|
|
|
|
|
|
|
|
|
|
assert_raise_message(AssertionError, "Arrays are not equal",
|
|
|
|
|
assert_allclose_dense_sparse, B, A)
|
|
|
|
|
|
|
|
|
|
|
2017-08-17 07:05:24 +08:00
|
|
|
def test_assert_raises_msg():
|
|
|
|
|
with assert_raises_regex(AssertionError, 'Hello world'):
|
|
|
|
|
with assert_raises(ValueError, msg='Hello world'):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2012-12-21 18:46:14 +08:00
|
|
|
def test_assert_raise_message():
|
|
|
|
|
def _raise_ValueError(message):
|
|
|
|
|
raise ValueError(message)
|
|
|
|
|
|
2015-05-13 01:56:42 +08:00
|
|
|
def _no_raise():
|
|
|
|
|
pass
|
|
|
|
|
|
2012-12-21 18:46:14 +08:00
|
|
|
assert_raise_message(ValueError, "test",
|
|
|
|
|
_raise_ValueError, "test")
|
|
|
|
|
|
|
|
|
|
assert_raises(AssertionError,
|
|
|
|
|
assert_raise_message, ValueError, "something else",
|
|
|
|
|
_raise_ValueError, "test")
|
|
|
|
|
|
|
|
|
|
assert_raises(ValueError,
|
|
|
|
|
assert_raise_message, TypeError, "something else",
|
|
|
|
|
_raise_ValueError, "test")
|
2013-05-28 19:48:57 +08:00
|
|
|
|
2015-05-13 01:56:42 +08:00
|
|
|
assert_raises(AssertionError,
|
|
|
|
|
assert_raise_message, ValueError, "test",
|
|
|
|
|
_no_raise)
|
|
|
|
|
|
|
|
|
|
# multiple exceptions in a tuple
|
|
|
|
|
assert_raises(AssertionError,
|
|
|
|
|
assert_raise_message, (ValueError, AttributeError),
|
|
|
|
|
"test", _no_raise)
|
|
|
|
|
|
2013-05-28 19:48:57 +08:00
|
|
|
|
2016-04-21 00:28:36 +08:00
|
|
|
def test_ignore_warning():
|
|
|
|
|
# This check that ignore_warning decorateur and context manager are working
|
|
|
|
|
# as expected
|
|
|
|
|
def _warning_function():
|
|
|
|
|
warnings.warn("deprecation warning", DeprecationWarning)
|
|
|
|
|
|
|
|
|
|
def _multiple_warning_function():
|
|
|
|
|
warnings.warn("deprecation warning", DeprecationWarning)
|
|
|
|
|
warnings.warn("deprecation warning")
|
|
|
|
|
|
|
|
|
|
# Check the function directly
|
|
|
|
|
assert_no_warnings(ignore_warnings(_warning_function))
|
|
|
|
|
assert_no_warnings(ignore_warnings(_warning_function,
|
|
|
|
|
category=DeprecationWarning))
|
|
|
|
|
assert_warns(DeprecationWarning, ignore_warnings(_warning_function,
|
|
|
|
|
category=UserWarning))
|
|
|
|
|
assert_warns(UserWarning,
|
|
|
|
|
ignore_warnings(_multiple_warning_function,
|
|
|
|
|
category=DeprecationWarning))
|
|
|
|
|
assert_warns(DeprecationWarning,
|
|
|
|
|
ignore_warnings(_multiple_warning_function,
|
|
|
|
|
category=UserWarning))
|
|
|
|
|
assert_no_warnings(ignore_warnings(_warning_function,
|
|
|
|
|
category=(DeprecationWarning,
|
|
|
|
|
UserWarning)))
|
|
|
|
|
|
|
|
|
|
# Check the decorator
|
|
|
|
|
@ignore_warnings
|
|
|
|
|
def decorator_no_warning():
|
|
|
|
|
_warning_function()
|
|
|
|
|
_multiple_warning_function()
|
|
|
|
|
|
|
|
|
|
@ignore_warnings(category=(DeprecationWarning, UserWarning))
|
|
|
|
|
def decorator_no_warning_multiple():
|
|
|
|
|
_multiple_warning_function()
|
|
|
|
|
|
|
|
|
|
@ignore_warnings(category=DeprecationWarning)
|
|
|
|
|
def decorator_no_deprecation_warning():
|
|
|
|
|
_warning_function()
|
|
|
|
|
|
|
|
|
|
@ignore_warnings(category=UserWarning)
|
|
|
|
|
def decorator_no_user_warning():
|
|
|
|
|
_warning_function()
|
|
|
|
|
|
|
|
|
|
@ignore_warnings(category=DeprecationWarning)
|
|
|
|
|
def decorator_no_deprecation_multiple_warning():
|
|
|
|
|
_multiple_warning_function()
|
|
|
|
|
|
|
|
|
|
@ignore_warnings(category=UserWarning)
|
|
|
|
|
def decorator_no_user_multiple_warning():
|
|
|
|
|
_multiple_warning_function()
|
|
|
|
|
|
|
|
|
|
assert_no_warnings(decorator_no_warning)
|
|
|
|
|
assert_no_warnings(decorator_no_warning_multiple)
|
|
|
|
|
assert_no_warnings(decorator_no_deprecation_warning)
|
|
|
|
|
assert_warns(DeprecationWarning, decorator_no_user_warning)
|
|
|
|
|
assert_warns(UserWarning, decorator_no_deprecation_multiple_warning)
|
|
|
|
|
assert_warns(DeprecationWarning, decorator_no_user_multiple_warning)
|
|
|
|
|
|
|
|
|
|
# Check the context manager
|
|
|
|
|
def context_manager_no_warning():
|
|
|
|
|
with ignore_warnings():
|
|
|
|
|
_warning_function()
|
|
|
|
|
|
|
|
|
|
def context_manager_no_warning_multiple():
|
|
|
|
|
with ignore_warnings(category=(DeprecationWarning, UserWarning)):
|
|
|
|
|
_multiple_warning_function()
|
|
|
|
|
|
|
|
|
|
def context_manager_no_deprecation_warning():
|
|
|
|
|
with ignore_warnings(category=DeprecationWarning):
|
|
|
|
|
_warning_function()
|
|
|
|
|
|
|
|
|
|
def context_manager_no_user_warning():
|
|
|
|
|
with ignore_warnings(category=UserWarning):
|
|
|
|
|
_warning_function()
|
|
|
|
|
|
|
|
|
|
def context_manager_no_deprecation_multiple_warning():
|
|
|
|
|
with ignore_warnings(category=DeprecationWarning):
|
|
|
|
|
_multiple_warning_function()
|
|
|
|
|
|
|
|
|
|
def context_manager_no_user_multiple_warning():
|
|
|
|
|
with ignore_warnings(category=UserWarning):
|
|
|
|
|
_multiple_warning_function()
|
|
|
|
|
|
|
|
|
|
assert_no_warnings(context_manager_no_warning)
|
|
|
|
|
assert_no_warnings(context_manager_no_warning_multiple)
|
|
|
|
|
assert_no_warnings(context_manager_no_deprecation_warning)
|
|
|
|
|
assert_warns(DeprecationWarning, context_manager_no_user_warning)
|
|
|
|
|
assert_warns(UserWarning, context_manager_no_deprecation_multiple_warning)
|
|
|
|
|
assert_warns(DeprecationWarning, context_manager_no_user_multiple_warning)
|
|
|
|
|
|
2018-07-18 04:50:32 +08:00
|
|
|
# Check that passing warning class as first positional argument
|
|
|
|
|
warning_class = UserWarning
|
|
|
|
|
match = "'obj' should be a callable.+you should use 'category=UserWarning'"
|
|
|
|
|
|
|
|
|
|
with pytest.raises(ValueError, match=match):
|
|
|
|
|
silence_warnings_func = ignore_warnings(warning_class)(
|
|
|
|
|
_warning_function)
|
|
|
|
|
silence_warnings_func()
|
|
|
|
|
|
|
|
|
|
with pytest.raises(ValueError, match=match):
|
|
|
|
|
@ignore_warnings(warning_class)
|
|
|
|
|
def test():
|
|
|
|
|
pass
|
|
|
|
|
|
2016-04-21 00:28:36 +08:00
|
|
|
|
2013-05-28 19:48:57 +08:00
|
|
|
class TestWarns(unittest.TestCase):
|
|
|
|
|
def test_warn(self):
|
|
|
|
|
def f():
|
|
|
|
|
warnings.warn("yo")
|
|
|
|
|
return 3
|
|
|
|
|
|
2018-06-11 14:59:12 +08:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
|
warnings.simplefilter("ignore", UserWarning)
|
|
|
|
|
filters_orig = warnings.filters[:]
|
|
|
|
|
assert_equal(assert_warns(UserWarning, f), 3)
|
|
|
|
|
# test that assert_warns doesn't have side effects on warnings
|
|
|
|
|
# filters
|
|
|
|
|
assert_equal(warnings.filters, filters_orig)
|
2014-10-13 20:50:45 +08:00
|
|
|
|
2013-05-28 19:48:57 +08:00
|
|
|
assert_raises(AssertionError, assert_no_warnings, f)
|
|
|
|
|
assert_equal(assert_no_warnings(lambda x: x, 1), 1)
|
|
|
|
|
|
|
|
|
|
def test_warn_wrong_warning(self):
|
|
|
|
|
def f():
|
|
|
|
|
warnings.warn("yo", DeprecationWarning)
|
|
|
|
|
|
|
|
|
|
failed = False
|
|
|
|
|
filters = sys.modules['warnings'].filters[:]
|
|
|
|
|
try:
|
|
|
|
|
try:
|
|
|
|
|
# Should raise an AssertionError
|
|
|
|
|
assert_warns(UserWarning, f)
|
|
|
|
|
failed = True
|
|
|
|
|
except AssertionError:
|
|
|
|
|
pass
|
|
|
|
|
finally:
|
|
|
|
|
sys.modules['warnings'].filters = filters
|
|
|
|
|
|
|
|
|
|
if failed:
|
|
|
|
|
raise AssertionError("wrong warning caught by assert_warn")
|
2017-07-12 00:42:10 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# Tests for docstrings:
|
|
|
|
|
|
|
|
|
|
def f_ok(a, b):
|
|
|
|
|
"""Function f
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
a : int
|
|
|
|
|
Parameter a
|
|
|
|
|
b : float
|
|
|
|
|
Parameter b
|
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
-------
|
|
|
|
|
c : list
|
|
|
|
|
Parameter c
|
|
|
|
|
"""
|
|
|
|
|
c = a + b
|
|
|
|
|
return c
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def f_bad_sections(a, b):
|
|
|
|
|
"""Function f
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
a : int
|
|
|
|
|
Parameter a
|
|
|
|
|
b : float
|
|
|
|
|
Parameter b
|
|
|
|
|
|
|
|
|
|
Results
|
|
|
|
|
-------
|
|
|
|
|
c : list
|
|
|
|
|
Parameter c
|
|
|
|
|
"""
|
|
|
|
|
c = a + b
|
|
|
|
|
return c
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def f_bad_order(b, a):
|
|
|
|
|
"""Function f
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
a : int
|
|
|
|
|
Parameter a
|
|
|
|
|
b : float
|
|
|
|
|
Parameter b
|
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
-------
|
|
|
|
|
c : list
|
|
|
|
|
Parameter c
|
|
|
|
|
"""
|
|
|
|
|
c = a + b
|
|
|
|
|
return c
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def f_missing(a, b):
|
|
|
|
|
"""Function f
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
a : int
|
|
|
|
|
Parameter a
|
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
-------
|
|
|
|
|
c : list
|
|
|
|
|
Parameter c
|
|
|
|
|
"""
|
|
|
|
|
c = a + b
|
|
|
|
|
return c
|
|
|
|
|
|
|
|
|
|
|
2018-02-11 00:47:42 +08:00
|
|
|
def f_check_param_definition(a, b, c, d, e):
|
2017-07-12 00:42:10 +08:00
|
|
|
"""Function f
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
a: int
|
|
|
|
|
Parameter a
|
|
|
|
|
b:
|
|
|
|
|
Parameter b
|
|
|
|
|
c :
|
|
|
|
|
Parameter c
|
|
|
|
|
d:int
|
|
|
|
|
Parameter d
|
2018-02-11 00:47:42 +08:00
|
|
|
e
|
|
|
|
|
No typespec is allowed without colon
|
2017-07-12 00:42:10 +08:00
|
|
|
"""
|
|
|
|
|
return a + b + c + d
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Klass(object):
|
|
|
|
|
def f_missing(self, X, y):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def f_bad_sections(self, X, y):
|
|
|
|
|
"""Function f
|
|
|
|
|
|
|
|
|
|
Parameter
|
|
|
|
|
----------
|
|
|
|
|
a : int
|
|
|
|
|
Parameter a
|
|
|
|
|
b : float
|
|
|
|
|
Parameter b
|
|
|
|
|
|
|
|
|
|
Results
|
|
|
|
|
-------
|
|
|
|
|
c : list
|
|
|
|
|
Parameter c
|
|
|
|
|
"""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MockEst(object):
|
|
|
|
|
def __init__(self):
|
|
|
|
|
"""MockEstimator"""
|
|
|
|
|
def fit(self, X, y):
|
|
|
|
|
return X
|
|
|
|
|
|
|
|
|
|
def predict(self, X):
|
|
|
|
|
return X
|
|
|
|
|
|
|
|
|
|
def predict_proba(self, X):
|
|
|
|
|
return X
|
|
|
|
|
|
|
|
|
|
def score(self, X):
|
|
|
|
|
return 1.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MockMetaEstimator(object):
|
|
|
|
|
def __init__(self, delegate):
|
|
|
|
|
"""MetaEstimator to check if doctest on delegated methods work.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
---------
|
|
|
|
|
delegate : estimator
|
|
|
|
|
Delegated estimator.
|
|
|
|
|
"""
|
|
|
|
|
self.delegate = delegate
|
|
|
|
|
|
|
|
|
|
@if_delegate_has_method(delegate=('delegate'))
|
|
|
|
|
def predict(self, X):
|
|
|
|
|
"""This is available only if delegate has predict.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
y : ndarray
|
|
|
|
|
Parameter y
|
|
|
|
|
"""
|
|
|
|
|
return self.delegate.predict(X)
|
|
|
|
|
|
|
|
|
|
@if_delegate_has_method(delegate=('delegate'))
|
2018-02-11 00:47:42 +08:00
|
|
|
@deprecated("Testing a deprecated delegated method")
|
2017-07-12 00:42:10 +08:00
|
|
|
def score(self, X):
|
|
|
|
|
"""This is available only if delegate has score.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
---------
|
|
|
|
|
y : ndarray
|
|
|
|
|
Parameter y
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
@if_delegate_has_method(delegate=('delegate'))
|
|
|
|
|
def predict_proba(self, X):
|
|
|
|
|
"""This is available only if delegate has predict_proba.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
---------
|
|
|
|
|
X : ndarray
|
|
|
|
|
Parameter X
|
|
|
|
|
"""
|
|
|
|
|
return X
|
|
|
|
|
|
|
|
|
|
@deprecated('Testing deprecated function with wrong params')
|
|
|
|
|
def fit(self, X, y):
|
|
|
|
|
"""Incorrect docstring but should not be tested"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_check_docstring_parameters():
|
|
|
|
|
try:
|
|
|
|
|
import numpydoc # noqa
|
|
|
|
|
assert sys.version_info >= (3, 5)
|
|
|
|
|
except (ImportError, AssertionError):
|
|
|
|
|
raise SkipTest(
|
|
|
|
|
"numpydoc is required to test the docstrings")
|
|
|
|
|
|
|
|
|
|
incorrect = check_docstring_parameters(f_ok)
|
2018-02-11 00:47:42 +08:00
|
|
|
assert incorrect == []
|
2017-07-12 00:42:10 +08:00
|
|
|
incorrect = check_docstring_parameters(f_ok, ignore=['b'])
|
2018-02-11 00:47:42 +08:00
|
|
|
assert incorrect == []
|
2017-07-12 00:42:10 +08:00
|
|
|
incorrect = check_docstring_parameters(f_missing, ignore=['b'])
|
2018-02-11 00:47:42 +08:00
|
|
|
assert incorrect == []
|
2017-07-12 00:42:10 +08:00
|
|
|
assert_raise_message(RuntimeError, 'Unknown section Results',
|
|
|
|
|
check_docstring_parameters, f_bad_sections)
|
|
|
|
|
assert_raise_message(RuntimeError, 'Unknown section Parameter',
|
|
|
|
|
check_docstring_parameters, Klass.f_bad_sections)
|
|
|
|
|
|
2018-02-11 00:47:42 +08:00
|
|
|
incorrect = check_docstring_parameters(f_check_param_definition)
|
|
|
|
|
assert (
|
|
|
|
|
incorrect == [
|
|
|
|
|
"sklearn.utils.tests.test_testing.f_check_param_definition There "
|
|
|
|
|
"was no space between the param name and colon ('a: int')",
|
|
|
|
|
"sklearn.utils.tests.test_testing.f_check_param_definition There "
|
|
|
|
|
"was no space between the param name and colon ('b:')",
|
|
|
|
|
"sklearn.utils.tests.test_testing.f_check_param_definition "
|
|
|
|
|
"Parameter 'c :' has an empty type spec. Remove the colon",
|
|
|
|
|
"sklearn.utils.tests.test_testing.f_check_param_definition There "
|
|
|
|
|
"was no space between the param name and colon ('d:int')",
|
|
|
|
|
])
|
|
|
|
|
|
2017-07-12 00:42:10 +08:00
|
|
|
messages = ["a != b", "arg mismatch: ['b']", "arg mismatch: ['X', 'y']",
|
|
|
|
|
"predict y != X",
|
|
|
|
|
"predict_proba arg mismatch: ['X']",
|
|
|
|
|
"score arg mismatch: ['X']",
|
|
|
|
|
".fit arg mismatch: ['X', 'y']"]
|
|
|
|
|
|
|
|
|
|
mock_meta = MockMetaEstimator(delegate=MockEst())
|
|
|
|
|
|
|
|
|
|
for mess, f in zip(messages,
|
|
|
|
|
[f_bad_order, f_missing, Klass.f_missing,
|
|
|
|
|
mock_meta.predict, mock_meta.predict_proba,
|
|
|
|
|
mock_meta.score, mock_meta.fit]):
|
|
|
|
|
incorrect = check_docstring_parameters(f)
|
2018-02-11 00:47:42 +08:00
|
|
|
assert len(incorrect) >= 1
|
|
|
|
|
assert mess in incorrect[0], '"%s" not in "%s"' % (mess, incorrect[0])
|
2018-04-23 20:58:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class RegistrationCounter(object):
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.nb_calls = 0
|
|
|
|
|
|
|
|
|
|
def __call__(self, to_register_func):
|
|
|
|
|
self.nb_calls += 1
|
|
|
|
|
assert to_register_func.func is _delete_folder
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_memmap(input_array, mmap_data, mmap_mode='r'):
|
|
|
|
|
assert isinstance(mmap_data, np.memmap)
|
|
|
|
|
writeable = mmap_mode != 'r'
|
|
|
|
|
assert mmap_data.flags.writeable is writeable
|
|
|
|
|
np.testing.assert_array_equal(input_array, mmap_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_tempmemmap(monkeypatch):
|
|
|
|
|
registration_counter = RegistrationCounter()
|
|
|
|
|
monkeypatch.setattr(atexit, 'register', registration_counter)
|
|
|
|
|
|
|
|
|
|
input_array = np.ones(3)
|
|
|
|
|
with TempMemmap(input_array) as data:
|
|
|
|
|
check_memmap(input_array, data)
|
|
|
|
|
temp_folder = os.path.dirname(data.filename)
|
|
|
|
|
if os.name != 'nt':
|
|
|
|
|
assert not os.path.exists(temp_folder)
|
|
|
|
|
assert registration_counter.nb_calls == 1
|
|
|
|
|
|
|
|
|
|
mmap_mode = 'r+'
|
|
|
|
|
with TempMemmap(input_array, mmap_mode=mmap_mode) as data:
|
|
|
|
|
check_memmap(input_array, data, mmap_mode=mmap_mode)
|
|
|
|
|
temp_folder = os.path.dirname(data.filename)
|
|
|
|
|
if os.name != 'nt':
|
|
|
|
|
assert not os.path.exists(temp_folder)
|
|
|
|
|
assert registration_counter.nb_calls == 2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_memmap_backed_data(monkeypatch):
|
|
|
|
|
registration_counter = RegistrationCounter()
|
|
|
|
|
monkeypatch.setattr(atexit, 'register', registration_counter)
|
|
|
|
|
|
|
|
|
|
input_array = np.ones(3)
|
|
|
|
|
data = create_memmap_backed_data(input_array)
|
|
|
|
|
check_memmap(input_array, data)
|
|
|
|
|
assert registration_counter.nb_calls == 1
|
|
|
|
|
|
|
|
|
|
data, folder = create_memmap_backed_data(input_array,
|
|
|
|
|
return_folder=True)
|
|
|
|
|
check_memmap(input_array, data)
|
|
|
|
|
assert folder == os.path.dirname(data.filename)
|
|
|
|
|
assert registration_counter.nb_calls == 2
|
|
|
|
|
|
|
|
|
|
mmap_mode = 'r+'
|
|
|
|
|
data = create_memmap_backed_data(input_array, mmap_mode=mmap_mode)
|
|
|
|
|
check_memmap(input_array, data, mmap_mode)
|
|
|
|
|
assert registration_counter.nb_calls == 3
|
|
|
|
|
|
|
|
|
|
input_list = [input_array, input_array + 1, input_array + 2]
|
|
|
|
|
mmap_data_list = create_memmap_backed_data(input_list)
|
|
|
|
|
for input_array, data in zip(input_list, mmap_data_list):
|
|
|
|
|
check_memmap(input_array, data)
|
|
|
|
|
assert registration_counter.nb_calls == 4
|