I have spent lots of time on this, and I know how to manually do it by slicing and indexing the boundary rows/cols, but there has to be a simpler way with SciPy.
I need to set the CVAL
(values to fill past the edges when mode=constant
) to NaN, however, this will return NaN.
I will explain it with code and figures:
import numpy as np
from scipy import ndimage
m = np.reshape(np.arange(0,100),(10,10)).astype(np.float)
Use SciPy ndimage uniform filter to calculate the mean using a 3x3 kernel:
filter = ndimage.uniform_filter(m, size=3, mode='constant')
print(filter[1][1]) # equal to 11
print(filter[9][9]) # I need 93.5, however it gets 41.55 due to zeros
As you can see, the first value comes out as 11, which is as expected, however, for any cell along the border, it will fill the values with zero (I have also tried all the other modes).
Here is what I need to achieve (left) vs mode=constant
and CVAL=0
(default 0)