eaglelandsonce commited on
Commit
37fcf28
1 Parent(s): db27fce

Update pages/17_Graphs2.py

Browse files
Files changed (1) hide show
  1. pages/17_Graphs2.py +8 -2
pages/17_Graphs2.py CHANGED
@@ -37,9 +37,10 @@ def create_sample_graph():
37
  num_nodes = 10
38
  num_edges = 15
39
 
 
40
  graph = nx.gnm_random_graph(num_nodes, num_edges, directed=True)
41
 
42
- # Create synthetic features
43
  years_published = np.random.randint(1990, 2022, size=num_nodes).astype(np.float32)
44
  num_authors = np.random.randint(1, 10, size=num_nodes).astype(np.float32)
45
  citation_weights = np.random.uniform(0.1, 5.0, size=num_edges).astype(np.float32)
@@ -48,6 +49,10 @@ def create_sample_graph():
48
  node_features = np.stack([years_published, num_authors], axis=-1)
49
  edge_features = citation_weights.reshape(-1, 1)
50
 
 
 
 
 
51
  graph_tensor = tfgnn.GraphTensor.from_pieces(
52
  node_sets={
53
  "papers": tfgnn.NodeSet.from_fields(
@@ -88,7 +93,8 @@ def main():
88
  st.subheader("Sample Graph Visualization")
89
  fig, ax = plt.subplots(figsize=(10, 8))
90
  pos = nx.spring_layout(nx_graph)
91
- nx.draw(nx_graph, pos, with_labels=True, node_color='lightblue',
 
92
  node_size=500, arrowsize=20, ax=ax)
93
  st.pyplot(fig)
94
 
 
37
  num_nodes = 10
38
  num_edges = 15
39
 
40
+ # Create a random graph
41
  graph = nx.gnm_random_graph(num_nodes, num_edges, directed=True)
42
 
43
+ # Generate synthetic features
44
  years_published = np.random.randint(1990, 2022, size=num_nodes).astype(np.float32)
45
  num_authors = np.random.randint(1, 10, size=num_nodes).astype(np.float32)
46
  citation_weights = np.random.uniform(0.1, 5.0, size=num_edges).astype(np.float32)
 
49
  node_features = np.stack([years_published, num_authors], axis=-1)
50
  edge_features = citation_weights.reshape(-1, 1)
51
 
52
+ # Assign random titles to nodes
53
+ paper_titles = [f"Paper {i+1}" for i in range(num_nodes)]
54
+ nx.set_node_attributes(graph, {i: {'title': title} for i, title in enumerate(paper_titles)})
55
+
56
  graph_tensor = tfgnn.GraphTensor.from_pieces(
57
  node_sets={
58
  "papers": tfgnn.NodeSet.from_fields(
 
93
  st.subheader("Sample Graph Visualization")
94
  fig, ax = plt.subplots(figsize=(10, 8))
95
  pos = nx.spring_layout(nx_graph)
96
+ labels = nx.get_node_attributes(nx_graph, 'title')
97
+ nx.draw(nx_graph, pos, labels=labels, with_labels=True, node_color='lightblue',
98
  node_size=500, arrowsize=20, ax=ax)
99
  st.pyplot(fig)
100