Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
167 lines (135 loc) · 5.69 KB
/
Copy pathapp.py
File metadata and controls
167 lines (135 loc) · 5.69 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import streamlit as st
import time
import os
import io
from streamlit.runtime.uploaded_file_manager import UploadedFile
from src.graph_with_subgraph import GraphWithSubgraph
from src.graph_types import GraphType
import src.random_graph as rg
import src.motif_statistics as stat
st.set_page_config(page_title="NEMO motif detection program")
st.title('NEMO motif detection program')
def form_callback(start_time):
"""
Handle form validation logic
"""
if st.session_state.get("uploaded_file") is None:
st.warning("Please upload a file.")
return
if st.session_state.get("graph_type") is None:
st.warning("Please select a graph type.")
return
if st.session_state.get("number_of_random_graphs") is None:
st.warning("Please select a number of random graphs between 5-100.")
return
# create graph from file
G = GraphWithSubgraph(
graph_type=st.session_state["graph_type"],
input=st.session_state["uploaded_file"],
motif_size=st.session_state["motif_size"],
)
# display graph properties
graph_properties = G.get_graph_properties()
st.write(f"Number of nodes: {graph_properties['Number of nodes']}")
st.write(f"Number of edges: {graph_properties['Number of edges']}")
st.write(f"Weight: {graph_properties['Weight']}")
st.write(f"Number of subgraphs: {graph_properties['Number of subgraphs']}")
# visualize the full graph if selected
if st.session_state["is_visualize_graph"]:
st.markdown("### Full Graph Visualization")
G.draw_graph()
# Visualize subgraphs if selected
if st.session_state["is_visualize_subgraph"]:
st.markdown("### Subgraph Visualization")
G.draw_subgraph()
# Generate random graphs
random_graphs = rg.generate_random_graphs(G, st.session_state['number_of_random_graphs'])
stats = stat.process_statistics(G, random_graphs)
end_time = time.time()
elapsed_time = end_time - start_time
st.write(f"Time elapsed: {elapsed_time:.2f} seconds")
# Visualize statistics
st.markdown("### Statistics Table")
stat.draw_statistics(stats)
# Download button if nemo count option is selected to subgraph collection
if st.session_state["nemo_count_option"] == "NemoCount":
G.generate_nemo_count()
if st.session_state["nemo_count_option"] == "SubgraphProfile":
G.generate_subgraph_profile()
if st.session_state["nemo_count_option"] == "SubgraphCollection":
G.generate_subgraph_collection()
def main():
# Initialize global session state for user form submission
if "graph_type" not in st.session_state:
st.session_state["graph_type"] = None
if "uploaded_file" not in st.session_state:
st.session_state["uploaded_file"] = None
if "prev_uploaded_file" not in st.session_state:
st.session_state["prev_uploaded_file"] = None
uploaded_file = st.file_uploader("")
if uploaded_file:
if uploaded_file != st.session_state["prev_uploaded_file"]:
st.session_state["uploaded_file"] = uploaded_file
st.session_state["prev_uploaded_file"] = uploaded_file
st.toast("Successfully uploaded file", icon="✅")
demo = st.button("Use Demo File")
if demo:
demo_path = "./NetMotif/data/bestTest.txt"
if os.path.exists(demo_path):
with open(demo_path, "rb") as file:
file_content = file.read()
# Simulate an uploaded file using BytesIO
uploaded_file = io.BytesIO(file_content)
uploaded_file.name = "bestTest.txt"
uploaded_file.type = "text/plain"
st.session_state["uploaded_file"] = uploaded_file
st.session_state["prev_uploaded_file"] = uploaded_file
st.toast("Successfully uploaded demo file", icon="✅")
with st.form(key="form"):
col1, col2 = st.columns([1, 2])
with col1:
graph_type = st.radio(
"Set Graph type:",
key="graph",
options=[GraphType.UNDIRECTED, GraphType.DIRECTED],
format_func=lambda x: x.value,
)
motif_size = st.number_input(
"Size of motif",
value=3,
placeholder="Input motif size...",
min_value=1,
max_value=5,
)
number_of_random_graphs = st.number_input(
"Number of random graphs",
value=20,
placeholder="Input number of graphs...",
min_value=5,
max_value=100,
)
nemo_count_type = st.radio(
"Nemo Data Options",
key="nemo_option",
options=["NemoCount", "SubgraphProfile", "SubgraphCollection"],
)
with col2:
st.write(
"NOTE: Uploading more than 1000 nodes might consume more processing time."
)
st.write("Visualize Options:")
is_visualize_graph = st.checkbox("Visualize graph")
is_visualize_subgraph = st.checkbox("Visualize subgraph")
submitted = st.form_submit_button(label="Submit")
if submitted:
st.session_state["is_visualize_graph"] = is_visualize_graph
st.session_state["is_visualize_subgraph"] = is_visualize_subgraph
st.session_state["graph_type"] = graph_type
st.session_state["motif_size"] = motif_size
st.session_state["number_of_random_graphs"] = number_of_random_graphs
st.session_state["nemo_count_option"] = nemo_count_type
start_time = time.time()
form_callback(start_time)
st.markdown("<br>", unsafe_allow_html=True)
if __name__ == "__main__":
main()