Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pydot: is it possible to plot two different nodes with the same string in them?

I'm using pydot in order to draw graphs in python. I'd like to represent a decision tree, say something like (a1,a2,a3 are attributes and two classes are 0 and 1:

       a1>3
      /    \
  a2>10    a3>-7
   /  \     /  \
  1    0   1    0

However, using pydot, only two leaves are created and the tree looks like this (png attached):

       a1>3
      /    \
  a2>10    a3>-7
      |  X  |
      1     0

Now, in this simple case the logic is fine but in larger trees it is messy internal nodes belonging to different branches are unified.

The simple code I'm using is:

import pydot
graph = pydot.Dot(graph_type='graph')
edge = pydot.Edge("a_1>3", "a_2>10")
graph.add_edge(edge)
edge = pydot.Edge("a_1>3", "a_3>-7")
graph.add_edge(edge)
edge = pydot.Edge("a_2>10", "1")
graph.add_edge(edge)
edge = pydot.Edge("a_2>10", "0")
graph.add_edge(edge)
edge = pydot.Edge("a_3>-7", "1")
graph.add_edge(edge)
edge = pydot.Edge("a_3>-7", "0")
graph.add_edge(edge)
graph.write_png('simpleTree.png')

I also tried creating different node objects than create the edges and than add it to the graph but it seems that pydot checks the node pool for nodes with the same name instead of creating a new one.

Any ideas? thanks!

the image created by the code above

like image 685
ScienceFriction Avatar asked Oct 22 '12 13:10

ScienceFriction


2 Answers

Your nodes always need a unique names, otherwise you cannot name them uniquely to attach edges between them. However, you can give each node a label, which is what is displayed when rendered.

So you'll need to add nodes with unique ids:

graph = pydot.Dot(graph_type='graph')
graph.add_node(pydot.Node('literal_0_0', label='0'))
graph.add_node(pydot.Node('literal_0_1', label='0'))
graph.add_node(pydot.Node('literal_1_0', label='1'))
graph.add_node(pydot.Node('literal_1_1', label='1'))

then add graph edges connecting those nodes:

edge = pydot.Edge("a_2>10", "literal_0_0")
graph.add_edge(edge)
edge = pydot.Edge("a_2>10", "literal_1_0")
graph.add_edge(edge)
edge = pydot.Edge("a_3>-7", "literal_0_1")
graph.add_edge(edge)
edge = pydot.Edge("a_3>-7", "literal_1_1")
graph.add_edge(edge)

Together with the rest of the edges you defined this makes:

graph with correct edges

like image 61
Martijn Pieters Avatar answered Sep 19 '22 17:09

Martijn Pieters


The "canonical" answer is to use the uuid module from the standard library, as networkx does here.

This is better than using id to create node names for pydot that correspond to the nodes in your original graph, because if (in theory) a node object gets deleted while you are building your pydot graph, then that id won't necessarily be unique. In contrast, the UUID objects created are unique, persistent and independent of the lifespan of the original nodes.

However for this to happen, something very weird must be going on while you create the pydot graph, which is rather unlikely. The advantage of using id is that you don't need to build and pass around a mapping from original nodes to UUID objects (so that you construct consistently the edges after adding the nodes).

One interesting case are nested graphs: two different graphs may contain the same hashable object in networkx (say a), then id cannot be used any more directly on the node. But in that case, id can still be used, by combining the (node, graph) pair as: str(id(node)) + str(id(graph)).

like image 42
Ioannis Filippidis Avatar answered Sep 19 '22 17:09

Ioannis Filippidis