linxy commited on
Commit
a1f356b
·
verified ·
1 Parent(s): d96e485

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +237 -0
app.py ADDED
@@ -0,0 +1,237 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+
4
+ import networkx as nx
5
+ import streamlit as st
6
+ from st_link_analysis import st_link_analysis, NodeStyle, EdgeStyle
7
+ from st_link_analysis.component.layouts import LAYOUTS
8
+ from datasets import load_dataset
9
+
10
+ LAYOUT_NAMES = list(LAYOUTS.keys())
11
+
12
+ st.set_page_config(layout="wide")
13
+
14
+ # Sample Data
15
+
16
+ elements = {
17
+ "nodes": [
18
+ {"data": {"id": 1, "label": "Node", "name": "Streamlit"}},
19
+ {"data": {"id": 2, "label": "PERSON", "name": "Hello"}},
20
+ {"data": {"id": 3, "label": "Node", "name": "World"}},
21
+ {"data": {"id": 4, "label": "POST", "content": "x"}},
22
+ {"data": {"id": 5, "label": "POST", "content": "y"}},
23
+ ],
24
+ "edges": [
25
+ {"data": {"id": 6, "label": "FOLLOWS", "source": 1, "target": 2}},
26
+ {"data": {"id": 7, "label": "FOLLOWS", "source": 2, "target": 3}},
27
+ {"data": {"id": 8, "label": "POSTED", "source": 3, "target": 4}},
28
+ {"data": {"id": 9, "label": "POSTED", "source": 1, "target": 5}},
29
+ {"data": {"id": 10, "label": "QUOTES", "source": 5, "target": 4}},
30
+ ],
31
+ }
32
+
33
+ # Style node & edge groups
34
+ node_styles = [
35
+ NodeStyle("Node", "#FF7F3E", "name"),
36
+ ]
37
+
38
+ edge_styles = [
39
+ EdgeStyle("RED", "#FFB6C1", directed=False),
40
+ EdgeStyle("BLUE", "#87CEEB", directed=False),
41
+ ]
42
+
43
+ # Render the component
44
+ @st.cache_data
45
+ def load_graphs(graphset_name: str):
46
+ graphs = nx.read_graph6(f"data/{graphset_name}")
47
+ return graphs
48
+ @st.cache_data
49
+ def load_graphs_from_dataset(graphset_name: str):
50
+ dataset = load_dataset("linxy/RamseyGraph", graphset_name, trust_remote_code=True)
51
+ return list(dataset["train"])
52
+
53
+ _FILENAMES = [
54
+ "r34_8",
55
+ "r35_12",
56
+ "r44_3",
57
+ "r37_22",
58
+ "r35_9",
59
+ "r44_7",
60
+ "r39_35",
61
+ "r45_24",
62
+ "r44_6",
63
+ "r44_11",
64
+ "r35_13",
65
+ "r44_2",
66
+ "r44_13",
67
+ "r35_8",
68
+ "r44_9",
69
+ "r34_2",
70
+ "r35_3",
71
+ "r36_4",
72
+ "r44_14",
73
+ "r34_6",
74
+ "r37_21",
75
+ "r35_7",
76
+ "r44_15",
77
+ "r34_7",
78
+ "r36_1",
79
+ "r35_6",
80
+ "r44_12",
81
+ "r36_17",
82
+ "r44_10",
83
+ "r44_8",
84
+ "r34_3",
85
+ "r35_2",
86
+ "r36_5",
87
+ "r44_16",
88
+ "r34_4",
89
+ "r36_16",
90
+ "r36_2",
91
+ "r35_5",
92
+ "r36_14",
93
+ "r35_1",
94
+ "r36_6",
95
+ "r36_10",
96
+ "r34_1",
97
+ "r36_7",
98
+ "r44_17",
99
+ "r36_12",
100
+ "r34_5",
101
+ "r38_27",
102
+ "r36_3",
103
+ "r35_4",
104
+ "r36_15",
105
+ "r44_5",
106
+ "r36_8",
107
+ "r55_42some",
108
+ "r35_10",
109
+ "r44_1",
110
+ "r35_11",
111
+ "r36_13",
112
+ "r46_35some",
113
+ "r44_4",
114
+ "r36_11",
115
+ "r36_9",
116
+ ]
117
+
118
+ def convert_graph_to_json(G, show_blue):
119
+ # 创建元素列表,存储节点和边的数据
120
+ elements = {"nodes": [], "edges": []}
121
+
122
+ # 遍历图的所有节点并将其添加到elements["nodes"]
123
+ for node in G.nodes(data=True):
124
+ # 假设你想为节点提供一个简单的 "label" 和 "name"
125
+ # 可以根据需求自定义数据结构
126
+ elements["nodes"].append(
127
+ {
128
+ "data": {
129
+ "id": str(node[0]),
130
+ "label": node[1].get("label", f"Node"), # 可根据实际属性设置
131
+ "name": node[1].get("name", f"Node{node[0]}"),
132
+ }
133
+ }
134
+ )
135
+
136
+ # 遍历图的所有边并将其添加到elements["edges"]
137
+ edge_set = set()
138
+ for i, edge in enumerate(G.edges(data=True)):
139
+ elements["edges"].append(
140
+ {
141
+ "data": {
142
+ "id": str(i + len(G.nodes)), # 确保edge的id与node的id不冲突
143
+ "label": edge[2].get("label", f"RED"),
144
+ "source": str(edge[0]),
145
+ "target": str(edge[1]),
146
+ }
147
+ }
148
+ )
149
+ edge_set.add((str(edge[0]), str(edge[1])))
150
+
151
+ if show_blue:
152
+ for i in range(len(G.nodes)):
153
+ for j in range(i+1, len(G.nodes)):
154
+ if (str(i), str(j)) in edge_set:
155
+ continue
156
+ elements["edges"].append(
157
+ {
158
+ "data": {
159
+ "id": str(len(G.nodes) + len(G.edges) + i*(len(G.nodes)) + j), # 确保edge的id与node的id不冲突
160
+ "label": "BLUE",
161
+ "source": str(i),
162
+ "target": str(j),
163
+ }
164
+ }
165
+ )
166
+
167
+
168
+ return elements
169
+
170
+
171
+ def convert_data_to_elements(G, show_blue):
172
+ elements = {"nodes": [], "edges": []}
173
+
174
+ for node in range(G['num_nodes']):
175
+ elements["nodes"].append(
176
+ {
177
+ "data": {
178
+ "id": str(node),
179
+ "label": "Node",
180
+ "name": f"Node {node}",
181
+ }
182
+ }
183
+ )
184
+
185
+ edge_set = set()
186
+ for i, (source, target) in enumerate(G["edges"]):
187
+ elements["edges"].append(
188
+ {
189
+ "data": {
190
+ "id": str(G['num_nodes'] + i),
191
+ "label": "RED",
192
+ "source": str(source),
193
+ "target": str(target),
194
+ }
195
+ }
196
+ )
197
+ edge_set.add((str(source), str(target)))
198
+
199
+ if show_blue:
200
+ for i in range(G['num_nodes']):
201
+ for j in range(i+1, G['num_nodes']):
202
+ if (str(i), str(j)) in edge_set:
203
+ continue
204
+ elements["edges"].append(
205
+ {
206
+ "data": {
207
+ "id": str(G['num_nodes'] + len(G["edges"]) + i*(G['num_nodes']) + j), # 确保edge的id与node的id不冲突
208
+ "label": "BLUE",
209
+ "source": str(i),
210
+ "target": str(j),
211
+ }
212
+ }
213
+ )
214
+ return elements
215
+
216
+ with st.sidebar:
217
+ st.title("Ramsey Graph")
218
+ # graphset_name = st.selectbox("R(s,t)_n", os.listdir("data"), index=0)
219
+ # dataset = load_dataset("linxy/RamseyGraph", graphset_name, trust_remote_code=True)
220
+ # graphs = load_graphs(graphset_name)
221
+ graphset_name = st.selectbox("R(s,t)_n", _FILENAMES, index=0)
222
+ graphs = load_graphs_from_dataset(graphset_name)
223
+ G = st.radio("G", graphs, index=0, format_func=lambda x: f"Graph with {x['num_nodes']} nodes and {len(x['edges'])} edges")
224
+
225
+ st.markdown("## Visualization Settings")
226
+ layout = st.selectbox("Layout Name", LAYOUT_NAMES, index=4)
227
+ show_blue = st.checkbox("Show Blue Edges", True)
228
+
229
+ st.markdown("## Graph Data")
230
+ elements = convert_data_to_elements(G, show_blue)
231
+ st.json(elements)
232
+ # print(json.dumps(elements, ensure_ascii=False, indent=2))
233
+
234
+ st.markdown("# Ramsey Graph")
235
+ s, t = graphset_name[1], graphset_name[2]
236
+ st.markdown(f"the graph does not contain a clique of size {s} (RED) or an independent set of size {t} (BLUE)")
237
+ st_link_analysis(elements, layout, node_styles, edge_styles)