Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling to_hist() and to_numpy() gives error when opening a CategoryAxis which has been saved to a root file #659

Closed
jlabounty opened this issue Aug 3, 2022 · 0 comments · Fixed by #671
Labels
bug The problem described is something that must be fixed

Comments

@jlabounty
Copy link

Hello,

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:

import hist
print(f'{hist.__version__=}')
import uproot
print(f'{uproot.__version__=}')
import numpy

cat_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)
)

with uproot.recreate("bug_output.root") as f:
    f['h'] = h
    f['h2'] = h2

    print(f['h'])
    print(f['h2'])

with uproot.open("bug_output.root") as f:
    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 fails
print(h_opened.to_numpy()) #works, but the bin edges are wrong
print(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!

@jlabounty jlabounty added the bug (unverified) The problem described would be a bug, but needs to be triaged label Aug 3, 2022
@jpivarski 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug The problem described is something that must be fixed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants