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: axis metadata was broken when rebinning #978

Merged
merged 1 commit into from
Jan 31, 2025
Merged
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
6 changes: 3 additions & 3 deletions src/boost_histogram/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def __init__(
if len(axes) == 1 and isinstance(axes[0], tuple(_histograms)):
cpp_hist: CppHistogram = axes[0] # type: ignore[assignment]
self._from_histogram_cpp(cpp_hist)
if metadata:
if metadata is not None:
self.metadata = metadata
return

Expand All @@ -261,7 +261,7 @@ def __init__(
if len(axes) == 1 and isinstance(axes[0], Histogram):
normal_hist: Histogram = axes[0]
self._from_histogram_object(normal_hist)
if metadata:
if metadata is not None:
self.metadata = metadata
return

Expand Down Expand Up @@ -959,7 +959,7 @@ def __getitem__(self: H, index: IndexingExpr) -> H | float | Accumulator:
j += group

variable_axis = Variable(
new_axes_indices, metadata=axes[i].metadata
new_axes_indices, __dict__=axes[i].metadata
)
axes[i] = variable_axis._ax

Expand Down
10 changes: 8 additions & 2 deletions tests/test_histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,19 +630,25 @@ def test_shrink_1d():
assert_array_equal(hs.view(), [1, 0, 0, 0, 0])


def test_rebin_1d():
h = bh.Histogram(bh.axis.Regular(20, 1, 5))
@pytest.mark.parametrize(
"metadata", [None, {}, {"a": "1"}], ids=["None", "empty", "dict"]
)
def test_rebin_1d(metadata):
h = bh.Histogram(bh.axis.Regular(20, 1, 5, metadata=metadata))
h.fill([1.1, 2.2, 3.3, 4.4])

hs = h[{0: slice(None, None, bh.rebin(4))}]
assert_array_equal(hs.view(), [1, 1, 1, 0, 1])
assert h.axes[0].metadata is hs.axes[0].metadata

hs = h[{0: bh.rebin(4)}]
assert_array_equal(hs.view(), [1, 1, 1, 0, 1])
assert h.axes[0].metadata is hs.axes[0].metadata

hs = h[{0: bh.rebin(groups=[1, 2, 3, 14])}]
assert_array_equal(hs.view(), [1, 0, 0, 3])
assert_array_equal(hs.axes.edges[0], [1.0, 1.2, 1.6, 2.2, 5.0])
assert h.axes[0].metadata is hs.axes[0].metadata


def test_shrink_rebin_1d():
Expand Down
Loading