Skip to content

Commit 1f1bd1e

Browse files
authored
Merge pull request #103 from antonsoo/fix/integer-argreduction-comparisons
Preserve integer comparisons in Numba argreductions
2 parents c566605 + 7aedaed commit 1f1bd1e

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

‎numpy_groupies/aggregate_numba.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,8 @@ def _inner(ri, val, ret, counter, mean, fill_value):
646646

647647

648648
class ArgMax(AggregateOp):
649-
mean_fill_value = np.nan
649+
mean_fill_value = 0
650+
mean_dtype = None
650651

651652
@classmethod
652653
def callable(cls, nans=False, reverse=False, scalar=False):

‎numpy_groupies/tests/test_numba_fastpath.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,25 @@ def test_size_detection_matches_explicit():
9999
np.testing.assert_array_equal(aggregate(group_idx, a, func="sum"), ref)
100100

101101

102+
@pytest.mark.parametrize("func", ["argmin", "argmax", "nanargmin", "nanargmax"])
103+
@pytest.mark.parametrize("dtype", [np.int64, np.uint64])
104+
@pytest.mark.parametrize("size", [3, np.int64(3)])
105+
def test_integer_argreductions_preserve_comparisons(func, dtype, size):
106+
base = int(np.iinfo(dtype).max) - 8
107+
a = np.array([base + 1, base + 2, base + 2, base + 4, base + 3], dtype=dtype)
108+
group_idx = np.array([0, 0, 0, 1, 1])
109+
expected = [0, 4, -1] if "min" in func else [1, 3, -1]
110+
np.testing.assert_array_equal(aggregate(group_idx, a, func=func, size=size), expected)
111+
112+
113+
@pytest.mark.parametrize("func", ["argmin", "argmax", "nanargmin", "nanargmax"])
114+
def test_integer_argreductions_along_axis(func):
115+
a = np.array([[2**53 + 1, 2**53, 2**53 + 2], [-(2**53), -(2**53) - 1, -(2**53) - 2]])
116+
expected = [[1, 2, -1], [1, 2, -1]] if "min" in func else [[0, 2, -1], [0, 2, -1]]
117+
result = aggregate(np.array([0, 0, 1]), a, func=func, axis=1, size=3)
118+
np.testing.assert_array_equal(result, expected)
119+
120+
102121
def test_errors_match_slow_path():
103122
rng = np.random.default_rng(46)
104123
group_idx = rng.integers(0, 5, 100)

0 commit comments

Comments
 (0)