scikit-hep / hist

Histogramming for analysis powered by boost-histogram
https://hist.readthedocs.io
BSD 3-Clause "New" or "Revised" License
128 stars 25 forks source link

[BUG] Indexing with negative numbers #559

Open andrzejnovak opened 9 months ago

andrzejnovak commented 9 months ago

Describe the bug

hist doesn't process negative indexing on axes.

Steps to reproduce

import hist
import numpy as np

h1 = hist.new.Reg(10, 0, 10).Weight().fill(np.random.normal(5, 2, int(1e6)))
# Works fine
h1[1:4]

# Throws an error
h1[1:-2]

Trace:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[8], line 1
----> 1 h1[1:-2]

File ~/software/micromamba/envs/def/lib/python3.11/site-packages/hist/basehist.py:324, in BaseHist.__getitem__(self, index)
    317 def __getitem__(  # type: ignore[override]
    318     self: T, index: IndexingExpr
    319 ) -> T | float | bh.accumulators.Accumulator:
    320     """
    321     Get histogram item.
    322     """
--> 324     return super().__getitem__(self._index_transform(index))

File ~/software/micromamba/envs/def/lib/python3.11/site-packages/boost_histogram/_internal/hist.py:908, in Histogram.__getitem__(self, index)
    906         assert isinstance(start, int)
    907         assert isinstance(stop, int)
--> 908         slices.append(_core.algorithm.slice_and_rebin(i, start, stop, merge))
    910 # Will be updated below
    911 if slices or pick_set or pick_each or integrations:

ValueError: begin < end required
matthewfeickert commented 9 months ago

@henryiii is this an intentional design choice, or is this an edge case that hadn't gotten caught yet?