Commit
·
0d09ea1
1
Parent(s):
ef08db2
Adding type hints to the functions in these scripts
Browse files
Data_Generation/Dataset_Generation_Functions.py
CHANGED
@@ -1,11 +1,10 @@
|
|
1 |
import numpy as np
|
2 |
-
import pandas as pd
|
3 |
-
import json
|
4 |
-
# from Data_Generation.Shape_Generation_Functions import basic_box, diagonal_box_split, horizontal_vertical_box_split, \
|
5 |
-
# back_slash_box, forward_slash_box, back_slash_plus_box, forward_slash_plus_box, hot_dog_box, hamburger_box, \
|
6 |
-
# x_hamburger_box, x_hot_dog_box, x_plus_box
|
7 |
from Data_Generation.Piecewise_Box_Functions import basic_box_array, back_slash_array, forward_slash_array, hamburger_array, hot_dog_array
|
|
|
|
|
8 |
# from Piecewise_Box_Functions import basic_box_array, back_slash_array, forward_slash_array, hamburger_array, hot_dog_array
|
|
|
|
|
9 |
import matplotlib.pyplot as plt
|
10 |
from json import JSONEncoder
|
11 |
|
@@ -13,10 +12,10 @@ from json import JSONEncoder
|
|
13 |
|
14 |
########################################################################################################################
|
15 |
# Make the data using all the code in Shape_Generation_Functions.py
|
16 |
-
def make_boxes(image_size, densities):
|
17 |
"""
|
18 |
:param image_size: [int] - the pixel height and width of the generated arrays
|
19 |
-
:param densities: [list] - of the values
|
20 |
:return: [list[tuple]] - [Array, Density, Thickness of each strut type]
|
21 |
"""
|
22 |
|
|
|
1 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
2 |
from Data_Generation.Piecewise_Box_Functions import basic_box_array, back_slash_array, forward_slash_array, hamburger_array, hot_dog_array
|
3 |
+
|
4 |
+
# For Internal Testing
|
5 |
# from Piecewise_Box_Functions import basic_box_array, back_slash_array, forward_slash_array, hamburger_array, hot_dog_array
|
6 |
+
import pandas as pd
|
7 |
+
import json
|
8 |
import matplotlib.pyplot as plt
|
9 |
from json import JSONEncoder
|
10 |
|
|
|
12 |
|
13 |
########################################################################################################################
|
14 |
# Make the data using all the code in Shape_Generation_Functions.py
|
15 |
+
def make_boxes(image_size: int, densities: list[float]) -> list[tuple]:
|
16 |
"""
|
17 |
:param image_size: [int] - the pixel height and width of the generated arrays
|
18 |
+
:param densities: [list[float]] - of the desired pixel values to apply to active pixels - Recommend values (0,1]
|
19 |
:return: [list[tuple]] - [Array, Density, Thickness of each strut type]
|
20 |
"""
|
21 |
|
Data_Generation/Piecewise_Box_Functions.py
CHANGED
@@ -2,28 +2,52 @@ 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
|
@@ -32,7 +56,13 @@ def hot_dog_array(image_size, 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
|
@@ -43,7 +73,13 @@ def hamburger_array(image_size, thickness):
|
|
43 |
|
44 |
########################################################################################################################
|
45 |
# The function to add thickness to struts in an array
|
46 |
-
def add_thickness(array_original, thickness):
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
A = array_original
|
48 |
if thickness == 0: # want an array of all 0's for thickness = 0
|
49 |
A[A > 0] = 0
|
@@ -52,13 +88,14 @@ def add_thickness(array_original, thickness):
|
|
52 |
filter = np.zeros((filter_size, filter_size))
|
53 |
filter[np.floor((filter_size - 1) / 2).astype(int), :] = filter[:, np.floor((filter_size - 1) / 2).astype(int)] =1
|
54 |
filter[np.ceil((filter_size - 1) / 2).astype(int), :] = filter[:, np.ceil((filter_size - 1) / 2).astype(int)] = 1
|
|
|
55 |
convolution = signal.convolve2d(A, filter, mode='same')
|
56 |
A = np.where(convolution <= 1, convolution, 1)
|
57 |
return A
|
58 |
|
59 |
|
60 |
# The function to efficiently combine arrays in a list
|
61 |
-
def combine_arrays(arrays):
|
62 |
output_array = np.sum(arrays, axis=0) # Add the list of arrays
|
63 |
output_array = np.array(output_array > 0, dtype=int) # Convert all values in array to 1
|
64 |
return output_array
|
|
|
2 |
from scipy import signal
|
3 |
|
4 |
|
5 |
+
def basic_box_array(image_size: int, thickness: int) -> np.ndarray:
|
6 |
+
"""
|
7 |
+
:param image_size: [int] - the size of the image that will be produced
|
8 |
+
:param thickness: [int] - the number of pixels to be activated surrounding the base shape
|
9 |
+
:return: [ndarray] - the output is a unit cell with outer pixels activated based on the desired thickness.
|
10 |
+
The activated pixels are 1 (white) and the deactivated pixels are 0 (black)
|
11 |
+
"""
|
12 |
A = np.ones((int(image_size), int(image_size))) # Initializes A matrix with 0 values
|
13 |
A[1:-1, 1:-1] = 0 # replaces all internal rows/columns with 0's
|
14 |
A = add_thickness(A, thickness)
|
15 |
return A
|
16 |
|
17 |
|
18 |
+
def back_slash_array(image_size: int, thickness: int) -> np.ndarray:
|
19 |
+
"""
|
20 |
+
:param image_size: [int] - the size of the image that will be produced
|
21 |
+
:param thickness: [int] - the number of pixels to be activated surrounding the base shape
|
22 |
+
:return: [ndarray] - the output is a unit cell with pixels activated along the downward diagonal based
|
23 |
+
on the desired thickness. The activated pixels are 1 (white) and the deactivated pixels are 0 (black)
|
24 |
+
"""
|
25 |
A = np.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
|
26 |
np.fill_diagonal(A, 1) # fills the diagonal with 1 values
|
27 |
A = add_thickness(A, thickness)
|
28 |
return A
|
29 |
|
30 |
|
31 |
+
def forward_slash_array(image_size: int, thickness: int) -> np.ndarray:
|
32 |
+
"""
|
33 |
+
:param image_size: [int] - the size of the image that will be produced
|
34 |
+
:param thickness: [int] - the number of pixels to be activated surrounding the base shape
|
35 |
+
:return: [ndarray] - the output is a unit cell with pixels activated along the upward diagonal based on the desired
|
36 |
+
thickness. The activated pixels are 1 (white) and the deactivated pixels are 0 (black)
|
37 |
+
"""
|
38 |
A = np.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
|
39 |
np.fill_diagonal(np.fliplr(A), 1) # Flips the array to then fill the diagonal the opposite direction
|
40 |
A = add_thickness(A, thickness)
|
41 |
return A
|
42 |
|
43 |
|
44 |
+
def hot_dog_array(image_size: int, thickness: int) -> np.ndarray:
|
45 |
+
"""
|
46 |
+
:param image_size: [int] - the size of the image that will be produced
|
47 |
+
:param thickness: [int] - the number of pixels to be activated surrounding the base shape
|
48 |
+
:return: [ndarray] - the output is a unit cell with outer pixel activated from the vertical center based on the
|
49 |
+
desired thickness. The activated pixels are 1 (white) and the deactivated pixels are 0 (black)
|
50 |
+
"""
|
51 |
# Places pixels down the vertical axis to split the box
|
52 |
A = np.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
|
53 |
A[:, np.floor((image_size - 1) / 2).astype(int)] = 1 # accounts for even and odd values of image_size
|
|
|
56 |
return A
|
57 |
|
58 |
|
59 |
+
def hamburger_array(image_size: int, thickness: int) -> np.ndarray:
|
60 |
+
"""
|
61 |
+
:param image_size: [int] - the size of the image that will be produced
|
62 |
+
:param thickness: [int] - the number of pixels to be activated surrounding the base shape
|
63 |
+
:return: [ndarray] - the output is a unit cell with outer pixel activated from the horizontal center based on the
|
64 |
+
desired thickness. The activated pixels are 1 (white) and the deactivated pixels are 0 (black)
|
65 |
+
"""
|
66 |
# Places pixels across the horizontal axis to split the box
|
67 |
A = np.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
|
68 |
A[np.floor((image_size - 1) / 2).astype(int), :] = 1 # accounts for even and odd values of image_size
|
|
|
73 |
|
74 |
########################################################################################################################
|
75 |
# The function to add thickness to struts in an array
|
76 |
+
def add_thickness(array_original, thickness: int) -> np.ndarray:
|
77 |
+
"""
|
78 |
+
:param array_original: [ndarray] - an array with thickness 1 of any shape type
|
79 |
+
:param thickness: [int] - the number of pixels to be activated surrounding the base shape
|
80 |
+
:return: [ndarray] - the output is a unit cell that has been convolved to expand the number of pixels activated
|
81 |
+
based on the desired thickness. The activated pixels are 1 (white) and the deactivated pixels are 0 (black)
|
82 |
+
"""
|
83 |
A = array_original
|
84 |
if thickness == 0: # want an array of all 0's for thickness = 0
|
85 |
A[A > 0] = 0
|
|
|
88 |
filter = np.zeros((filter_size, filter_size))
|
89 |
filter[np.floor((filter_size - 1) / 2).astype(int), :] = filter[:, np.floor((filter_size - 1) / 2).astype(int)] =1
|
90 |
filter[np.ceil((filter_size - 1) / 2).astype(int), :] = filter[:, np.ceil((filter_size - 1) / 2).astype(int)] = 1
|
91 |
+
# The filter is made into a '+' shape using these functions
|
92 |
convolution = signal.convolve2d(A, filter, mode='same')
|
93 |
A = np.where(convolution <= 1, convolution, 1)
|
94 |
return A
|
95 |
|
96 |
|
97 |
# The function to efficiently combine arrays in a list
|
98 |
+
def combine_arrays(arrays: list[np.ndarray]) -> np.ndarray:
|
99 |
output_array = np.sum(arrays, axis=0) # Add the list of arrays
|
100 |
output_array = np.array(output_array > 0, dtype=int) # Convert all values in array to 1
|
101 |
return output_array
|
Data_Plotting/Plot_TSNE.py
CHANGED
@@ -3,7 +3,7 @@ import matplotlib.pyplot as plt
|
|
3 |
import numpy as np
|
4 |
|
5 |
# Latent Feature Cluster for Training Data using T-SNE
|
6 |
-
def TSNE_reduction(latent_points, perplexity=30, learning_rate=20):
|
7 |
latent_dimensionality = len(latent_points[0])
|
8 |
model = TSNE(n_components=2, random_state=0, perplexity=perplexity,
|
9 |
learning_rate=learning_rate) # Perplexity(5-50) | learning_rate(10-1000)
|
@@ -25,11 +25,14 @@ def TSNE_reduction(latent_points, perplexity=30, learning_rate=20):
|
|
25 |
|
26 |
def plot_dimensionality_reduction(x, y, label_set, title):
|
27 |
plt.title(title)
|
|
|
28 |
if label_set[0].dtype == float:
|
29 |
plt.scatter(x, y, c=label_set)
|
30 |
cbar = plt.colorbar()
|
31 |
cbar.set_label('Average Density', fontsize=12)
|
32 |
print("using scatter")
|
|
|
|
|
33 |
else:
|
34 |
for label in set(label_set):
|
35 |
cond = np.where(np.array(label_set) == str(label))
|
|
|
3 |
import numpy as np
|
4 |
|
5 |
# Latent Feature Cluster for Training Data using T-SNE
|
6 |
+
def TSNE_reduction(latent_points: list, perplexity=30, learning_rate=20):
|
7 |
latent_dimensionality = len(latent_points[0])
|
8 |
model = TSNE(n_components=2, random_state=0, perplexity=perplexity,
|
9 |
learning_rate=learning_rate) # Perplexity(5-50) | learning_rate(10-1000)
|
|
|
25 |
|
26 |
def plot_dimensionality_reduction(x, y, label_set, title):
|
27 |
plt.title(title)
|
28 |
+
# Color points based on their density
|
29 |
if label_set[0].dtype == float:
|
30 |
plt.scatter(x, y, c=label_set)
|
31 |
cbar = plt.colorbar()
|
32 |
cbar.set_label('Average Density', fontsize=12)
|
33 |
print("using scatter")
|
34 |
+
|
35 |
+
# Color points based on a discrete label
|
36 |
else:
|
37 |
for label in set(label_set):
|
38 |
cond = np.where(np.array(label_set) == str(label))
|
app.py
CHANGED
@@ -69,7 +69,7 @@ if st.button('Generate Samples'): # Generate the samples
|
|
69 |
# Output Entire Dataset
|
70 |
st.write("Click 'Generate Dataset' to generate the dataset based on the conditions set previously:")
|
71 |
if st.button('Generate Dataset'): # Generate the dataset
|
72 |
-
boxes = make_boxes(image_size, densities) # Create all
|
73 |
# Unpack all of the data
|
74 |
box_arrays, box_density, basic_box_thickness, forward_slash_box_thickness, back_slash_box_thickness,hot_dog_box_thickness, hamburger_box_thickness\
|
75 |
= list(zip(*boxes))[0], list(zip(*boxes))[1], list(zip(*boxes))[2], list(zip(*boxes))[3], list(zip(*boxes))[4], list(zip(*boxes))[5], list(zip(*boxes))[6]
|
|
|
69 |
# Output Entire Dataset
|
70 |
st.write("Click 'Generate Dataset' to generate the dataset based on the conditions set previously:")
|
71 |
if st.button('Generate Dataset'): # Generate the dataset
|
72 |
+
boxes = make_boxes(image_size, densities) # Create all the data points
|
73 |
# Unpack all of the data
|
74 |
box_arrays, box_density, basic_box_thickness, forward_slash_box_thickness, back_slash_box_thickness,hot_dog_box_thickness, hamburger_box_thickness\
|
75 |
= list(zip(*boxes))[0], list(zip(*boxes))[1], list(zip(*boxes))[2], list(zip(*boxes))[3], list(zip(*boxes))[4], list(zip(*boxes))[5], list(zip(*boxes))[6]
|