AddLat2D / Data_Generation /Piecewise_Box_Functions.py
marta-marta's picture
Replaced update array with addition of arrays, use convolution filter to streamline thickness calculation. All for loops were removed from these main functions
5b65c9e
raw
history blame
2.38 kB
import numpy as np
from scipy import signal
def basic_box_array(image_size, thickness):
A = np.ones((int(image_size), int(image_size))) # Initializes A matrix with 0 values
A[1:-1, 1:-1] = 0 # replaces all internal rows/columns with 0's
A = add_thickness(A, thickness)
return A
def back_slash_array(image_size, thickness):
A = np.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
np.fill_diagonal(A, 1) # fills the diagonal with 1 values
A = add_thickness(A, thickness)
return A
def forward_slash_array(image_size, thickness):
A = np.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
np.fill_diagonal(np.fliplr(A), 1) # Flips the array to then fill the diagonal the opposite direction
A = add_thickness(A, thickness)
return A
def hot_dog_array(image_size, thickness):
# Places pixels down the vertical axis to split the box
A = np.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
A[:, np.floor((image_size - 1) / 2).astype(int)] = 1 # accounts for even and odd values of image_size
A[:, np.ceil((image_size - 1) / 2).astype(int)] = 1
A = add_thickness(A, thickness)
return A
def hamburger_array(image_size, thickness):
# Places pixels across the horizontal axis to split the box
A = np.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
A[np.floor((image_size - 1) / 2).astype(int), :] = 1 # accounts for even and odd values of image_size
A[np.ceil((image_size - 1) / 2).astype(int), :] = 1
A = add_thickness(A, thickness)
return A
def add_thickness(array_original, thickness):
A = array_original
if thickness == 0: # want an array of all 0's for thickness = 0
A[A > 0] = 0
else:
filter_size = 2*thickness - 1 # the size of the filter needs to extend far enough to reach the base shape
filter = np.zeros((filter_size, filter_size))
filter[np.floor((filter_size - 1) / 2).astype(int), :] = filter[:, np.floor((filter_size - 1) / 2).astype(int)] =1
filter[np.ceil((filter_size - 1) / 2).astype(int), :] = filter[:, np.ceil((filter_size - 1) / 2).astype(int)] = 1
convolution = signal.convolve2d(A, filter, mode='same')
A = np.where(convolution <= 1, convolution, 1)
return A