Search code examples
pythongraphnlpnetworkx

Networkx decoding to str TypeError


I am trying to create a graph representation from some text, and keep getting a TypeError. Here is an example code:

document="Let us assume, as a running example, that my data is composed of word embeddings of the\
 English language. I want to gain insights about the word distribution in the embedding space,\
 specifically, if there are any clusters of very similar words, if there are words that are\
 completely different from the rest, if there are words very similar to every other word, and so \
on. And I want to gain these insights visually, so that it is easier to understand and share with my collegues."

document = preprocess_document(document)
nodes = get_entities(document)
edges= get_relations(document)

G= nx.Graph()
G.add_nodes_from(nodes)
G.add_edges_from(edges)

plt.figure(figsize=(10,10))
pos = nx.nx_agraph.graphviz_layout(G)

nx.draw(G, pos=pos, with_labels=True)
edge_labels = nx.get_edge_attributes(G, 'weight')
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels)
plt.title(title)
plt.show()

I get the error:

----> 1 plot_graph(G)

<ipython-input-174-287c83e8e288> in plot_graph(G, title)
      4 
      5     # define position of nodes in figure
----> 6     pos = nx.nx_agraph.graphviz_layout(G)
      7 
      8     # draw nodes and edges

~/anaconda3/envs/vineeth/lib/python3.6/site-packages/networkx/drawing/nx_agraph.py in graphviz_layout(G, prog, root, args)
    235     This is a wrapper for pygraphviz_layout.
    236     """
--> 237     return pygraphviz_layout(G, prog=prog, root=root, args=args)
    238 
    239 

~/anaconda3/envs/vineeth/lib/python3.6/site-packages/networkx/drawing/nx_agraph.py in pygraphviz_layout(G, prog, root, args)
    282     if root is not None:
    283         args += f"-Groot={root}"
--> 284     A = to_agraph(G)
    285     A.layout(prog=prog, args=args)
    286     node_pos = {}

~/anaconda3/envs/vineeth/lib/python3.6/site-packages/networkx/drawing/nx_agraph.py in to_agraph(N)
    146     # add nodes
    147     for n, nodedata in N.nodes(data=True):
--> 148         A.add_node(n)
    149         # Add node data
    150         a = A.get_node(n)

~/anaconda3/envs/vineeth/lib/python3.6/site-packages/pygraphviz/agraph.py in add_node(self, n, **attr)
    311         except KeyError:
    312             nh = gv.agnode(self.handle, n, _Action.create)
--> 313         node = Node(self, nh=nh)
    314         node.attr.update(**attr)
    315 

~/anaconda3/envs/vineeth/lib/python3.6/site-packages/pygraphviz/agraph.py in __new__(self, graph, name, nh)
   1562     def __new__(self, graph, name=None, nh=None):
   1563         if nh is not None:
-> 1564             n = super(Node, self).__new__(self, gv.agnameof(nh), graph.encoding)
   1565         else:
   1566             n = super(Node, self).__new__(self, name)

TypeError: decoding to str: need a bytes-like object, NoneType found

The error seems to be in the nx.nx_agraph.graphviz_layout(G) part of the code. I have tried changing the document, but the same error keeps popping up. I have manually verified that none of the nodes or edges have a NaN or are empty.

Can someone please help resolve this?


Solution

  • To make it easier for future readers I'll post this partial answer here (it doesn't really solve why your Graphviz layout doesn't work.) But your code works by change the layout like for example:

    pos = nx.spring_layout(G)