Skip to content

Commit

Permalink
fix: categorical axes need special flow handling (#564)
Browse files Browse the repository at this point in the history
  • Loading branch information
henryiii authored Apr 28, 2021
1 parent 5fa891b commit 9afbcd5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
2 changes: 2 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
## UPCOMING

* Fix scaling a weighted storage [#559][]
* Fix partial summation over a Categorical axis [#564][]
* Support running type checking from Python < 3.8 [#542][]

[#542]: https://github.com/scikit-hep/boost-histogram/pull/542
[#559]: https://github.com/scikit-hep/boost-histogram/pull/559
[#564]: https://github.com/scikit-hep/boost-histogram/pull/564

## Version 1.0

Expand Down
16 changes: 12 additions & 4 deletions src/boost_histogram/_internal/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,23 @@ def _process_loc(
Compute start and stop into actual start and stop values in Boost.Histogram.
None -> -1 or 0 for start, -> len or len+1 for stop. If start or stop are
callable, then call them with the axes.
For a non-ordered axes, flow is all or nothing, so this will ensure overflow
is turned off if underflow is not None.
"""

def _process_internal(item: Optional[AxCallOrInt], default: int) -> int:
return default if item is None else item(self) if callable(item) else item

begin = _process_internal(start, -1 if self._ax.traits_underflow else 0)
end = _process_internal(
stop, len(self) + (1 if self._ax.traits_overflow else 0)
)
underflow = -1 if self._ax.traits_underflow else 0
overflow = 1 if self._ax.traits_overflow else 0

# Non-ordered axes only use flow if integrating from None to None
if not self._ax.traits_ordered and not (start is None and stop is None):
overflow = 0

begin = _process_internal(start, underflow)
end = _process_internal(stop, len(self) + overflow)

return begin, end

Expand Down
2 changes: 1 addition & 1 deletion src/register_algorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void register_algorithms(py::module& algorithm) {
self.begin.index,
self.end.index,
merge,
self.crop);
self.crop ? "slice_mode.crop" : "slice_mode.shrink");
} else {
return py::
str("reduce_command(shrink{0}({1}, lower={2}, upper={3}{4}))")
Expand Down
27 changes: 27 additions & 0 deletions tests/test_histogram_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,30 @@ def test_axes_tuple_Nd():

assert b1.ndim == 3
assert a1.ndim == 2


# issue 556
def test_single_flow_bin():
# Flow is removed for category axes unless full sum is used
h = bh.Histogram(bh.axis.IntCategory([0, 1, 2]))
h.view(True)[:] = 1

assert h[::sum] == 4
assert h[0::sum] == 3
assert h[1::sum] == 2
assert h[2::sum] == 1
with pytest.raises(ValueError):
h[3::sum]

assert h[1:2][sum] == 4

h = bh.Histogram(bh.axis.Integer(0, 3))
h.view(True)[:] = 1

assert h[::sum] == 5
assert h[0::sum] == 4
assert h[1::sum] == 3
assert h[2::sum] == 2
assert h[3::sum] == 1

assert h[1:2][sum] == 5

0 comments on commit 9afbcd5

Please sign in to comment.