1

Hello!
I'm trying to use numba for faster numpy array's looping. My code:

@nb.njit
def _complicated(heat_list_, raw_array_, x_min_, x_step_, y_min_, y_step_):
        
        array = np.array(raw_array_)
        z_val = array[2, :]
        x_val = array[0, :]
        y_val = array[1, :]

        for i in range(len(z_val)):
            heat_list_[np.round_((y_val[i] - y_min_) / y_step_)][np.round_((x_val[i] - x_min_) / x_step_)] = z_val[i]

        return heat_list_

heat_list_, raw_list_ are numpy.ndarrays
x_min_, x_step_, y_min_, y_step_ are floats
When I run this function, the following error appears:

---------------------------------------------------------------------------
TypingError                               Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_28508/1500348391.py in <module>
----> 1 _complicated(data1.heat_list, data1.raw_array, data1.x_min, data1.x_step, data1.y_min, data1.y_step)

~\miniconda3\envs\pyquac\lib\site-packages\numba\core\dispatcher.py in _compile_for_args(self, *args, **kws)
    418                 e.patch_message(msg)
    419 
--> 420             error_rewrite(e, 'typing')
    421         except errors.UnsupportedError as e:
    422             # Something unsupported is present in the user code, add help info

~\miniconda3\envs\pyquac\lib\site-packages\numba\core\dispatcher.py in error_rewrite(e, issue_type)
    359                 raise e
    360             else:
--> 361                 raise e.with_traceback(None)
    362 
    363         argtypes = []

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<built-in function array>) found for signature:
 
 >>> array(array(float64, 2d, C))
 
There are 2 candidate implementations:
      - Of which 2 did not match due to:
      Overload in function 'array': File: numba\core\typing\npydecl.py: Line 489.
        With argument(s): '(array(float64, 2d, C))':
       Rejected as the implementation raised a specific error:
         TypingError: array(float64, 2d, C) not allowed in a homogeneous sequence
  raised from C:\Users\nokol\miniconda3\envs\pyquac\lib\site-packages\numba\core\typing\npydecl.py:457

During: resolving callee type: Function(<built-in function array>)
During: typing of call at C:\Users\nokol\AppData\Local\Temp/ipykernel_28508/1750607535.py (14)


File "..\..\..\..\AppData\Local\Temp\ipykernel_28508\1750607535.py", line 14:
<source missing, REPL/exec in use?>

I don't understand why it happens, since the function itself is simple, and the data types in it are supported by numba.
I would be glad if someone could help me figure it out.
PS
numba version == 0.53.1

python version == 3.9.7

ver 2 I've changed my code a little bit, but I still get an error:

@nb.generated_jit(nopython=True)
def _complicated(heat_list_, raw_array_x, raw_array_y, raw_array_z, x_min_, x_step_, y_min_, y_step_):

    for i in range(len(raw_array_y)):
        heat_list_[np.round_((raw_array_y[i] - y_min_) / y_step_), 
                   np.round_((raw_array_x[i] - x_min_) / x_step_)] = raw_array_z[i]

    return heat_list_
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_17704/3567269810.py in <module>
----> 1 data1.njit_result()

~\AppData\Local\Temp/ipykernel_17704/1768352675.py in njit_result(self)
    336 
    337         if len(self.x_raw) >= 2:
--> 338             return _complicated(self.heat_list, x_val, y_val, z_val,
    339                                 self.x_min, self.x_step, self.y_min, self.y_step, len_of_z)
    340         else:

~\miniconda3\envs\pyquac\lib\site-packages\numba\core\dispatcher.py in _compile_for_args(self, *args, **kws)
    499                     e.patch_message('\n'.join((str(e).rstrip(), help_msg)))
    500             # ignore the FULL_TRACEBACKS config, this needs reporting!
--> 501             raise e
    502         finally:
    503             self._types_active_call = []

~\miniconda3\envs\pyquac\lib\site-packages\numba\core\dispatcher.py in _compile_for_args(self, *args, **kws)
    432         return_val = None
    433         try:
--> 434             return_val = self.compile(tuple(argtypes))
    435         except errors.ForceLiteralArg as e:
    436             # Received request for compiler re-entry with the list of arguments

~\miniconda3\envs\pyquac\lib\site-packages\numba\core\dispatcher.py in compile(self, sig)
    977                 with ev.trigger_event("numba:compile", data=ev_details):
    978                     try:
--> 979                         cres = self._compiler.compile(args, return_type)
    980                     except errors.ForceLiteralArg as e:
    981                         def folded(args, kws):

~\miniconda3\envs\pyquac\lib\site-packages\numba\core\dispatcher.py in compile(self, args, return_type)
    139 
    140     def compile(self, args, return_type):
--> 141         status, retval = self._compile_cached(args, return_type)
    142         if status:
    143             return retval

~\miniconda3\envs\pyquac\lib\site-packages\numba\core\dispatcher.py in _compile_cached(self, args, return_type)
    153 
    154         try:
--> 155             retval = self._compile_core(args, return_type)
    156         except errors.TypingError as e:
    157             self._failed_cache[key] = e

~\miniconda3\envs\pyquac\lib\site-packages\numba\core\dispatcher.py in _compile_core(self, args, return_type)
    165         flags = self._customize_flags(flags)
    166 
--> 167         impl = self._get_implementation(args, {})
    168         cres = compiler.compile_extra(self.targetdescr.typing_context,
    169                                       self.targetdescr.target_context,

~\miniconda3\envs\pyquac\lib\site-packages\numba\core\dispatcher.py in _get_implementation(self, args, kws)
    201 
    202     def _get_implementation(self, args, kws):
--> 203         impl = self.py_func(*args, **kws)
    204         # Check the generating function and implementation signatures are
    205         # compatible, otherwise compiling would fail later.

~\AppData\Local\Temp/ipykernel_17704/1768352675.py in _complicated(heat_list_, raw_array_x, raw_array_y, raw_array_z, x_min_, x_step_, y_min_, y_step_, len_of_z)
     12 def _complicated(heat_list_, raw_array_x, raw_array_y, raw_array_z, x_min_, x_step_, y_min_, y_step_,
     13                  len_of_z):
---> 14     for i in range(len(raw_array_y)):
     15         heat_list_[np.round_((raw_array_y[i] - y_min_) / y_step_), 
     16                    np.round_((raw_array_x[i] - x_min_) / x_step_)] = raw_array_z[i]

TypeError: object of type 'Array' has no len()

1 Answer 1

1

Unlike Numpy, Numba do not support calling np.array with a parameter that is already a Numpy array. It seems like a bug or an unsupported feature of Numpy (and could be reported on the bug tracker if so). In your case, this is not critical as this call is not required since the input is already a Numpy array. If this is not always the case, then you may need to use the generated-jit decorator. However, a simpler solution is just to ensure the input parameter is a Numpy array, and otherwise, make the conversion before calling the Numba function.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried everything you say, but now it gives a rather strange error, referring to ...range(len(z_val)). It says that TypeError: 'Integer' object cannot be interpreted as an integer
and also TypeError: object of type 'Array' has no len()
It seems the input type is weird in you are calling the function. I cannot reproduce this specific issue with raw_list_ of type float64[:,:,:]. Can you provide the exact input types (ie. the dimension of the arrays, the type of the items)? Besides this, is advise you to specify the type to Numba directly in the decorator to catch typing errors earlier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.