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