EconForge / interpolation.py

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

AttributeError: 'Integer' object has no attribute 'literal_value' #115

Closed kp992 closed 2 months ago

kp992 commented 3 months ago
File ~/mambaforge/envs/qe/lib/python3.10/site-packages/interpolation/splines/eval_splines.py:100, in __eval_spline(grid, C, points, out, k, diff, extrap_mode)
     98 @overload(_eval_spline)
     99 def __eval_spline(grid, C, points, out=None, k=1, diff="None", extrap_mode="linear"):
--> 100     kk = (k).literal_value
    101     diffs = (diff).literal_value
    102     extrap_ = (extrap_mode).literal_value

AttributeError: 'Integer' object has no attribute 'literal_value'

Found in: https://github.com/QuantEcon/lecture-python-advanced.myst/issues/163

kp992 commented 3 months ago

@albop Can you please guide if there are any workarounds? The error is propagated through eval_linear function (Usage: eval_linear(x_grid, V[s], np.array([x[s]])))

Mv77 commented 3 months ago

Hey @albop, I am running into this same issue with interpolation=2.2.6. The following block from the examples in the docs reproduces the error:

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

The message is

(...)
  File "C:\Users\mateo\anaconda3\envs\interptest\lib\site-packages\numba\core\typing\templates.py", line 785, in _build_impl
    ovf_result = self._overload_func(*args, **kws)
  File "C:\Users\mateo\anaconda3\envs\interptest\lib\site-packages\interpolation\splines\eval_splines.py", line 100, in __eval_spline        
    kk = (k).literal_value
AttributeError: 'Integer' object has no attribute 'literal_value'

Here is a yml and package list for the env that I am using

name: interptest
channels:
  - anaconda
  - conda-forge
  - defaults
dependencies:
  - python=3.10
  - pip
  - packaging
  - pip:
    - interpolation == 2.2.6
# Name                    Version                   Build  Channel
bzip2                     1.0.8                he774522_0    anaconda
ca-certificates           2023.08.22           haa95532_0    anaconda
interpolation             2.2.6                    pypi_0    pypi
libffi                    3.4.4                hd77b12b_0    anaconda
llvmlite                  0.43.0                   pypi_0    pypi
numba                     0.60.0                   pypi_0    pypi
numpy                     2.0.0                    pypi_0    pypi
openssl                   3.0.12               h2bbff1b_0    anaconda
packaging                 23.1            py310haa95532_0    anaconda
pip                       23.3            py310haa95532_0    anaconda
python                    3.10.13              he1021f5_0    anaconda
scipy                     1.13.1                   pypi_0    pypi
setuptools                68.0.0          py310haa95532_0    anaconda
sqlite                    3.41.2               h2bbff1b_0    anaconda
tk                        8.6.12               h2bbff1b_0    anaconda
tzdata                    2023c                h04d1e81_0    anaconda
vc                        14.2                 h21ff451_1    anaconda
vs2015_runtime            14.27.29016          h5e58377_2    anaconda
wheel                     0.41.2          py310haa95532_0    anaconda
xz                        5.4.2                h8cc25b3_0    anaconda
zlib                      1.2.13               h8cc25b3_0    anaconda

It does not work with python 3.11 either.

The latest build I was able to get to work (with a bunch of warnings) was interpolation==2..2.4 with numba==0.58.0

sklam commented 3 months ago

Please see https://github.com/numba/numba/issues/9634#issuecomment-2206633298 for patch suggestion. The summary is that prefer_literal=True is needed. Problem was hidden in 0.59.1 by the deprecated old-style error.

mmcky commented 3 months ago

thanks @sklam for the suggestion here.