scikit-hep / histbook

Versatile, high-performance histogram toolkit for Numpy.
BSD 3-Clause "New" or "Revised" License
109 stars 9 forks source link

histbook import from itself bug #28

Closed imandr closed 6 years ago

imandr commented 6 years ago

This code gives me an error:

import math
from histbook import Hist, bin, beside
from vega import VegaLite as canvas

hist = Hist(bin("sqrt(x**2 + y**2)", 5, 0, 1),
             bin("atan2(y, x)", 3, -math.pi, math.pi))
hist.fill(x=numpy.random.normal(0, 1, 1000000),
           y=numpy.random.normal(0, 1, 1000000))
beside(hist.step("sqrt(y**2 + x**2)"), hist.step("atan2(y,x)")).to(canvas)

Error:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-574307c16e07> in <module>()
      4 
      5 hist = Hist(bin("sqrt(x**2 + y**2)", 5, 0, 1),
----> 6              bin("atan2(y, x)", 3, -math.pi, math.pi))
      7 hist.fill(x=numpy.random.normal(0, 1, 1000000),
      8            y=numpy.random.normal(0, 1, 1000000))

/home/ivm/anaconda2/lib/python2.7/site-packages/histbook-1.1.0-py2.7.egg/histbook/hist.pyc in __init__(self, *axis, **opts)
    435                 newaxis.append(old)
    436             else:
--> 437                 expr, label = histbook.expr.Expr.parse(old._expr, defs=defs, returnlabel=True)
    438                 new = old.relabel(label)
    439                 new._original = old._expr

/home/ivm/anaconda2/lib/python2.7/site-packages/histbook-1.1.0-py2.7.egg/histbook/expr.pyc in parse(expression, defs, returnlabel)
    327 
    328         if returnlabel:
--> 329             return recurse(pyast, relations=True), label
    330         else:
    331             return recurse(pyast, relations=True)

/home/ivm/anaconda2/lib/python2.7/site-packages/histbook-1.1.0-py2.7.egg/histbook/expr.pyc in recurse(node, relations)
    319 
    320                 if fcn is None:
--> 321                     raise ExpressionError("unhandled function in expression: {0}".format(histbook.util.astunparse.tostring(node).strip()))
    322 
    323                 return Call(fcn, *(recurse(x, relations=(i == 0 and fcn == "where")) for i, x in enumerate(node.args)))

NameError: global name 'histbook' is not defined
jpivarski commented 6 years ago

Forgot to import.

I just committed to master.

That code block should now give you an error message about a missing function. I'm adopting NumExpr standards, in which "atan2" is named "arctan2" (for all inverse trig and hyperbolic functions). It's a breaking change, but it's best to pull that bandaid quickly.