Skip to content

Instantly share code, notes, and snippets.

@grovduck
Last active November 15, 2017 21:32
Show Gist options
  • Save grovduck/3671f55b23ce2cf004e27662be4ad17c to your computer and use it in GitHub Desktop.
Save grovduck/3671f55b23ce2cf004e27662be4ad17c to your computer and use it in GitHub Desktop.
[Masked window from numpy array] #numpy #algorithms
import numpy.ma as ma
def extract_masked_window(arr, mask, offset=(0, 0)):
"""
Extract the (possibly) masked window from a numpy array
Parameters
----------
arr : array-like
The array from which to extract
mask : array-like, boolean
The mask to extract. Note that values to extract are marked as True
and values to ignore are marked as False.
offset : tuple
Row and column offsets into the array
Returns
-------
ret : masked array
The masked subset window
"""
#TODO: Needs a lot more error checking and tests
r, c = offset
h, w = mask.shape
return ma.masked_array(arr[r:r+h, c:c+w], mask=~mask)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment