title: Local mean filter of a numpy array with missing data
body: <p>I'd like to make a local mean filter of an image stored as a numpy array. The image has some missing pixels near the edges, represented with a valid mask (a bool array).</p>
  
  <p>I could use <a href="http://scikit-image.org/docs/dev/api/skimage.filters.rank.html#mean" rel="noreferrer"><code>skimage.filters.rank</code></a>, but my images are outside of the <code>[-1, 1]</code> range, and for some reason scikit-image has that as a requirement.</p>
  
  <p>There's also <a href="http://docs.astropy.org/en/stable/convolution/#convolution-and-filtering-astropy-convolution" rel="noreferrer"><code>astropy.convolution</code></a>, but it interpolates missing data. For a simple mean there's no need to interpolate. Just average only valid pixels. The input and output valid masks are the same.</p>
  
  <p>Simply setting invalid pixels to zero is not an option as it would contaminate valid pixels averages nearby.</p>
  
  <p>There's also <a href="https://stackoverflow.com/questions/38318362/2d-convolution-in-python-with-missing-data">this question</a>, but it's not a duplicate since it asks about the more generic convolution (this is just averaging).</p>