Skip to content

Instantly share code, notes, and snippets.

@matthewfeickert
Last active October 29, 2020 23:44
Show Gist options
  • Save matthewfeickert/2afe18c6b06ed14d889c3102513a621a to your computer and use it in GitHub Desktop.
Save matthewfeickert/2afe18c6b06ed14d889c3102513a621a to your computer and use it in GitHub Desktop.
hist, uproot4 issue
.ipynb_checkpoints/

Issue

Minimal failing example

$ python minimal-failing.py
hists in file: ['mass;1']
Traceback (most recent call last):
  File "minimal-failing.py", line 100, in <module>
    main()
  File "minimal-failing.py", line 96, in main
    minimal_failing(root_file)
  File "minimal-failing.py", line 73, in minimal_failing
    hist_mass.plot()
  File "/home/feickert/.venvs/hist-issue/lib/python3.7/site-packages/hist/basehist.py", line 204, in plot
    return self.plot1d(*args, **kwargs)
  File "/home/feickert/.venvs/hist-issue/lib/python3.7/site-packages/hist/basehist.py", line 222, in plot1d
    return hist.plot.histplot(self, ax=ax, **kwargs)
  File "/home/feickert/.venvs/hist-issue/lib/python3.7/site-packages/mplhep/plot.py", line 132, in histplot
    h = np.asarray(h).astype(float)
TypeError: Cannot cast array data from dtype([('value', '<f8'), ('variance', '<f8')]) to dtype('float64') according to the rule 'unsafe'

Minimal passing example

$ python minimal-failing.py pass
Saved figure
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import numpy as np
import uproot as uproot3
import uproot4 as uproot
import hist
from hist import Hist
import sys
def write_root_file(name="example.root"):
values = np.array(
[
0.0,
0.0,
0.0,
0.0,
0.0,
0.1305842101573944,
1.8680684566497803,
5.039996147155762,
10.908727645874023,
16.122821807861328,
19.942882537841797,
20.3310489654541,
20.61619758605957,
19.830669403076172,
16.36935806274414,
14.46778678894043,
12.30238151550293,
10.680359840393066,
8.495804786682129,
6.819866180419922,
5.613673210144043,
4.6372199058532715,
3.0081684589385986,
2.6621906757354736,
1.9703816175460815,
1.5290508270263672,
1.142599105834961,
1.1284815073013306,
0.9037942886352539,
0.5355855226516724,
0.2520864009857178,
0.3385327458381653,
0.2901037633419037,
0.15997962653636932,
0.126237154006958,
0.07963479310274124,
0.04193861410021782,
0.0325426310300827,
0.03738107904791832,
0.005638935137540102,
0.0,
0.0,
0.006189885549247265,
0.0,
0.0,
0.0,
0.010353004559874535,
0.0,
0.0,
0.0,
]
)
edges = np.arange(0, 10000 + 200, 200)
with uproot3.recreate(name, compression=uproot3.ZLIB(4)) as outfile:
outfile["mass"] = (values, edges)
def minimal_failing(root_file):
print(f"hists in file: {root_file.keys()}")
hist_mass = root_file["mass"].to_hist()
hist_mass.plot()
def minimal_passing(root_file):
values, edges = root_file["mass"].to_numpy()
hist_mass = Hist(
hist.axis.Regular(len(edges) - 1, edges[0], edges[-1], name="mass"),
storage=hist.storage.Double(),
)
hist_mass[:] = values
artists = hist_mass.plot()
fig = artists[0].step.figure
fig.savefig("mass_plot.png")
print("Saved figure")
def main(input=sys.argv):
write_root_file("example.root")
root_file = uproot.open("example.root")
if input[-1] == "pass":
minimal_passing(root_file)
else:
minimal_failing(root_file)
if __name__ == "__main__":
main()
hist[plot]==2.0.1
uproot4==0.0.27
uproot~=3.12
jupyter~=1.0
jupyterlab~=2.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment