Spaces:
Sleeping
Sleeping
Commit
·
6bf314a
1
Parent(s):
8aa9b0c
Implement network visualization with Django models and save as HTML
Browse files- visualize_network.py +137 -0
visualize_network.py
ADDED
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import networkx as nx
|
3 |
+
import django
|
4 |
+
import os
|
5 |
+
|
6 |
+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "BridgeMentor.settings")
|
7 |
+
django.setup()
|
8 |
+
|
9 |
+
from core.models import Author, Institution, Concept, AuthorConcept, Affiliation, Work, Topic, Subfield, Field, Domain, AuthorTopic # noqa: E402
|
10 |
+
from pyvis.network import Network # noqa: E402
|
11 |
+
|
12 |
+
G = nx.Graph()
|
13 |
+
|
14 |
+
# Add authors
|
15 |
+
for a in Author.objects.all()[:100]:
|
16 |
+
G.add_node(a.id, label=a.name, group="Author")
|
17 |
+
|
18 |
+
# Add institutions
|
19 |
+
for i in Institution.objects.all()[:100]:
|
20 |
+
G.add_node(i.id, label=i.name, group="Institution")
|
21 |
+
|
22 |
+
# Add affiliations
|
23 |
+
for af in Affiliation.objects.select_related('author', 'institution')[:100]:
|
24 |
+
G.add_edge(af.author.id, af.institution.id, title="affiliated_with")
|
25 |
+
|
26 |
+
# Add concepts
|
27 |
+
for c in Concept.objects.all()[:100]:
|
28 |
+
G.add_node(c.id, label=c.name, group="Concept")
|
29 |
+
|
30 |
+
# AuthorConcept edges
|
31 |
+
for ac in AuthorConcept.objects.select_related('author', 'concept')[:100]:
|
32 |
+
G.add_edge(ac.author.id, ac.concept.id, title="interested_in")
|
33 |
+
|
34 |
+
# Works and their authors
|
35 |
+
for w in Work.objects.select_related('author')[:100]:
|
36 |
+
G.add_node(w.id, label=w.title, group="Work")
|
37 |
+
G.add_edge(w.author.id, w.id, title="authored")
|
38 |
+
|
39 |
+
# Topics
|
40 |
+
for t in Topic.objects.select_related('subfield')[:100]:
|
41 |
+
G.add_node(t.id, label=t.name, group="Topic")
|
42 |
+
G.add_edge(t.id, t.subfield.id, title="belongs_to")
|
43 |
+
|
44 |
+
# AuthorTopics
|
45 |
+
for at in AuthorTopic.objects.select_related('author', 'topic')[:100]:
|
46 |
+
G.add_edge(at.author.id, at.topic.id, title="researches")
|
47 |
+
|
48 |
+
# Subfields
|
49 |
+
for sf in Subfield.objects.select_related('field')[:100]:
|
50 |
+
G.add_node(sf.id, label=sf.name, group="Subfield")
|
51 |
+
G.add_edge(sf.id, sf.field.id, title="part_of")
|
52 |
+
|
53 |
+
# Fields
|
54 |
+
for f in Field.objects.select_related('domain')[:100]:
|
55 |
+
G.add_node(f.id, label=f.name, group="Field")
|
56 |
+
G.add_edge(f.id, f.domain.id, title="within")
|
57 |
+
|
58 |
+
# Domains
|
59 |
+
for d in Domain.objects.all()[:100]:
|
60 |
+
G.add_node(d.id, label=d.name, group="Domain")
|
61 |
+
|
62 |
+
|
63 |
+
# Generate minimal graph
|
64 |
+
net = Network(
|
65 |
+
height="800px",
|
66 |
+
width="100%",
|
67 |
+
bgcolor="#ffffff",
|
68 |
+
font_color="black",
|
69 |
+
heading="Norway Computer Science Research Graph", # Remove heading
|
70 |
+
# directed=False, # Simpler view
|
71 |
+
# select_menu=True, # Enables dropdown to highlight node + neighbors
|
72 |
+
# filter_menu=True # Enables advanced filtering by attributes
|
73 |
+
)
|
74 |
+
|
75 |
+
# Reduce interactivity and noise
|
76 |
+
net.barnes_hut(gravity=-20000, central_gravity=0.3)
|
77 |
+
net.set_options("""
|
78 |
+
{
|
79 |
+
"nodes": {
|
80 |
+
"shape": "dot",
|
81 |
+
"size": 10,
|
82 |
+
"font": {
|
83 |
+
"size": 12,
|
84 |
+
"face": "Arial"
|
85 |
+
}
|
86 |
+
},
|
87 |
+
"edges": {
|
88 |
+
"color": {
|
89 |
+
"inherit": true
|
90 |
+
},
|
91 |
+
"smooth": false
|
92 |
+
},
|
93 |
+
"layout": {
|
94 |
+
"improvedLayout": false
|
95 |
+
},
|
96 |
+
"interaction": {
|
97 |
+
"hover": true,
|
98 |
+
"tooltipDelay": 200,
|
99 |
+
"hideEdgesOnDrag": true,
|
100 |
+
"navigationButtons": true,
|
101 |
+
"keyboard": {
|
102 |
+
"enabled": true,
|
103 |
+
"speed": {
|
104 |
+
"x": 10,
|
105 |
+
"y": 10,
|
106 |
+
"zoom": 0.02
|
107 |
+
},
|
108 |
+
"bindToWindow": true
|
109 |
+
}
|
110 |
+
},
|
111 |
+
"physics": {
|
112 |
+
"enabled": true,
|
113 |
+
"barnesHut": {
|
114 |
+
"gravitationalConstant": -20000,
|
115 |
+
"centralGravity": 0.3,
|
116 |
+
"springLength": 95
|
117 |
+
}
|
118 |
+
},
|
119 |
+
"manipulation": {
|
120 |
+
"enabled": false
|
121 |
+
},
|
122 |
+
"configure": {
|
123 |
+
"enabled": false,
|
124 |
+
"filter": ["nodes", "edges", "physics"],
|
125 |
+
"showButton": false
|
126 |
+
}
|
127 |
+
}
|
128 |
+
""")
|
129 |
+
|
130 |
+
net.from_nx(G)
|
131 |
+
html_content = net.generate_html()
|
132 |
+
|
133 |
+
|
134 |
+
# Save the modified HTML
|
135 |
+
html_path = "test.html"
|
136 |
+
with open(html_path, "w", encoding="utf-8") as f:
|
137 |
+
f.write(html_content)
|