Skip to content

[BUG] plot1d doesn't work with IntCat axis #560

@andrzejnovak

Description

@andrzejnovak

Describe the bug

plo1d (with overlay for a categorical axis) only works for StrCat but not for IntCat

Steps to reproduce

import hist
import numpy as np

h2 = hist.new.Reg(10, 0, 10).IntCat([], growth=True).Weight().fill(np.random.normal(5, 2, int(1e6)), np.random.choice([1,2,3,4], int(1e6)))
h2.plot1d()
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[13], line 1
----> 1 h2.plot1d()

File ~/software/micromamba/envs/def/lib/python3.11/site-packages/hist/basehist.py:424, in BaseHist.plot1d(self, ax, overlay, **kwargs)
    422 cat_ax = self.axes[overlay]
    423 cats = cat_ax if cat_ax.traits.discrete else np.arange(len(cat_ax.centers))
--> 424 d1hists = [self[{overlay: cat}] for cat in cats]
    425 if "label" in kwargs:
    426     if not isinstance(kwargs["label"], str) and len(kwargs["label"]) == len(
    427         cats
    428     ):

File ~/software/micromamba/envs/def/lib/python3.11/site-packages/hist/basehist.py:424, in <listcomp>(.0)
    422 cat_ax = self.axes[overlay]
    423 cats = cat_ax if cat_ax.traits.discrete else np.arange(len(cat_ax.centers))
--> 424 d1hists = [self[{overlay: cat}] for cat in cats]
    425 if "label" in kwargs:
    426     if not isinstance(kwargs["label"], str) and len(kwargs["label"]) == len(
    427         cats
    428     ):

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:842, in Histogram.__getitem__(self, index)
    838 def __getitem__(  # noqa: C901
    839     self: H, index: IndexingExpr
    840 ) -> Union[H, float, Accumulator]:
--> 842     indexes = self._compute_commonindex(index)
    844     # If this is (now) all integers, return the bin contents
    845     # But don't try *dict!
    846     if not hasattr(indexes, "items") and all(
    847         isinstance(a, SupportsIndex) for a in indexes
    848     ):

File ~/software/micromamba/envs/def/lib/python3.11/site-packages/boost_histogram/_internal/hist.py:750, in Histogram._compute_commonindex(self, index)
    748         indexes[i] = [self._compute_uhi_index(ind, i) for ind in indexes[i]]
    749     else:
--> 750         indexes[i] = self._compute_uhi_index(indexes[i], i)
    752 return indexes

File ~/software/micromamba/envs/def/lib/python3.11/site-packages/boost_histogram/_internal/hist.py:709, in Histogram._compute_uhi_index(self, index, axis)
    707 if isinstance(index, SupportsIndex):
    708     if abs(int(index)) >= self._hist.axis(axis).size:
--> 709         raise IndexError("histogram index is out of range")
    710     return int(index) % self._hist.axis(axis).size
    712 return index

IndexError: histogram index is out of range

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions