Skip to content

Commit a9dc4a5

Browse files
authored
Fix projections and fix test for projection, add sum behavior (#141)
1 parent 6b40245 commit a9dc4a5

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

boost_histogram/_hist.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def _compute_getitem(self, index):
110110
except RuntimeError:
111111
pass
112112

113-
projections = []
113+
integrations = set()
114114
slices = []
115115

116116
# Compute needed slices and projections
@@ -125,7 +125,7 @@ def _compute_getitem(self, index):
125125
if ind.step is not None:
126126
if hasattr(ind.step, "projection"):
127127
if ind.step.projection:
128-
projections.append(i)
128+
integrations.add(i)
129129
if ind.start is not None or ind.stop is not None:
130130
raise IndexError(
131131
"Currently cut projections are not supported"
@@ -150,7 +150,11 @@ def _compute_getitem(self, index):
150150
slices.append(_core.algorithm.slice_and_rebin(i, begin, end, merge))
151151

152152
reduced = self.reduce(*slices)
153-
return reduced.project(*projections) if projections else reduced
153+
if not integrations:
154+
return reduced
155+
else:
156+
projections = [i for i in range(self.rank) if i not in integrations]
157+
return reduced.project(*projections) if projections else self.sum(flow=True)
154158

155159

156160
def _compute_setitem(self, index, value):

tests/test_indexing.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,18 @@ def test_ellipsis():
9292

9393

9494
def test_basic_projection():
95-
h2 = bh.histogram(bh.axis.regular(10, 0, 1), bh.axis.regular(10, 0, 1))
96-
h1 = bh.histogram(bh.axis.regular(10, 0, 1))
95+
h2 = bh.histogram(
96+
bh.axis.regular(10, 0, 10),
97+
bh.axis.regular(10, 0, 10),
98+
bh.axis.regular(10, 0, 10),
99+
)
100+
h1 = bh.histogram(bh.axis.regular(10, 0, 10))
97101

98-
contents = [[2, 2, 2, 3, 4, 5, 6], [1, 2, 2, 3, 2, 1, 2]]
102+
contents = [[2, 2, 2, 3, 4, 5, 6], [1, 2, 2, 3, 2, 1, 2], [-12, 33, 4, 9, 2, 4, 9]]
99103

100104
h1.fill(contents[0])
101105
h2.fill(*contents)
102106

103-
assert h1 == h2[:, :: bh.project]
107+
assert h1 == h2[:, :: bh.project, :: bh.project]
108+
assert h1 == h2[..., :: bh.project, :: bh.project]
109+
assert h2.sum(flow=True) == h2[:: bh.project, :: bh.project, :: bh.project]

0 commit comments

Comments
 (0)