Skip to content

Commit 3919b73

Browse files
fix: make non-uniform rebinning work for Weight() and friends (#972)
* fix: make non-uniform rebinning work for Weight() and friends * update changelog * silence pylint * fix: use integrated support for adding views Signed-off-by: Henry Schreiner <[email protected]> --------- Signed-off-by: Henry Schreiner <[email protected]> Co-authored-by: Henry Schreiner <[email protected]>
1 parent 323fcc9 commit 3919b73

File tree

4 files changed

+101
-5
lines changed

4 files changed

+101
-5
lines changed

.github/CONTRIBUTING.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ specific jobs:
4242
```console
4343
$ nox -l # List all the defined sessions
4444
$ nox -s lint # Lint only
45-
$ nox -s tests-3.9 # Python 3.9 tests only
45+
$ nox -s tests # Tests only
4646
$ nox -s docs -- serve # Build and serve the docs
4747
$ nox -s make_pickle # Make a pickle file for this version
4848
```
4949

50-
Nox handles everything for you, including setting up an temporary virtual
50+
Nox handles everything for you, including setting up a temporary virtual
5151
environment for each run.
5252

5353
### Pip

docs/changelog.md

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
## Version 1.5
44

5+
### Version 1.5.1
6+
7+
Make non-uniform rebinning work for Weight() and friends [#972][]
8+
9+
[#972]: https://github.com/scikit-hep/boost-histogram/pull/972
10+
511
### Version 1.5.0
612

713
#### Features

src/boost_histogram/_internal/hist.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -921,9 +921,9 @@ def __getitem__(self: H, index: IndexingExpr) -> H | float | Accumulator:
921921
for new_j, group in enumerate(groups):
922922
for _ in range(group):
923923
pos = [slice(None)] * (i)
924-
new_view[(*pos, new_j + 1, ...)] += reduced_view[
925-
(*pos, j, ...)
926-
]
924+
new_view[(*pos, new_j + 1, ...)] += _to_view(
925+
reduced_view[(*pos, j, ...)]
926+
)
927927
j += 1
928928

929929
reduced = new_reduced

tests/test_storage.py

+90
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,93 @@ def test_empty_axis_histogram(storage):
301301
)
302302
assert h2d.sum() == h2d.storage_type.accumulator()
303303
assert h2d.sum(flow=True) == h2d.storage_type.accumulator()
304+
305+
306+
# Issue #971
307+
def test_non_uniform_rebin_with_weights():
308+
# 1D
309+
h = bh.Histogram(bh.axis.Regular(20, 1, 5), storage=bh.storage.Weight())
310+
h.fill([1.1, 2.2, 3.3, 4.4])
311+
312+
rslt = np.array(
313+
[(1.0, 1.0), (1.0, 1.0), (1.0, 1.0), (0.0, 0.0), (1.0, 1.0)],
314+
dtype=[("value", "<f8"), ("variance", "<f8")],
315+
)
316+
317+
hs = h[{0: slice(None, None, bh.rebin(4))}]
318+
assert_array_equal(hs.view(), rslt)
319+
320+
hs = h[{0: bh.rebin(4)}]
321+
assert_array_equal(hs.view(), rslt)
322+
323+
hs = h[{0: bh.rebin(groups=[1, 2, 3, 14])}]
324+
assert_array_equal(
325+
hs.view(),
326+
np.array(
327+
[(1.0, 1.0), (0.0, 0.0), (0.0, 0.0), (3.0, 3.0)],
328+
dtype=[("value", "<f8"), ("variance", "<f8")],
329+
),
330+
)
331+
assert_array_equal(hs.axes.edges[0], [1.0, 1.2, 1.6, 2.2, 5.0])
332+
333+
# nD
334+
h = bh.Histogram(
335+
bh.axis.Regular(20, 1, 3),
336+
bh.axis.Regular(30, 1, 3),
337+
bh.axis.Regular(40, 1, 3),
338+
storage=bh.storage.Weight(),
339+
)
340+
341+
s = bh.tag.Slicer()
342+
343+
assert h[{0: s[:: bh.rebin(groups=[1, 2, 17])]}].axes.size == (3, 30, 40)
344+
assert h[{1: s[:: bh.rebin(groups=[1, 2, 27])]}].axes.size == (20, 3, 40)
345+
assert h[{2: s[:: bh.rebin(groups=[1, 2, 37])]}].axes.size == (20, 30, 3)
346+
assert np.all(
347+
np.isclose(
348+
h[{0: s[:: bh.rebin(groups=[1, 2, 17])]}].axes[0].edges,
349+
[1.0, 1.1, 1.3, 3.0],
350+
)
351+
)
352+
assert np.all(
353+
np.isclose(
354+
h[{1: s[:: bh.rebin(groups=[1, 2, 27])]}].axes[1].edges,
355+
[1.0, 1.06666667, 1.2, 3.0],
356+
)
357+
)
358+
assert np.all(
359+
np.isclose(
360+
h[{2: s[:: bh.rebin(groups=[1, 2, 37])]}].axes[2].edges,
361+
[1.0, 1.05, 1.15, 3.0],
362+
)
363+
)
364+
365+
assert h[
366+
{0: s[:: bh.rebin(groups=[1, 2, 17])], 2: s[:: bh.rebin(groups=[1, 2, 37])]}
367+
].axes.size == (3, 30, 3)
368+
assert np.all(
369+
np.isclose(
370+
h[
371+
{
372+
0: s[:: bh.rebin(groups=[1, 2, 17])],
373+
2: s[:: bh.rebin(groups=[1, 2, 37])],
374+
}
375+
]
376+
.axes[0]
377+
.edges,
378+
[1.0, 1.1, 1.3, 3],
379+
)
380+
)
381+
assert np.all(
382+
np.isclose(
383+
h[
384+
{
385+
0: s[:: bh.rebin(groups=[1, 2, 17])],
386+
2: s[:: bh.rebin(groups=[1, 2, 37])],
387+
}
388+
]
389+
.axes[2]
390+
.edges,
391+
[1.0, 1.05, 1.15, 3.0],
392+
)
393+
)

0 commit comments

Comments
 (0)