掩盖2d数组中数据点的外边缘

时间:2017-12-21 19:15:52

标签: python 2d mask

我正在尝试围绕不是Nan的2d值数组创建一个掩码。我的目标是掩盖实际数据的2个位置内的点。下面概述了一个快速示例,但这将应用于大小为301,301的二维数组。

array = ([[nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,],
          [nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,],
          [nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,],
          [nan, nan,   8,   5, nan,   3, nan, nan, nan, nan,],
          [nan, nan, nan,   2,   1, nan, nan, nan,   9, nan,],
          [nan, nan,   4, nan, nan, nan, nan, nan, nan, nan,],
          [nan, nan,   8, nan, nan, nan, nan, nan,   3, nan,],
          [nan, nan,   3, nan, nan, nan, nan, nan, nan, nan,],
          [nan, nan,   2, nan, nan, nan, nan, nan,   6, nan,],
          [nan, nan,   6, nan, nan, nan, nan, nan,   4, nan,],
          [nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,],
          [nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,],
          [nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,]])

mask = ([[False, False, False, False, False, False, False, False, False, False],
         [ True,  True,  True,  True,  True,  True,  True,  True, False, False],
         [ True,  True,  True,  True,  True,  True,  True,  True,  True,  True],
         [ True,  True,  True,  True,  True,  True,  True,  True,  True,  True],
         [ True,  True,  True,  True,  True,  True,  True,  True,  True,  True],
         [ True,  True,  True,  True,  True,  True,  True,  True,  True,  True],
         [ True,  True,  True,  True,  True,  True,  True,  True,  True,  True],
         [ True,  True,  True,  True,  True, False,  True,  True,  True,  True],
         [ True,  True,  True,  True,  True, False,  True,  True,  True,  True],
         [ True,  True,  True,  True,  True, False,  True,  True,  True,  True],
         [ True,  True,  True,  True,  True, False,  True,  True,  True,  True],
         [ True,  True,  True,  True,  True, False,  True,  True,  True,  True],
         [False, False, False, False, False, False, False, False, False, False]])

我找不到类似的例子,所以任何帮助或建议都会受到赞赏。

2 个答案:

答案 0 :(得分:0)

假设arr是包含您的值的numpy数组:

d = 2  # margin width around value cells to be set true in the mask
# indices of not-nan array cells
value_indices = np.argwhere(~np.isnan(arr))
# we initialize the mask with False
mask = np.zeros_like(arr).astype(bool)
# set the mask cells around indices with values (i.e. not nan) to True
for i, j in value_indices:
    mask[i-d:i+d+1, j-d:j+d+1] = True

答案 1 :(得分:0)

以下可能不太优雅,但它会起作用。它假定所有行具有相同的长度:

from itertools import product
from math import isnan

def mask(array, row, col):
    nan = float('nan')
    return any(not isnan(array[r][c]) for r, c in product(
        range(max(0, row-2), min(len(array)-1, row+3)), 
        range(max(0, col-2), min(len(array[0])-1, col+3))))

>>> masked = [[mask(array, r, c) for c in range(len(array[r]))] for r in range(len(array))]
[[False, False, False, False, False, False, False, False, False, False],
 [True, True, True, True, True, True, True, True, False, False],
 [True, True, True, True, True, True, True, True, True, True],
 [True, True, True, True, True, True, True, True, True, True],
 [True, True, True, True, True, True, True, True, True, True],
 [True, True, True, True, True, True, True, True, True, True],
 [True, True, True, True, True, True, True, True, True, True],
 [True, True, True, True, True, False, True, True, True, True],
 [True, True, True, True, True, False, True, True, True, True],
 [True, True, True, True, True, False, True, True, True, True],
 [True, True, True, True, True, False, True, True, True, True],
 [True, True, True, True, True, False, True, True, True, True],
 [False, False, False, False, False, False, False, False, False, False]]