| 1 |
""" |
| 2 |
altgraph.Dot - Interface to the dot language |
| 3 |
============================================ |
| 4 |
|
| 5 |
The :py:mod:`~altgraph.Dot` module provides a simple interface to the |
| 6 |
file format used in the |
| 7 |
`graphviz <http://www.research.att.com/sw/tools/graphviz/>`_ |
| 8 |
program. The module is intended to offload the most tedious part of the process |
| 9 |
(the **dot** file generation) while transparently exposing most of its |
| 10 |
features. |
| 11 |
|
| 12 |
To display the graphs or to generate image files the |
| 13 |
`graphviz <http://www.research.att.com/sw/tools/graphviz/>`_ |
| 14 |
package needs to be installed on the system, moreover the :command:`dot` and |
| 15 |
:command:`dotty` programs must be accesible in the program path so that they |
| 16 |
can be ran from processes spawned within the module. |
| 17 |
|
| 18 |
Example usage |
| 19 |
------------- |
| 20 |
|
| 21 |
Here is a typical usage:: |
| 22 |
|
| 23 |
from altgraph import Graph, Dot |
| 24 |
|
| 25 |
# create a graph |
| 26 |
edges = [ (1,2), (1,3), (3,4), (3,5), (4,5), (5,4) ] |
| 27 |
graph = Graph.Graph(edges) |
| 28 |
|
| 29 |
# create a dot representation of the graph |
| 30 |
dot = Dot.Dot(graph) |
| 31 |
|
| 32 |
# display the graph |
| 33 |
dot.display() |
| 34 |
|
| 35 |
# save the dot representation into the mydot.dot file |
| 36 |
dot.save_dot(file_name='mydot.dot') |
| 37 |
|
| 38 |
# save dot file as gif image into the graph.gif file |
| 39 |
dot.save_img(file_name='graph', file_type='gif') |
| 40 |
|
| 41 |
Directed graph and non-directed graph |
| 42 |
------------------------------------- |
| 43 |
|
| 44 |
Dot class can use for both directed graph and non-directed graph |
| 45 |
by passing ``graphtype`` parameter. |
| 46 |
|
| 47 |
Example:: |
| 48 |
|
| 49 |
# create directed graph(default) |
| 50 |
dot = Dot.Dot(graph, graphtype="digraph") |
| 51 |
|
| 52 |
# create non-directed graph |
| 53 |
dot = Dot.Dot(graph, graphtype="graph") |
| 54 |
|
| 55 |
Customizing the output |
| 56 |
---------------------- |
| 57 |
|
| 58 |
The graph drawing process may be customized by passing |
| 59 |
valid :command:`dot` parameters for the nodes and edges. For a list of all |
| 60 |
parameters see the `graphviz <http://www.research.att.com/sw/tools/graphviz/>`_ |
| 61 |
documentation. |
| 62 |
|
| 63 |
Example:: |
| 64 |
|
| 65 |
# customizing the way the overall graph is drawn |
| 66 |
dot.style(size='10,10', rankdir='RL', page='5, 5' , ranksep=0.75) |
| 67 |
|
| 68 |
# customizing node drawing |
| 69 |
dot.node_style(1, label='BASE_NODE',shape='box', color='blue' ) |
| 70 |
dot.node_style(2, style='filled', fillcolor='red') |
| 71 |
|
| 72 |
# customizing edge drawing |
| 73 |
dot.edge_style(1, 2, style='dotted') |
| 74 |
dot.edge_style(3, 5, arrowhead='dot', label='binds', labelangle='90') |
| 75 |
dot.edge_style(4, 5, arrowsize=2, style='bold') |
| 76 |
|
| 77 |
|
| 78 |
.. note:: |
| 79 |
|
| 80 |
dotty (invoked via :py:func:`~altgraph.Dot.display`) may not be able to |
| 81 |
display all graphics styles. To verify the output save it to an image file |
| 82 |
and look at it that way. |
| 83 |
|
| 84 |
Valid attributes |
| 85 |
---------------- |
| 86 |
|
| 87 |
- dot styles, passed via the :py:meth:`Dot.style` method:: |
| 88 |
|
| 89 |
rankdir = 'LR' (draws the graph horizontally, left to right) |
| 90 |
ranksep = number (rank separation in inches) |
| 91 |
|
| 92 |
- node attributes, passed via the :py:meth:`Dot.node_style` method:: |
| 93 |
|
| 94 |
style = 'filled' | 'invisible' | 'diagonals' | 'rounded' |
| 95 |
shape = 'box' | 'ellipse' | 'circle' | 'point' | 'triangle' |
| 96 |
|
| 97 |
- edge attributes, passed via the :py:meth:`Dot.edge_style` method:: |
| 98 |
|
| 99 |
style = 'dashed' | 'dotted' | 'solid' | 'invis' | 'bold' |
| 100 |
arrowhead = 'box' | 'crow' | 'diamond' | 'dot' | 'inv' | 'none' |
| 101 |
| 'tee' | 'vee' |
| 102 |
weight = number (the larger the number the closer the nodes will be) |
| 103 |
|
| 104 |
- valid `graphviz colors |
| 105 |
<http://www.research.att.com/~erg/graphviz/info/colors.html>`_ |
| 106 |
|
| 107 |
- for more details on how to control the graph drawing process see the |
| 108 |
`graphviz reference |
| 109 |
<http://www.research.att.com/sw/tools/graphviz/refs.html>`_. |
| 110 |
""" |
| 111 |
import os |
| 112 |
import warnings |
| 113 |
|
| 114 |
from altgraph import GraphError |
| 115 |
|
| 116 |
|
| 117 |
class Dot(object): |
| 118 |
""" |
| 119 |
A class providing a **graphviz** (dot language) representation |
| 120 |
allowing a fine grained control over how the graph is being |
| 121 |
displayed. |
| 122 |
|
| 123 |
If the :command:`dot` and :command:`dotty` programs are not in the current |
| 124 |
system path their location needs to be specified in the contructor. |
| 125 |
""" |
| 126 |
|
| 127 |
def __init__( |
| 128 |
self, |
| 129 |
graph=None, |
| 130 |
nodes=None, |
| 131 |
edgefn=None, |
| 132 |
nodevisitor=None, |
| 133 |
edgevisitor=None, |
| 134 |
name="G", |
| 135 |
dot="dot", |
| 136 |
dotty="dotty", |
| 137 |
neato="neato", |
| 138 |
graphtype="digraph", |
| 139 |
): |
| 140 |
""" |
| 141 |
Initialization. |
| 142 |
""" |
| 143 |
self.name, self.attr = name, {} |
| 144 |
|
| 145 |
assert graphtype in ["graph", "digraph"] |
| 146 |
self.type = graphtype |
| 147 |
|
| 148 |
self.temp_dot = "tmp_dot.dot" |
| 149 |
self.temp_neo = "tmp_neo.dot" |
| 150 |
|
| 151 |
self.dot, self.dotty, self.neato = dot, dotty, neato |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
self.nodes, self.edges = {}, {} |
| 156 |
|
| 157 |
if graph is not None and nodes is None: |
| 158 |
nodes = graph |
| 159 |
if graph is not None and edgefn is None: |
| 160 |
|
| 161 |
def edgefn(node, graph=graph): |
| 162 |
return graph.out_nbrs(node) |
| 163 |
|
| 164 |
if nodes is None: |
| 165 |
nodes = () |
| 166 |
|
| 167 |
seen = set() |
| 168 |
for node in nodes: |
| 169 |
if nodevisitor is None: |
| 170 |
style = {} |
| 171 |
else: |
| 172 |
style = nodevisitor(node) |
| 173 |
if style is not None: |
| 174 |
self.nodes[node] = {} |
| 175 |
self.node_style(node, **style) |
| 176 |
seen.add(node) |
| 177 |
if edgefn is not None: |
| 178 |
for head in seen: |
| 179 |
for tail in (n for n in edgefn(head) if n in seen): |
| 180 |
if edgevisitor is None: |
| 181 |
edgestyle = {} |
| 182 |
else: |
| 183 |
edgestyle = edgevisitor(head, tail) |
| 184 |
if edgestyle is not None: |
| 185 |
if head not in self.edges: |
| 186 |
self.edges[head] = {} |
| 187 |
self.edges[head][tail] = {} |
| 188 |
self.edge_style(head, tail, **edgestyle) |
| 189 |
|
| 190 |
def style(self, **attr): |
| 191 |
""" |
| 192 |
Changes the overall style |
| 193 |
""" |
| 194 |
self.attr = attr |
| 195 |
|
| 196 |
def display(self, mode="dot"): |
| 197 |
""" |
| 198 |
Displays the current graph via dotty |
| 199 |
""" |
| 200 |
|
| 201 |
if mode == "neato": |
| 202 |
self.save_dot(self.temp_neo) |
| 203 |
neato_cmd = "%s -o %s %s" % (self.neato, self.temp_dot, self.temp_neo) |
| 204 |
os.system(neato_cmd) |
| 205 |
else: |
| 206 |
self.save_dot(self.temp_dot) |
| 207 |
|
| 208 |
plot_cmd = "%s %s" % (self.dotty, self.temp_dot) |
| 209 |
os.system(plot_cmd) |
| 210 |
|
| 211 |
def node_style(self, node, **kwargs): |
| 212 |
""" |
| 213 |
Modifies a node style to the dot representation. |
| 214 |
""" |
| 215 |
if node not in self.edges: |
| 216 |
self.edges[node] = {} |
| 217 |
self.nodes[node] = kwargs |
| 218 |
|
| 219 |
def all_node_style(self, **kwargs): |
| 220 |
""" |
| 221 |
Modifies all node styles |
| 222 |
""" |
| 223 |
for node in self.nodes: |
| 224 |
self.node_style(node, **kwargs) |
| 225 |
|
| 226 |
def edge_style(self, head, tail, **kwargs): |
| 227 |
""" |
| 228 |
Modifies an edge style to the dot representation. |
| 229 |
""" |
| 230 |
if tail not in self.nodes: |
| 231 |
raise GraphError("invalid node %s" % (tail,)) |
| 232 |
|
| 233 |
try: |
| 234 |
if tail not in self.edges[head]: |
| 235 |
self.edges[head][tail] = {} |
| 236 |
self.edges[head][tail] = kwargs |
| 237 |
except KeyError: |
| 238 |
raise GraphError("invalid edge %s -> %s " % (head, tail)) |
| 239 |
|
| 240 |
def iterdot(self): |
| 241 |
|
| 242 |
if self.type == "digraph": |
| 243 |
yield "digraph %s {\n" % (self.name,) |
| 244 |
elif self.type == "graph": |
| 245 |
yield "graph %s {\n" % (self.name,) |
| 246 |
|
| 247 |
else: |
| 248 |
raise GraphError("unsupported graphtype %s" % (self.type,)) |
| 249 |
|
| 250 |
|
| 251 |
for attr_name, attr_value in sorted(self.attr.items()): |
| 252 |
yield '%s="%s";' % (attr_name, attr_value) |
| 253 |
yield "\n" |
| 254 |
|
| 255 |
|
| 256 |
cpatt = '%s="%s",' |
| 257 |
epatt = "];\n" |
| 258 |
|
| 259 |
|
| 260 |
for node_name, node_attr in sorted(self.nodes.items()): |
| 261 |
yield '\t"%s" [' % (node_name,) |
| 262 |
for attr_name, attr_value in sorted(node_attr.items()): |
| 263 |
yield cpatt % (attr_name, attr_value) |
| 264 |
yield epatt |
| 265 |
|
| 266 |
|
| 267 |
for head in sorted(self.edges): |
| 268 |
for tail in sorted(self.edges[head]): |
| 269 |
if self.type == "digraph": |
| 270 |
yield '\t"%s" -> "%s" [' % (head, tail) |
| 271 |
else: |
| 272 |
yield '\t"%s" -- "%s" [' % (head, tail) |
| 273 |
for attr_name, attr_value in sorted(self.edges[head][tail].items()): |
| 274 |
yield cpatt % (attr_name, attr_value) |
| 275 |
yield epatt |
| 276 |
|
| 277 |
|
| 278 |
yield "}\n" |
| 279 |
|
| 280 |
def __iter__(self): |
| 281 |
return self.iterdot() |
| 282 |
|
| 283 |
def save_dot(self, file_name=None): |
| 284 |
""" |
| 285 |
Saves the current graph representation into a file |
| 286 |
""" |
| 287 |
|
| 288 |
if not file_name: |
| 289 |
warnings.warn(DeprecationWarning, "always pass a file_name", stacklevel=2) |
| 290 |
file_name = self.temp_dot |
| 291 |
|
| 292 |
with open(file_name, "w") as fp: |
| 293 |
for chunk in self.iterdot(): |
| 294 |
fp.write(chunk) |
| 295 |
|
| 296 |
def save_img(self, file_name=None, file_type="gif", mode="dot"): |
| 297 |
""" |
| 298 |
Saves the dot file as an image file |
| 299 |
""" |
| 300 |
|
| 301 |
if not file_name: |
| 302 |
warnings.warn(DeprecationWarning, "always pass a file_name", stacklevel=2) |
| 303 |
file_name = "out" |
| 304 |
|
| 305 |
if mode == "neato": |
| 306 |
self.save_dot(self.temp_neo) |
| 307 |
neato_cmd = "%s -o %s %s" % (self.neato, self.temp_dot, self.temp_neo) |
| 308 |
os.system(neato_cmd) |
| 309 |
plot_cmd = self.dot |
| 310 |
else: |
| 311 |
self.save_dot(self.temp_dot) |
| 312 |
plot_cmd = self.dot |
| 313 |
|
| 314 |
file_name = "%s.%s" % (file_name, file_type) |
| 315 |
create_cmd = "%s -T%s %s -o %s" % ( |
| 316 |
plot_cmd, |
| 317 |
file_type, |
| 318 |
self.temp_dot, |
| 319 |
file_name, |
| 320 |
) |
| 321 |
os.system(create_cmd) |