marta-marta commited on
Commit
5b65c9e
·
1 Parent(s): 9e671c6

Replaced update array with addition of arrays, use convolution filter to streamline thickness calculation. All for loops were removed from these main functions

Browse files
Data_Generation/Piecewise_Box_Functions.py CHANGED
@@ -1,143 +1,55 @@
1
  import numpy as np
2
  from scipy import signal
3
- import math
4
- import matplotlib.pyplot as plt
5
- import itertools
6
- def basic_box_array(image_size):
7
  A = np.ones((int(image_size), int(image_size))) # Initializes A matrix with 0 values
8
- # Creates the outside edges of the box
9
- # for i in range(image_size):
10
- # for j in range(image_size):
11
- # if i == 0 or j == 0 or i == image_size - 1 or j == image_size - 1:
12
- # A[i][j] = 1
13
- # A[1:-1, 1:-1] = 1
14
- # np.pad(A[1:-1,1:-1], pad_width=((1, 1), (1, 1)), mode='constant', constant_values=1)
15
- A[1:-1, 1:-1] = 0
16
  return A
17
 
18
 
19
- def back_slash_array(image_size):
20
  A = np.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
21
- # for i in range(image_size):
22
- # for j in range(image_size):
23
- # if i == j:
24
- # A[i][j] = 1
25
- np.fill_diagonal(A, 1)
26
-
27
  return A
28
 
29
 
30
- def forward_slash_array(image_size):
31
  A = np.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
32
- # for i in range(image_size):
33
- # for j in range(image_size):
34
- # if i == (image_size-1)-j:
35
- # A[i][j] = 1
36
- np.fill_diagonal(np.fliplr(A), 1)
37
  return A
38
 
39
 
40
- def hot_dog_array(image_size):
41
  # Places pixels down the vertical axis to split the box
42
  A = np.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
43
- # for i in range(image_size):
44
- # for j in range(image_size):
45
- # if j == math.floor((image_size - 1) / 2) or j == math.ceil((image_size - 1) / 2):
46
- # A[i][j] = 1
47
-
48
- A[:, np.floor((image_size - 1) / 2).astype(int)] = 1
49
  A[:, np.ceil((image_size - 1) / 2).astype(int)] = 1
 
50
  return A
51
 
52
 
53
- def hamburger_array(image_size):
54
  # Places pixels across the horizontal axis to split the box
55
  A = np.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
56
- # for i in range(image_size):
57
- # for j in range(image_size):
58
- # if i == math.floor((image_size - 1) / 2) or i == math.ceil((image_size - 1) / 2):
59
- # A[i][j] = 1
60
- A[np.floor((image_size - 1) / 2).astype(int), :] = 1
61
  A[np.ceil((image_size - 1) / 2).astype(int), :] = 1
 
62
  return A
63
 
64
 
65
- # def update_array(array_original, array_new, image_size):
66
- # A = array_original
67
- # for i in range(image_size):
68
- # for j in range(image_size):
69
- # if array_new[i][j] == 1:
70
- # A[i][j] = 1
71
- # return A
72
- def update_array(array_original, array_new):
73
- A = array_original
74
- A[array_new == 1] = 1
75
- return A
76
-
77
-
78
- # def add_pixels(array_original, additional_pixels):
79
- # # Adds pixels to the thickness of each component of the box
80
- # A = array_original
81
- # filter = np.array(([0, 1, 0], [1, 1, 1], [0, 1, 0])) # This filter will only add value where there are pixels on
82
- # # the top, bottom, left or right of a pixel
83
- #
84
- # # This filter adds thickness based on the desired number of additional pixels
85
- # for item in range(additional_pixels):
86
- # convolution = signal.convolve2d(A, filter, mode='same')
87
- # A = np.where(convolution <= 1, convolution, 1)
88
- # return A
89
-
90
-
91
- def add_pixels(array_original, thickness):
92
  A = array_original
93
- # if thickness !=0:
94
-
95
- # filter = np.array(([0, 1, 0], [1, 1, 1], [0, 1, 0]))
96
- # filter = np.stack([filter] * additional_pixels, axis=-1)
97
- filter_size = 2*thickness+1
98
- filter = np.zeros((filter_size,filter_size))
99
- filter[np.floor((filter_size - 1) / 2).astype(int), :] = filter[:, np.floor((filter_size - 1) / 2).astype(int)] =1
100
- filter[np.ceil((filter_size - 1) / 2).astype(int), :] = filter[:, np.ceil((filter_size - 1) / 2).astype(int)] = 1
101
-
102
- # filter[0,0] = filter[-1,0] = filter[0,-1] = filter[-1,-1] = 0
103
- print(filter)
104
- convolution = signal.convolve2d(A, filter, mode='same')
105
- A = np.where(convolution <= 1, convolution, 1)
106
  return A
