0

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')
Des
  • 93
  • 1
  • 6

1 Answers1

1

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.

jylls
  • 4,395
  • 2
  • 10
  • 21
  • I tried it, it works, and in just 2 lines ! I must say, I was trying to avoid a solution that iteratively goes through all the nodes (for performance reasons on large graphs). That is why I suggested [this solution](https://github.com/WestHealth/pyvis/pull/155) to the PyVis developers. – Des Jun 26 '22 at 19:01
  • Speaking of large graphs, this one is quite large (as you spotted). @jylls, do you know how to quickly slice the graph so as to only visualize, say, the first 100 nodes ? – Des Jun 26 '22 at 19:04
  • 1
    You could use the `subgraph` function from networkx to do that (doc [here](https://networkx.org/documentation/stable/reference/classes/generated/networkx.Graph.subgraph.html)). The code would then look like `netxI=netxH.subgraph([i for i in range(100)])` – jylls Jun 26 '22 at 20:23