I have a numpy ndarray comprises of zeros, ones and NaNs. I would like to use a majority filter on that array, meaning that I would like to set a kernel window (e.g., 3X3 cells) that will go over the array and will change the value of the cell in the center to the value that occur the most in its neighbors. This filter should sustain two constrains, it should ignore NaNs and if the value of the center cell is one, then it should keep it one.
Here is a small example of what I'm looking for: input array:
array([[ 1., 1., 1., 0., 0.],
[ 1., 1., nan, 1., 1.],
[nan, 1., 1., 0., 1.],
[ 0., 0., 0., 0., 1.]])
Apply majority filter output array:
array([[ 1., 1., 1., 1., 1.],
[ 1., 1., nan, 1., 1.],
[nan, 1., 1., 1., 1.],
[ 0., 0., 0., 1., 1.]])
I was looking at scipy filters but could not find anything adequate. I thought to build a generic convolved filter, but I'm not sure how to do that for majority purpose. It feels that this is quit basic filter that should be out there, but I can't seem to find it.