Keep getting TypingError

Hello, I am not able to understand why I keep getting this error:

Traceback (most recent call last):
  File "/mnt/c/Users/alexr/OneDrive/Documenti/programming/py/project/run.py", line 5, in <module>
    from helpers import *
  File "/mnt/c/Users/alexr/OneDrive/Documenti/programming/py/project/helpers.py", line 26, in <module>
    @nu.guvectorize([nu.void(vectorC2, readonly_vectorC, vectorC2, vectorC2)], '(n, n),(m),(n, n)->(n, n)')
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex_unix/.local/lib/python3.12/site-packages/numba/np/ufunc/decorators.py", line 203, in wrap
    guvec.add(fty)
  File "/home/alex_unix/.local/lib/python3.12/site-packages/numba/np/ufunc/gufunc.py", line 137, in add
    self.gufunc_builder.add(fty)
  File "/home/alex_unix/.local/lib/python3.12/site-packages/numba/np/ufunc/ufuncbuilder.py", line 257, in add
    cres, args, return_type = _compile_element_wise_function(
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex_unix/.local/lib/python3.12/site-packages/numba/np/ufunc/ufuncbuilder.py", line 175, in _compile_element_wise_function
    cres = nb_func.compile(sig, **targetoptions)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex_unix/.local/lib/python3.12/site-packages/numba/np/ufunc/ufuncbuilder.py", line 123, in compile
    return self._compile_core(sig, flags, locals)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex_unix/.local/lib/python3.12/site-packages/numba/np/ufunc/ufuncbuilder.py", line 156, in _compile_core
    cres = compiler.compile_extra(typingctx, targetctx,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex_unix/.local/lib/python3.12/site-packages/numba/core/compiler.py", line 744, in compile_extra
    return pipeline.compile_extra(func)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex_unix/.local/lib/python3.12/site-packages/numba/core/compiler.py", line 438, in compile_extra
    return self._compile_bytecode()
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex_unix/.local/lib/python3.12/site-packages/numba/core/compiler.py", line 506, in _compile_bytecode
    return self._compile_core()
           ^^^^^^^^^^^^^^^^^^^^
  File "/home/alex_unix/.local/lib/python3.12/site-packages/numba/core/compiler.py", line 485, in _compile_core
    raise e
  File "/home/alex_unix/.local/lib/python3.12/site-packages/numba/core/compiler.py", line 472, in _compile_core
    pm.run(self.state)
  File "/home/alex_unix/.local/lib/python3.12/site-packages/numba/core/compiler_machinery.py", line 368, in run
    raise patched_exception
  File "/home/alex_unix/.local/lib/python3.12/site-packages/numba/core/compiler_machinery.py", line 356, in run
    self._runPass(idx, pass_inst, state)
  File "/home/alex_unix/.local/lib/python3.12/site-packages/numba/core/compiler_lock.py", line 35, in _acquire_compile_lock
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex_unix/.local/lib/python3.12/site-packages/numba/core/compiler_machinery.py", line 311, in _runPass
    mutated |= check(pss.run_pass, internal_state)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex_unix/.local/lib/python3.12/site-packages/numba/core/compiler_machinery.py", line 273, in check
    mangled = func(compiler_state)
              ^^^^^^^^^^^^^^^^^^^^
  File "/home/alex_unix/.local/lib/python3.12/site-packages/numba/core/typed_passes.py", line 112, in run_pass
    typemap, return_type, calltypes, errs = type_inference_stage(
                                            ^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex_unix/.local/lib/python3.12/site-packages/numba/core/typed_passes.py", line 93, in type_inference_stage
    errs = infer.propagate(raise_errors=raise_errors)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex_unix/.local/lib/python3.12/site-packages/numba/core/typeinfer.py", line 1091, in propagate
    raise errors[0]
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<numba._GUFunc 'retention_func'>) found for signature:
 
 >>> retention_func(float64, readonly array(float64, 1d, C))
 
There are 2 candidate implementations:
  - Of which 2 did not match due to:
  Overload in function 'GUFunc._type_me': File: numba/np/ufunc/gufunc.py: Line 147.
    With argument(s): '(float64, readonly array(float64, 1d, C))':
   Rejected as the implementation raised a specific error:
     TypingError: cannot call <numba._GUFunc 'retention_func'> with types (float64, Array(float64, 1, 'C', True, aligned=True))
  raised from /home/alex_unix/.local/lib/python3.12/site-packages/numba/np/ufunc/gufunc.py:182

During: resolving callee type: Function(<numba._GUFunc 'retention_func'>)
During: typing of call at /mnt/c/Users/alexr/OneDrive/Documenti/programming/py/project/helpers.py (30)


File "helpers.py", line 30:
def super_retention_func(x, a, mat, res):
    <source elided>
        for j in range(x.shape[0]):
            res[i,j] = mat[i, j]*retention_func(x[i, j], a)
            ^

.
From this code:

import numpy as np
import numba as nu

ScaInt = nu.types.int32
ScaFlo = nu.types.float64
@nu.vectorize([ScaFlo(ScaInt, ScaFlo, ScaFlo, ScaFlo), ScaFlo(ScaFlo, ScaFlo, ScaFlo, ScaFlo)])
def logistic(x, supremum, midpoint, steepness):
    result = supremum/(1+np.exp(-steepness*(x-midpoint)))

    return result 

@nu.vectorize([ScaFlo(ScaFlo, ScaFlo, ScaFlo, ScaFlo, ScaFlo)])
def pol3(x, Kfirst, Ksecond, Kthird, Kfourth):
    result = Kfirst*x**3 + Ksecond*x**2 + Kthird*x + Kfourth

    return result 

readonly_vectorC = nu.types.Array(dtype=nu.float64, ndim=1, layout="C", readonly=True)
@nu.guvectorize([nu.void(ScaFlo, readonly_vectorC, ScaFlo)], '(),(n)->()')
def retention_func(x, a, res):															
    res = np.exp(pol3(x, a[0], a[1], a[2], 0)) + logistic(x, a[3], a[4], a[5])

vectorC2 = nu.types.Array(dtype=nu.float64, ndim=2, layout="C")
@nu.guvectorize([nu.void(vectorC2, readonly_vectorC, vectorC2, vectorC2)], '(n, n),(m),(n, n)->(n, n)')
def super_retention_func(x, a, mat, res):
    for i in range(x.shape[0]):
        for j in range(x.shape[0]):
            res[i,j] = mat[i, j]*retention_func(x[i, j], a)          #Here error

Thank you very much for your time.

Hey @Ale9183,

The error indicates that the return type of retention_func should be a 0-D array instead of a scalar.

@nu.guvectorize([nu.void(ScaFlo, readonly_vectorC, nu.types.float64[:])], '(),(n)->()')
def retention_func(x, a, res):
    res[0] = np.exp(pol3(x, a[0], a[1], a[2], 0)) + logistic(x, a[3], a[4], a[5])

In super_retention_func, you need to ensure that the result argument passed to retention_func is a 0-D array accordingly.

@nu.guvectorize([nu.void(vectorC2, readonly_vectorC, vectorC2, vectorC2)], '(n, n),(m),(n, n)->(n, n)')
def super_retention_func(x, a, mat, res):
    temp_res = np.zeros(1, dtype=np.float64)  # Create a 0-D array to hold the result
    for i in range(x.shape[0]):
        for j in range(x.shape[0]):
            retention_func(x[i, j], a, temp_res)
            res[i, j] = mat[i, j] * temp_res[0]

Hope it works…

1 Like

It works!
Thank you so much for your wisdom.
You saved me.

Warm regards