Skip to content

Commit 332753c

Browse files
author
brandner
committed
use numpy mask for modifying pixel array
1 parent 35f3db5 commit 332753c

File tree

1 file changed

+6
-23
lines changed

1 file changed

+6
-23
lines changed

agent_patch_distr.py

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -57,34 +57,17 @@ def modifiy_pixel_array_circle(pixel_array, m_x, m_y, radius):
5757
# instead of id = 1 until pixel id = ENV_WIDTH (and respectively for ENV_HEIGHT)
5858
ENV_HEIGHT, ENV_WIDTH = np.shape(pixel_array)
5959

60-
left_end_circle = int(m_x - radius)
61-
right_end_circle = int(m_x + radius + 1)
62-
upper_end_circle = int(m_y - radius)
63-
lower_end_circle = int(m_y + radius + 1)
64-
65-
# adapt _end_circle such that no pixels are placed outside arena.
66-
if left_end_circle < 0:
67-
left_end_circle = 0
68-
if right_end_circle > ENV_WIDTH:
69-
right_end_circle = ENV_WIDTH
70-
if upper_end_circle < 0:
71-
upper_end_circle = 0
72-
if lower_end_circle > ENV_HEIGHT:
73-
lower_end_circle = ENV_HEIGHT
60+
# create arrays of indices
61+
x_ind = np.arange(0, ENV_WIDTH)
62+
y_ind = np.arange(0, ENV_HEIGHT)
7463

7564
# create numpy mask
76-
77-
for x in range(left_end_circle, right_end_circle):
78-
for y in range(upper_end_circle, lower_end_circle):
79-
dx = x - m_x
80-
dy = y - m_y
81-
distance_squared = dx * dx + dy * dy
82-
83-
if distance_squared <= (radius * radius):
84-
pixel_array[y, x] = pixel_array[y, x] + 1
65+
mask_circle = (x_ind[np.newaxis, :] - m_x)**2 + (y_ind[:, np.newaxis] - m_y)**2 <= radius**2
66+
pixel_array[mask_circle] += 1
8567

8668
return pixel_array
8769

70+
8871
def save_pixel_array(normalized_pixel_array, locations_or_circles, agent_or_patch,
8972
R, N, radii_resources, N_R, num_batches, folderpath):
9073
""" Saves a pixel array for adapting plots."""

0 commit comments

Comments
 (0)