File size: 6,956 Bytes
558fff7
 
 
 
29b4b22
2dbee31
558fff7
7f44091
558fff7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be9bc89
 
 
 
558fff7
be9bc89
 
 
558fff7
be9bc89
 
 
558fff7
be9bc89
 
558fff7
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import numpy as np
from scipy import signal
import huggingface_hub  # for loading model
import streamlit as st
import tensorflow
# HELLO HUGGING FACE


def basic_box_array(image_size: int, thickness: int) -> np.ndarray:
    """
    :param image_size: [int] - the size of the image that will be produced
    :param thickness: [int] - the number of pixels to be activated surrounding the base shape
    :return: [ndarray] - the output is a unit cell with outer pixels activated based on the desired thickness.
    The activated pixels are 1 (white) and the deactivated pixels are 0 (black)
    """
    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: int, thickness: int) -> np.ndarray:
    """
    :param image_size: [int] - the size of the image that will be produced
    :param thickness: [int] - the number of pixels to be activated surrounding the base shape
    :return: [ndarray] - the output is a unit cell with pixels activated along the downward diagonal based
    on the desired thickness. The activated pixels are 1 (white) and the deactivated pixels are 0 (black)
    """
    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: int, thickness: int) -> np.ndarray:
    """
    :param image_size: [int] - the size of the image that will be produced
    :param thickness: [int] - the number of pixels to be activated surrounding the base shape
    :return: [ndarray] - the output is a unit cell with pixels activated along the upward diagonal based on the desired
    thickness. The activated pixels are 1 (white) and the deactivated pixels are 0 (black)
    """
    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: int, thickness: int) -> np.ndarray:
    """
    :param image_size: [int] - the size of the image that will be produced
    :param thickness: [int] - the number of pixels to be activated surrounding the base shape
    :return: [ndarray] - the output is a unit cell with outer pixel activated from the vertical center based on the
    desired thickness. The activated pixels are 1 (white) and the deactivated pixels are 0 (black)
    """
    # 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: int, thickness: int) -> np.ndarray:
    """
    :param image_size: [int] - the size of the image that will be produced
    :param thickness: [int] - the number of pixels to be activated surrounding the base shape
    :return: [ndarray] - the output is a unit cell with outer pixel activated from the horizontal center based on the
    desired thickness. The activated pixels are 1 (white) and the deactivated pixels are 0 (black)
    """
    # 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


########################################################################################################################
# The function to add thickness to struts in an array
def add_thickness(array_original, thickness: int) -> np.ndarray:
    """
    :param array_original: [ndarray] - an array with thickness 1 of any shape type
    :param thickness: [int] - the number of pixels to be activated surrounding the base shape
    :return: [ndarray] - the output is a unit cell that has been convolved to expand the number of pixels activated
    based on the desired thickness. The activated pixels are 1 (white) and the deactivated pixels are 0 (black)
    """
    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
        # The filter is made into a '+' shape using these functions
        convolution = signal.convolve2d(A, filter, mode='same')
        A = np.where(convolution <= 1, convolution, 1)
    return A


# The function to efficiently combine arrays in a list
def combine_arrays(arrays):
    output_array = np.sum(arrays, axis=0)  # Add the list of arrays
    output_array = np.array(output_array > 0, dtype=int)  # Convert all values in array to 1
    return output_array


########################################################################################################################
# Provide the Options for users to select from
shape_options = ("basic_box", "diagonal_box_split", "horizontal_vertical_box_split", "back_slash_box", "forward_slash_box",
"back_slash_plus_box", "forward_slash_plus_box", "hot_dog_box", "hamburger_box", "x_hamburger_box",
"x_hot_dog_box", "x_plus_box")
density_options = ["{:.2f}".format(x) for x in np.linspace(0.1, 1, 10)]
thickness_options = [str(int(x)) for x in np.linspace(0, 10, 11)]
interpolation_options = [str(int(x)) for x in [3, 5, 10, 20]]

# Provide User Options
# Select Shapes
shape_1 = st.selectbox("Shape 1", shape_options)
shape_2 = st.selectbox("Shape 2", shape_options)

# Select Density
density_1 = st.selectbox("Density 1:", density_options)
density_2 = st.selectbox("Density 2:", density_options)

# Select Thickness
thickness_1 = st.selectbox("Thickness 1", thickness_options)
thickness_2 = st.selectbox("Thickness 2", thickness_options)

# Select Interpolation Length
interp_length = st.selectbox("Interpolation Length", interpolation_options)


# Load the models from existing huggingface model
# Load the encoder model
encoder_model_boxes = huggingface_hub.from_pretrained_keras("cmudrc/2d-lattice-encoder")
# Load the decoder model
decoder_model_boxes = huggingface_hub.from_pretrained_keras("cmudrc/2d-lattice-decoder")