2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2013 INRIA
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
27 class ECPlotter(object):
28 def plot(self, ec, fpath = None, format= PFormats.FIGURE, persist = False):
29 graph, labels = self._ec2graph(ec)
33 if persist and not fpath:
35 dirpath = tempfile.mkdtemp()
36 fpath = os.path.join(dirpath, "%s_%s" % (ec.exp_id, ec.run_id))
39 if format == PFormats.FIGURE:
40 import matplotlib.pyplot as plt
41 pos = networkx.graphviz_layout(graph, prog="neato")
42 networkx.draw(graph, pos = pos, node_color="white",
43 node_size = 500, with_labels=True)
45 label = "\n".join(map(lambda v: "%s: %s" % (v[0], v[1]), labels.iteritems()))
46 plt.annotate(label, xy=(0.0, 0.95), xycoords='axes fraction')
52 plt.savefig(fpath, bbox_inches="tight")
56 elif format == PFormats.DOT:
61 networkx.write_dot(graph, fpath)
64 subprocess.call(["dot", "-Tps", fpath, "-o", "%s.ps" % fpath])
65 subprocess.call(["evince","%s.ps" % fpath])
69 def _ec2graph(self, ec):
70 graph = networkx.Graph(graph = dict(overlap = "false"))
75 for guid, rm in ec._resources.iteritems():
76 label = rm.get_rtype()
79 label = "%d %s" % (guid, label),
80 width = 50/72.0, # 1 inch = 72 points
86 for guid2 in rm.connections:
87 # Avoid adding a same connection twice
88 if (guid2, guid) not in connections:
89 connections.add((guid, guid2))
91 for (guid1, guid2) in connections:
92 graph.add_edge(guid1, guid2)