diff options
| author | Johannes Schönberger <hannesschoenberger@gmail.com> | 2013-01-13 12:51:02 +0100 |
|---|---|---|
| committer | Johannes Schönberger <jschoenberger@demuc.de> | 2013-06-06 21:15:44 +0200 |
| commit | 474ec4858cd3b6a09cd8bec01c67759d95896cca (patch) | |
| tree | d619fa7943a1de04d377bc40a6d3c53227b9cbe7 /numpy | |
| parent | 27f0781e545b892f9393bad35fec149ed0718864 (diff) | |
| download | numpy-474ec4858cd3b6a09cd8bec01c67759d95896cca.tar.gz | |
Add tests for filled_like function
Diffstat (limited to 'numpy')
| -rw-r--r-- | numpy/core/numeric.py | 10 | ||||
| -rw-r--r-- | numpy/core/tests/test_numeric.py | 65 |
2 files changed, 54 insertions, 21 deletions
diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index 9a4146f9e..ce05b3a3f 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -223,12 +223,6 @@ def ones_like(a, dtype=None, order='K', subok=True): multiarray.copyto(res, 1, casting='unsafe') return res -def _check_dtype_nan(dtype): - if not issubdtype(dtype, 'float'): - raise ValueError('Invalid dtype because only floating point numbers ' - 'can represent non-numbers as defined in ' - 'IEEE 754-1985.') - def filled(shape, val, dtype=None, order='C'): """ Return a new array of given shape and type, filled with `val`. @@ -251,8 +245,6 @@ def filled(shape, val, dtype=None, order='C'): empty : Return a new uninitialized array. """ - - _check_dtype_nan(dtype) a = empty(shape, dtype, order) multiarray.copyto(a, val, casting='unsafe') return a @@ -292,8 +284,6 @@ def filled_like(a, val, dtype=None, order='K', subok=True): filled : Fill a new array. """ - - _check_dtype_nan(dtype) res = empty_like(a, dtype=dtype, order=order, subok=subok) multiarray.copyto(res, val, casting='unsafe') return res diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py index 5c8de3734..971bb94fb 100644 --- a/numpy/core/tests/test_numeric.py +++ b/numpy/core/tests/test_numeric.py @@ -1356,10 +1356,14 @@ class TestLikeFuncs(TestCase): (arange(24).reshape(4,3,2).swapaxes(0,1), '?'), ] - def check_like_function(self, like_function, value): + def check_like_function(self, like_function, value, fill_value=False): + if fill_value: + fill_kwarg = {'val': value} + else: + fill_kwarg = {} for d, dtype in self.data: # default (K) order, dtype - dz = like_function(d, dtype=dtype) + dz = like_function(d, dtype=dtype, **fill_kwarg) assert_equal(dz.shape, d.shape) assert_equal(array(dz.strides)*d.dtype.itemsize, array(d.strides)*dz.dtype.itemsize) @@ -1370,10 +1374,18 @@ class TestLikeFuncs(TestCase): else: assert_equal(dz.dtype, np.dtype(dtype)) if not value is None: - assert_(all(dz == value)) + if fill_value: + try: + z = dz.dtype.type(value) + except OverflowError: + pass + else: + assert_(all(dz == z)) + else: + assert_(all(dz == value)) # C order, default dtype - dz = like_function(d, order='C', dtype=dtype) + dz = like_function(d, order='C', dtype=dtype, **fill_kwarg) assert_equal(dz.shape, d.shape) assert_(dz.flags.c_contiguous) if dtype is None: @@ -1381,10 +1393,18 @@ class TestLikeFuncs(TestCase): else: assert_equal(dz.dtype, np.dtype(dtype)) if not value is None: - assert_(all(dz == value)) + if fill_value: + try: + z = dz.dtype.type(value) + except OverflowError: + pass + else: + assert_(all(dz == z)) + else: + assert_(all(dz == value)) # F order, default dtype - dz = like_function(d, order='F', dtype=dtype) + dz = like_function(d, order='F', dtype=dtype, **fill_kwarg) assert_equal(dz.shape, d.shape) assert_(dz.flags.f_contiguous) if dtype is None: @@ -1392,10 +1412,18 @@ class TestLikeFuncs(TestCase): else: assert_equal(dz.dtype, np.dtype(dtype)) if not value is None: - assert_(all(dz == value)) + if fill_value: + try: + z = dz.dtype.type(value) + except OverflowError: + pass + else: + assert_(all(dz == z)) + else: + assert_(all(dz == value)) # A order - dz = like_function(d, order='A', dtype=dtype) + dz = like_function(d, order='A', dtype=dtype, **fill_kwarg) assert_equal(dz.shape, d.shape) if d.flags.f_contiguous: assert_(dz.flags.f_contiguous) @@ -1406,15 +1434,23 @@ class TestLikeFuncs(TestCase): else: assert_equal(dz.dtype, np.dtype(dtype)) if not value is None: - assert_(all(dz == value)) + if fill_value: + try: + z = dz.dtype.type(value) + except OverflowError: + pass + else: + assert_(all(dz == z)) + else: + assert_(all(dz == value)) # Test the 'subok' parameter a = np.matrix([[1,2],[3,4]]) - b = like_function(a) + b = like_function(a, **fill_kwarg) assert_(type(b) is np.matrix) - b = like_function(a, subok=False) + b = like_function(a, subok=False, **fill_kwarg) assert_(type(b) is not np.matrix) def test_ones_like(self): @@ -1426,6 +1462,13 @@ class TestLikeFuncs(TestCase): def test_empty_like(self): self.check_like_function(np.empty_like, None) + def test_filled_like(self): + self.check_like_function(np.filled_like, 0, True) + self.check_like_function(np.filled_like, 1, True) + self.check_like_function(np.filled_like, 1000, True) + self.check_like_function(np.filled_like, 123.456, True) + self.check_like_function(np.filled_like, np.inf, True) + class _TestCorrelate(TestCase): def _setup(self, dt): self.x = np.array([1, 2, 3, 4, 5], dtype=dt) |
