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>
26 msg = "Networkx library is not installed, you will not be able to plot."
27 logger = Logger("Plotter")
31 import matplotlib.pyplot as plt
33 msg = ("Matplotlib library is not installed, you will not be able "
34 "generate PNG plots.")
35 logger = Logger("Plotter")
42 class ECPlotter(object):
43 def plot(self, ec, dirpath = None, format= PFormats.FIGURE,
45 graph, labels = self._ec2graph(ec)
51 dirpath = tempfile.mkdtemp()
53 fpath = os.path.join(dirpath, "%s_%s" % (ec.exp_id, ec.run_id))
55 if format == PFormats.FIGURE:
56 pos = networkx.graphviz_layout(graph, prog="neato")
57 networkx.draw(graph, pos = pos, node_color="white",
58 node_size = 500, with_labels=True)
60 label = "\n".join(map(lambda v: "%s: %s" % (v[0], v[1]), labels.iteritems()))
61 plt.annotate(label, xy=(0.05, 0.95), xycoords='axes fraction')
65 plt.savefig(fpath, bbox_inches="tight")
70 elif format == PFormats.DOT:
73 networkx.write_dot(graph, fpath)
77 subprocess.call(["dot", "-Tps", fpath, "-o", "%s.ps" % fpath])
78 subprocess.call(["evince","%s.ps" % fpath])
82 def _ec2graph(self, ec):
83 graph = networkx.Graph(graph = dict(overlap = "false"))
88 for guid, rm in ec._resources.iteritems():
89 label = rm.get_rtype()
92 label = "%d %s" % (guid, label),
93 width = 50/72.0, # 1 inch = 72 points
99 for guid2 in rm.connections:
100 # Avoid adding a same connection twice
101 if (guid2, guid) not in connections:
102 connections.add((guid, guid2))
104 for (guid1, guid2) in connections:
105 graph.add_edge(guid1, guid2)