AddLat2D / Data_Generation /Piecewise_Box_Functions.py
marta-marta's picture
Working on how to more efficiently apply density array
78a2f2b
raw
history blame
2.93 kB
import matplotlib.pyplot as plt
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
basic = basic_box_array(10, 1)
densitys = [0.25,0,75,1]
# multiple = np.repeat(basic, 3, axis=1 +)#np.multiply(basic,densitys)
multiple = np.array([basic]*4, order='C')
# multiple = np.concatenate(4*[[basic]], axis=0)
# multiple = np.swapaxes(multiple, 0,2)
# multiple = np.stack(4*[[basic]], axis=-1)
# multiple = np.moveaxis(multiple, [0], [2])
print(multiple)
print(np.shape(multiple))
multiple = np.multiply(densitys, multiple)
print(np.shape(multiple))
# print(multiple)
plt.imshow((multiple[0]))
plt.show()