[Numpy-discussion] the neighbourhood of each element of an array

Pierre GM pgmdevlist@gmail....
Fri Feb 23 14:19:17 CST 2007

On Friday 23 February 2007 14:53:05 Zachary Pincus wrote:
> Scipy's ndimage module has a function that takes a generic callback
> and calls it with the values of each neighborhood (of a given size,
> and optionally with a particular "mask" footprint) centered on each
> array element. That function handles boundary conditions, etc nicely.
> Unfortunately, I'm not sure if it works with masked arrays, and I
> think it hands a ravel'd set of pixels back to the callback function.
> You could probably hack masking in there by passing it the mask
> concatenated to the array, and then deal with the mask explicitly.

Without really thinking about it:
The easiest would be to process the masked array in steps:
* process the _data part of the maskedarray (or its filled version) with the 
function: that will be your new _data. 
* if the mask is not nomask, process the _mask part of the maskedarray to get 
a new _mask
* Set to True any element of the new mask that contains a True value: in other 
terms, mask the values that have a masked neighbor. 

More information about the Numpy-discussion mailing list