Skip to content

Commit

Permalink
Fix cumsum
Browse files Browse the repository at this point in the history
  • Loading branch information
dcherian committed Jul 28, 2024
1 parent a4c0c94 commit fee245f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 4 additions & 1 deletion numpy_groupies/aggregate_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,10 @@ def _cumsum(group_idx, a, size, fill_value=None, dtype=None):

increasing = np.arange(len(a), dtype=int)
group_starts = _min(group_idx_srt, increasing, size, fill_value=0)[group_idx_srt]
a_srt_cumsum += -a_srt_cumsum[group_starts] + a_srt[group_starts]
# First subtract large numbers
a_srt_cumsum -= a_srt_cumsum[group_starts]
# Then add potentially small numbers
a_srt_cumsum += a_srt[group_starts]
return a_srt_cumsum[invsortidx]


Expand Down
11 changes: 11 additions & 0 deletions numpy_groupies/tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,3 +570,14 @@ def test_var_with_nan_fill_value(aggregate_all, ddof, nan_inds, func):
group_idx, a, axis=-1, fill_value=np.nan, func=func, ddof=ddof
)
np.testing.assert_equal(actual, expected)


def test_cumsum_accuracy(aggregate_all):
array = np.array(
[0.00000000e00, 0.00000000e00, 0.00000000e00, 3.27680000e04, 9.99999975e-06]
)
group_idx = np.array([0, 0, 0, 0, 1])

actual = aggregate_all(group_idx, array, axis=-1, func="cumsum")
expected = array
np.testing.assert_allclose(actual, expected)

0 comments on commit fee245f

Please sign in to comment.