I tried to use @njit speed up a function I call quite a bit. Originally the inputs were pandas arrays, but I rewrote everything in numpy so that it might work with numba. However, adding the decorator gives the following traceback. It could be that these are unsupported features. I upgraded to 0.50.0 and cross checked with https://numba.pydata.org/numba-doc/dev/reference/numpysupported.html. I see that np.sum is not listed so am thinking that might be the issue. Perhaps a minor tweak could make the njit decorator work?
#Input: numpy arrays of same length
import numpy as np
from numba import njit
@njit
def get_result(args, a, b, c, d):
args_one = np.argwhere( np.logical_and( a[args] == 1 , b[args] < d ) ) #.flatten() #this fixes it
args_two = np.argwhere( np.logical_and( a[args] == 2 , b[args] > d ) ) #.flatten() #this fixes it
one_val = np.sum( ( d - b[args_one] ) * c[args_one] )
two_val = np.sum( ( b[args_two] - d ) * c[args_two] )
return one_val + two_val
def test():
np.random.seed(0)
a = np.random.randint(1,3, 100)
b = np.random.uniform(50,100, 100)
c = np.random.randint(1,2000, 100)
d = 75
args = np.asarray(np.arange(10,20))
res = get_result(args, a, b, c, d)
print(res)
if __name__ == '__main__':
test()
# Traceback (most recent call last):
# File "test_numba.py", line 24, in <module>
# test()
# File "test_numba.py", line 20, in test
# res = get_result(args, a, b, c, d)
# File "/home/user/.local/lib/python3.6/site-packages/numba/core/dispatcher.py", line 415, in _compile_for_args
# error_rewrite(e, 'typing')
# File "/home/user/.local/lib/python3.6/site-packages/numba/core/dispatcher.py", line 358, in error_rewrite
# reraise(type(e), e, None)
# File "/home/user/.local/lib/python3.6/site-packages/numba/core/utils.py", line 80, in reraise
# raise value.with_traceback(tb)
# numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
# No implementation of function Function(<built-in function getitem>) found for signature:
#
# >>> getitem(array(float64, 1d, C), array(int64, 2d, A))
#
# There are 16 candidate implementations:
# - Of which 14 did not match due to:
# Overload in function 'getitem': File: <built-in>: Line <N/A>.
# With argument(s): '(array(float64, 1d, C), array(int64, 2d, A))':
# No match.
# - Of which 2 did not match due to:
# Overload in function 'getitem': File: <built-in>: Line <N/A>.
# With argument(s): '(array(float64, 1d, C), array(int64, 2d, A))':
# Rejected as the implementation raised a specific error:
# TypeError: unsupported array index type array(int64, 2d, A) in [array(int64, 2d, A)]
# raised from /home/user/.local/lib/python3.6/site-packages/numba/core/typing/arraydecl.py:69
#
# During: typing of intrinsic-call at test_numba.py (9)
#
# File "test_numba.py", line 9:
# def get_result(args, a, b, c, d):
# <source elided>
# args_two = np.argwhere( np.logical_and( a[args] == 2 , b[args] > d ) )
# one_val = np.sum( ( d - b[args_one] ) * c[args_one] )
# ^