Search code examples
data-visualizationnetworkxpyvisdgl

Cannot visualize the Cora dataset using PyVis


I get an AssertionError when I try to visualize the Cora dataset using PyVis. The Cora graph is loaded from the Deep Graph Library and converted into a simple NetworkX Graph before passing to PyVis. Below is a MWE. Thank you for any help :)

import dgl
from pyvis.network import Network
import networkx as nx

dataset = dgl.data.CoraGraphDataset()

g = Network(height=800, width=800, notebook=True)

netxG = nx.Graph(dataset[0].to_networkx())

g.from_nx(netxG)
g.show('ex.html')

Solution

  • The AssertionError seems to occur due to a labelling issue with the nodes in the graph:

    /usr/local/lib/python3.7/dist-packages/pyvis/network.py in add_node(self, n_id, label, shape, **options)
        204         :type y: num (optional)
        205         """
    --> 206         assert isinstance(n_id, str) or isinstance(n_id, int)
        207         if label:
        208             node_label = label
    
    AssertionError: 
    

    Relabeling the graph with the networkx function nx.relabel solved the issue for me.

    See full code below:

    import dgl
    from pyvis.network import Network
    import networkx as nx
    
    dataset = dgl.data.CoraGraphDataset()
    
    g = Network(height=800, width=800, notebook=True)
    
    netxG = nx.Graph(dataset[0].to_networkx())
    
    mapping = {i:i for i in range(netxG.size())} #Setting mapping for the relabeling
    netxH = nx.relabel_nodes(netxG,mapping) #relabeling nodes
    
    g.from_nx(netxH)
    g.show('ex.html')
    

    And the pyvis output gives:

    enter image description here

    Fair warning, it's a big graph (2708 nodes and 10556 edges) so the pyvis visualization takes a while to load in the browser.