marta-marta commited on
Commit
bfd79e7
·
1 Parent(s): 73d009a

Adding type hints to the functions in these scripts

Browse files
Files changed (1) hide show
  1. app.py +10 -11
app.py CHANGED
@@ -10,7 +10,8 @@ from Data_Generation.Piecewise_Box_Functions import basic_box_array, forward_sla
10
  from Data_Plotting.Plot_TSNE import TSNE_reduction, plot_dimensionality_reduction
11
  ########################################################################################################################
12
  # User Inputs
13
- image_size = st.slider('Select a value for the image size', min_value=9, max_value=16)
 
14
 
15
  density_selection = st.slider('Select a value for the number of equally spaced density values (0, 1]', min_value=1, max_value=10)
16
  ########################################################################################################################
@@ -35,11 +36,10 @@ for i in [1, 2, 3, 4]:
35
  st.write("Click 'Generate Samples' to show some density values that would exist in your dataset:")
36
 
37
  # Show samples of various density values
38
-
39
  if st.button('Generate Samples'): # Generate the samples
40
  plt.figure(1)
41
  st.header("Sample Density Figures:")
42
- max_figures = min(density_selection, 5)
43
  for i in range(max_figures):
44
  plt.subplot(1, max_figures+1, i+1), plt.imshow(sample_density[i], cmap='gray', vmin=0, vmax=1)
45
  if i != 0: # Show y-label for only first figure
@@ -69,22 +69,21 @@ 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 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]
76
 
77
  # Plot TSNE of the data
78
- # define a function to flatten a 2D array
79
- def flatten_array(array):
80
  return array.flatten()
81
-
82
  # apply the flatten_array function to each array in the list and create a list of flattened arrays
83
  flattened_arrays = np.array([flatten_array(a) for a in box_arrays])
84
  # calculate the average density for each array
85
  avg_density = np.sum(flattened_arrays, axis=1)/(np.shape(box_arrays[0])[0]*np.shape(box_arrays[0])[1])
86
 
87
-
88
  # Perform the TSNE Reduction
89
  x, y, title, embedding = TSNE_reduction(flattened_arrays)
90
  plt.figure(3)
@@ -96,7 +95,7 @@ if st.button('Generate Dataset'): # Generate the dataset
96
  plt.figure(3)
97
  st.pyplot(plt.figure(3))
98
 
99
-
100
  class NumpyArrayEncoder(JSONEncoder):
101
  def default(self, obj):
102
  if isinstance(obj, np.ndarray):
@@ -104,7 +103,7 @@ if st.button('Generate Dataset'): # Generate the dataset
104
  return JSONEncoder.default(self, obj)
105
 
106
  # Save the arrays in a JSON format so they can be read
107
- box_arrays = [json.dumps(x, cls=NumpyArrayEncoder) for x in box_arrays] # find argument for json dumps for numpy
108
 
109
  # Create a dataframe to convert the data to a csv file
110
  dataframe = (pd.DataFrame((box_arrays, box_density, basic_box_thickness, forward_slash_box_thickness,
 
10
  from Data_Plotting.Plot_TSNE import TSNE_reduction, plot_dimensionality_reduction
11
  ########################################################################################################################
12
  # User Inputs
13
+ image_size = st.slider('Select a value for the image size', min_value=9, max_value=16) # Max value is limited due to
14
+ # computational limitations of streamlit
15
 
16
  density_selection = st.slider('Select a value for the number of equally spaced density values (0, 1]', min_value=1, max_value=10)
17
  ########################################################################################################################
 
36
  st.write("Click 'Generate Samples' to show some density values that would exist in your dataset:")
37
 
38
  # Show samples of various density values
 
39
  if st.button('Generate Samples'): # Generate the samples
40
  plt.figure(1)
41
  st.header("Sample Density Figures:")
42
+ max_figures = min(density_selection, 5) # Determine the number of figures to display
43
  for i in range(max_figures):
44
  plt.subplot(1, max_figures+1, i+1), plt.imshow(sample_density[i], cmap='gray', vmin=0, vmax=1)
45
  if i != 0: # Show y-label for only first figure
 
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
+
74
+ # Unpack all the data
75
  box_arrays, box_density, basic_box_thickness, forward_slash_box_thickness, back_slash_box_thickness,hot_dog_box_thickness, hamburger_box_thickness\
76
  = 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]
77
 
78
  # Plot TSNE of the data
79
+ # Determine the labels of the TSNE Plot
80
+ def flatten_array(array): # define a function to flatten a 2D array
81
  return array.flatten()
 
82
  # apply the flatten_array function to each array in the list and create a list of flattened arrays
83
  flattened_arrays = np.array([flatten_array(a) for a in box_arrays])
84
  # calculate the average density for each array
85
  avg_density = np.sum(flattened_arrays, axis=1)/(np.shape(box_arrays[0])[0]*np.shape(box_arrays[0])[1])
86
 
 
87
  # Perform the TSNE Reduction
88
  x, y, title, embedding = TSNE_reduction(flattened_arrays)
89
  plt.figure(3)
 
95
  plt.figure(3)
96
  st.pyplot(plt.figure(3))
97
 
98
+ # Create a class to read the information from the generated CSV file
99
  class NumpyArrayEncoder(JSONEncoder):
100
  def default(self, obj):
101
  if isinstance(obj, np.ndarray):
 
103
  return JSONEncoder.default(self, obj)
104
 
105
  # Save the arrays in a JSON format so they can be read
106
+ box_arrays = [json.dumps(x, cls=NumpyArrayEncoder) for x in box_arrays]
107
 
108
  # Create a dataframe to convert the data to a csv file
109
  dataframe = (pd.DataFrame((box_arrays, box_density, basic_box_thickness, forward_slash_box_thickness,