Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@matthewfeickert
Last active March 15, 2021 20:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewfeickert/ab6ac8677aad2e04738111d0af3e0549 to your computer and use it in GitHub Desktop.
Save matthewfeickert/ab6ac8677aad2e04738111d0af3e0549 to your computer and use it in GitHub Desktop.
demo of failing example of hist with np.sqrt
.ipynb_checkpoints/
example.root

Issue

Binder

At the moment there does not seem to be support for NumPy ufuncs for hist histograms created from uproot's .to_hist() API.

Environment Setup

$ git clone git@gist.github.com:ab6ac8677aad2e04738111d0af3e0549.git hist-issue
$ cd hist-issue
$ pyenv virtualenv 3.8.7 hist-issue
$ pyenv activate hist-issue
(hist-issue) $ pip install --upgrade --quiet pip setuptools wheel
(hist-issue) $ cat requirements.txt
hist[plot]==2.2.0
uproot~=4.0.6
uproot3~=3.14.4
jupyter~=1.0
jupyterlab~=2.2
(hist-issue) $ pip install -r requirements.txt

Minimal failing example

(hist-issue) $ 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 74, in minimal_failing
    sqrt_values = np.sqrt(hist_mass)
TypeError: ufunc 'sqrt' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

Minimal passing example

(hist-issue) $  python minimal-failing.py pass
np.sqrt gives: [0.         0.         0.         0.         0.         0.36136437
 1.36677301 2.24499357 3.3028363  4.01532338 4.46574546 4.50899645
 4.54050631 4.45316398 4.04590633 3.8036544  3.50747509 3.26808198
 2.91475639 2.61148735 2.36931914 2.15342051 1.73440724 1.6316221
 1.40370282 1.23654795 1.06892428 1.0623001  0.95068096 0.73183709
 0.50208207 0.58183567 0.53861281 0.39997453 0.35529868 0.28219637
 0.20478919 0.18039576 0.19334187 0.07509284 0.         0.
 0.07867583 0.         0.         0.         0.10174972 0.
 0.         0.        ]
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import sys
import hist
import numpy as np
import uproot3
import uproot
from hist import Hist
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()
sqrt_values = np.sqrt(hist_mass)
print(f"np.sqrt gives: {sqrt_values}")
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
sqrt_values = np.sqrt(hist_mass)
print(f"np.sqrt gives: {sqrt_values}")
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.2.0
uproot~=4.0.6
uproot3~=3.14.4
jupyter~=1.0
jupyterlab~=2.2
@matthewfeickert
Copy link
Author

@henryiii has pointed out this isn't really using the proper API and I should be using

sqrt_values = np.sqrt(hist_mass.values())

Which is already what I'm doing in the minimal_passing example, so that's seem fair. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment