PythonNumba - Compiling ahead of time - Cannot determine Numba type of <class 'function'>

I’m using the smallest_circle library in Python, and I’d like to compile it ahead of time (not jit) with Numba for speeding up the process as I call if often with different datasets.

The full code is available here, and below an excerpt:

@cc.export('is_in_circle', 'b1(f8[:], f8[:])')
def is_in_circle(c, p):
    if (c is not None) and (math.hypot(p[0] - c[0], p[1] - c[1]) <= c[2] * _MULTIPLICATIVE_EPSILON):
        res = True
    else:
        res = False
    return res


@cc.export('make_circle', '(f8[:,:],)')
def make_circle(points):
    # points : numpy.ndarray (3,2) float64
    # Convert to float and randomize order
    shuffled = points[np.random.permutation(len(points))]

    # Progressively add points to circle or recompute circle
    first_pass = True
    c = (0,0,0)
    for (i, p) in enumerate(shuffled):
        if first_pass is True or not is_in_circle(c, p):
            first_pass = False
            c = _make_circle_one_point(shuffled[: i + 1], p)
    return c

# other functions....

if __name__ == '__main__':
    cc.compile()

When I try to compile the functions ahead of time, I get the following error:

numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Untyped global name 'is_in_circle': Cannot determine Numba type of <class 'function'>
File "smallest_circle.py", line 57:
def make_circle(points):
    <source elided>
    for (i, p) in enumerate(shuffled):
        if first_pass is True or not is_in_circle(c, p):
        ^

It’s quite strange to me, as I explicitly specified the return type of the function is_in_circle. I also moved the function below the code, in case it wasn’t compiled before but didn’t change anything.

Using the whole library with the decorator @njit works like a charm.

What am I missing?

Thanks

I don’t know anything about AOT compiling, but I usually use @njit(cache=True) to solve this problem

2 Likes

It does the job perfectly. Thank you very much.