return self.A[self.box_slice(j, k)]

def box_slice(self, j, k):
    """ square, jk-r : jk+r clipped to A.shape """
r = self.radius
return np.s_[max(j - r, 0):min(j + r + 1, self.A.shape[0]), max(k - r, 0):
    min(k + r + 1, self.A.shape[1])]
if __name__ == '__main__':
    A = np.arange(5 * 7).reshape((5, 7))
print('A:\n', A)
B = Box(A, radius=2)
print('B.box( 0, 0 ):\n', B.box(0, 0))
print('B.box( 0, 1 ):\n', B.box(0, 1))
print('B.box( 1, 2 ):\n', B.box(1, 2))
Как разрезать массив Numpy до границы?