-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
100 lines (82 loc) · 3.04 KB
/
app.py
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
import streamlit as st
import matplotlib.pyplot as plt
from affinity_propagation import affinity_prop
from generate_data import generate_data
def validate_preference(preference):
if preference == "median":
return True
try:
float(preference)
return True
except ValueError:
return False
def main():
st.set_page_config(
page_title="Affinity Propagation clustering",
)
with st.sidebar.form(key="Data Generator"):
st.write("## Data Generator")
data_size = st.slider("Data size", min_value=100, max_value=300)
data_cluster_size = st.slider(
"Number of Clusters", min_value=1, max_value=10, value=2
)
cluster_std = st.slider(
"Standard Deviation of Clusters", min_value=0.0, max_value=10.0, value=1.0
)
generate_button = st.form_submit_button(label="Generate")
with st.sidebar.form(key="Affinity Propagation"):
st.write("## Affinity Propagation Options")
max_iterations = st.slider(
"Maximum iterations", min_value=100, max_value=500, value=100
)
local_threshold = st.slider(
"Local threshold", min_value=10, max_value=30, value=10
)
damping_factor = st.slider(
"Damping factor", min_value=0.1, max_value=1.0, value=0.7
)
preference = st.text_input("Preference", value="median")
train_button = st.form_submit_button(label="Train")
st.title(
"Affinity Propagation clustering",
)
if "fig" not in st.session_state:
st.session_state.fig, st.session_state.ax = plt.subplots(figsize=(14, 12))
st.session_state.ax.axes.xaxis.set_visible(False)
st.session_state.ax.axes.yaxis.set_visible(False)
st.session_state.data_plot = st.pyplot(st.session_state.fig)
st.session_state.res_box = st.empty()
else:
st.session_state.ax.clear()
st.session_state.res_box.empty()
if generate_button:
st.session_state.sample_data = generate_data(
data_cluster_size, cluster_std, data_size
)[0]
st.session_state.ax.plot(
st.session_state.sample_data[:, 0],
st.session_state.sample_data[:, 1],
".",
alpha=0.5,
)
st.session_state.data_plot.pyplot(st.session_state.fig)
if train_button:
if not validate_preference(preference):
st.error("Preference must be either 'median' or a numeric value.")
return
if "sample_data" not in st.session_state:
st.error("You must generate data first.")
return
affinity_prop(
st.session_state.sample_data,
maxiter=max_iterations,
damping_factor=damping_factor,
preference=preference,
local_thresh=local_threshold,
data_plot=st.session_state.data_plot,
fig=st.session_state.fig,
ax=st.session_state.ax,
c=st.session_state.res_box,
)
if __name__ == "__main__":
main()