I'm trying to build up with numba a function which returns a numpy array evaluated over another array I'll post a simple code without njit:
import numpy as np
import numba as nb
def prueba(arr, eva):
mask = []
for i in range(len(arr)):
mask.append(arr[i])
return eva[mask]
It works correctly, as expected:
>>> prueba(np.array([1,2,3]), np.array([5,6,7,8,9,10]))
array([6, 7, 8])
Nevertheless, when I try to compile it with numba in nopython mode (@njit) it throws an error
@nb.njit
def prueba(arr, eva):
mask = []
for i in range(len(arr)):
mask.append(arr[i])
return eva[mask]
>>> prueba(np.array([1,2,3]), np.array([5,6,7,8,9,10]))
---------------------------------------------------------------------------
TypingError Traceback (most recent call last)
<ipython-input-9-111474f08921> in <module>
----> 1 prueba(np.array([1,2,3]), np.array([5,6,7,8,9,10]))
~/.local/lib/python3.7/site-packages/numba/dispatcher.py in _compile_for_args(self, *args, **kws)
399 e.patch_message(msg)
400
--> 401 error_rewrite(e, 'typing')
402 except errors.UnsupportedError as e:
403 # Something unsupported is present in the user code, add help info
~/.local/lib/python3.7/site-packages/numba/dispatcher.py in error_rewrite(e, issue_type)
342 raise e
343 else:
--> 344 reraise(type(e), e, None)
345
346 argtypes = []
~/.local/lib/python3.7/site-packages/numba/six.py in reraise(tp, value, tb)
666 value = tp()
667 if value.__traceback__ is not tb:
--> 668 raise value.with_traceback(tb)
669 raise value
670
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Invalid use of Function(<built-in function getitem>) with argument(s) of type(s): (array(int64, 1d, C), list(int64))
* parameterized
In definition 0:
All templates rejected with literals.
In definition 1:
All templates rejected without literals.
In definition 2:
All templates rejected with literals.
In definition 3:
All templates rejected without literals.
In definition 4:
All templates rejected with literals.
In definition 5:
All templates rejected without literals.
In definition 6:
All templates rejected with literals.
In definition 7:
All templates rejected without literals.
In definition 8:
All templates rejected with literals.
In definition 9:
All templates rejected without literals.
In definition 10:
All templates rejected with literals.
In definition 11:
All templates rejected without literals.
In definition 12:
TypeError: unsupported array index type list(int64) in [list(int64)]
raised from /home/donielix/.local/lib/python3.7/site-packages/numba/typing/arraydecl.py:71
In definition 13:
TypeError: unsupported array index type list(int64) in [list(int64)]
raised from /home/donielix/.local/lib/python3.7/site-packages/numba/typing/arraydecl.py:71
This error is usually caused by passing an argument of a type that is unsupported by the named function.
[1] During: typing of intrinsic-call at <ipython-input-8-1b5c9f1a65d5> (6)
[2] During: typing of static-get-item at <ipython-input-8-1b5c9f1a65d5> (6)
File "<ipython-input-8-1b5c9f1a65d5>", line 6:
def prueba(arr, eva):
<source elided>
mask.append(arr[i])
return eva[mask]
^
So my question is, why this simple code gives an unexpected error? And how should I workaround this issue?
b?b[a]?maskis a list. Innumpyeva[mask]converts toeva[np.array(mask)]. It looks likenumbadoesn't do that, and instead rejects array indexing with the list. This may be test code, but why are you building a list from an array, and then using that list as index? That's poorly written for bothnumpyandnumba.