hist icon indicating copy to clipboard operation
hist copied to clipboard

[FEATURE] Support for empty bin ranges

Open alexander-held opened this issue 2 years ago • 1 comments

(opening as feature request, unsure if it's technically more of a bug)

It is currently not possible to have a histogram with an axis where lower and upper bound match:

import hist

h = hist.Hist(hist.axis.Regular(bins=1, start=0, stop=0, name="x"))

results in

Traceback (most recent call last):
  File "[...]/test.py", line 3, in <module>
    h = hist.Hist(hist.axis.Regular(bins=1, start=0, stop=0, name="x"))
  File "[...]/lib/python3.9/site-packages/hist/axis/__init__.py", line 112, in __init__
    super().__init__(
  File "[...]/lib/python3.9/site-packages/boost_histogram/_internal/axis.py", line 347, in __init__
    ax = ca.regular_uoflow(bins, start, stop)
ValueError: range of axis is zero

The use case I have in mind for this is making a histogram for an unknown distribution, where lower/upper bounds are automatically discovered, and all values happen to be equal.

numpy seems to support this, but then seems to adjust bin edges:

>>> import numpy as np
>>> np.histogram([0, 0, 0], bins=1, range=(0,0))
(array([3]), array([-0.5,  0.5]))

Describe the problem, if any, that your feature request is related to

This came up via uproot-browser when plotting a histogram for a branch containing only zeros.

Describe the feature you'd like

Support for axes with an empty bin range.

Describe alternatives, if any, you've considered

Workarounds seem possible by externally checking that start/stop are not equal, and offsetting one of them if needed.

alexander-held avatar Mar 16 '22 18:03 alexander-held

numpy seems to support this, but then seems to adjust bin edges:

Ah, good, we could do the same thing. FYI, Regular(N, 0, 0) is not valid, since we don't include the upper edge, but the special numpy Regular axis mimics NumPy, so it would be valid. But let's mimic NumPy. Guessing it's just adding +/- .5?

henryiii avatar Mar 16 '22 19:03 henryiii