Skip to content

Commit 8901037

Browse files
authored
feat: Allow addition of exactly 0 (#370)
1 parent 730bdaa commit 8901037

File tree

4 files changed

+61
-7
lines changed

4 files changed

+61
-7
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,5 @@ compile_commands.json
6969
# Testing
7070
/.benchmarks/*
7171

72-
# IDEs
73-
/.idea
72+
# PyCharm
73+
.idea

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ message(STATUS "boost-histogram ${BOOST_HISTOGRAM_VERSION}")
220220

221221
if(BUILD_TESTING)
222222

223-
python_import(numpy pytest pytest-benchmark six typing)
223+
python_import(numpy pytest pytest-benchmark typing)
224224

225225
# Support for running from build directory
226226
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/pytest.ini"

src/boost_histogram/_internal/hist.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,25 @@ def __array__(self):
155155
return self.view(False)
156156

157157
def __add__(self, other):
158-
return self.__class__(self._hist.__add__(other._hist))
158+
if hasattr(other, "_hist"):
159+
return self.__class__(self._hist.__add__(other._hist))
160+
else:
161+
retval = self.copy()
162+
retval += other
163+
return retval
159164

160165
def __iadd__(self, other):
161-
self._hist.__iadd__(other._hist)
162-
self.axes = self._generate_axes_()
163-
return self
166+
if isinstance(other, (int, float)) and other == 0:
167+
return self
168+
if hasattr(other, "_hist"):
169+
self._hist.__iadd__(other._hist)
170+
# Addition may change category axes
171+
self.axes = self._generate_axes_()
172+
return self
173+
return NotImplemented
174+
175+
def __radd__(self, other):
176+
return self + other
164177

165178
def __eq__(self, other):
166179
return self._hist == other._hist

tests/test_histogram.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,47 @@ def test_fill_2d(flow):
283283

284284

285285
def test_add_2d(flow):
286+
h0 = bh.Histogram(
287+
bh.axis.Integer(-1, 2, underflow=flow, overflow=flow),
288+
bh.axis.Regular(4, -2, 2, underflow=flow, overflow=flow),
289+
)
290+
assert isinstance(h0, bh.Histogram)
291+
292+
h0.fill(-1, -2)
293+
h0.fill(-1, -1)
294+
h0.fill(0, 0)
295+
h0.fill(0, 1)
296+
h0.fill(1, 0)
297+
h0.fill(3, -1)
298+
h0.fill(0, -3)
299+
300+
m = [
301+
[1, 1, 0, 0, 0, 0],
302+
[0, 0, 1, 1, 0, 1],
303+
[0, 0, 1, 0, 0, 0],
304+
[0, 1, 0, 0, 0, 0],
305+
[0, 0, 0, 0, 0, 0],
306+
]
307+
308+
h = h0.copy()
309+
h += h
310+
for i in range(-flow, h.axes[0].size + flow):
311+
for j in range(-flow, h.axes[1].size + flow):
312+
assert h[bh.tag.at(i), bh.tag.at(j)] == 2 * m[i][j]
313+
314+
h = sum([h0, h0])
315+
for i in range(-flow, h.axes[0].size + flow):
316+
for j in range(-flow, h.axes[1].size + flow):
317+
assert h[bh.tag.at(i), bh.tag.at(j)] == 2 * m[i][j]
318+
319+
h = 0 + h0 + h0
320+
for i in range(-flow, h.axes[0].size + flow):
321+
for j in range(-flow, h.axes[1].size + flow):
322+
assert h[bh.tag.at(i), bh.tag.at(j)] == 2 * m[i][j]
323+
324+
325+
@pytest.mark.parametrize("flow", [True, False])
326+
def test_add_2d_fancy(flow):
286327
h = bh.Histogram(
287328
bh.axis.Integer(-1, 2, underflow=flow, overflow=flow),
288329
bh.axis.Regular(4, -2, 2, underflow=flow, overflow=flow),

0 commit comments

Comments
 (0)