EconForge / interpolation.py

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

eval_spline() doesn't recognize argument `k=` #106

Closed fblasutt closed 1 year ago

fblasutt commented 1 year ago

Hello,

I need to interpolate a point using a jit function, where interpolation also needs to happen within a jit function. I get an error when I use eval_spline, while eval_cubic works. I could reproduce my error using this simple code:

from numba import njit
from interpolation.splines import prefilter,eval_spline
from interpolation.splines import filter_cubic,eval_cubic
import numpy as np

@njit
def cube(x):
    return x**3.0

grid=((0.0,1.0,100),)
fgrid=cube(np.linspace(*grid[0])) 
point=np.array([0.33333333]) 

@njit
def interp1(grid1,fgrid1,point1):

    coeffs1 = prefilter(grid1, fgrid1,k=3)
    return eval_spline(grid1,coeffs1,point1,out=None, k=3, diff="None", extrap_mode="linear") 

@njit
def interp2(grid2,fgrid2,point2):

    coeffs2 = filter_cubic(grid2, fgrid2) 
    return eval_cubic(grid2,coeffs2,point2)  

fpoint2=interp2(grid,fgrid,point)
fpoint1=interp1(grid,fgrid,point)

I get the following traceback:

Traceback (most recent call last):

  File ~\anaconda3\envs\pythonjax\lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File c:\users\fabio\dropbox\lic_cop\drop.py:34
    fpoint1=interp1(grid,fgrid,point)

  File ~\anaconda3\envs\pythonjax\lib\site-packages\numba\core\dispatcher.py:468 in _compile_for_args
    error_rewrite(e, 'typing')

  File ~\anaconda3\envs\pythonjax\lib\site-packages\numba\core\dispatcher.py:409 in error_rewrite
    raise e.with_traceback(None)

TypingError: Internal error at <numba.core.typeinfer.CallConstraint object at 0x000001ACAFEBD220>.
got an unexpected keyword argument 'k'
During: resolving callee type: type(CPUDispatcher(<function eval_spline at 0x000001ACAE8DC040>))
During: typing of call at c:\users\fabio\dropbox\lic_cop\drop.py (21)

Enable logging at debug level for details.

****************************************
Entering post mortem debugging...
****************************************
> c:\users\fabio\anaconda3\envs\pythonjax\lib\site-packages\numba\core\dispatcher.py(409)error_rewrite()
    407                 raise e
    408             else:
--> 409                 raise e.with_traceback(None)
    410 
    411         argtypes = []

I use Python 3.9.17, numba 0.57.0, and numpy 1.24.3. Do you have any idea of why I am getting this error, while eval_cubic works? If I omit keyword k in eval_spline, I get:

Traceback (most recent call last):

  File ~\anaconda3\envs\pythonjax\lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File c:\users\fabio\dropbox\lic_cop\drop.py:34
    fpoint1=interp1(grid,fgrid,point)

  File ~\anaconda3\envs\pythonjax\lib\site-packages\numba\core\dispatcher.py:468 in _compile_for_args
    error_rewrite(e, 'typing')

  File ~\anaconda3\envs\pythonjax\lib\site-packages\numba\core\dispatcher.py:409 in error_rewrite
    raise e.with_traceback(None)

TypingError: Cannot request literal type.

File "drop.py", line 21:
def interp1(grid1,fgrid1,point1):
    <source elided>
    coeffs1 = prefilter(grid1, fgrid1,k=3)
    return eval_spline(grid1,coeffs1,point1,out=None, diff="None", extrap_mode="linear") 
    ^

During: resolving callee type: type(CPUDispatcher(<function eval_spline at 0x0000012D667CC040>))
During: typing of call at c:\users\fabio\dropbox\lic_cop\drop.py (21)

****************************************
Entering post mortem debugging...
****************************************
> c:\users\fabio\anaconda3\envs\pythonjax\lib\site-packages\numba\core\dispatcher.py(409)error_rewrite()
    407                 raise e
    408             else:
--> 409                 raise e.with_traceback(None)
    410 
    411         argtypes = []

I would like to use eval_spline in the next step of my project to compute derivatives (not in this example yet). Thank you in advance for your help.

fblasutt commented 1 year ago

Oh, now I realize my mistake: the correct keyword in eval_spline is "order", not "k".

I think it could be helpful to change the part of the docs on this page after "To interpolate values:.." and write

eval_spline(G, C, P, out=None, order=3, diff="None", extrap_mode="linear")

instead of

eval_spline(G, C, P, out=None, k=3, diff="None", extrap_mode="linear")

albop commented 1 year ago

Yep. Well done!

albop commented 1 year ago

Actually, in the next release, I will change the argument order to k.

Because:

albop commented 1 year ago

Also I'm reopening this issue and changing the title to track the matter.

albop commented 1 year ago

Pff, I confounded it with the other issue. closing again.