EconForge / interpolation.py

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

Question: Key differences between scipy interpn #28

Closed MaximilianHoffmann closed 4 years ago

MaximilianHoffmann commented 5 years ago

especially, what happens outside of the regular grid, can I switch extrapolation off and define a fill value == 0 ?

Sorry. that this is no real issue. Happy to migrate it to stackoverflow or so

albop commented 5 years ago

I don't mind keeping the issue here: the code base is evolving a lot so stackoverflow might be premature. A few elements to answer your question:

MaximilianHoffmann commented 5 years ago

Thanks for your answer. I am interested in the library mainly, because I was looking for a fast alternative to scipy for interpolating image volumes. Especially I want to rotate volumes , which leads to a lot of extrapolation in the current version, this is why I am interested in the fill_values option

albop commented 5 years ago

Hi @MaximilianHoffmann . We now have options to extrapolate: you can do eval_linear(grid, x, point, options.NEAREST) or eval_linear(grid, x, point, options.CONSTANT) (which fills outside values with 0). There are more details in the README file. Is it what you needed ?

MaximilianHoffmann commented 5 years ago

Perfect, this is what I needed. Thanks a lot

guptapraful commented 4 years ago

Hi @albop ,

Thanks for the excellent package! I've a quick question:

When I do eval_linear(grid, x, point, options.CONSTANT) with grid = (np.linspace(0,1,10), np.linspace(0,1,10)), it returns an error with the following traceback:

File "testing_eval_linear.py", line 46, in <module>
    ans1 = eval_linear(grid1, values, points, xto.CONSTANT) # 10000 vector
  File "/usr/local/lib/python3.7/site-packages/numba/dispatcher.py", line 376, in _compile_for_args
    error_rewrite(e, 'typing')
  File "/usr/local/lib/python3.7/site-packages/numba/dispatcher.py", line 343, in error_rewrite
    reraise(type(e), e, None)
  File "/usr/local/lib/python3.7/site-packages/numba/six.py", line 658, in reraise
    raise value.with_traceback(tb)
numba.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Invalid use of Function(<function _eval_linear at 0x116b89620>) with argument(s) of type(s): (tuple(array(float64, 1d, C) x 2), array(float64, 2d, C), array(float64, 2d, C), instance.jitclass.c_CONSTANT#116c21860<>)
 *parameterized

while with grid = UCGrid((0, 1.0, 10), (0.0, 1.0, 10)), it just works perfectly fine.

here is the code from the readme:

import numpy as np

from interpolation.splines import UCGrid, CGrid, nodes

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

grid = UCGrid((-1.0, 1.0, 10), (-1.0, 1.0, 10))

gp = nodes(grid)   # 100x2 matrix

values = f(gp[:,0], gp[:,1]).reshape((10,10))

from interpolation.splines import eval_linear
from interpolation.splines import extrap_options as xto
points = np.random.random((100,2))*3-1

grid1 = (np.linspace(0,1,10), np.linspace(0,1,10))
ans1 = eval_linear(grid1, values, points, xto.CONSTANT) # This line gives an error

grid2 = UCGrid((0, 1.0, 10), (0.0, 1.0, 10))
ans2 = eval_linear(grid2, values, points, xto.CONSTANT) # This works fine

Is the extrapolation utility only available with grids defined in tuple of tuples format and not tuple of arrays? Thanks a lot :)

albop commented 4 years ago

it shouldn't be the case. Let me check

On Mon, Sep 30, 2019 at 5:20 PM Praful Gupta notifications@github.com wrote:

Hi @albop https://github.com/albop ,

Thanks for the excellent package! I've a quick question:

When I do eval_linear(grid, x, point, options.CONSTANT) with grid = (np.linspace(0,1,10), np.linspace(0,1,10)), it returns an error with the following traceback: ` File "testing_eval_linear.py", line 46, in ans1 = eval_linear(grid1, values, points, xto.CONSTANT) # 10000 vector File "/usr/local/lib/python3.7/site-packages/numba/dispatcher.py", line 376, in _compile_for_args error_rewrite(e, 'typing') File "/usr/local/lib/python3.7/site-packages/numba/dispatcher.py", line 343, in error_rewrite reraise(type(e), e, None) File "/usr/local/lib/python3.7/site-packages/numba/six.py", line 658, in reraise raise value.with_traceback(tb) numba.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend) Invalid use of Function(<function _eval_linear at 0x116b89620>) with argument(s) of type(s): (tuple(array(float64, 1d, C) x 2), array(float64, 2d, C), array(float64, 2d, C), instance.jitclass.c_CONSTANT#116c21860<>)

  • parameterized`

while with grid = UCGrid((0, 1.0, 10), (0.0, 1.0, 10)), it just works perfectly fine.

here is the code from the readme:

import numpy as np

from interpolation.splines import UCGrid, CGrid, nodes

f = lambda x,y: np.sin(np.sqrt(x2+y2+0.00001))/np.sqrt(x2+y2+0.00001)

grid = UCGrid((-1.0, 1.0, 10), (-1.0, 1.0, 10))

gp = nodes(grid) # 100x2 matrix

values = f(gp[:,0], gp[:,1]).reshape((10,10))

from interpolation.splines import eval_linear from interpolation.splines import extrap_options as xto points = np.random.random((100,2))*3-1

grid1 = (np.linspace(0,1,10), np.linspace(0,1,10)) ans1 = eval_linear(grid1, values, points, xto.CONSTANT) # This line gives an error

grid2 = UCGrid((0, 1.0, 10), (0.0, 1.0, 10)) ans2 = eval_linear(grid2, values, points, xto.CONSTANT) # This works fine

Is the extrapolation utility only available with grids defined in tuple of tuples format and not tuple of arrays? Thanks a lot :)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/EconForge/interpolation.py/issues/28?email_source=notifications&email_token=AACDSKOXNWAG2ITDKHXQCBTQMIKLXA5CNFSM4FUHAS62YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD76AZNQ#issuecomment-536612022, or mute the thread https://github.com/notifications/unsubscribe-auth/AACDSKIMOZ7A4FCVKZH26ZTQMIKLXANCNFSM4FUHAS6Q .

albop commented 4 years ago

@guptapraful : I think I fixed the problem in master. Would you mind checking whether it gives reasonable results ?

guptapraful commented 4 years ago

Hi @albop, It works perfectly now! Thanks!