Spaces:
Sleeping
Sleeping
| 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) | |