From bcaabe1da3d2f7905b287d430f00c46c8d75209c Mon Sep 17 00:00:00 2001 From: MilesCranmer Date: Wed, 3 Oct 2018 12:04:16 -0400 Subject: TST: add tests for in1d/isin - These tests hit the areas of the old algorithm which are now replaced for the majority of cases - Another test tests behaviour of boolean input to isin/in1d --- numpy/lib/tests/test_arraysetops.py | 99 +++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) (limited to 'numpy/lib/tests/test_arraysetops.py') diff --git a/numpy/lib/tests/test_arraysetops.py b/numpy/lib/tests/test_arraysetops.py index e64634b69..707c84101 100644 --- a/numpy/lib/tests/test_arraysetops.py +++ b/numpy/lib/tests/test_arraysetops.py @@ -296,6 +296,75 @@ class TestSetOps: assert_array_equal(in1d([], []), []) + def test_in1d_hit_alternate_algorithm_floats(self): + # Perform the above test but with floats + # This forces the old in1d (pre-Oct 1/2018) algorithm + for mult in (1, 10): + # One check without np.array to make sure lists are handled correct + a = [5, 7, 1, 2] + b = [2, 4, 3, 1, 5] * mult + ec = np.array([True, False, True, True]) + c = in1d(np.array(a, dtype=np.float32), + np.array(b, dtype=np.float32), assume_unique=True) + assert_array_equal(c, ec) + + a[0] = 8 + ec = np.array([False, False, True, True]) + c = in1d(np.array(a, dtype=np.float32), + np.array(b, dtype=np.float32), assume_unique=True) + assert_array_equal(c, ec) + + a[0], a[3] = 4, 8 + ec = np.array([True, False, True, False]) + c = in1d(np.array(a, dtype=np.float32), + np.array(b, dtype=np.float32), assume_unique=True) + assert_array_equal(c, ec) + + a = np.array([5, 4, 5, 3, 4, 4, 3, 4, 3, 5, 2, 1, 5, 5]) + b = [2, 3, 4] * mult + ec = [False, True, False, True, True, True, True, True, True, + False, True, False, False, False] + c = in1d(np.array(a, dtype=np.float32), + np.array(b, dtype=np.float32)) + assert_array_equal(c, ec) + + b = b + [5, 5, 4] * mult + ec = [True, True, True, True, True, True, True, True, True, True, + True, False, True, True] + c = in1d(np.array(a, dtype=np.float32), + np.array(b, dtype=np.float32)) + assert_array_equal(c, ec) + + a = np.array([5, 7, 1, 2]) + b = np.array([2, 4, 3, 1, 5] * mult) + ec = np.array([True, False, True, True]) + c = in1d(np.array(a, dtype=np.float32), + np.array(b, dtype=np.float32)) + assert_array_equal(c, ec) + + a = np.array([5, 7, 1, 1, 2]) + b = np.array([2, 4, 3, 3, 1, 5] * mult) + ec = np.array([True, False, True, True, True]) + c = in1d(np.array(a, dtype=np.float32), + np.array(b, dtype=np.float32)) + assert_array_equal(c, ec) + + a = np.array([5, 5]) + b = np.array([2, 2] * mult) + ec = np.array([False, False]) + c = in1d(np.array(a, dtype=np.float32), + np.array(b, dtype=np.float32)) + assert_array_equal(c, ec) + + a = np.array([5]) + b = np.array([2]) + ec = np.array([False]) + c = in1d(np.array(a, dtype=np.float32), + np.array(b, dtype=np.float32)) + assert_array_equal(c, ec) + + assert_array_equal(in1d([], []), []) + def test_in1d_char_array(self): a = np.array(['a', 'b', 'c', 'd', 'e', 'c', 'e', 'b']) b = np.array(['a', 'c']) @@ -314,6 +383,13 @@ class TestSetOps: b = [2, 3, 4] * mult assert_array_equal(np.invert(in1d(a, b)), in1d(a, b, invert=True)) + for mult in (1, 10): + a = np.array([5, 4, 5, 3, 4, 4, 3, 4, 3, 5, 2, 1, 5, 5], + dtype=np.float32) + b = [2, 3, 4] * mult + b = np.array(b, dtype=np.float32) + assert_array_equal(np.invert(in1d(a, b)), in1d(a, b, invert=True)) + def test_in1d_ravel(self): # Test that in1d ravels its input arrays. This is not documented # behavior however. The test is to ensure consistentency. @@ -327,6 +403,29 @@ class TestSetOps: assert_array_equal(in1d(a, long_b, assume_unique=True), ec) assert_array_equal(in1d(a, long_b, assume_unique=False), ec) + def test_in1d_hit_alternate_algorithm(self): + """Hit the standard isin code with integers""" + # Need extreme range to hit standard code + a = np.array([5, 4, 5, 3, 4, 4, 1e9], dtype=np.int64) + b = np.array([2, 3, 4, 1e9], dtype=np.int64) + expected = np.array([0, 1, 0, 1, 1, 1, 1], dtype=np.bool) + assert_array_equal(expected, in1d(a, b)) + assert_array_equal(np.invert(expected), in1d(a, b, invert=True)) + + a = np.array([5, 7, 1, 2], dtype=np.int64) + b = np.array([2, 4, 3, 1, 5, 1e9], dtype=np.int64) + ec = np.array([True, False, True, True]) + c = in1d(a, b, assume_unique=True) + assert_array_equal(c, ec) + + def test_in1d_boolean(self): + """Test that in1d works for boolean input""" + a = np.array([True, False]) + b = np.array([False, False, False]) + expected = np.array([False, True]) + assert_array_equal(expected, in1d(a, b)) + assert_array_equal(np.invert(expected), in1d(a, b, invert=True)) + def test_in1d_first_array_is_object(self): ar1 = [None] ar2 = np.array([1]*10) -- cgit v1.2.1 From d6437066f27f81c4a78fb377ef1c61b4969f8159 Mon Sep 17 00:00:00 2001 From: MilesCranmer Date: Wed, 26 Dec 2018 19:49:54 -0500 Subject: TST: Extend np.in1d tests to old algorithm - Add flag ``_slow_integer`` to np.isin/np.in1d to force the use of the old isin/in1d algorithm for integers. --- numpy/lib/tests/test_arraysetops.py | 44 +++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) (limited to 'numpy/lib/tests/test_arraysetops.py') diff --git a/numpy/lib/tests/test_arraysetops.py b/numpy/lib/tests/test_arraysetops.py index 707c84101..8de1cee6f 100644 --- a/numpy/lib/tests/test_arraysetops.py +++ b/numpy/lib/tests/test_arraysetops.py @@ -204,8 +204,8 @@ class TestSetOps: return a in b isin_slow = np.vectorize(_isin_slow, otypes=[bool], excluded={1}) - def assert_isin_equal(a, b): - x = isin(a, b) + def assert_isin_equal(a, b, old_algorithm=None): + x = isin(a, b, _slow_integer=old_algorithm) y = isin_slow(a, b) assert_array_equal(x, y) @@ -213,28 +213,39 @@ class TestSetOps: a = np.arange(24).reshape([2, 3, 4]) b = np.array([[10, 20, 30], [0, 1, 3], [11, 22, 33]]) assert_isin_equal(a, b) + assert_isin_equal(a, b, old_algorithm=True) # array-likes as both arguments c = [(9, 8), (7, 6)] d = (9, 7) assert_isin_equal(c, d) + assert_isin_equal(c, d, old_algorithm=True) # zero-d array: f = np.array(3) assert_isin_equal(f, b) assert_isin_equal(a, f) assert_isin_equal(f, f) + assert_isin_equal(f, b, old_algorithm=True) + assert_isin_equal(a, f, old_algorithm=True) + assert_isin_equal(f, f, old_algorithm=True) # scalar: assert_isin_equal(5, b) assert_isin_equal(a, 6) assert_isin_equal(5, 6) + assert_isin_equal(5, b, old_algorithm=True) + assert_isin_equal(a, 6, old_algorithm=True) + assert_isin_equal(5, 6, old_algorithm=True) # empty array-like: x = [] assert_isin_equal(x, b) assert_isin_equal(a, x) assert_isin_equal(x, x) + assert_isin_equal(x, b, old_algorithm=True) + assert_isin_equal(a, x, old_algorithm=True) + assert_isin_equal(x, x, old_algorithm=True) def test_in1d(self): # we use two different sizes for the b array here to test the @@ -246,16 +257,22 @@ class TestSetOps: ec = np.array([True, False, True, True]) c = in1d(a, b, assume_unique=True) assert_array_equal(c, ec) + c = in1d(a, b, assume_unique=True, _slow_integer=True) + assert_array_equal(c, ec) a[0] = 8 ec = np.array([False, False, True, True]) c = in1d(a, b, assume_unique=True) assert_array_equal(c, ec) + c = in1d(a, b, assume_unique=True, _slow_integer=True) + assert_array_equal(c, ec) a[0], a[3] = 4, 8 ec = np.array([True, False, True, False]) c = in1d(a, b, assume_unique=True) assert_array_equal(c, ec) + c = in1d(a, b, assume_unique=True, _slow_integer=True) + assert_array_equal(c, ec) a = np.array([5, 4, 5, 3, 4, 4, 3, 4, 3, 5, 2, 1, 5, 5]) b = [2, 3, 4] * mult @@ -263,36 +280,48 @@ class TestSetOps: False, True, False, False, False] c = in1d(a, b) assert_array_equal(c, ec) + c = in1d(a, b, _slow_integer=True) + assert_array_equal(c, ec) b = b + [5, 5, 4] * mult ec = [True, True, True, True, True, True, True, True, True, True, True, False, True, True] c = in1d(a, b) assert_array_equal(c, ec) + c = in1d(a, b, _slow_integer=True) + assert_array_equal(c, ec) a = np.array([5, 7, 1, 2]) b = np.array([2, 4, 3, 1, 5] * mult) ec = np.array([True, False, True, True]) c = in1d(a, b) assert_array_equal(c, ec) + c = in1d(a, b, _slow_integer=True) + assert_array_equal(c, ec) a = np.array([5, 7, 1, 1, 2]) b = np.array([2, 4, 3, 3, 1, 5] * mult) ec = np.array([True, False, True, True, True]) c = in1d(a, b) assert_array_equal(c, ec) + c = in1d(a, b, _slow_integer=True) + assert_array_equal(c, ec) a = np.array([5, 5]) b = np.array([2, 2] * mult) ec = np.array([False, False]) c = in1d(a, b) assert_array_equal(c, ec) + c = in1d(a, b, _slow_integer=True) + assert_array_equal(c, ec) a = np.array([5]) b = np.array([2]) ec = np.array([False]) c = in1d(a, b) assert_array_equal(c, ec) + c = in1d(a, b, _slow_integer=True) + assert_array_equal(c, ec) assert_array_equal(in1d([], []), []) @@ -382,6 +411,8 @@ class TestSetOps: a = np.array([5, 4, 5, 3, 4, 4, 3, 4, 3, 5, 2, 1, 5, 5]) b = [2, 3, 4] * mult assert_array_equal(np.invert(in1d(a, b)), in1d(a, b, invert=True)) + assert_array_equal(np.invert(in1d(a, b)), + in1d(a, b, invert=True, _slow_integer=True)) for mult in (1, 10): a = np.array([5, 4, 5, 3, 4, 4, 3, 4, 3, 5, 2, 1, 5, 5], @@ -389,6 +420,8 @@ class TestSetOps: b = [2, 3, 4] * mult b = np.array(b, dtype=np.float32) assert_array_equal(np.invert(in1d(a, b)), in1d(a, b, invert=True)) + assert_array_equal(np.invert(in1d(a, b)), + in1d(a, b, invert=True, _slow_integer=True)) def test_in1d_ravel(self): # Test that in1d ravels its input arrays. This is not documented @@ -402,10 +435,15 @@ class TestSetOps: assert_array_equal(in1d(a, b, assume_unique=False), ec) assert_array_equal(in1d(a, long_b, assume_unique=True), ec) assert_array_equal(in1d(a, long_b, assume_unique=False), ec) + assert_array_equal(in1d(a, b, assume_unique=True, _slow_integer=True), ec) + assert_array_equal(in1d(a, b, assume_unique=False, _slow_integer=True), ec) + assert_array_equal(in1d(a, long_b, assume_unique=True, _slow_integer=True), ec) + assert_array_equal(in1d(a, long_b, assume_unique=False, _slow_integer=True), ec) def test_in1d_hit_alternate_algorithm(self): """Hit the standard isin code with integers""" # Need extreme range to hit standard code + # This hits it without the use of _slow_integer a = np.array([5, 4, 5, 3, 4, 4, 1e9], dtype=np.int64) b = np.array([2, 3, 4, 1e9], dtype=np.int64) expected = np.array([0, 1, 0, 1, 1, 1, 1], dtype=np.bool) @@ -424,7 +462,9 @@ class TestSetOps: b = np.array([False, False, False]) expected = np.array([False, True]) assert_array_equal(expected, in1d(a, b)) + assert_array_equal(expected, in1d(a, b, _slow_integer=True)) assert_array_equal(np.invert(expected), in1d(a, b, invert=True)) + assert_array_equal(np.invert(expected), in1d(a, b, invert=True, _slow_integer=True)) def test_in1d_first_array_is_object(self): ar1 = [None] -- cgit v1.2.1 From 179d1575ebdf850add391db87da6b1f4d3209e3c Mon Sep 17 00:00:00 2001 From: MilesCranmer Date: Thu, 9 Jun 2022 20:48:45 -0400 Subject: MAINT: Fix linting errors in in1d tests --- numpy/lib/tests/test_arraysetops.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) (limited to 'numpy/lib/tests/test_arraysetops.py') diff --git a/numpy/lib/tests/test_arraysetops.py b/numpy/lib/tests/test_arraysetops.py index 8de1cee6f..60d9b7c87 100644 --- a/numpy/lib/tests/test_arraysetops.py +++ b/numpy/lib/tests/test_arraysetops.py @@ -435,10 +435,16 @@ class TestSetOps: assert_array_equal(in1d(a, b, assume_unique=False), ec) assert_array_equal(in1d(a, long_b, assume_unique=True), ec) assert_array_equal(in1d(a, long_b, assume_unique=False), ec) - assert_array_equal(in1d(a, b, assume_unique=True, _slow_integer=True), ec) - assert_array_equal(in1d(a, b, assume_unique=False, _slow_integer=True), ec) - assert_array_equal(in1d(a, long_b, assume_unique=True, _slow_integer=True), ec) - assert_array_equal(in1d(a, long_b, assume_unique=False, _slow_integer=True), ec) + assert_array_equal(in1d(a, b, assume_unique=True, _slow_integer=True), + ec) + assert_array_equal(in1d(a, b, assume_unique=False, _slow_integer=True), + ec) + assert_array_equal(in1d(a, long_b, assume_unique=True, + _slow_integer=True), + ec) + assert_array_equal(in1d(a, long_b, assume_unique=False, + _slow_integer=True), + ec) def test_in1d_hit_alternate_algorithm(self): """Hit the standard isin code with integers""" @@ -461,10 +467,14 @@ class TestSetOps: a = np.array([True, False]) b = np.array([False, False, False]) expected = np.array([False, True]) - assert_array_equal(expected, in1d(a, b)) - assert_array_equal(expected, in1d(a, b, _slow_integer=True)) - assert_array_equal(np.invert(expected), in1d(a, b, invert=True)) - assert_array_equal(np.invert(expected), in1d(a, b, invert=True, _slow_integer=True)) + assert_array_equal(expected, + in1d(a, b)) + assert_array_equal(expected, + in1d(a, b, _slow_integer=True)) + assert_array_equal(np.invert(expected), + in1d(a, b, invert=True)) + assert_array_equal(np.invert(expected), + in1d(a, b, invert=True, _slow_integer=True)) def test_in1d_first_array_is_object(self): ar1 = [None] -- cgit v1.2.1 From d1a5309810cc55da8443568d215f1992c7a261cd Mon Sep 17 00:00:00 2001 From: MilesCranmer Date: Fri, 10 Jun 2022 14:40:25 -0400 Subject: MAINT: Update tests to use new `method` argument --- numpy/lib/tests/test_arraysetops.py | 39 +++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 19 deletions(-) (limited to 'numpy/lib/tests/test_arraysetops.py') diff --git a/numpy/lib/tests/test_arraysetops.py b/numpy/lib/tests/test_arraysetops.py index 60d9b7c87..affd2e2d6 100644 --- a/numpy/lib/tests/test_arraysetops.py +++ b/numpy/lib/tests/test_arraysetops.py @@ -205,7 +205,8 @@ class TestSetOps: isin_slow = np.vectorize(_isin_slow, otypes=[bool], excluded={1}) def assert_isin_equal(a, b, old_algorithm=None): - x = isin(a, b, _slow_integer=old_algorithm) + method = "sort" if old_algorithm else "dictionary" + x = isin(a, b, method=old_algorithm) y = isin_slow(a, b) assert_array_equal(x, y) @@ -257,21 +258,21 @@ class TestSetOps: ec = np.array([True, False, True, True]) c = in1d(a, b, assume_unique=True) assert_array_equal(c, ec) - c = in1d(a, b, assume_unique=True, _slow_integer=True) + c = in1d(a, b, assume_unique=True, method="dictionary") assert_array_equal(c, ec) a[0] = 8 ec = np.array([False, False, True, True]) c = in1d(a, b, assume_unique=True) assert_array_equal(c, ec) - c = in1d(a, b, assume_unique=True, _slow_integer=True) + c = in1d(a, b, assume_unique=True, method="dictionary") assert_array_equal(c, ec) a[0], a[3] = 4, 8 ec = np.array([True, False, True, False]) c = in1d(a, b, assume_unique=True) assert_array_equal(c, ec) - c = in1d(a, b, assume_unique=True, _slow_integer=True) + c = in1d(a, b, assume_unique=True, method="dictionary") assert_array_equal(c, ec) a = np.array([5, 4, 5, 3, 4, 4, 3, 4, 3, 5, 2, 1, 5, 5]) @@ -280,7 +281,7 @@ class TestSetOps: False, True, False, False, False] c = in1d(a, b) assert_array_equal(c, ec) - c = in1d(a, b, _slow_integer=True) + c = in1d(a, b, method="dictionary") assert_array_equal(c, ec) b = b + [5, 5, 4] * mult @@ -288,7 +289,7 @@ class TestSetOps: True, False, True, True] c = in1d(a, b) assert_array_equal(c, ec) - c = in1d(a, b, _slow_integer=True) + c = in1d(a, b, method="dictionary") assert_array_equal(c, ec) a = np.array([5, 7, 1, 2]) @@ -296,7 +297,7 @@ class TestSetOps: ec = np.array([True, False, True, True]) c = in1d(a, b) assert_array_equal(c, ec) - c = in1d(a, b, _slow_integer=True) + c = in1d(a, b, method="dictionary") assert_array_equal(c, ec) a = np.array([5, 7, 1, 1, 2]) @@ -304,7 +305,7 @@ class TestSetOps: ec = np.array([True, False, True, True, True]) c = in1d(a, b) assert_array_equal(c, ec) - c = in1d(a, b, _slow_integer=True) + c = in1d(a, b, method="dictionary") assert_array_equal(c, ec) a = np.array([5, 5]) @@ -312,7 +313,7 @@ class TestSetOps: ec = np.array([False, False]) c = in1d(a, b) assert_array_equal(c, ec) - c = in1d(a, b, _slow_integer=True) + c = in1d(a, b, method="dictionary") assert_array_equal(c, ec) a = np.array([5]) @@ -320,7 +321,7 @@ class TestSetOps: ec = np.array([False]) c = in1d(a, b) assert_array_equal(c, ec) - c = in1d(a, b, _slow_integer=True) + c = in1d(a, b, method="dictionary") assert_array_equal(c, ec) assert_array_equal(in1d([], []), []) @@ -412,7 +413,7 @@ class TestSetOps: b = [2, 3, 4] * mult assert_array_equal(np.invert(in1d(a, b)), in1d(a, b, invert=True)) assert_array_equal(np.invert(in1d(a, b)), - in1d(a, b, invert=True, _slow_integer=True)) + in1d(a, b, invert=True, method="dictionary")) for mult in (1, 10): a = np.array([5, 4, 5, 3, 4, 4, 3, 4, 3, 5, 2, 1, 5, 5], @@ -421,7 +422,7 @@ class TestSetOps: b = np.array(b, dtype=np.float32) assert_array_equal(np.invert(in1d(a, b)), in1d(a, b, invert=True)) assert_array_equal(np.invert(in1d(a, b)), - in1d(a, b, invert=True, _slow_integer=True)) + in1d(a, b, invert=True, method="dictionary")) def test_in1d_ravel(self): # Test that in1d ravels its input arrays. This is not documented @@ -435,21 +436,21 @@ class TestSetOps: assert_array_equal(in1d(a, b, assume_unique=False), ec) assert_array_equal(in1d(a, long_b, assume_unique=True), ec) assert_array_equal(in1d(a, long_b, assume_unique=False), ec) - assert_array_equal(in1d(a, b, assume_unique=True, _slow_integer=True), + assert_array_equal(in1d(a, b, assume_unique=True, method="dictionary"), ec) - assert_array_equal(in1d(a, b, assume_unique=False, _slow_integer=True), + assert_array_equal(in1d(a, b, assume_unique=False, method="dictionary"), ec) assert_array_equal(in1d(a, long_b, assume_unique=True, - _slow_integer=True), + method="dictionary"), ec) assert_array_equal(in1d(a, long_b, assume_unique=False, - _slow_integer=True), + method="dictionary"), ec) def test_in1d_hit_alternate_algorithm(self): """Hit the standard isin code with integers""" # Need extreme range to hit standard code - # This hits it without the use of _slow_integer + # This hits it without the use of method='dictionary' a = np.array([5, 4, 5, 3, 4, 4, 1e9], dtype=np.int64) b = np.array([2, 3, 4, 1e9], dtype=np.int64) expected = np.array([0, 1, 0, 1, 1, 1, 1], dtype=np.bool) @@ -470,11 +471,11 @@ class TestSetOps: assert_array_equal(expected, in1d(a, b)) assert_array_equal(expected, - in1d(a, b, _slow_integer=True)) + in1d(a, b, method="dictionary")) assert_array_equal(np.invert(expected), in1d(a, b, invert=True)) assert_array_equal(np.invert(expected), - in1d(a, b, invert=True, _slow_integer=True)) + in1d(a, b, invert=True, method="dictionary")) def test_in1d_first_array_is_object(self): ar1 = [None] -- cgit v1.2.1 From a8da1ef8db9d30c2dbc6b8fdd0c5d3b190dfad7b Mon Sep 17 00:00:00 2001 From: MilesCranmer Date: Fri, 10 Jun 2022 14:45:36 -0400 Subject: MAINT: Fix linting error in test --- numpy/lib/tests/test_arraysetops.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'numpy/lib/tests/test_arraysetops.py') diff --git a/numpy/lib/tests/test_arraysetops.py b/numpy/lib/tests/test_arraysetops.py index affd2e2d6..e4f37556a 100644 --- a/numpy/lib/tests/test_arraysetops.py +++ b/numpy/lib/tests/test_arraysetops.py @@ -438,7 +438,8 @@ class TestSetOps: assert_array_equal(in1d(a, long_b, assume_unique=False), ec) assert_array_equal(in1d(a, b, assume_unique=True, method="dictionary"), ec) - assert_array_equal(in1d(a, b, assume_unique=False, method="dictionary"), + assert_array_equal(in1d(a, b, assume_unique=False, + method="dictionary"), ec) assert_array_equal(in1d(a, long_b, assume_unique=True, method="dictionary"), -- cgit v1.2.1 From d7e2582cd33b22a767286e8a3d95b336dfe51a34 Mon Sep 17 00:00:00 2001 From: MilesCranmer Date: Fri, 10 Jun 2022 16:49:35 -0400 Subject: MAINT: bool instead of np.bool_ dtype --- numpy/lib/tests/test_arraysetops.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'numpy/lib/tests/test_arraysetops.py') diff --git a/numpy/lib/tests/test_arraysetops.py b/numpy/lib/tests/test_arraysetops.py index e4f37556a..d54ca1673 100644 --- a/numpy/lib/tests/test_arraysetops.py +++ b/numpy/lib/tests/test_arraysetops.py @@ -454,7 +454,7 @@ class TestSetOps: # This hits it without the use of method='dictionary' a = np.array([5, 4, 5, 3, 4, 4, 1e9], dtype=np.int64) b = np.array([2, 3, 4, 1e9], dtype=np.int64) - expected = np.array([0, 1, 0, 1, 1, 1, 1], dtype=np.bool) + expected = np.array([0, 1, 0, 1, 1, 1, 1], dtype=bool) assert_array_equal(expected, in1d(a, b)) assert_array_equal(np.invert(expected), in1d(a, b, invert=True)) -- cgit v1.2.1 From 6d91753cfbc95c9968262c8bc0a3ffc9c184cb01 Mon Sep 17 00:00:00 2001 From: MilesCranmer Date: Fri, 10 Jun 2022 20:28:10 -0400 Subject: MAINT: Add back in1d tests of old method --- numpy/lib/tests/test_arraysetops.py | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'numpy/lib/tests/test_arraysetops.py') diff --git a/numpy/lib/tests/test_arraysetops.py b/numpy/lib/tests/test_arraysetops.py index d54ca1673..3dc125734 100644 --- a/numpy/lib/tests/test_arraysetops.py +++ b/numpy/lib/tests/test_arraysetops.py @@ -258,21 +258,21 @@ class TestSetOps: ec = np.array([True, False, True, True]) c = in1d(a, b, assume_unique=True) assert_array_equal(c, ec) - c = in1d(a, b, assume_unique=True, method="dictionary") + c = in1d(a, b, assume_unique=True, method="sort") assert_array_equal(c, ec) a[0] = 8 ec = np.array([False, False, True, True]) c = in1d(a, b, assume_unique=True) assert_array_equal(c, ec) - c = in1d(a, b, assume_unique=True, method="dictionary") + c = in1d(a, b, assume_unique=True, method="sort") assert_array_equal(c, ec) a[0], a[3] = 4, 8 ec = np.array([True, False, True, False]) c = in1d(a, b, assume_unique=True) assert_array_equal(c, ec) - c = in1d(a, b, assume_unique=True, method="dictionary") + c = in1d(a, b, assume_unique=True, method="sort") assert_array_equal(c, ec) a = np.array([5, 4, 5, 3, 4, 4, 3, 4, 3, 5, 2, 1, 5, 5]) @@ -281,7 +281,7 @@ class TestSetOps: False, True, False, False, False] c = in1d(a, b) assert_array_equal(c, ec) - c = in1d(a, b, method="dictionary") + c = in1d(a, b, method="sort") assert_array_equal(c, ec) b = b + [5, 5, 4] * mult @@ -289,7 +289,7 @@ class TestSetOps: True, False, True, True] c = in1d(a, b) assert_array_equal(c, ec) - c = in1d(a, b, method="dictionary") + c = in1d(a, b, method="sort") assert_array_equal(c, ec) a = np.array([5, 7, 1, 2]) @@ -297,7 +297,7 @@ class TestSetOps: ec = np.array([True, False, True, True]) c = in1d(a, b) assert_array_equal(c, ec) - c = in1d(a, b, method="dictionary") + c = in1d(a, b, method="sort") assert_array_equal(c, ec) a = np.array([5, 7, 1, 1, 2]) @@ -305,7 +305,7 @@ class TestSetOps: ec = np.array([True, False, True, True, True]) c = in1d(a, b) assert_array_equal(c, ec) - c = in1d(a, b, method="dictionary") + c = in1d(a, b, method="sort") assert_array_equal(c, ec) a = np.array([5, 5]) @@ -313,7 +313,7 @@ class TestSetOps: ec = np.array([False, False]) c = in1d(a, b) assert_array_equal(c, ec) - c = in1d(a, b, method="dictionary") + c = in1d(a, b, method="sort") assert_array_equal(c, ec) a = np.array([5]) @@ -321,7 +321,7 @@ class TestSetOps: ec = np.array([False]) c = in1d(a, b) assert_array_equal(c, ec) - c = in1d(a, b, method="dictionary") + c = in1d(a, b, method="sort") assert_array_equal(c, ec) assert_array_equal(in1d([], []), []) @@ -413,7 +413,7 @@ class TestSetOps: b = [2, 3, 4] * mult assert_array_equal(np.invert(in1d(a, b)), in1d(a, b, invert=True)) assert_array_equal(np.invert(in1d(a, b)), - in1d(a, b, invert=True, method="dictionary")) + in1d(a, b, invert=True, method="sort")) for mult in (1, 10): a = np.array([5, 4, 5, 3, 4, 4, 3, 4, 3, 5, 2, 1, 5, 5], @@ -422,7 +422,7 @@ class TestSetOps: b = np.array(b, dtype=np.float32) assert_array_equal(np.invert(in1d(a, b)), in1d(a, b, invert=True)) assert_array_equal(np.invert(in1d(a, b)), - in1d(a, b, invert=True, method="dictionary")) + in1d(a, b, invert=True, method="sort")) def test_in1d_ravel(self): # Test that in1d ravels its input arrays. This is not documented @@ -436,16 +436,16 @@ class TestSetOps: assert_array_equal(in1d(a, b, assume_unique=False), ec) assert_array_equal(in1d(a, long_b, assume_unique=True), ec) assert_array_equal(in1d(a, long_b, assume_unique=False), ec) - assert_array_equal(in1d(a, b, assume_unique=True, method="dictionary"), + assert_array_equal(in1d(a, b, assume_unique=True, method="sort"), ec) assert_array_equal(in1d(a, b, assume_unique=False, - method="dictionary"), + method="sort"), ec) assert_array_equal(in1d(a, long_b, assume_unique=True, - method="dictionary"), + method="sort"), ec) assert_array_equal(in1d(a, long_b, assume_unique=False, - method="dictionary"), + method="sort"), ec) def test_in1d_hit_alternate_algorithm(self): @@ -472,11 +472,11 @@ class TestSetOps: assert_array_equal(expected, in1d(a, b)) assert_array_equal(expected, - in1d(a, b, method="dictionary")) + in1d(a, b, method="sort")) assert_array_equal(np.invert(expected), in1d(a, b, invert=True)) assert_array_equal(np.invert(expected), - in1d(a, b, invert=True, method="dictionary")) + in1d(a, b, invert=True, method="sort")) def test_in1d_first_array_is_object(self): ar1 = [None] -- cgit v1.2.1 From 7a1ee13ee28083c484a42a657067570773bcddbe Mon Sep 17 00:00:00 2001 From: MilesCranmer Date: Fri, 10 Jun 2022 20:41:07 -0400 Subject: MAINT: Fix misplaced default in in1d test --- numpy/lib/tests/test_arraysetops.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'numpy/lib/tests/test_arraysetops.py') diff --git a/numpy/lib/tests/test_arraysetops.py b/numpy/lib/tests/test_arraysetops.py index 3dc125734..cbf39c604 100644 --- a/numpy/lib/tests/test_arraysetops.py +++ b/numpy/lib/tests/test_arraysetops.py @@ -205,8 +205,8 @@ class TestSetOps: isin_slow = np.vectorize(_isin_slow, otypes=[bool], excluded={1}) def assert_isin_equal(a, b, old_algorithm=None): - method = "sort" if old_algorithm else "dictionary" - x = isin(a, b, method=old_algorithm) + method = "sort" if old_algorithm else "auto" + x = isin(a, b, method=method) y = isin_slow(a, b) assert_array_equal(x, y) -- cgit v1.2.1 From 34a3358b86143971dd10a89c03b44eda9916428c Mon Sep 17 00:00:00 2001 From: MilesCranmer Date: Fri, 17 Jun 2022 11:55:05 -0400 Subject: TST: Use new "kind" argument over "method" --- numpy/lib/tests/test_arraysetops.py | 40 ++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 20 deletions(-) (limited to 'numpy/lib/tests/test_arraysetops.py') diff --git a/numpy/lib/tests/test_arraysetops.py b/numpy/lib/tests/test_arraysetops.py index cbf39c604..2e25bbd9b 100644 --- a/numpy/lib/tests/test_arraysetops.py +++ b/numpy/lib/tests/test_arraysetops.py @@ -205,8 +205,8 @@ class TestSetOps: isin_slow = np.vectorize(_isin_slow, otypes=[bool], excluded={1}) def assert_isin_equal(a, b, old_algorithm=None): - method = "sort" if old_algorithm else "auto" - x = isin(a, b, method=method) + kind = "sort" if old_algorithm else "auto" + x = isin(a, b, kind=kind) y = isin_slow(a, b) assert_array_equal(x, y) @@ -258,21 +258,21 @@ class TestSetOps: ec = np.array([True, False, True, True]) c = in1d(a, b, assume_unique=True) assert_array_equal(c, ec) - c = in1d(a, b, assume_unique=True, method="sort") + c = in1d(a, b, assume_unique=True, kind="sort") assert_array_equal(c, ec) a[0] = 8 ec = np.array([False, False, True, True]) c = in1d(a, b, assume_unique=True) assert_array_equal(c, ec) - c = in1d(a, b, assume_unique=True, method="sort") + c = in1d(a, b, assume_unique=True, kind="sort") assert_array_equal(c, ec) a[0], a[3] = 4, 8 ec = np.array([True, False, True, False]) c = in1d(a, b, assume_unique=True) assert_array_equal(c, ec) - c = in1d(a, b, assume_unique=True, method="sort") + c = in1d(a, b, assume_unique=True, kind="sort") assert_array_equal(c, ec) a = np.array([5, 4, 5, 3, 4, 4, 3, 4, 3, 5, 2, 1, 5, 5]) @@ -281,7 +281,7 @@ class TestSetOps: False, True, False, False, False] c = in1d(a, b) assert_array_equal(c, ec) - c = in1d(a, b, method="sort") + c = in1d(a, b, kind="sort") assert_array_equal(c, ec) b = b + [5, 5, 4] * mult @@ -289,7 +289,7 @@ class TestSetOps: True, False, True, True] c = in1d(a, b) assert_array_equal(c, ec) - c = in1d(a, b, method="sort") + c = in1d(a, b, kind="sort") assert_array_equal(c, ec) a = np.array([5, 7, 1, 2]) @@ -297,7 +297,7 @@ class TestSetOps: ec = np.array([True, False, True, True]) c = in1d(a, b) assert_array_equal(c, ec) - c = in1d(a, b, method="sort") + c = in1d(a, b, kind="sort") assert_array_equal(c, ec) a = np.array([5, 7, 1, 1, 2]) @@ -305,7 +305,7 @@ class TestSetOps: ec = np.array([True, False, True, True, True]) c = in1d(a, b) assert_array_equal(c, ec) - c = in1d(a, b, method="sort") + c = in1d(a, b, kind="sort") assert_array_equal(c, ec) a = np.array([5, 5]) @@ -313,7 +313,7 @@ class TestSetOps: ec = np.array([False, False]) c = in1d(a, b) assert_array_equal(c, ec) - c = in1d(a, b, method="sort") + c = in1d(a, b, kind="sort") assert_array_equal(c, ec) a = np.array([5]) @@ -321,7 +321,7 @@ class TestSetOps: ec = np.array([False]) c = in1d(a, b) assert_array_equal(c, ec) - c = in1d(a, b, method="sort") + c = in1d(a, b, kind="sort") assert_array_equal(c, ec) assert_array_equal(in1d([], []), []) @@ -413,7 +413,7 @@ class TestSetOps: b = [2, 3, 4] * mult assert_array_equal(np.invert(in1d(a, b)), in1d(a, b, invert=True)) assert_array_equal(np.invert(in1d(a, b)), - in1d(a, b, invert=True, method="sort")) + in1d(a, b, invert=True, kind="sort")) for mult in (1, 10): a = np.array([5, 4, 5, 3, 4, 4, 3, 4, 3, 5, 2, 1, 5, 5], @@ -422,7 +422,7 @@ class TestSetOps: b = np.array(b, dtype=np.float32) assert_array_equal(np.invert(in1d(a, b)), in1d(a, b, invert=True)) assert_array_equal(np.invert(in1d(a, b)), - in1d(a, b, invert=True, method="sort")) + in1d(a, b, invert=True, kind="sort")) def test_in1d_ravel(self): # Test that in1d ravels its input arrays. This is not documented @@ -436,22 +436,22 @@ class TestSetOps: assert_array_equal(in1d(a, b, assume_unique=False), ec) assert_array_equal(in1d(a, long_b, assume_unique=True), ec) assert_array_equal(in1d(a, long_b, assume_unique=False), ec) - assert_array_equal(in1d(a, b, assume_unique=True, method="sort"), + assert_array_equal(in1d(a, b, assume_unique=True, kind="sort"), ec) assert_array_equal(in1d(a, b, assume_unique=False, - method="sort"), + kind="sort"), ec) assert_array_equal(in1d(a, long_b, assume_unique=True, - method="sort"), + kind="sort"), ec) assert_array_equal(in1d(a, long_b, assume_unique=False, - method="sort"), + kind="sort"), ec) def test_in1d_hit_alternate_algorithm(self): """Hit the standard isin code with integers""" # Need extreme range to hit standard code - # This hits it without the use of method='dictionary' + # This hits it without the use of kind='dictionary' a = np.array([5, 4, 5, 3, 4, 4, 1e9], dtype=np.int64) b = np.array([2, 3, 4, 1e9], dtype=np.int64) expected = np.array([0, 1, 0, 1, 1, 1, 1], dtype=bool) @@ -472,11 +472,11 @@ class TestSetOps: assert_array_equal(expected, in1d(a, b)) assert_array_equal(expected, - in1d(a, b, method="sort")) + in1d(a, b, kind="sort")) assert_array_equal(np.invert(expected), in1d(a, b, invert=True)) assert_array_equal(np.invert(expected), - in1d(a, b, invert=True, method="sort")) + in1d(a, b, invert=True, kind="sort")) def test_in1d_first_array_is_object(self): ar1 = [None] -- cgit v1.2.1 From 8f5764447cdf6f8ab21ba0f863c65a8d7a7728b5 Mon Sep 17 00:00:00 2001 From: MilesCranmer Date: Fri, 17 Jun 2022 11:59:00 -0400 Subject: MAINT: kind now uses "mergesort" instead of "sort" --- numpy/lib/tests/test_arraysetops.py | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) (limited to 'numpy/lib/tests/test_arraysetops.py') diff --git a/numpy/lib/tests/test_arraysetops.py b/numpy/lib/tests/test_arraysetops.py index 2e25bbd9b..fd3dc4932 100644 --- a/numpy/lib/tests/test_arraysetops.py +++ b/numpy/lib/tests/test_arraysetops.py @@ -205,7 +205,7 @@ class TestSetOps: isin_slow = np.vectorize(_isin_slow, otypes=[bool], excluded={1}) def assert_isin_equal(a, b, old_algorithm=None): - kind = "sort" if old_algorithm else "auto" + kind = "mergesort" if old_algorithm else None x = isin(a, b, kind=kind) y = isin_slow(a, b) assert_array_equal(x, y) @@ -258,21 +258,21 @@ class TestSetOps: ec = np.array([True, False, True, True]) c = in1d(a, b, assume_unique=True) assert_array_equal(c, ec) - c = in1d(a, b, assume_unique=True, kind="sort") + c = in1d(a, b, assume_unique=True, kind="mergesort") assert_array_equal(c, ec) a[0] = 8 ec = np.array([False, False, True, True]) c = in1d(a, b, assume_unique=True) assert_array_equal(c, ec) - c = in1d(a, b, assume_unique=True, kind="sort") + c = in1d(a, b, assume_unique=True, kind="mergesort") assert_array_equal(c, ec) a[0], a[3] = 4, 8 ec = np.array([True, False, True, False]) c = in1d(a, b, assume_unique=True) assert_array_equal(c, ec) - c = in1d(a, b, assume_unique=True, kind="sort") + c = in1d(a, b, assume_unique=True, kind="mergesort") assert_array_equal(c, ec) a = np.array([5, 4, 5, 3, 4, 4, 3, 4, 3, 5, 2, 1, 5, 5]) @@ -281,7 +281,7 @@ class TestSetOps: False, True, False, False, False] c = in1d(a, b) assert_array_equal(c, ec) - c = in1d(a, b, kind="sort") + c = in1d(a, b, kind="mergesort") assert_array_equal(c, ec) b = b + [5, 5, 4] * mult @@ -289,7 +289,7 @@ class TestSetOps: True, False, True, True] c = in1d(a, b) assert_array_equal(c, ec) - c = in1d(a, b, kind="sort") + c = in1d(a, b, kind="mergesort") assert_array_equal(c, ec) a = np.array([5, 7, 1, 2]) @@ -297,7 +297,7 @@ class TestSetOps: ec = np.array([True, False, True, True]) c = in1d(a, b) assert_array_equal(c, ec) - c = in1d(a, b, kind="sort") + c = in1d(a, b, kind="mergesort") assert_array_equal(c, ec) a = np.array([5, 7, 1, 1, 2]) @@ -305,7 +305,7 @@ class TestSetOps: ec = np.array([True, False, True, True, True]) c = in1d(a, b) assert_array_equal(c, ec) - c = in1d(a, b, kind="sort") + c = in1d(a, b, kind="mergesort") assert_array_equal(c, ec) a = np.array([5, 5]) @@ -313,7 +313,7 @@ class TestSetOps: ec = np.array([False, False]) c = in1d(a, b) assert_array_equal(c, ec) - c = in1d(a, b, kind="sort") + c = in1d(a, b, kind="mergesort") assert_array_equal(c, ec) a = np.array([5]) @@ -321,7 +321,7 @@ class TestSetOps: ec = np.array([False]) c = in1d(a, b) assert_array_equal(c, ec) - c = in1d(a, b, kind="sort") + c = in1d(a, b, kind="mergesort") assert_array_equal(c, ec) assert_array_equal(in1d([], []), []) @@ -413,7 +413,7 @@ class TestSetOps: b = [2, 3, 4] * mult assert_array_equal(np.invert(in1d(a, b)), in1d(a, b, invert=True)) assert_array_equal(np.invert(in1d(a, b)), - in1d(a, b, invert=True, kind="sort")) + in1d(a, b, invert=True, kind="mergesort")) for mult in (1, 10): a = np.array([5, 4, 5, 3, 4, 4, 3, 4, 3, 5, 2, 1, 5, 5], @@ -422,7 +422,7 @@ class TestSetOps: b = np.array(b, dtype=np.float32) assert_array_equal(np.invert(in1d(a, b)), in1d(a, b, invert=True)) assert_array_equal(np.invert(in1d(a, b)), - in1d(a, b, invert=True, kind="sort")) + in1d(a, b, invert=True, kind="mergesort")) def test_in1d_ravel(self): # Test that in1d ravels its input arrays. This is not documented @@ -436,16 +436,16 @@ class TestSetOps: assert_array_equal(in1d(a, b, assume_unique=False), ec) assert_array_equal(in1d(a, long_b, assume_unique=True), ec) assert_array_equal(in1d(a, long_b, assume_unique=False), ec) - assert_array_equal(in1d(a, b, assume_unique=True, kind="sort"), + assert_array_equal(in1d(a, b, assume_unique=True, kind="mergesort"), ec) assert_array_equal(in1d(a, b, assume_unique=False, - kind="sort"), + kind="mergesort"), ec) assert_array_equal(in1d(a, long_b, assume_unique=True, - kind="sort"), + kind="mergesort"), ec) assert_array_equal(in1d(a, long_b, assume_unique=False, - kind="sort"), + kind="mergesort"), ec) def test_in1d_hit_alternate_algorithm(self): @@ -472,11 +472,11 @@ class TestSetOps: assert_array_equal(expected, in1d(a, b)) assert_array_equal(expected, - in1d(a, b, kind="sort")) + in1d(a, b, kind="mergesort")) assert_array_equal(np.invert(expected), in1d(a, b, invert=True)) assert_array_equal(np.invert(expected), - in1d(a, b, invert=True, kind="sort")) + in1d(a, b, invert=True, kind="mergesort")) def test_in1d_first_array_is_object(self): ar1 = [None] -- cgit v1.2.1 From 43b4dafd6fb96535520cccf36d39ed5cb9446177 Mon Sep 17 00:00:00 2001 From: MilesCranmer Date: Fri, 17 Jun 2022 18:17:16 -0400 Subject: TST: validate in1d errors --- numpy/lib/tests/test_arraysetops.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'numpy/lib/tests/test_arraysetops.py') diff --git a/numpy/lib/tests/test_arraysetops.py b/numpy/lib/tests/test_arraysetops.py index fd3dc4932..8b28fa9e5 100644 --- a/numpy/lib/tests/test_arraysetops.py +++ b/numpy/lib/tests/test_arraysetops.py @@ -542,6 +542,40 @@ class TestSetOps: result = np.in1d(ar1, ar2, invert=True) assert_array_equal(result, np.invert(expected)) + def test_in1d_errors(self): + """Test that in1d raises expected errors.""" + + # Error 1: `kind` is not one of 'mergesort' 'dictionary' or None. + ar1 = np.array([1, 2, 3, 4, 5]) + ar2 = np.array([2, 4, 6, 8, 10]) + assert_raises(ValueError, in1d, ar1, ar2, kind='quicksort') + + # Error 2: `kind="dictionary"` does not work for non-integral arrays. + obj_ar1 = np.array([1, 'a', 3, 'b', 5], dtype=object) + obj_ar2 = np.array([1, 'a', 3, 'b', 5], dtype=object) + assert_raises(ValueError, in1d, obj_ar1, obj_ar2, kind='dictionary') + + for dtype in [np.int32, np.int64]: + ar1 = np.array([-1, 2, 3, 4, 5], dtype=dtype) + # The range of this array will overflow: + overflow_ar2 = np.array([-1, np.iinfo(dtype).max], dtype=dtype) + + # Error 3: `kind="dictionary"` will trigger a runtime error + # if there is an integer overflow expected when computing the + # range of ar2 + assert_raises( + RuntimeError, + in1d, ar1, overflow_ar2, kind='dictionary' + ) + + # Non-error: `kind=None` will *not* trigger a runtime error + # if there is an integer overflow, it will switch to + # the `mergesort` algorithm. + result = np.in1d(ar1, overflow_ar2, kind=None) + assert_array_equal(result, [True] + [False] * 4) + result = np.in1d(ar1, overflow_ar2, kind='mergesort') + assert_array_equal(result, [True] + [False] * 4) + def test_union1d(self): a = np.array([5, 4, 7, 1, 2]) b = np.array([2, 4, 3, 3, 2, 1, 5]) -- cgit v1.2.1 From 4ed458f16d9dd64554ccf49e315c5b8fb577d4cd Mon Sep 17 00:00:00 2001 From: MilesCranmer Date: Sat, 18 Jun 2022 13:49:03 -0400 Subject: MAINT: change kind names for in1d - Switch dictionary->table, mergesort->sort --- numpy/lib/tests/test_arraysetops.py | 52 ++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 26 deletions(-) (limited to 'numpy/lib/tests/test_arraysetops.py') diff --git a/numpy/lib/tests/test_arraysetops.py b/numpy/lib/tests/test_arraysetops.py index 8b28fa9e5..57152e4d5 100644 --- a/numpy/lib/tests/test_arraysetops.py +++ b/numpy/lib/tests/test_arraysetops.py @@ -205,7 +205,7 @@ class TestSetOps: isin_slow = np.vectorize(_isin_slow, otypes=[bool], excluded={1}) def assert_isin_equal(a, b, old_algorithm=None): - kind = "mergesort" if old_algorithm else None + kind = "sort" if old_algorithm else None x = isin(a, b, kind=kind) y = isin_slow(a, b) assert_array_equal(x, y) @@ -258,21 +258,21 @@ class TestSetOps: ec = np.array([True, False, True, True]) c = in1d(a, b, assume_unique=True) assert_array_equal(c, ec) - c = in1d(a, b, assume_unique=True, kind="mergesort") + c = in1d(a, b, assume_unique=True, kind="sort") assert_array_equal(c, ec) a[0] = 8 ec = np.array([False, False, True, True]) c = in1d(a, b, assume_unique=True) assert_array_equal(c, ec) - c = in1d(a, b, assume_unique=True, kind="mergesort") + c = in1d(a, b, assume_unique=True, kind="sort") assert_array_equal(c, ec) a[0], a[3] = 4, 8 ec = np.array([True, False, True, False]) c = in1d(a, b, assume_unique=True) assert_array_equal(c, ec) - c = in1d(a, b, assume_unique=True, kind="mergesort") + c = in1d(a, b, assume_unique=True, kind="sort") assert_array_equal(c, ec) a = np.array([5, 4, 5, 3, 4, 4, 3, 4, 3, 5, 2, 1, 5, 5]) @@ -281,7 +281,7 @@ class TestSetOps: False, True, False, False, False] c = in1d(a, b) assert_array_equal(c, ec) - c = in1d(a, b, kind="mergesort") + c = in1d(a, b, kind="sort") assert_array_equal(c, ec) b = b + [5, 5, 4] * mult @@ -289,7 +289,7 @@ class TestSetOps: True, False, True, True] c = in1d(a, b) assert_array_equal(c, ec) - c = in1d(a, b, kind="mergesort") + c = in1d(a, b, kind="sort") assert_array_equal(c, ec) a = np.array([5, 7, 1, 2]) @@ -297,7 +297,7 @@ class TestSetOps: ec = np.array([True, False, True, True]) c = in1d(a, b) assert_array_equal(c, ec) - c = in1d(a, b, kind="mergesort") + c = in1d(a, b, kind="sort") assert_array_equal(c, ec) a = np.array([5, 7, 1, 1, 2]) @@ -305,7 +305,7 @@ class TestSetOps: ec = np.array([True, False, True, True, True]) c = in1d(a, b) assert_array_equal(c, ec) - c = in1d(a, b, kind="mergesort") + c = in1d(a, b, kind="sort") assert_array_equal(c, ec) a = np.array([5, 5]) @@ -313,7 +313,7 @@ class TestSetOps: ec = np.array([False, False]) c = in1d(a, b) assert_array_equal(c, ec) - c = in1d(a, b, kind="mergesort") + c = in1d(a, b, kind="sort") assert_array_equal(c, ec) a = np.array([5]) @@ -321,7 +321,7 @@ class TestSetOps: ec = np.array([False]) c = in1d(a, b) assert_array_equal(c, ec) - c = in1d(a, b, kind="mergesort") + c = in1d(a, b, kind="sort") assert_array_equal(c, ec) assert_array_equal(in1d([], []), []) @@ -413,7 +413,7 @@ class TestSetOps: b = [2, 3, 4] * mult assert_array_equal(np.invert(in1d(a, b)), in1d(a, b, invert=True)) assert_array_equal(np.invert(in1d(a, b)), - in1d(a, b, invert=True, kind="mergesort")) + in1d(a, b, invert=True, kind="sort")) for mult in (1, 10): a = np.array([5, 4, 5, 3, 4, 4, 3, 4, 3, 5, 2, 1, 5, 5], @@ -422,7 +422,7 @@ class TestSetOps: b = np.array(b, dtype=np.float32) assert_array_equal(np.invert(in1d(a, b)), in1d(a, b, invert=True)) assert_array_equal(np.invert(in1d(a, b)), - in1d(a, b, invert=True, kind="mergesort")) + in1d(a, b, invert=True, kind="sort")) def test_in1d_ravel(self): # Test that in1d ravels its input arrays. This is not documented @@ -436,22 +436,22 @@ class TestSetOps: assert_array_equal(in1d(a, b, assume_unique=False), ec) assert_array_equal(in1d(a, long_b, assume_unique=True), ec) assert_array_equal(in1d(a, long_b, assume_unique=False), ec) - assert_array_equal(in1d(a, b, assume_unique=True, kind="mergesort"), + assert_array_equal(in1d(a, b, assume_unique=True, kind="sort"), ec) assert_array_equal(in1d(a, b, assume_unique=False, - kind="mergesort"), + kind="sort"), ec) assert_array_equal(in1d(a, long_b, assume_unique=True, - kind="mergesort"), + kind="sort"), ec) assert_array_equal(in1d(a, long_b, assume_unique=False, - kind="mergesort"), + kind="sort"), ec) def test_in1d_hit_alternate_algorithm(self): """Hit the standard isin code with integers""" # Need extreme range to hit standard code - # This hits it without the use of kind='dictionary' + # This hits it without the use of kind='table' a = np.array([5, 4, 5, 3, 4, 4, 1e9], dtype=np.int64) b = np.array([2, 3, 4, 1e9], dtype=np.int64) expected = np.array([0, 1, 0, 1, 1, 1, 1], dtype=bool) @@ -472,11 +472,11 @@ class TestSetOps: assert_array_equal(expected, in1d(a, b)) assert_array_equal(expected, - in1d(a, b, kind="mergesort")) + in1d(a, b, kind="sort")) assert_array_equal(np.invert(expected), in1d(a, b, invert=True)) assert_array_equal(np.invert(expected), - in1d(a, b, invert=True, kind="mergesort")) + in1d(a, b, invert=True, kind="sort")) def test_in1d_first_array_is_object(self): ar1 = [None] @@ -545,35 +545,35 @@ class TestSetOps: def test_in1d_errors(self): """Test that in1d raises expected errors.""" - # Error 1: `kind` is not one of 'mergesort' 'dictionary' or None. + # Error 1: `kind` is not one of 'sort' 'table' or None. ar1 = np.array([1, 2, 3, 4, 5]) ar2 = np.array([2, 4, 6, 8, 10]) assert_raises(ValueError, in1d, ar1, ar2, kind='quicksort') - # Error 2: `kind="dictionary"` does not work for non-integral arrays. + # Error 2: `kind="table"` does not work for non-integral arrays. obj_ar1 = np.array([1, 'a', 3, 'b', 5], dtype=object) obj_ar2 = np.array([1, 'a', 3, 'b', 5], dtype=object) - assert_raises(ValueError, in1d, obj_ar1, obj_ar2, kind='dictionary') + assert_raises(ValueError, in1d, obj_ar1, obj_ar2, kind='table') for dtype in [np.int32, np.int64]: ar1 = np.array([-1, 2, 3, 4, 5], dtype=dtype) # The range of this array will overflow: overflow_ar2 = np.array([-1, np.iinfo(dtype).max], dtype=dtype) - # Error 3: `kind="dictionary"` will trigger a runtime error + # Error 3: `kind="table"` will trigger a runtime error # if there is an integer overflow expected when computing the # range of ar2 assert_raises( RuntimeError, - in1d, ar1, overflow_ar2, kind='dictionary' + in1d, ar1, overflow_ar2, kind='table' ) # Non-error: `kind=None` will *not* trigger a runtime error # if there is an integer overflow, it will switch to - # the `mergesort` algorithm. + # the `sort` algorithm. result = np.in1d(ar1, overflow_ar2, kind=None) assert_array_equal(result, [True] + [False] * 4) - result = np.in1d(ar1, overflow_ar2, kind='mergesort') + result = np.in1d(ar1, overflow_ar2, kind='sort') assert_array_equal(result, [True] + [False] * 4) def test_union1d(self): -- cgit v1.2.1 From 3b117e7e8c0f9a84185181a7fdbb7e4de979f0ec Mon Sep 17 00:00:00 2001 From: MilesCranmer Date: Wed, 22 Jun 2022 17:15:24 -0400 Subject: TST: Reduce code re-use with pytest mark --- numpy/lib/tests/test_arraysetops.py | 182 ++++++++---------------------------- 1 file changed, 40 insertions(+), 142 deletions(-) (limited to 'numpy/lib/tests/test_arraysetops.py') diff --git a/numpy/lib/tests/test_arraysetops.py b/numpy/lib/tests/test_arraysetops.py index 57152e4d5..26bf82b33 100644 --- a/numpy/lib/tests/test_arraysetops.py +++ b/numpy/lib/tests/test_arraysetops.py @@ -195,7 +195,8 @@ class TestSetOps: assert_equal(actual, expected) assert actual.dtype == expected.dtype - def test_isin(self): + @pytest.mark.parametrize("kind", [None, "sort", "table"]) + def test_isin(self, kind): # the tests for in1d cover most of isin's behavior # if in1d is removed, would need to change those tests to test # isin instead. @@ -204,8 +205,7 @@ class TestSetOps: return a in b isin_slow = np.vectorize(_isin_slow, otypes=[bool], excluded={1}) - def assert_isin_equal(a, b, old_algorithm=None): - kind = "sort" if old_algorithm else None + def assert_isin_equal(a, b): x = isin(a, b, kind=kind) y = isin_slow(a, b) assert_array_equal(x, y) @@ -214,41 +214,31 @@ class TestSetOps: a = np.arange(24).reshape([2, 3, 4]) b = np.array([[10, 20, 30], [0, 1, 3], [11, 22, 33]]) assert_isin_equal(a, b) - assert_isin_equal(a, b, old_algorithm=True) # array-likes as both arguments c = [(9, 8), (7, 6)] d = (9, 7) assert_isin_equal(c, d) - assert_isin_equal(c, d, old_algorithm=True) # zero-d array: f = np.array(3) assert_isin_equal(f, b) assert_isin_equal(a, f) assert_isin_equal(f, f) - assert_isin_equal(f, b, old_algorithm=True) - assert_isin_equal(a, f, old_algorithm=True) - assert_isin_equal(f, f, old_algorithm=True) # scalar: assert_isin_equal(5, b) assert_isin_equal(a, 6) assert_isin_equal(5, 6) - assert_isin_equal(5, b, old_algorithm=True) - assert_isin_equal(a, 6, old_algorithm=True) - assert_isin_equal(5, 6, old_algorithm=True) # empty array-like: x = [] assert_isin_equal(x, b) assert_isin_equal(a, x) assert_isin_equal(x, x) - assert_isin_equal(x, b, old_algorithm=True) - assert_isin_equal(a, x, old_algorithm=True) - assert_isin_equal(x, x, old_algorithm=True) - def test_in1d(self): + @pytest.mark.parametrize("kind", [None, "sort", "table"]) + def test_in1d(self, kind): # we use two different sizes for the b array here to test the # two different paths in in1d(). for mult in (1, 10): @@ -256,144 +246,57 @@ class TestSetOps: a = [5, 7, 1, 2] b = [2, 4, 3, 1, 5] * mult ec = np.array([True, False, True, True]) - c = in1d(a, b, assume_unique=True) - assert_array_equal(c, ec) - c = in1d(a, b, assume_unique=True, kind="sort") + c = in1d(a, b, assume_unique=True, kind=kind) assert_array_equal(c, ec) a[0] = 8 ec = np.array([False, False, True, True]) - c = in1d(a, b, assume_unique=True) - assert_array_equal(c, ec) - c = in1d(a, b, assume_unique=True, kind="sort") + c = in1d(a, b, assume_unique=True, kind=kind) assert_array_equal(c, ec) a[0], a[3] = 4, 8 ec = np.array([True, False, True, False]) - c = in1d(a, b, assume_unique=True) - assert_array_equal(c, ec) - c = in1d(a, b, assume_unique=True, kind="sort") + c = in1d(a, b, assume_unique=True, kind=kind) assert_array_equal(c, ec) a = np.array([5, 4, 5, 3, 4, 4, 3, 4, 3, 5, 2, 1, 5, 5]) b = [2, 3, 4] * mult ec = [False, True, False, True, True, True, True, True, True, False, True, False, False, False] - c = in1d(a, b) - assert_array_equal(c, ec) - c = in1d(a, b, kind="sort") + c = in1d(a, b, kind=kind) assert_array_equal(c, ec) b = b + [5, 5, 4] * mult ec = [True, True, True, True, True, True, True, True, True, True, True, False, True, True] - c = in1d(a, b) - assert_array_equal(c, ec) - c = in1d(a, b, kind="sort") + c = in1d(a, b, kind=kind) assert_array_equal(c, ec) a = np.array([5, 7, 1, 2]) b = np.array([2, 4, 3, 1, 5] * mult) ec = np.array([True, False, True, True]) - c = in1d(a, b) - assert_array_equal(c, ec) - c = in1d(a, b, kind="sort") + c = in1d(a, b, kind=kind) assert_array_equal(c, ec) a = np.array([5, 7, 1, 1, 2]) b = np.array([2, 4, 3, 3, 1, 5] * mult) ec = np.array([True, False, True, True, True]) - c = in1d(a, b) - assert_array_equal(c, ec) - c = in1d(a, b, kind="sort") + c = in1d(a, b, kind=kind) assert_array_equal(c, ec) a = np.array([5, 5]) b = np.array([2, 2] * mult) ec = np.array([False, False]) - c = in1d(a, b) - assert_array_equal(c, ec) - c = in1d(a, b, kind="sort") + c = in1d(a, b, kind=kind) assert_array_equal(c, ec) a = np.array([5]) b = np.array([2]) ec = np.array([False]) - c = in1d(a, b) - assert_array_equal(c, ec) - c = in1d(a, b, kind="sort") + c = in1d(a, b, kind=kind) assert_array_equal(c, ec) - assert_array_equal(in1d([], []), []) - - def test_in1d_hit_alternate_algorithm_floats(self): - # Perform the above test but with floats - # This forces the old in1d (pre-Oct 1/2018) algorithm - for mult in (1, 10): - # One check without np.array to make sure lists are handled correct - a = [5, 7, 1, 2] - b = [2, 4, 3, 1, 5] * mult - ec = np.array([True, False, True, True]) - c = in1d(np.array(a, dtype=np.float32), - np.array(b, dtype=np.float32), assume_unique=True) - assert_array_equal(c, ec) - - a[0] = 8 - ec = np.array([False, False, True, True]) - c = in1d(np.array(a, dtype=np.float32), - np.array(b, dtype=np.float32), assume_unique=True) - assert_array_equal(c, ec) - - a[0], a[3] = 4, 8 - ec = np.array([True, False, True, False]) - c = in1d(np.array(a, dtype=np.float32), - np.array(b, dtype=np.float32), assume_unique=True) - assert_array_equal(c, ec) - - a = np.array([5, 4, 5, 3, 4, 4, 3, 4, 3, 5, 2, 1, 5, 5]) - b = [2, 3, 4] * mult - ec = [False, True, False, True, True, True, True, True, True, - False, True, False, False, False] - c = in1d(np.array(a, dtype=np.float32), - np.array(b, dtype=np.float32)) - assert_array_equal(c, ec) - - b = b + [5, 5, 4] * mult - ec = [True, True, True, True, True, True, True, True, True, True, - True, False, True, True] - c = in1d(np.array(a, dtype=np.float32), - np.array(b, dtype=np.float32)) - assert_array_equal(c, ec) - - a = np.array([5, 7, 1, 2]) - b = np.array([2, 4, 3, 1, 5] * mult) - ec = np.array([True, False, True, True]) - c = in1d(np.array(a, dtype=np.float32), - np.array(b, dtype=np.float32)) - assert_array_equal(c, ec) - - a = np.array([5, 7, 1, 1, 2]) - b = np.array([2, 4, 3, 3, 1, 5] * mult) - ec = np.array([True, False, True, True, True]) - c = in1d(np.array(a, dtype=np.float32), - np.array(b, dtype=np.float32)) - assert_array_equal(c, ec) - - a = np.array([5, 5]) - b = np.array([2, 2] * mult) - ec = np.array([False, False]) - c = in1d(np.array(a, dtype=np.float32), - np.array(b, dtype=np.float32)) - assert_array_equal(c, ec) - - a = np.array([5]) - b = np.array([2]) - ec = np.array([False]) - c = in1d(np.array(a, dtype=np.float32), - np.array(b, dtype=np.float32)) - assert_array_equal(c, ec) - - assert_array_equal(in1d([], []), []) + assert_array_equal(in1d([], [], kind=kind), []) def test_in1d_char_array(self): a = np.array(['a', 'b', 'c', 'd', 'e', 'c', 'e', 'b']) @@ -404,27 +307,29 @@ class TestSetOps: assert_array_equal(c, ec) - def test_in1d_invert(self): + @pytest.mark.parametrize("kind", [None, "sort", "table"]) + def test_in1d_invert(self, kind): "Test in1d's invert parameter" # We use two different sizes for the b array here to test the # two different paths in in1d(). for mult in (1, 10): a = np.array([5, 4, 5, 3, 4, 4, 3, 4, 3, 5, 2, 1, 5, 5]) b = [2, 3, 4] * mult - assert_array_equal(np.invert(in1d(a, b)), in1d(a, b, invert=True)) - assert_array_equal(np.invert(in1d(a, b)), - in1d(a, b, invert=True, kind="sort")) - - for mult in (1, 10): - a = np.array([5, 4, 5, 3, 4, 4, 3, 4, 3, 5, 2, 1, 5, 5], - dtype=np.float32) - b = [2, 3, 4] * mult - b = np.array(b, dtype=np.float32) - assert_array_equal(np.invert(in1d(a, b)), in1d(a, b, invert=True)) - assert_array_equal(np.invert(in1d(a, b)), - in1d(a, b, invert=True, kind="sort")) - - def test_in1d_ravel(self): + assert_array_equal(np.invert(in1d(a, b, kind=kind)), + in1d(a, b, invert=True, kind=kind)) + + # float: + if kind in {None, "sort"}: + for mult in (1, 10): + a = np.array([5, 4, 5, 3, 4, 4, 3, 4, 3, 5, 2, 1, 5, 5], + dtype=np.float32) + b = [2, 3, 4] * mult + b = np.array(b, dtype=np.float32) + assert_array_equal(np.invert(in1d(a, b, kind=kind)), + in1d(a, b, invert=True, kind=kind)) + + @pytest.mark.parametrize("kind", [None, "sort", "table"]) + def test_in1d_ravel(self, kind): # Test that in1d ravels its input arrays. This is not documented # behavior however. The test is to ensure consistentency. a = np.arange(6).reshape(2, 3) @@ -432,20 +337,16 @@ class TestSetOps: long_b = np.arange(3, 63).reshape(30, 2) ec = np.array([False, False, False, True, True, True]) - assert_array_equal(in1d(a, b, assume_unique=True), ec) - assert_array_equal(in1d(a, b, assume_unique=False), ec) - assert_array_equal(in1d(a, long_b, assume_unique=True), ec) - assert_array_equal(in1d(a, long_b, assume_unique=False), ec) - assert_array_equal(in1d(a, b, assume_unique=True, kind="sort"), + assert_array_equal(in1d(a, b, assume_unique=True, kind=kind), ec) assert_array_equal(in1d(a, b, assume_unique=False, - kind="sort"), + kind=kind), ec) assert_array_equal(in1d(a, long_b, assume_unique=True, - kind="sort"), + kind=kind), ec) assert_array_equal(in1d(a, long_b, assume_unique=False, - kind="sort"), + kind=kind), ec) def test_in1d_hit_alternate_algorithm(self): @@ -464,19 +365,16 @@ class TestSetOps: c = in1d(a, b, assume_unique=True) assert_array_equal(c, ec) - def test_in1d_boolean(self): + @pytest.mark.parametrize("kind", [None, "sort", "table"]) + def test_in1d_boolean(self, kind): """Test that in1d works for boolean input""" a = np.array([True, False]) b = np.array([False, False, False]) expected = np.array([False, True]) assert_array_equal(expected, - in1d(a, b)) - assert_array_equal(expected, - in1d(a, b, kind="sort")) - assert_array_equal(np.invert(expected), - in1d(a, b, invert=True)) + in1d(a, b, kind=kind)) assert_array_equal(np.invert(expected), - in1d(a, b, invert=True, kind="sort")) + in1d(a, b, invert=True, kind=kind)) def test_in1d_first_array_is_object(self): ar1 = [None] -- cgit v1.2.1 From 1d3bdd15f6c12874e6d659a87aed21d58ebd272a Mon Sep 17 00:00:00 2001 From: Miles Cranmer Date: Wed, 22 Jun 2022 23:06:34 +0000 Subject: TST: Skip empty arrays for kind="table" --- numpy/lib/tests/test_arraysetops.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'numpy/lib/tests/test_arraysetops.py') diff --git a/numpy/lib/tests/test_arraysetops.py b/numpy/lib/tests/test_arraysetops.py index 26bf82b33..d91d36282 100644 --- a/numpy/lib/tests/test_arraysetops.py +++ b/numpy/lib/tests/test_arraysetops.py @@ -232,10 +232,11 @@ class TestSetOps: assert_isin_equal(5, 6) # empty array-like: - x = [] - assert_isin_equal(x, b) - assert_isin_equal(a, x) - assert_isin_equal(x, x) + if kind in {None, "sort"}: + x = [] + assert_isin_equal(x, b) + assert_isin_equal(a, x) + assert_isin_equal(x, x) @pytest.mark.parametrize("kind", [None, "sort", "table"]) def test_in1d(self, kind): @@ -296,7 +297,8 @@ class TestSetOps: c = in1d(a, b, kind=kind) assert_array_equal(c, ec) - assert_array_equal(in1d([], [], kind=kind), []) + if kind in {None, "sort"}: + assert_array_equal(in1d([], [], kind=kind), []) def test_in1d_char_array(self): a = np.array(['a', 'b', 'c', 'd', 'e', 'c', 'e', 'b']) -- cgit v1.2.1