Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow axis + other #982

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/boost_histogram/tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,19 @@ def __init__(
edges: Sequence[int | float] | None = None,
axis: PlottableAxis | None = None,
) -> None:
if (
sum(i is not None for i in [factor_or_axis, factor, groups, edges, axis])
!= 1
):
if isinstance(factor_or_axis, int):
factor = factor_or_axis
elif factor_or_axis is not None:
axis = factor_or_axis

total_args = sum(i is not None for i in [factor, groups, edges])
if total_args != 1 and axis is None:
raise ValueError("Exactly one argument should be provided")

self.groups = groups
self.edges = edges
self.axis = axis
self.factor = factor
if isinstance(factor_or_axis, int):
self.factor = factor_or_axis
elif factor_or_axis is not None:
self.axis = factor_or_axis

def __repr__(self) -> str:
repr_str = f"{self.__class__.__name__}"
Expand Down Expand Up @@ -177,10 +177,10 @@ def group_mapping(self, axis: PlottableAxis) -> Sequence[int]:
return [self.factor] * len(axis)
if self.edges is not None or self.axis is not None:
newedges = None
if self.axis is not None and hasattr(self.axis, "edges"):
newedges = self.axis.edges
elif self.edges is not None:
if self.edges is not None:
newedges = self.edges
elif self.axis is not None and hasattr(self.axis, "edges"):
newedges = self.axis.edges

if newedges is not None and hasattr(axis, "edges"):
assert newedges[0] == axis.edges[0], "Edges must start at first bin"
Expand Down
18 changes: 18 additions & 0 deletions tests/test_histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,24 @@
assert_array_equal(hs.view(flow=True), [2, 2])


def test_rebin_change_axis_int():
h = bh.Histogram(bh.axis.Regular(5, 0, 5))
h.fill([-1, 1.1, 2.2, 3.3, 4.4, 5.5])
hs = h[bh.rebin(edges=[0, 3, 5.0], axis=bh.axis.Integer(10, 12))]
assert_array_equal(hs.view(), [2, 2])
assert_array_equal(hs.view(flow=True), [1, 2, 2, 1])
assert_array_equal(hs.axes.edges[0], [10, 11, 12])


def test_rebin_change_axis_cat():
h = bh.Histogram(bh.axis.Regular(5, 0, 5))
h.fill([-1, 1.1, 2.2, 3.3, 4.4, 5.5])
hs = h[bh.rebin(groups=[2, 2, 1], axis=bh.axis.StrCategory(["a", "b"]))]

Check failure on line 703 in tests/test_histogram.py

View workflow job for this annotation

GitHub Actions / CMake 🐍 3.9

test_rebin_change_axis_cat IndexError: index 3 is out of bounds for axis 0 with size 3

Check failure on line 703 in tests/test_histogram.py

View workflow job for this annotation

GitHub Actions / CMake 🐍 3.13

test_rebin_change_axis_cat IndexError: index 3 is out of bounds for axis 0 with size 3

Check failure on line 703 in tests/test_histogram.py

View workflow job for this annotation

GitHub Actions / CMake 🐍 pypy3.10

test_rebin_change_axis_cat IndexError: index 3 is out of bounds for axis 0 with size 3

Check failure on line 703 in tests/test_histogram.py

View workflow job for this annotation

GitHub Actions / CMake 🐍 3.8

test_rebin_change_axis_cat IndexError: index 3 is out of bounds for axis 0 with size 3
assert_array_equal(hs.view(), [2, 2])
assert_array_equal(hs.view(flow=True), [1, 2, 2, 1])
assert_array_equal(hs.axes.edges[0], [10, 11, 12])


def test_shrink_rebin_1d():
h = bh.Histogram(bh.axis.Regular(20, 0, 4))
h.fill(1.1)
Expand Down
Loading