xarray-spatial icon indicating copy to clipboard operation
xarray-spatial copied to clipboard

add enhanced focal stats

Open brendancol opened this issue 5 years ago • 5 comments

Add new kernels:

  • [ ] add custom (allow user to supply 2d array representing convolution kernel)
  • [ ] add circle
  • [ ] add wedge
  • [ ] add cone

brendancol avatar Feb 11 '20 02:02 brendancol

import numba

@numba.stencil
def _smooth(x):
    return (x[-1, -1] + x[-1, 0] + x[-1, 1] +
            x[ 0, -1] + x[ 0, 0] + x[ 0, 1] +
            x[ 1, -1] + x[ 1, 0] + x[ 1, 1]) // 9

brendancol avatar Feb 11 '20 06:02 brendancol

I'm not too familiar with the stencil notation in Numba. I see the utility in it but the example above is the same as below which could also be used as a convolution kernel with other methods.

kernel = np.ones((3, 3))
kernel  /= kernel.sum()

What is your opinion on allowing convolution with the GPU? It's relatively easy to do, though some overhead accounting needs to be done to account for hardware and library versions.

I've tried this example with my data and it works well, especially for larger kernel and image sizes.

I believe the max kernel size is (32, 32), which will work well for most kernels, but causes some issues with the annulus kernel (#125) depending on the radius.

I haven't tried to use Dask yet, so perhaps that takes care of some of these issues or could help with large convolution kernels on large arrays.

chase-dwelle avatar Sep 02 '20 12:09 chase-dwelle

@chase-dwelle hey hey. Numba has support for CUDA GPUs. I really want to make a push to make these run on GPU, but we haven't quite got there yet. @thuydotm is helping to review your PR and we'll get it merged ASAP. Datashader has some really interesting implementation examples for numba on gpus.

brendancol avatar Sep 02 '20 14:09 brendancol

@brendancol, will this issue be worked on?

giancastro avatar May 05 '21 14:05 giancastro

This look great

Kernel125 avatar Jul 14 '21 11:07 Kernel125

Closing as we now allow custom kernel, kernels of any shape can be accepted as input of focal_stats()

thuydotm avatar Jun 02 '23 13:06 thuydotm