Skip to content

Commit

Permalink
Fix rebinning logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Saransh-cpp committed Feb 14, 2024
1 parent 0b6f4d3 commit d734fa4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
13 changes: 7 additions & 6 deletions src/boost_histogram/_internal/hist.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,9 +851,9 @@ def __getitem__(self: H, index: IndexingExpr) -> H | float | Accumulator:
# This ensures that callable start/stop are handled
start, stop = self.axes[i]._process_loc(ind.start, ind.stop)

groups = []
if ind != slice(None):
merge = 1
groups = []
if ind.step is not None:
if getattr(ind.step, "factor", None) is not None:
merge = ind.step.factor
Expand Down Expand Up @@ -891,10 +891,11 @@ def __getitem__(self: H, index: IndexingExpr) -> H | float | Accumulator:
axes = [reduced.axis(x) for x in range(reduced.rank())]
reduced_view = reduced.view(flow=True)
new_axes_indices = [axes[i].edges[0]]

j: int = 0
for group in groups:
new_axes_indices += [axes[i].edges[j + 1 : j + group + 1][-1]]
j = group
j += group

variable_axis = Variable(
new_axes_indices, metadata=axes[i].metadata
Expand All @@ -906,16 +907,16 @@ def __getitem__(self: H, index: IndexingExpr) -> H | float | Accumulator:

logger.debug("Axes: %s", axes)

# redistribute the bin contents here

new_reduced = reduced.__class__(axes)
new_reduced.view(flow=True)[...] = reduced_view
new_reduced.fill(
np.add.reduceat(reduced.to_numpy()[0], new_axes_indices, axis=i)
)
reduced = new_reduced

# Will be updated below
if slices or pick_set or pick_each or integrations:
reduced = self._hist
else:
elif len(groups) == 0:
logger.debug("Reduce actions are all empty, just making a copy")
reduced = copy.copy(self._hist)

Expand Down
2 changes: 1 addition & 1 deletion src/boost_histogram/tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def __init__(
def __repr__(self) -> str:
repr_str = f"{self.__class__.__name__}"
args: dict[str, int | Sequence[int] | None] = {
"value": self.factor,
"factor": self.factor,
"groups": self.groups,
}
for k, v in args.items():
Expand Down
2 changes: 1 addition & 1 deletion tests/test_histogram_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def test_repr():
assert repr(bh.overflow + 1) == "overflow + 1"
assert repr(bh.overflow - 1) == "overflow - 1"

assert repr(bh.rebin(2)) == "Rebinner(value=2)"
assert repr(bh.rebin(2)) == "Rebinner(factor=2)"


# Was broken in 0.6.1
Expand Down

0 comments on commit d734fa4

Please sign in to comment.