diff options
author | Sebastian Berg <sebastian@sipsolutions.net> | 2012-10-11 18:07:03 +0200 |
---|---|---|
committer | Sebastian Berg <sebastian@sipsolutions.net> | 2013-04-11 18:52:03 +0200 |
commit | 908e06c3c465434023649b0ca522836580c5cfdc (patch) | |
tree | 98ff9a320863f4fd7d861c2c217ecd0c314ef37f /numpy/lib/tests/test_function_base.py | |
parent | 79126f1c6084c56348b71e1b91fb4b6bc9de86b2 (diff) | |
download | numpy-908e06c3c465434023649b0ca522836580c5cfdc.tar.gz |
ENH: larger fixes for np.delete and np.insert functions
There were several smaller to larger problems for these two functions,
that this addresses:
* delete did not handle out of bound values graciously (ignoring negative
ones)
* both were unnecessarily slow due to use of sets
* insert did not handle unsorted indices correctly
Further changes:
* Add FutureWarning for boolean obj, so it can be handled similar to a
boolean mask with indexing.
* Add FutureWarning to remove inconsistent special cases for 0-d arrays
(neither insertion nor deletion along an axis make sense for a scalar)
* Allow insertion of an array with more then one element along axis when
obj is a sequence with a single item. (i.e. array([1])).
* Reintroduce speed optimization for scalar in insert that existed in 1.6.
Diffstat (limited to 'numpy/lib/tests/test_function_base.py')
-rw-r--r-- | numpy/lib/tests/test_function_base.py | 53 |
1 files changed, 51 insertions, 2 deletions
diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index 28786dc3e..b9b13a6a9 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -5,7 +5,7 @@ import numpy as np from numpy.testing import ( run_module_suite, TestCase, assert_, assert_equal, assert_array_equal, assert_almost_equal, assert_array_almost_equal, - assert_raises, assert_allclose, assert_array_max_ulp + assert_raises, assert_allclose, assert_array_max_ulp, assert_warns ) from numpy.random import rand from numpy.lib import * @@ -174,9 +174,14 @@ class TestInsert(TestCase): assert_equal(insert(a, 3, 1), [1, 2, 3, 1]) assert_equal(insert(a, [1, 1, 1], [1, 2, 3]), [1, 1, 2, 3, 2, 3]) assert_equal(insert(a, 1,[1,2,3]), [1, 1, 2, 3, 2, 3]) - assert_equal(insert(a,[1,2,3],9),[1,9,2,9,3,9]) + assert_equal(insert(a,[1,-1,3],9),[1,9,2,9,3,9]) + assert_equal(insert(a,slice(-1,None,-1), 9),[9,1,9,2,9,3]) + assert_warns(FutureWarning, insert, a, np.array([True]*4), 9) + #assert_equal(insert(a, np.array([True]*4), 9), [9,1,9,2,9,3,9]) + assert_equal(insert(a,[-1,1,3], [7,8,9]),[1,8,2,7,3,9]) b = np.array([0, 1], dtype=np.float64) assert_equal(insert(b, 0, b[0]), [0., 0., 1.]) + def test_multidim(self): a = [[1, 1, 1]] r = [[2, 2, 2], @@ -185,6 +190,17 @@ class TestInsert(TestCase): assert_equal(insert(a, 0, 2, axis=0), r) assert_equal(insert(a, 2, 2, axis=1), [[1, 1, 2, 1]]) + a = np.array([[1, 1], [2, 2], [3, 3]]) + b = np.arange(1,4).repeat(3).reshape(3,3) + c = np.concatenate((a[:,0:1], np.arange(1,4).repeat(3).reshape(3,3).T, + a[:,1:2]), axis=1) + assert_equal(insert(a, [1], [[1],[2],[3]], axis=1), b) + assert_equal(insert(a, [1], [1, 2, 3], axis=1), c) + # scalars behave differently, in this case exactly opposite: + assert_equal(insert(a, 1, [1, 2, 3], axis=1), b) + assert_equal(insert(a, 1, [[1],[2],[3]], axis=1), c) + + class TestAmax(TestCase): def test_basic(self): a = [3, 4, 5, 10, -3, -5, 6.0] @@ -302,6 +318,39 @@ class TestDiff(TestCase): assert_array_equal(diff(x, n=2, axis=0), out4) +class TestDelete(TestCase): + def setUp(self): + self.a = np.arange(5) + self.nd_a = np.arange(5).repeat(2).reshape(1,5,2) + + def _check_inverse_of_slicing(self, indices): + a_del = delete(self.a, indices) + assert_array_equal(setxor1d(a_del, self.a[indices,]), self.a) + nd_a_del = delete(self.nd_a, indices, axis=1) + xor = setxor1d(nd_a_del[0,:,0], self.nd_a[0,indices,0]) + assert_array_equal(xor, self.nd_a[0,:,0]) + + def test_slices(self): + lims = [-6, -2, 0, 1, 2, 4, 5] + steps = [-3, -1, 1, 3] + for start in lims: + for stop in lims: + for step in steps: + s = slice(start, stop, step) + self._check_inverse_of_slicing(s) + + def test_fancy(self): + self._check_inverse_of_slicing([0, -1, 2, 2]) + a = np.array([True, False, False], dtype=bool) + assert_warns(FutureWarning, delete, self.a, a) + #self._check_inverse_of_slicing(a) + self._check_inverse_of_slicing(np.array([[0,1],[2,1]])) + + def test_single(self): + self._check_inverse_of_slicing(0) + self._check_inverse_of_slicing(-4) + + class TestGradient(TestCase): def test_basic(self): v = [[1, 1], [3, 4]] |