Hi, I have the below code (self-containing example).
When running without njit(), the output to the console is:
0.8906
0.0
When I add @njit() as decorator, Numba chokes on the last line of the code. The line before that executes fine.
The only difference in the last line is that the w argument is now set to None. Which should be handled by the if w is not None statement. Yet I get the following error code from numba.
Unknown attribute 'astype' of type none
File "slope_debug.py", line 7:
def slope(x, y, w):
<source elided>
if w is not None:
w = w.astype(np.float64)
^
How can the errored line even be reached? Am I making some rookie mistake?
import numpy as np
from numba import jit, njit
@njit()
def slope(x, y, w):
if w is not None:
w = w.astype(np.float64)
x2 = x.reshape(-1, 1)
w2 = w.reshape(-1, 1)
xw = x2 * np.sqrt(w2)
yw = y * np.sqrt(w)
xw = xw.astype(np.float64)
yw = yw.astype(np.float64)
slope, p, q, r = np.linalg.lstsq(xw, yw)
slope = slope[0]
return slope
else:
return 0.0
x = np.array([3.0, 4.0, 5.0, 6.0])
y = np.array([2.3, 4.5, 3.4, 4.0])
print(slope(x=x, y=y, w=np.array([1.0, 1.0, 0.5, 0.0])))
print(slope(x=x, y=y, w=None))
I’m using numba 0.56.0.
Thanks in advance