Spaces:
Sleeping
Sleeping
Delete network_utils.py
Browse files- network_utils.py +0 -67
network_utils.py
DELETED
@@ -1,67 +0,0 @@
|
|
1 |
-
import networkx as nx
|
2 |
-
import pandas as pd
|
3 |
-
import matplotlib.pyplot as plt
|
4 |
-
from edgebundling import run_and_plot_bundling
|
5 |
-
from matplotlib.colors import Normalize
|
6 |
-
|
7 |
-
def create_citation_graph(df):
|
8 |
-
# Create a directed graph
|
9 |
-
G = nx.DiGraph()
|
10 |
-
|
11 |
-
# Add nodes (papers) to the graph with their positions
|
12 |
-
pos = {} # Dictionary to store positions
|
13 |
-
for idx, row in df.iterrows():
|
14 |
-
G.add_node(
|
15 |
-
row['id'],
|
16 |
-
X=row['x'],
|
17 |
-
Y=row['y'],
|
18 |
-
publication_year=row['publication_year'],
|
19 |
-
color=row['color']
|
20 |
-
)
|
21 |
-
pos[row['id']] = (row['x'], row['y'])
|
22 |
-
|
23 |
-
# Add edges based on references
|
24 |
-
for idx, row in df.iterrows():
|
25 |
-
source_id = row['id']
|
26 |
-
refs = row['referenced_works']
|
27 |
-
if isinstance(refs, list):
|
28 |
-
references = refs
|
29 |
-
elif isinstance(refs, str):
|
30 |
-
references = refs.split(', ')
|
31 |
-
else:
|
32 |
-
references = []
|
33 |
-
for ref in references:
|
34 |
-
if ref in df['id'].values:
|
35 |
-
G.add_edge(source_id, ref)
|
36 |
-
|
37 |
-
G = G.to_undirected()
|
38 |
-
return G
|
39 |
-
|
40 |
-
def draw_citation_graph(G, bundle_edges=False, path=None, min_max_coordinates=None, node_colors=None):
|
41 |
-
pos = {}
|
42 |
-
for node in G.nodes():
|
43 |
-
pos[node] = (G.nodes[node]['X'], G.nodes[node]['Y'])
|
44 |
-
fig, ax = plt.subplots(figsize=(20, 20))
|
45 |
-
plt.margins(0, 0) # Remove margins
|
46 |
-
if bundle_edges:
|
47 |
-
# Turning color into rgb
|
48 |
-
node_colors = {node: tuple(int(G.nodes[node]['color'].lstrip('#')[i:i+2], 16)/255 for i in (0, 2, 4)) + (1.0,) for node in G.nodes()}
|
49 |
-
|
50 |
-
for u, v in G.edges():
|
51 |
-
x1, y1 = G.nodes[u]['X'], G.nodes[u]['Y']
|
52 |
-
x2, y2 = G.nodes[v]['X'], G.nodes[v]['Y']
|
53 |
-
G[u][v]['dist'] = ((x1 - x2)**2 + (y1 - y2)**2)**0.5
|
54 |
-
|
55 |
-
run_and_plot_bundling(G, method="hammer", ax=ax, edge_gradient=True,
|
56 |
-
node_colors=node_colors, linewidths=.8, alpha=.5)
|
57 |
-
else:
|
58 |
-
nx.draw(G, pos=pos, node_size=0, with_labels=False, edge_color='#f98e31', alpha=0.3)
|
59 |
-
|
60 |
-
plt.axis('off')
|
61 |
-
plt.gca().set_aspect('equal')
|
62 |
-
if min_max_coordinates is not None:
|
63 |
-
plt.xlim(min_max_coordinates[0], min_max_coordinates[1])
|
64 |
-
plt.ylim(min_max_coordinates[2], min_max_coordinates[3])
|
65 |
-
|
66 |
-
if path is not None:
|
67 |
-
plt.savefig(path, bbox_inches='tight', pad_inches=0, dpi=800, transparent=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|