gmedin commited on
Commit
122c116
·
verified ·
1 Parent(s): 2aa8945

Bug fixes (duplicate edges, preserving start node)

Browse files
Files changed (1) hide show
  1. app.py +30 -31
app.py CHANGED
@@ -58,21 +58,19 @@ def dynamic_visualize_graph(graph, start_node, layers=3, top_k=5):
58
  var options = {
59
  "physics": {
60
  "barnesHut": {
61
- "gravitationalConstant": -20000,
62
- "centralGravity": 0.6
63
- }
64
  }
65
- }
66
- """)
67
-
68
-
69
 
70
  visited_nodes = set()
 
71
  current_nodes = [int(start_node)] # Convert start_node to int
 
72
  # Add the starting node, color it red, and include a tooltip
73
  start_title = graph.nodes[int(start_node)].get('title', 'No title available') # Get the title attribute
74
- if isinstance(start_title, float) and math.isnan(start_title): # Check if the title is NaN
75
- start_title = "No title available"
76
  net.add_node(int(start_node), label=str(start_node), color="red", title=start_title)
77
  visited_nodes.add(int(start_node))
78
 
@@ -87,14 +85,12 @@ def dynamic_visualize_graph(graph, start_node, layers=3, top_k=5):
87
 
88
  for neighbor, weight in neighbors:
89
  if neighbor not in visited_nodes:
90
- neighbor_title = graph.nodes[neighbor].get('title', 'No title available')
91
- if isinstance(neighbor_title, float) and math.isnan(neighbor_title): # Check if the title is NaN
92
- neighbor_title = "No title available"
93
- if not isinstance(neighbor_title, str):
94
- neighbor_title = str(neighbor_title) # Ensure it's converted to a string
95
- net.add_node(neighbor, label=str(neighbor), title=neighbor_title)
96
-
97
- net.add_edge(node, neighbor, label=f"w:{weight}") # Add the edge regardless of visited status
98
  visited_nodes.add(neighbor)
99
  next_nodes.append(neighbor) # Always add to next_nodes for further expansion
100
 
@@ -104,6 +100,7 @@ def dynamic_visualize_graph(graph, start_node, layers=3, top_k=5):
104
  html_content = net.generate_html()
105
  st.components.v1.html(html_content, height=600, scrolling=False)
106
 
 
107
  def display_node_info(graph, node_id):
108
  """
109
  Display all attributes of a node and its edges in the graph.
@@ -141,21 +138,17 @@ st.title("Interactive Graph Expansion with Tooltips")
141
  # Brand Selection
142
  selected_brand = st.selectbox("Select a brand:", options=list(BRAND_GRAPHS.keys()))
143
 
144
- # Load the graph for the selected brand
145
- G = load_graph(selected_brand)
146
-
147
- # Filter the graph for popular nodes and edges
148
- node_degree_threshold = 1 # Minimum degree for nodes
149
- edge_weight_threshold = 1 # Minimum weight for edges
150
- G_filtered = filter_graph(G, node_threshold=node_degree_threshold, edge_threshold=edge_weight_threshold)
151
- #print('spot check degree', G.degree(389062))
152
- #print('spot check degree', G_filtered.degree(389062))
153
- #display_node_info(G_filtered, 389062)
154
  import random
155
- # Randomly sample a starting node
156
- # Initialize session state for the starting node
157
- #if "start_node" not in st.session_state:
158
- st.session_state.start_node = random.choice(list(G_filtered.nodes)) # Randomly select a node once
 
 
 
 
 
 
159
 
160
  # Input: Starting node
161
  start_node = st.number_input(
@@ -163,6 +156,12 @@ start_node = st.number_input(
163
  value=st.session_state.start_node,
164
  step=1
165
  )
 
 
 
 
 
 
166
  layers = st.slider("Depth to explore:", 1, 6, value=3)
167
  top_k = st.slider("Branching factor (per node):", 1, 6, value=3)
168
 
 
58
  var options = {
59
  "physics": {
60
  "barnesHut": {
61
+ "gravitationalConstant": -15000,
62
+ "centralGravity": 0.8
63
+ }
64
  }
65
+ }
66
+ """)
 
 
67
 
68
  visited_nodes = set()
69
+ added_edges = set() # Track edges to avoid duplicates
70
  current_nodes = [int(start_node)] # Convert start_node to int
71
+
72
  # Add the starting node, color it red, and include a tooltip
73
  start_title = graph.nodes[int(start_node)].get('title', 'No title available') # Get the title attribute
 
 
74
  net.add_node(int(start_node), label=str(start_node), color="red", title=start_title)
75
  visited_nodes.add(int(start_node))
76
 
 
85
 
86
  for neighbor, weight in neighbors:
87
  if neighbor not in visited_nodes:
88
+ neighbor_title = graph.nodes[neighbor].get('title', 'No title available') # Get tooltip
89
+ net.add_node(neighbor, label=str(neighbor), title=neighbor_title) # Add node with tooltip
90
+ edge = (node, neighbor) # Represent the edge as a tuple
91
+ if edge not in added_edges:
92
+ net.add_edge(node, neighbor, label=f"w:{weight}") # Add the edge only if not already added
93
+ added_edges.add(edge) # Track this edge as added
 
 
94
  visited_nodes.add(neighbor)
95
  next_nodes.append(neighbor) # Always add to next_nodes for further expansion
96
 
 
100
  html_content = net.generate_html()
101
  st.components.v1.html(html_content, height=600, scrolling=False)
102
 
103
+
104
  def display_node_info(graph, node_id):
105
  """
106
  Display all attributes of a node and its edges in the graph.
 
138
  # Brand Selection
139
  selected_brand = st.selectbox("Select a brand:", options=list(BRAND_GRAPHS.keys()))
140
 
 
 
 
 
 
 
 
 
 
 
141
  import random
142
+
143
+ # Check if the brand has changed
144
+ if "selected_brand" not in st.session_state or st.session_state.selected_brand != selected_brand:
145
+ # Load the new graph and reset the start node
146
+ st.session_state.selected_brand = selected_brand
147
+ G = load_graph(selected_brand)
148
+ st.session_state.start_node = random.choice(list(G.nodes))
149
+ else:
150
+ # Use the existing graph
151
+ G = load_graph(selected_brand)
152
 
153
  # Input: Starting node
154
  start_node = st.number_input(
 
156
  value=st.session_state.start_node,
157
  step=1
158
  )
159
+
160
+ # Filter the graph for popular nodes and edges
161
+ node_degree_threshold = 1 # Minimum degree for nodes
162
+ edge_weight_threshold = 1 # Minimum weight for edges
163
+ G_filtered = filter_graph(G, node_threshold=node_degree_threshold, edge_threshold=edge_weight_threshold)
164
+
165
  layers = st.slider("Depth to explore:", 1, 6, value=3)
166
  top_k = st.slider("Branching factor (per node):", 1, 6, value=3)
167