Reputation: 1073
so I have a function in numba that is not compiling for some reason (calculates area under ROC curve). I am also not sure how I would be able to debug the function because my debugger is not working when I set a breakpoint in the numba decorated function.
Here is the function:
@nb.njit()
def auc_numba(fcst, obs):
L = obs.size
i_ord = fcst.argsort()
sumV = 0.
sumV2 = 0.
sumW = 0.
sumW2 = 0.
n = 0
m = 0
i = 0
while True:
nn = mm = 0
while True:
j = i_ord[i]
if obs[j]:
mm += 1
else:
nn += 1
if i == L - 1:
break
jp1 = i_ord[i + 1]
if fcst[j] != fcst[jp1]:
break
i += 1
sumW += nn * (m + mm / 2.0)
sumW2 += nn * (m + mm / 2.0) * (m + mm / 2.0)
sumV += mm * (n + nn / 2.0)
sumV2 += mm * (n + nn / 2.0) * (n + nn / 2.0)
n += nn
m += mm
i += 1
if i >= L:
break
theta = sumV / (m * n)
v = sumV2 / ((m - 1) * n * n) - sumV * sumV / (m * (m - 1) * n * n)
w = sumW2 / ((n - 1) * m * m) - sumW * sumW / (n * (n - 1) * m * m)
sd_auc = np.sqrt(v / m + w / n)
return np.array([theta, sd_auc])
What I am thinking is that there is something wrong with the while loops that I have implemented. It's possible that the types are wrong, hence the break is not being activated and the function is running forever.
Here is some sample data to test:
obs = np.array([1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1])
fcst = np.array([0.7083333, 0.5416667, 0.875, 0.5833333, 0.2083333, 0.8333333, 0.1666667, 0.9583333, 0.625, 0.1666667, 0.5, 1.0, 0.6666667, 0.2083333, 0.875, 0.75, 0.625, 0.3333333, 0.8333333, 0.2083333, 0.125, 0.0, 0.875, 0.8333333, 0.125, 0.5416667, 0.75])
When I run this without the decorator I get [0.89488636 0.06561209] which are the correct values.
So I guess if I could just get some help understanding why it is not compiling and maybe some tips on how to debug in numba?
Upvotes: 2
Views: 226
Reputation: 68732
There is something strange going on with the double while True
loop. For whatever reason (and I don't understand it), if you create two variables x
and y
at the top and then:
x = 1
y = 0
while True:
nn = mm = 0
while x > y:
and keep everything else the same, the code works. I'm going to submit an issue to the Numba tracker since this seems like a bug to me.
Update: The numba issue can be found here
Upvotes: 3