Skip to content

Commit 4062bdd

Browse files
committed
feat: allow shortcut rebin and sum
1 parent 1e0c278 commit 4062bdd

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

src/boost_histogram/_internal/hist.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -442,14 +442,13 @@ def _compute_commonindex(self, index):
442442

443443
# Allow [bh.loc(...)] to work
444444
for i in range(len(indexes)):
445-
if callable(indexes[i]):
445+
# Support sum and rebin directly
446+
if indexes[i] is sum or hasattr(indexes[i], "factor"):
447+
indexes[i] = slice(None, None, indexes[i])
448+
# General locators
449+
elif callable(indexes[i]):
446450
indexes[i] = indexes[i](self.axes[i])
447-
elif hasattr(indexes[i], "flow"):
448-
if indexes[i].flow == 1:
449-
indexes[i] = hist.axis(i).size
450-
elif indexes[i].flow == -1:
451-
indexes[i] = -1
452-
elif isinstance(indexes[i], int):
451+
elif hasattr(indexes[i], "__index__"):
453452
if abs(indexes[i]) >= hist.axis(i).size:
454453
raise IndexError("histogram index is out of range")
455454
indexes[i] %= hist.axis(i).size

tests/test_histogram.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,9 +569,13 @@ def test_shrink_1d():
569569
def test_rebin_1d():
570570
h = bh.Histogram(bh.axis.Regular(20, 1, 5))
571571
h.fill(1.1)
572+
572573
hs = h[{0: slice(None, None, bh.rebin(4))}]
573574
assert_array_equal(hs.view(), [1, 0, 0, 0, 0])
574575

576+
hs = h[{0: bh.rebin(4)}]
577+
assert_array_equal(hs.view(), [1, 0, 0, 0, 0])
578+
575579

576580
def test_shrink_rebin_1d():
577581
h = bh.Histogram(bh.axis.Regular(20, 0, 4))
@@ -594,6 +598,7 @@ def test_rebin_nd():
594598
assert h[{0: s[:: bh.rebin(2)], 2: s[:: bh.rebin(2)]}].axes.size == (10, 30, 20)
595599

596600
assert h[{1: s[:: bh.sum]}].axes.size == (20, 40)
601+
assert h[{1: bh.sum}].axes.size == (20, 40)
597602

598603

599604
# CLASSIC: This used to have metadata too, but that does not compare equal

tests/test_histogram_indexing.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ def test_get_1D_slice():
7373

7474
assert len(h1[2:4].view()) == 2
7575
assert len(h1[2 : 4 : bh.rebin(2)].view()) == 1
76+
assert len(h1[:: bh.rebin(2)].view()) == 5
77+
78+
# Shortcut
79+
assert len(h1[bh.rebin(2)].view()) == 5
7680

7781
assert h1[2:4].metadata == {"that": 3}
7882

@@ -123,6 +127,11 @@ def test_basic_projection():
123127
assert h1 == h2[..., ::sum, ::sum]
124128
assert h2.sum(flow=True) == h2[::sum, ::sum, ::sum]
125129

130+
# Shortcut
131+
assert h1 == h2[:, sum, sum]
132+
assert h1 == h2[..., sum, sum]
133+
assert h2.sum(flow=True) == h2[sum, sum, sum]
134+
126135

127136
def test_slicing_projection():
128137
h1 = bh.Histogram(
@@ -192,6 +201,9 @@ def test_mix_value_with_slice_2():
192201
assert_array_equal(h[:, :, True].view(), vals)
193202
assert_array_equal(h[:, :, False].view(), 0)
194203

204+
h2 = h[bh.rebin(2), bh.rebin(5), :]
205+
assert_array_equal(h2.shape, (5, 2, 2))
206+
195207

196208
def test_repr():
197209
assert repr(bh.loc(2)) == "loc(2)"

0 commit comments

Comments
 (0)