Image-Py / imagepy

Image process framework based on plugin like imagej, it is esay to glue with scipy.ndimage, scikit-image, opencv, simpleitk, mayavi...and any libraries based on numpy
http://imagepy.org
BSD 4-Clause "Original" or "Old" License
1.29k stars 330 forks source link

update some function to fit numba4.3 #74

Open yxdragon opened 4 years ago

yxdragon commented 4 years ago

as numba update a lot, many function does not work now. I want to modify all functions in jit(nopython=True) mode.

But I failed to modify a function (spend two hours) @jni I think you are aware of numba, can you help to rewrite the function below? https://github.com/Image-Py/imagepy/blob/master/imagepy/ipyalg/graph/sknw.py

@jit(nopython=True)
def neighbors(shape):
    dim = len(shape)
    block = np.ones([3]*dim)
    block[tuple([1]*dim)] = 0
    idx = np.where(block>0)
    idx = np.array(idx, dtype=np.uint8).T
    idx = np.array(idx-[1]*dim)
    acc = np.cumprod((1,)+shape[::-1][:-1])
    return np.dot(idx, acc[::-1])

in the old version, this function need not jit, but now, if function a call function b and a is a jit function, then b must be jit too. thanks!

jni commented 4 years ago

@yxdragon hello again! Long time no see! 😊

I’m not sure what this function is supposed to do? Is this the same as this scikit-image function?

https://github.com/scikit-image/scikit-image/blob/master/skimage/morphology/_util.py#L54

What we do is precompute this in Python then pass the resulting array into Numba/Cython as an input.

yxdragon commented 4 years ago

@jni there are some functions using numba in imagepy.ipyalg. As numba update to 0.43, these functions do not work now. I want to repair them, but it seems not easy to me.

As the function "neighbors" upon, (that is the code in sknw, I learn from your skan. the function count the neighbors pixel's index in ravel coordinate)

It works well in numba0.41, and very fast. but now, it doesnot work. So did your skan works now? And can you help to modify it? or give some guide?

jni commented 4 years ago

did your skan works now?

Yes, it's working with the latest numba, I just checked. The trick is that I never or rarely use NumPy functions inside my Numba code. Everything NumPy I precompute in a non-compiled function, and then I pass in pre-allocated arrays to Numba. Only simple for-loops are involved in Numba.

The above function should not need to be jitted: it creates a very small array that you can reuse. So, make that a pure Python function, return the array, and and pass the resulting array to the jitted functions that need it.

yxdragon commented 4 years ago

ok, split the all function into python part and jit part. now all function can work with numba 0.46, thanks!