Applying a circular mask with periodic boundary conditions in python

This question is related to: How to apply a disk-shaped mask to a numpy array?

From the solution: https://stackoverflow.com/a/167269/2326323# you can get a circular mask as follows:

>>> new_arr
array([[ True,  True,  True,  True,    1.,  1.,  1.,  True],
       [ True,  True,  True,  True,  True,  1., True, True],
       [ True,  True,  True,  True,    1.,  1.,  1.,  True],
       [ True,  True,  True,  True,    1.,  1.,  1.,  True],
       [ 1.,    True,    1.,    1.,    1.,  1.,  1.,  1.  ],
       [ 1.,      1.,    1.,    1.,    1.,  1.,  1.,  1.  ],
       [ 1.,    True,    1.,    1.,    1.,  1.,  1.,  1.  ],
       [ True,  True,  True,  True,    1.,  1.,  1.,  True]])

so that the array wraps around its columns and rows?

+4
source share
2 answers

One way could be to create a mask of the required size in the center of the array, and then use it np.rollto move the mask along the axis (this causes the mask to wrap around the edges of the array).

Following the method in the related question and answer:

ones = np.ones((8, 8))

a, b = 3, 3
n = 8
r = 3
mask = x**2 + y**2 <= r**2

mask :

array([[False, False, False,  True, False, False, False, False],
       [False,  True,  True,  True,  True,  True, False, False],
       [False,  True,  True,  True,  True,  True, False, False],
       [ True,  True,  True,  True,  True,  True,  True, False],
       [False,  True,  True,  True,  True,  True, False, False],
       [False,  True,  True,  True,  True,  True, False, False],
       [False, False, False,  True, False, False, False, False],
       [False, False, False, False, False, False, False, False]], dtype=bool)

mask ones...

>>> rolled_mask = np.roll(np.roll(mask, -2, axis=0), -2, axis=1)
>>> ones[rolled_mask] = 255
>>> ones
array([[ 255.,  255.,  255.,  255.,    1.,    1.,    1.,  255.],
       [ 255.,  255.,  255.,  255.,  255.,    1.,  255.,  255.],
       [ 255.,  255.,  255.,  255.,    1.,    1.,    1.,  255.],
       [ 255.,  255.,  255.,  255.,    1.,    1.,    1.,  255.],
       [   1.,  255.,    1.,    1.,    1.,    1.,    1.,    1.],
       [   1.,    1.,    1.,    1.,    1.,    1.,    1.,    1.],
       [   1.,  255.,    1.,    1.,    1.,    1.,    1.,    1.],
       [ 255.,  255.,  255.,  255.,    1.,    1.,    1.,  255.]])
+3

, :

>>> N = 10
>>> row, col = 8, 7
>>> radius = 4
>>> rows = np.arange(N) - row
>>> rows = np.minimum(np.abs(rows), rows % N)
>>> cols = np.arange(N) - col
>>> cols = np.minimum(np.abs(cols), cols % N)
>>> (rows[:, None]**2 + cols**2 <= radius**2).astype(int)
array([[1, 0, 0, 0, 1, 1, 1, 1, 1, 1],
       [0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
       [0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
       [0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
       [1, 0, 0, 0, 1, 1, 1, 1, 1, 1],
       [1, 0, 0, 0, 1, 1, 1, 1, 1, 1],
       [1, 1, 0, 1, 1, 1, 1, 1, 1, 1],
       [1, 0, 0, 0, 1, 1, 1, 1, 1, 1]])
+1

All Articles