[BUG] MeanViews has unexpected behavior for 0-D entries
Opened this issue · 0 comments
yimuchen commented
Describe the bug
When reducing a 1-D histogram to 0-D via a hist.Hist.profile call, the resulting MeanView does not handle method calls as expected:
Steps to reproduce
import hist
import numpy as np
h = hist.Hist(hist.axis.Regular(10, -10, 10, name='x'))
h.fill(np.random.normal(size=1000))
profile = h.profile('x').view()
print(profile) # MeanView: (count, value, _sum_of_deltas_squared)
# (1000., 0.02, 1302.2964)
print(profile.item()[1]) # 0.02
profile.mean()
profile.variance()
The last 2 line raises the following error message:
- For
profile.mean()
:
TypeError Traceback (most recent call last)
File /opt/conda/lib/python3.8/site-packages/numpy/core/_methods.py:179, in _mean(a, axis, dtype, out, keepdims, where)
[176](file:///opt/conda/lib/python3.8/site-packages/numpy/core/_methods.py?line=175) dtype = mu.dtype('f4')
[177](file:///opt/conda/lib/python3.8/site-packages/numpy/core/_methods.py?line=176) is_float16_result = True
--> [179](file:///opt/conda/lib/python3.8/site-packages/numpy/core/_methods.py?line=178) ret = umr_sum(arr, axis, dtype, out, keepdims, where=where)
[180](file:///opt/conda/lib/python3.8/site-packages/numpy/core/_methods.py?line=179) if isinstance(ret, mu.ndarray):
[181](file:///opt/conda/lib/python3.8/site-packages/numpy/core/_methods.py?line=180) ret = um.true_divide(
[182](file:///opt/conda/lib/python3.8/site-packages/numpy/core/_methods.py?line=181) ret, rcount, out=ret, casting='unsafe', subok=False)
TypeError: cannot perform reduce with flexible type
- For
profile.variance()
:
File /opt/conda/lib/python3.8/site-packages/boost_histogram/_internal/view.py:243, in MeanView.variance(self)
[240](file:///opt/conda/lib/python3.8/site-packages/boost_histogram/_internal/view.py?line=239) @property
[241](file:///opt/conda/lib/python3.8/site-packages/boost_histogram/_internal/view.py?line=240) def variance(self) -> "np.typing.NDArray[Any]":
[242](file:///opt/conda/lib/python3.8/site-packages/boost_histogram/_internal/view.py?line=241) with np.errstate(divide="ignore", invalid="ignore"):
--> [243](file:///opt/conda/lib/python3.8/site-packages/boost_histogram/_internal/view.py?line=242) return self["_sum_of_deltas_squared"] / (self["count"] - 1)
File /opt/conda/lib/python3.8/site-packages/boost_histogram/_internal/view.py:18, in View.__getitem__(self, ind)
[16](file:///opt/conda/lib/python3.8/site-packages/boost_histogram/_internal/view.py?line=15) # If the shape is empty, return the parent type
[17](file:///opt/conda/lib/python3.8/site-packages/boost_histogram/_internal/view.py?line=16) if not sliced.shape:
---> [18](file:///opt/conda/lib/python3.8/site-packages/boost_histogram/_internal/view.py?line=17) return self._PARENT._make(*sliced) # type: ignore[attr-defined, no-any-return]
[20](file:///opt/conda/lib/python3.8/site-packages/boost_histogram/_internal/view.py?line=19) # If the dtype has changed, return a normal array (no longer a record)
[21](file:///opt/conda/lib/python3.8/site-packages/boost_histogram/_internal/view.py?line=20) if sliced.dtype != self.dtype:
TypeError: iteration over a 0-d array
It looks like special case handling needs to be done for 0-D results for View.