Commit
·
13b5dca
1
Parent(s):
062bd85
Need new libs to be considered in the app
Browse files- Data_Plotting/Plot_TSNE.py +35 -24
- app.py +8 -2
Data_Plotting/Plot_TSNE.py
CHANGED
@@ -23,15 +23,41 @@ def TSNE_reduction(latent_points, perplexity=30, learning_rate=20):
|
|
23 |
return x, y, title, embedding
|
24 |
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
########################################################################################################################
|
|
|
|
|
|
|
27 |
import pandas as pd
|
28 |
import json
|
29 |
|
30 |
-
"""
|
31 |
df = pd.read_csv('2D_Lattice.csv')
|
32 |
-
row = 0
|
33 |
-
box = df.iloc[row,1]
|
34 |
-
array = np.array(json.loads(box))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
# For plotting CSV data
|
37 |
# define a function to flatten a box
|
@@ -42,27 +68,12 @@ def flatten_box(box_str):
|
|
42 |
|
43 |
# apply the flatten_box function to each row of the dataframe and create a list of flattened arrays
|
44 |
flattened_arrays = df['Array'].apply(flatten_box).tolist()
|
|
|
45 |
|
46 |
-
|
47 |
-
x, y,
|
48 |
-
|
49 |
-
plt.scatter(x,y)
|
50 |
plt.title(title)
|
51 |
-
plt.
|
|
|
52 |
"""
|
53 |
|
54 |
-
# def plot_dimensionality_reduction(x, y, label_set, title):
|
55 |
-
# plt.title(title)
|
56 |
-
# if label_set[0].dtype == float:
|
57 |
-
# plt.scatter(x, y, c=label_set)
|
58 |
-
# plt.colorbar()
|
59 |
-
# print("using scatter")
|
60 |
-
# else:
|
61 |
-
# for label in set(label_set):
|
62 |
-
# cond = np.where(np.array(label_set) == str(label))
|
63 |
-
# plt.plot(x[cond], y[cond], marker='o', linestyle='none', label=label)
|
64 |
-
#
|
65 |
-
# plt.legend(numpoints=1)
|
66 |
-
#
|
67 |
-
# plt.show()
|
68 |
-
# plt.close()
|
|
|
23 |
return x, y, title, embedding
|
24 |
|
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 |
+
plt.colorbar()
|
31 |
+
print("using scatter")
|
32 |
+
else:
|
33 |
+
for label in set(label_set):
|
34 |
+
cond = np.where(np.array(label_set) == str(label))
|
35 |
+
plt.plot(x[cond], y[cond], marker='o', linestyle='none', label=label)
|
36 |
+
|
37 |
+
plt.legend(numpoints=1)
|
38 |
########################################################################################################################
|
39 |
+
"""
|
40 |
+
# Use for personal plotting
|
41 |
+
|
42 |
import pandas as pd
|
43 |
import json
|
44 |
|
|
|
45 |
df = pd.read_csv('2D_Lattice.csv')
|
46 |
+
# row = 0
|
47 |
+
# box = df.iloc[row,1]
|
48 |
+
# array = np.array(json.loads(box))
|
49 |
+
|
50 |
+
# Select a subset of the data to use
|
51 |
+
number_samples = 10000
|
52 |
+
perplexity = 300
|
53 |
+
|
54 |
+
random_samples = sorted(np.random.randint(0,len(df), number_samples)) # Generates ordered samples
|
55 |
+
|
56 |
+
df = df.iloc[random_samples]
|
57 |
+
|
58 |
+
print(df)
|
59 |
+
print(np.shape(df))
|
60 |
+
|
61 |
|
62 |
# For plotting CSV data
|
63 |
# define a function to flatten a box
|
|
|
68 |
|
69 |
# apply the flatten_box function to each row of the dataframe and create a list of flattened arrays
|
70 |
flattened_arrays = df['Array'].apply(flatten_box).tolist()
|
71 |
+
avg_density = np.sum(flattened_arrays, axis=1)/(len(flattened_arrays[0]))
|
72 |
|
73 |
+
x, y, title, embedding = TSNE_reduction(flattened_arrays, perplexity=perplexity)
|
74 |
+
plot_dimensionality_reduction(x, y, avg_density, title)
|
|
|
|
|
75 |
plt.title(title)
|
76 |
+
plt.savefig('TSNE_Partial_Factorial_Perplexity_' + str(perplexity) + "_Data_Samples_" + str(number_samples))
|
77 |
+
|
78 |
"""
|
79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
CHANGED
@@ -7,7 +7,7 @@ import streamlit as st
|
|
7 |
|
8 |
from Data_Generation.Dataset_Generation_Functions import make_boxes
|
9 |
from Data_Generation.Piecewise_Box_Functions import basic_box_array, forward_slash_array, combine_arrays, add_thickness
|
10 |
-
from Data_Plotting.Plot_TSNE import TSNE_reduction
|
11 |
########################################################################################################################
|
12 |
# User Inputs
|
13 |
image_size = st.slider('Select a value for the image size', min_value=9, max_value=16)
|
@@ -81,11 +81,17 @@ if st.button('Generate Dataset'): # Generate the dataset
|
|
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 |
|
85 |
# Perform the TSNE Reduction
|
86 |
x, y, title, embedding = TSNE_reduction(flattened_arrays)
|
87 |
plt.figure(3)
|
88 |
-
|
|
|
|
|
|
|
89 |
plt.title(title)
|
90 |
plt.figure(3)
|
91 |
st.pyplot(plt.figure(3))
|
|
|
7 |
|
8 |
from Data_Generation.Dataset_Generation_Functions import make_boxes
|
9 |
from Data_Generation.Piecewise_Box_Functions import basic_box_array, forward_slash_array, combine_arrays, add_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)
|
|
|
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)
|
91 |
+
# set the color values for the plot
|
92 |
+
plot_dimensionality_reduction(x, y, avg_density, title)
|
93 |
+
|
94 |
+
# plt.scatter(x, y)
|
95 |
plt.title(title)
|
96 |
plt.figure(3)
|
97 |
st.pyplot(plt.figure(3))
|