You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have been using uproot to save/load hist objects from root files. I have noticed that calling to_hist() on a THn object which contains a category axis yields a ValueError: could not broadcast input array from shape (N,...) into shape (N+1,...). This snippet of code reproduces the issue:
importhistprint(f'{hist.__version__=}')
importuprootprint(f'{uproot.__version__=}')
importnumpycat_axis=hist.axis.IntCategory([1,2,3], label='Category')
reg_axis=hist.axis.Regular(100,0,100, label='Random')
h=hist.Hist(cat_axis)
h2=hist.Hist(cat_axis, reg_axis)
h.fill(
numpy.random.randint(1,4,1000)
)
h2.fill(
numpy.random.randint(1,4,1000), numpy.random.normal(20,5,1000)
)
withuproot.recreate("bug_output.root") asf:
f['h'] =hf['h2'] =h2print(f['h'])
print(f['h2'])
withuproot.open("bug_output.root") asf:
h_opened=f['h']
h2_opened=f['h2']
print(h_opened)
print(h2_opened)
#this fails print(h_opened.to_hist())
print(h2_opened.to_hist())
#this also failsprint(h_opened.to_numpy()) #works, but the bin edges are wrongprint(h2_opened.to_numpy()) #fails
(gm2) jlab@SB3:~/github/g2_analysis/analysis$ python hist_uproot_category_bug.py
hist.__version__='2.6.1'
uproot.__version__='4.3.3'<TH1D (version 3) at 0x7fa5dc5650c0><TH2D (version 4) at 0x7fa5db88e1a0><TH1D (version 3) at 0x7fa5eb986710><TH2D (version 4) at 0x7fa5eb793400>
Traceback (most recent call last):
File "/home/jlab/github/g2_analysis/analysis/hist_uproot_category_bug.py", line 39, in<module>print(h_opened.to_hist())
File "/home/jlab/miniconda3/envs/gm2/lib/python3.10/site-packages/uproot/behaviors/TH1.py", line 206, in to_hist
self.to_boost(metadata=boost_metadata, axis_metadata=boost_axis_metadata)
File "/home/jlab/miniconda3/envs/gm2/lib/python3.10/site-packages/uproot/behaviors/TH1.py", line 322, in to_boost
view.value = values
File "/home/jlab/miniconda3/envs/gm2/lib/python3.10/site-packages/boost_histogram/_internal/view.py", line 59, in fset
self[name] = value
File "/home/jlab/miniconda3/envs/gm2/lib/python3.10/site-packages/boost_histogram/_internal/view.py", line 39, in __setitem__
super().__setitem__(ind, value) # type: ignore[no-untyped-call]
ValueError: could not broadcast input array from shape (4,) into shape (5,)
The 2D to_numpy() call fails with:
Traceback (most recent call last):
File "/home/jlab/github/g2_analysis/analysis/hist_uproot_category_bug.py", line 40, in<module>print(h2_opened.to_numpy())
File "/home/jlab/miniconda3/envs/gm2/lib/python3.10/site-packages/uproot/behaviors/TH2.py", line 96, in to_numpy
values = self.values(flow=flow)
File "/home/jlab/miniconda3/envs/gm2/lib/python3.10/site-packages/uproot/behaviors/TH2.py", line 52, in values
values = numpy.transpose(values.reshape(yaxis_fNbins + 2, xaxis_fNbins + 2))
ValueError: cannot reshape array of size 408 into shape (102,5)
I have tested this on uproot version 4.3.3 and hist version 2.6.1. I think this is related to the handling of over/underflow bins discussed here. As far as I know ROOT doesn't have an exact analogue to the CategoryAxis, so should an error be raised when trying to write this object to a root file in the first place?
Thank you for the great deal of work which goes into maintaining this project!
The text was updated successfully, but these errors were encountered:
jpivarski
added
bug
The problem described is something that must be fixed
and removed
bug (unverified)
The problem described would be a bug, but needs to be triaged
labels
Aug 11, 2022
Hello,
I have been using uproot to save/load hist objects from root files. I have noticed that calling
to_hist()
on aTHn
object which contains a category axis yields aValueError: could not broadcast input array from shape (N,...) into shape (N+1,...)
. This snippet of code reproduces the issue:The 2D
to_numpy()
call fails with:I have tested this on uproot version
4.3.3
and hist version2.6.1
. I think this is related to the handling of over/underflow bins discussed here. As far as I know ROOT doesn't have an exact analogue to the CategoryAxis, so should an error be raised when trying to write this object to a root file in the first place?Thank you for the great deal of work which goes into maintaining this project!
The text was updated successfully, but these errors were encountered: