2015-06-02 04:08:31 +08:00
|
|
|
from nose.tools import assert_equal
|
|
|
|
|
import numpy as np
|
|
|
|
|
|
2015-07-31 00:48:04 +08:00
|
|
|
from ..function_transformer import FunctionTransformer
|
2015-06-02 04:08:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def _make_func(args_store, kwargs_store, func=lambda X, *a, **k: X):
|
|
|
|
|
def _func(X, *args, **kwargs):
|
|
|
|
|
args_store.append(X)
|
|
|
|
|
args_store.extend(args)
|
|
|
|
|
kwargs_store.update(kwargs)
|
|
|
|
|
return func(X)
|
|
|
|
|
|
|
|
|
|
return _func
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_delegate_to_func():
|
|
|
|
|
# (args|kwargs)_store will hold the positional and keyword arguments
|
2015-07-31 00:48:04 +08:00
|
|
|
# passed to the function inside the FunctionTransformer.
|
2015-06-02 04:08:31 +08:00
|
|
|
args_store = []
|
|
|
|
|
kwargs_store = {}
|
|
|
|
|
X = np.arange(10).reshape((5, 2))
|
|
|
|
|
np.testing.assert_array_equal(
|
2015-07-31 00:48:04 +08:00
|
|
|
FunctionTransformer(_make_func(args_store, kwargs_store)).transform(X),
|
2015-06-02 04:08:31 +08:00
|
|
|
X,
|
|
|
|
|
'transform should have returned X unchanged',
|
|
|
|
|
)
|
|
|
|
|
|
2015-07-31 00:48:04 +08:00
|
|
|
# The function should only have recieved X.
|
2015-06-02 04:08:31 +08:00
|
|
|
assert_equal(
|
|
|
|
|
args_store,
|
2015-07-31 00:48:04 +08:00
|
|
|
[X],
|
2015-06-02 04:08:31 +08:00
|
|
|
'Incorrect positional arguments passed to func: {args}'.format(
|
|
|
|
|
args=args_store,
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
assert_equal(
|
|
|
|
|
kwargs_store,
|
|
|
|
|
{},
|
|
|
|
|
'Unexpected keyword arguments passed to func: {args}'.format(
|
|
|
|
|
args=kwargs_store,
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
2015-07-31 00:48:04 +08:00
|
|
|
# reset the argument stores.
|
2015-06-09 00:53:25 +08:00
|
|
|
args_store[:] = [] # python2 compatible inplace list clear.
|
2015-07-31 00:48:04 +08:00
|
|
|
kwargs_store.clear()
|
|
|
|
|
y = object()
|
2015-06-02 04:08:31 +08:00
|
|
|
|
|
|
|
|
np.testing.assert_array_equal(
|
2015-07-31 00:48:04 +08:00
|
|
|
FunctionTransformer(
|
2015-06-02 04:08:31 +08:00
|
|
|
_make_func(args_store, kwargs_store),
|
2015-07-31 00:48:04 +08:00
|
|
|
pass_y=True,
|
|
|
|
|
).transform(X, y),
|
2015-06-02 04:08:31 +08:00
|
|
|
X,
|
|
|
|
|
'transform should have returned X unchanged',
|
|
|
|
|
)
|
|
|
|
|
|
2015-07-31 00:48:04 +08:00
|
|
|
# The function should have recieved X and y.
|
2015-06-02 04:08:31 +08:00
|
|
|
assert_equal(
|
|
|
|
|
args_store,
|
2015-07-31 00:48:04 +08:00
|
|
|
[X, y],
|
2015-06-02 04:08:31 +08:00
|
|
|
'Incorrect positional arguments passed to func: {args}'.format(
|
|
|
|
|
args=args_store,
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
assert_equal(
|
|
|
|
|
kwargs_store,
|
2015-07-31 00:48:04 +08:00
|
|
|
{},
|
|
|
|
|
'Unexpected keyword arguments passed to func: {args}'.format(
|
2015-06-02 04:08:31 +08:00
|
|
|
args=kwargs_store,
|
|
|
|
|
),
|
|
|
|
|
)
|
2015-07-31 00:48:04 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_np_log():
|
|
|
|
|
X = np.arange(10).reshape((5, 2))
|
|
|
|
|
|
|
|
|
|
# Test that the numpy.log example still works.
|
|
|
|
|
np.testing.assert_array_equal(
|
|
|
|
|
FunctionTransformer(np.log).transform(X),
|
|
|
|
|
np.log(X),
|
|
|
|
|
)
|