107
-
108
-
109
- # def create_array(basic_box_thickness, forward_slash_thickness, back_slash_thickness, hamburger_thickness, hot_dog_thickness):
110
- #
111
-
112
- # TESTING
113
- image_size = 9
114
- # test = forward_slash_array(image_size)
115
- test = hamburger_array((image_size))
116
- back = back_slash_array((image_size))
117
- hot = hot_dog_array(image_size)
118
- forward = forward_slash_array(image_size)
119
- basic = basic_box_array((image_size))
120
-
121
- # test = update_array(test, back)
122
- # test = update_array(test, hot)
123
- # test = update_array(test, forward)
124
- test = test + back + forward + hot + basic
125
- test = np.array(test > 0, dtype=int)
126
-
127
- # test = add_pixels(test, 1)
128
- print(test)
129
- plt.imshow(test)
130
- plt.show()
131
-
132
-
133
- # basic_box_thickness = np.linspace(0,14, num=15)
134
- # print(basic_box_thickness)
135
- # forward_slash_thickness = np.linspace(0,14, num=15)
136
- # back_slash_thickness = np.linspace(0,14, num=15)
137
- # hamburger_thickness = np.linspace(0,14, num=15)
138
- # hot_dog_thickness =np.linspace(0,14, num=15)
139
- # print(np.meshgrid((basic_box_thickness, forward_slash_thickness, back_slash_thickness, hamburger_thickness, hot_dog_thickness)))
140
-
141
- # all_thicknesses = list(itertools.product(basic_box_thickness, repeat=5))
142
- # print(all_thicknesses)
143
- # print(np.shape(all_thicknesses))
 
1
  import numpy as np
2
  from scipy import signal
3
+
4
+
5
+ def basic_box_array(image_size, thickness):
 
6
  A = np.ones((int(image_size), int(image_size))) # Initializes A matrix with 0 values
7
+ A[1:-1, 1:-1] = 0 # replaces all internal rows/columns with 0's
8
+ A = add_thickness(A, thickness)
 
 
 
 
 
 
9
  return A
10
 
11
 
12
+ def back_slash_array(image_size, thickness):
13
  A = np.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
14
+ np.fill_diagonal(A, 1) # fills the diagonal with 1 values
15
+ A = add_thickness(A, thickness)
 
 
 
 
16
  return A
17
 
18
 
19
+ def forward_slash_array(image_size, thickness):
20
  A = np.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
21
+ np.fill_diagonal(np.fliplr(A), 1) # Flips the array to then fill the diagonal the opposite direction
22
+ A = add_thickness(A, thickness)
 
 
 
23
  return A
24
 
25
 
26
+ def hot_dog_array(image_size, thickness):
27
  # Places pixels down the vertical axis to split the box
28
  A = np.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
29
+ A[:, np.floor((image_size - 1) / 2).astype(int)] = 1 # accounts for even and odd values of image_size
 
 
 
 
 
30
  A[:, np.ceil((image_size - 1) / 2).astype(int)] = 1
31
+ A = add_thickness(A, thickness)
32
  return A
33
 
34
 
35
+ def hamburger_array(image_size, thickness):
36
  # Places pixels across the horizontal axis to split the box
37
  A = np.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
38
+ A[np.floor((image_size - 1) / 2).astype(int), :] = 1 # accounts for even and odd values of image_size
 
 
 
 
39
  A[np.ceil((image_size - 1) / 2).astype(int), :] = 1
40
+ A = add_thickness(A, thickness)
41
  return A
42
 
43
 
44
+ def add_thickness(array_original, thickness):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  A = array_original
46
+ if thickness == 0: # want an array of all 0's for thickness = 0
47
+ A[A > 0] = 0
48
+ else:
49
+ filter_size = 2*thickness - 1 # the size of the filter needs to extend far enough to reach the base shape
50
+ filter = np.zeros((filter_size, filter_size))
51
+ filter[np.floor((filter_size - 1) / 2).astype(int), :] = filter[:, np.floor((filter_size - 1) / 2).astype(int)] =1
52
+ filter[np.ceil((filter_size - 1) / 2).astype(int), :] = filter[:, np.ceil((filter_size - 1) / 2).astype(int)] = 1
53
+ convolution = signal.convolve2d(A, filter, mode='same')
54
+ A = np.where(convolution <= 1, convolution, 1)
 
 
 
 
55
  return A