From b939bc62f130ebf4b9f15ff8a20cfb547fdc1e49 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 20 May 2021 14:59:31 -0400 Subject: [PATCH] fix: support scaling and adding flow bins --- src/boost_histogram/_internal/hist.py | 2 +- tests/test_histogram.py | 2 +- tests/test_histogram_indexing.py | 26 ++++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/boost_histogram/_internal/hist.py b/src/boost_histogram/_internal/hist.py index 659c672c9..d9c217a3d 100644 --- a/src/boost_histogram/_internal/hist.py +++ b/src/boost_histogram/_internal/hist.py @@ -380,7 +380,7 @@ def _compute_inplace_op( ) ) else: - view = self.view(flow=False) + view = self.view(flow=True) getattr(view, name)(other) self._variance_known = False return self diff --git a/tests/test_histogram.py b/tests/test_histogram.py index d2bea341b..aa443fa42 100644 --- a/tests/test_histogram.py +++ b/tests/test_histogram.py @@ -1178,7 +1178,7 @@ def test_add_broadcast(): h2 = h + [[1]] assert h2.sum() == 10 * 20 - assert h2.sum(flow=True) == 10 * 20 + assert h2.sum(flow=True) == 12 * 22 h3 = h + np.ones((10, 20)) assert h3.sum() == 10 * 20 diff --git a/tests/test_histogram_indexing.py b/tests/test_histogram_indexing.py index aeb33937d..f449ba285 100644 --- a/tests/test_histogram_indexing.py +++ b/tests/test_histogram_indexing.py @@ -429,3 +429,29 @@ def test_single_flow_bin(): assert h[3::sum] == 1 assert h[1:2][sum] == 5 + + +# issue 579 + + +def test_scale_flowbins(): + w = 1e-1 + x = np.random.normal(loc=0.4, scale=0.4, size=100) + + h = bh.Histogram(bh.axis.Variable([0, 0.5, 1]), storage=bh.storage.Weight()) + + h.fill(x, weight=w) + + ref_value = h.values(flow=True) * 5 + scale_value = (h * 5).values(flow=True) + + assert scale_value == approx(ref_value) + + +def test_add_flowbins(): + h = bh.Histogram(bh.axis.Variable([0, 0.5, 1]), storage=bh.storage.Weight()) + + ref_value = h.values(flow=True) + 5 + scale_value = (h + 5).values(flow=True) + + assert scale_value == approx(ref_value)