scikit-hep/hist

[BUG] Projected slice doesn't match UHI `::sum`

agoose77 opened this issue · 2 comments

Describe the bug

Binder

In this gist, I construct a from-scratch Hist object and populate it with some known data. When taking a projection of a slice, the result differs depending upon whether the projection is performed with the canonical project method, or a sum over the other axis.

This might be the consequence of a tricky thing I just ran into: project() will include bins in the underflow/overflow of axes other than the one you're projecting onto, whereas ::sum will not. For example:

>>> import hist
>>> h = hist.Hist(hist.axis.Regular(2, 0, 2), hist.axis.Regular(2, 0, 2))
>>> h.fill(0.5, 0.5)
Hist(
  Regular(2, 0, 2, label='Axis 0'),
  Regular(2, 0, 2, label='Axis 1'),
  storage=Double()) # Sum: 1.0
>>> h[1:, :]
Hist(
  Regular(1, 1, 2, label='Axis 0'),
  Regular(2, 0, 2, label='Axis 1'),
  storage=Double()) # Sum: 0.0 (1.0 with flow)
>>> h[1::sum, :].counts()
array([0., 0.])
>>> h[1:, :].project(1).counts()
array([1., 0.])

Whether this is a bug or not depends on the interpretation of projection, I suppose. In the UHI case, the behavior is at least explicitly documented, but I don't see any documentation for the intended project() behavior. My naive expectation was that it would respect the slice bounds, but I see now that the complication arises from the fact that the sliced histogram just moves bins outside of the slice into the flow bins...

Edit: Ah, this was noted in the discussion #384 as well