Skip to content

Commit 29a0f10

Browse files
authored
fix: bool pick for 2D+ (#398)
1 parent 95705dd commit 29a0f10

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/boost_histogram/_internal/hist.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,9 @@ def __getitem__(self, index): # noqa: C901
565565

566566
indexes = self._compute_commonindex(index)
567567

568+
# Workaround for missing "pick" and no reduction on bool axes
569+
pick_bool = {}
570+
568571
# If this is (now) all integers, return the bin contents
569572
# But don't try *dict!
570573
if not hasattr(indexes, "items"):
@@ -579,7 +582,11 @@ def __getitem__(self, index): # noqa: C901
579582
# Compute needed slices and projections
580583
for i, ind in enumerate(indexes):
581584
if hasattr(ind, "__index__"):
582-
ind = slice(ind.__index__(), ind.__index__() + 1, sum)
585+
if isinstance(self.axes[i]._ax, _core.axis.boolean):
586+
pick_bool[i] = ind
587+
ind = slice(None, None, sum)
588+
else:
589+
ind = slice(ind.__index__(), ind.__index__() + 1, sum)
583590

584591
elif not isinstance(ind, slice):
585592
raise IndexError(
@@ -612,6 +619,13 @@ def __getitem__(self, index): # noqa: C901
612619

613620
slices.append(_core.algorithm.slice_and_rebin(i, start, stop, merge))
614621

622+
for i, select in pick_bool.items():
623+
self = self.copy()
624+
view = self.view(flow=True)
625+
view[
626+
tuple(~select if j == i else slice(None) for j in range(self.ndim))
627+
] = 0
628+
615629
reduced = self._hist.reduce(*slices)
616630

617631
if not integrations:

tests/test_histogram.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,18 @@ def test_pickle_1():
626626
assert a == b
627627

628628

629+
def test_pick_bool():
630+
a = bh.Histogram(bh.axis.Boolean(), bh.axis.Boolean(metadata={"one": 1}))
631+
632+
a.fill([True, True, False, False], [True, False, True, True])
633+
a.fill([True, True, True], True)
634+
635+
assert_array_equal(a[True, :].view(), [1, 4])
636+
assert_array_equal(a[False, :].view(), [0, 2])
637+
assert_array_equal(a[:, False].view(), [0, 1])
638+
assert_array_equal(a[:, True].view(), [2, 4])
639+
640+
629641
def test_pickle_bool():
630642
a = bh.Histogram(bh.axis.Boolean(), bh.axis.Boolean(metadata={"one": 1}))
631643
assert isinstance(a, bh.Histogram)

0 commit comments

Comments
 (0)