1 """\
2 Pydot visualization plugin for FuzzPy.
3
4 @author: Xavier Spriet
5 @contact: linkadmin@gmail.com
6 @license: LGPL-3
7 """
8
9 from ..fgraph import FuzzyGraph
10 from ..graph import Graph
11 from abc_plugin import AbstractPlugin
12
13 VIS_PLUGIN = 'FuzzPyDot'
14 VIS_TYPES = [FuzzyGraph, Graph]
15 VIS_FORMATS = ['bmp', 'canon', 'dot', 'xdot', 'eps', 'fig', 'gd', 'gd2',
16 'gif', 'gtk', 'ico', 'imap', 'cmapx', 'jpg', 'jpeg', 'jpe', 'pdf',
17 'plain', 'plain-ext', 'png', 'ps', 'ps2', 'svg', 'svgz', 'tif', 'tiff',
18 'vml', 'vmlz', 'vrml', 'wbmp', 'xlib']
22 """\
23 Pydot visualization plugin for fuzzpy
24
25 This plugin converts vertices and edges to pydot elements then create
26 a rendering based on the 'format' attribute (default=png).
27 """
28 - def __init__(self, obj=None, *args, **kwargs):
29 """\
30 Plugin constructor.
31
32 Maps the specified graph object locally and marshalls method
33 keywords to local arguments for the PyDot object.
34
35 @param obj: The graph or fuzzy graph to visualize.
36 @type obj: L{Graph} or L{FuzzyGraph}
37 """
38 import pydot
39 self.pydot = pydot
40 self._G = obj
41
42 if ('name' in kwargs.keys()):
43 self.name = kwargs['name']
44 else:
45 name = 'FuzzPy Graph'
46
47 @staticmethod
49 """\
50 Return whether this plugin is supported.
51
52 @return: True if the plugin can run in this environment.
53 @rtype: C{bool}
54 """
55 try:
56 import pydot
57 except ImportError:
58 warning.warn(("PyDot plugin will not run on this system. "
59 "You must install the PyDot Python module first."))
60 return False
61 return True
62
64 """\
65 Converts vertices to PyDot nodes.
66
67 Adds GraphViz attributes to each vertex and converts it into a
68 pydot.Node object.
69
70 @return: List of pydot.Node objects.
71 @rtype: C{list}
72 """
73 vertices = []
74 for vertex in self._G.vertices():
75 if isinstance(self._G, FuzzyGraph):
76 node = self.pydot.Node(
77 name="g_%s" % str(vertex),
78 label=str(vertex),
79 weight=str(self._G.mu(vertex)),
80 penwidth="%.2f" % self._G.mu(vertex)
81 )
82 elif isinstance(self._G, Graph):
83 node = self.pydot.Node(
84 name="g_%s" % str(vertex),
85 label=str(vertex),
86 )
87 vertices.append(node)
88 return vertices
89
91 """\
92 Converts edges to PyDot edges.
93
94 Adds GraphViz attributes to edges and registers them all as PyDot
95 edge objects.
96
97 @returns: List of pydot.Edge objects.
98 @rtype: C{list}
99 """
100 edges = []
101 for edge in self._G.edges():
102 if isinstance(self._G, FuzzyGraph):
103 connector = self.pydot.Edge(
104 src="g_%s" % str(edge.head),
105 dst="g_%s" % str(edge.tail),
106 weight=str(self._G.mu(edge.tail, edge.head)),
107 penwidth=str((self._G.mu(edge.tail, edge.head) \
108 + 0.05) * 2.0),
109
110 style="setlinewidth(%f)" % ((self._G.mu(edge.tail, \
111 edge.head) + 0.05) * 2.0)
112 )
113 elif isinstance(self._G, Graph):
114 connector = self.pydot.Edge(
115 "g_%s" % str(edge.head),
116 "g_%s" % str(edge.tail),
117 )
118 edges.append(connector)
119 return edges
120
122 """\
123 PyDot visualization method.
124
125 Converts all vertices and edges from the graph provided to the
126 plugin's constructor into a PyDot graph and converts any apppropriate
127 attributes to be passed to GraphViz.
128
129 @return: Output data string.
130 @rtype: C{str}
131 """
132
133 if kwargs.has_key('format') and kwargs['format'] in VIS_FORMATS:
134 output_format = kwargs['format']
135 else:
136 output_format = 'png'
137
138 if self._G.directed == True:
139 gtype = 'digraph'
140 else:
141 gtype = 'graph'
142
143 D = self.pydot.Dot(rankdir='RT', graph_type=gtype)
144
145
146 for vertex in self.marshall_vertices():
147 D.add_node(vertex)
148
149 for edge in self.marshall_edges():
150 D.add_edge(edge)
151
152
153 return (output_format, D.create(format=output_format))
154
155 AbstractPlugin.register(FuzzPyDot)
156