EconForge / interpolation.py

BSD 2-Clause "Simplified" License
123 stars 35 forks source link

Error when running example code in the documentation #96

Closed efvega closed 1 year ago

efvega commented 1 year ago

Hello,

I created a fresh virtual environment and followed the instructions for installing the development version of interpolation. The example code does not run, but luckily it seems that the fix is relatively straightforward.

Setup virtual env

pip -m venv interp-venv source interp-venv/bin/activate pip install --upgrade pip

Install interpolation

pip install poetry pip install git+https://github.com/econforge/interpolation.py.git/

Create script with contents copied from the docs:

import numpy as np

from interpolation.splines import UCGrid, CGrid, nodes

# we interpolate function
f = lambda x,y: np.sin(np.sqrt(x**2+y**2+0.00001))/np.sqrt(x**2+y**2+0.00001)

# uniform cartesian grid
grid = UCGrid((-1.0, 1.0, 10), (-1.0, 1.0, 10))

# get grid points
gp = nodes(grid)   # 100x2 matrix

# compute values on grid points
values = f(gp[:,0], gp[:,1]).reshape((10,10))

from interpolation.splines import eval_linear
# interpolate at one point
point = np.array([0.1,0.45]) # 1d array
val = eval_linear(grid, values, point)  # float

# interpolate at many points:
points = np.random.random((10000,2))
eval_linear(grid, values, points) # 10000 vector

# output can be preallocated
out = np.zeros(10000)
eval_linear(grid, values, points, out) # 10000 vector

Running this script yields the following error:

Traceback (most recent call last):
  File "/home/efvega/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/213.7172.26/plugins/python/helpers/pydev/pydevd.py", line 1483, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/home/efvega/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/213.7172.26/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/home/efvega/heap/interp/test.py", line 9, in <module>
    grid = UCGrid((-1.0, 1.0, 10), (-1.0, 1.0, 10))
  File "/home/efvega/heap/interp-venv/lib/python3.8/site-packages/interpolation/splines/__init__.py", line 13, in UCGrid
    tt = numba.typeof((10.0, 1.0, 1))
NameError: name 'numba' is not defined
python-BaseException

The fix that got the script working for me was simply to add the numba import statement to the interpolation/splines/__init__.py file. Hope this helps!

Mv77 commented 1 year ago

Noticed this same error, https://github.com/EconForge/interpolation.py/pull/95 includes a fix!

efvega commented 1 year ago

Awesome, thanks for the fix @Mv77!

albop commented 1 year ago

Thank you both @efvega and @Mv77 . It should all be now in 2.2.2 (now on pypi)