1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
#!/usr/bin/env python2.7
# ----------------------------------
"""
Module pylib_graphs.py
Code and datatypes for generating gnuplot plots. Much of the data
collection and conversion is somewhat specialized for the DP project,
but most of the code can be resused or extended easily.
Copyright 2004 Stephan Schulz, schulz@eprover.org
This code is part of the support structure for the equational
heorem prover E. Visit
http://www.eprover.org
for more information.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program ; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston,
MA 02111-1307 USA
The original copyright holder can be contacted as
Stephan Schulz (I4)
Technische Universitaet Muenchen
Institut fuer Informatik
Boltzmannstrasse 3
Garching bei Muenchen
Germany
or via email (address above).
"""
import re
import sys
import os
import pylib_io
import pylib_basics
import pylib_eprots
import pylib_psfixbb
split_res = re.compile("_");
filter_re = re.compile(".*");
name_break = re.compile("[, ]+");
class titled_object(object):
"""
An object with a title that can be converted into a file name
for writing a version of it.
Yes, I'm going wild on inheritance ;-)
"""
def __init__(self, title = None):
self.set_title(title)
def get_title(self):
return self.title;
def set_title(self, title):
self.title = title
def filename(self, appendix):
if self.title:
tmp = re.sub(name_break, "_", self.title);
else:
tmp = "Unnamed"
return tmp+appendix
class graph:
"""
Represents a single graph (data+title). This is just a simple
container.
"""
def __init__(self, title="", data=None):
self.title=title;
self.style = None
if data:
self.data=data;
else:
self.data = [];
def get_title(self):
return self.title
def set_style(self, style):
self.style = style
def get_style(self):
if self.style:
return str(self.style)
return ""
def add_problem_data(self, median_list):
for i in median_list:
xval = int(split_res.split(i[0])[-1]);
yval = i[1];
self.data.append( (xval, yval) );
self.data.sort();
def parse(self, file, pattern=re.compile(".*")):
p = pylib_eprots.eprotocol(file, True);
f = p.filter(pattern);
tmp = f.collect_medians()
self.add_problem_data(tmp);
def __repr__(self):
res = "# "+self.title + "\n";
for i in self.data:
xval = i[0];
yval = i[1];
res = res+ ("%4d %7.4f\n" % (xval, yval));
return res;
style_dict = {}
class plot(titled_object):
"""
Represents a plot: Labels+Format+Graphs.
"""
def __init__(self, title = "Unnamed plot", graph_descs = []):
self.graphs = []
self.set_title(title)
for i in graph_descs:
g = graph(i[0])
g.parse(i[1], i[2])
if g.get_title() in style_dict:
g.set_style(style_dict[g.get_title()])
else:
print "Warning, unknown graph title "+g.get_title()
self.add_graph(g)
self.title = title
self.xlabel = "'Instance size'";
self.ylabel = "'Run time (s)'";
self.generic_options = """
set style data linespoints
set key left
set pointsize 2
set autoscale fix
"""
self.file_options ="""
set terminal postscript eps 16
set size square
"""
def add_graph(self, graph):
self.graphs.append(graph)
def set_labels(xlabel = None, ylabel = None):
self.xlabel = xlabel
self.ylabel = ylabel
def gnuplot_labels(self):
"""
Return a string containing options to pass to gnuplot.
"""
xl=""
yl=""
if self.xlabel:
xl = "set xlabel "+self.xlabel+"\n"
if self.ylabel:
yl = "set ylabel "+self.ylabel+"\n"
return xl+yl
def settings(self, file=None, log=False):
"""
return a sequence of 'set' commands for gnuplot.
"""
res = self.generic_options+self.gnuplot_labels()
if file:
res = res+self.file_options
res = res+"set output \""+file+"\"\n";
if log:
res = res+"set logscale y\n";
return res
def plot_command(self):
"""
Return a plot command that will plot all graphs, properly
labeled.
"""
sep = "";
res = "plot "
for i in self.graphs:
res = res+ sep+"'-' title \""+i.title+\
"\" with linespoints "+i.get_style();
sep = ", "
res = res+"\n";
for i in self.graphs:
res = res+i.__repr__()+"e\n";
#print res
return res;
def full_filename(self, log=False):
"""
Return a filename suitable for an eps file of this graph.
Generated from the title.
"""
tmp = ".eps"
if log:
tmp = "_log.eps"
return self.filename(tmp);
def gnuplot(self, file=False, log=False):
"""
Call gnuplot and do the plotting.
"""
filename = None;
if file:
filename = self.full_filename(log)
pipe=os.popen("gnuplot", "w")
pipe.write(self.settings(filename, log))
pipe.write(self.plot_command())
pipe.flush()
if not file:
print " Return to continue!"
sys.stdin.readline()
pipe.close() # Has to come first to make gnuplot actually
# and reliably write the file!
if file:
pylib_psfixbb.fixbb(filename)
class statistic_sample(titled_object):
"""
A sample of (numeric) data and methods of displaying and analyzing it.
"""
def __init__(self, title=None, data=[]):
self.set_title(title)
self.data = data[:]
self.statistics = False
self.file_options ="""
set terminal postscript eps 16 color
set size square
"""
def parse_from_eprot(self, file, pattern=re.compile(".*")):
"""
Parse an E protocol and collect the times to extend the
sample.
"""
p = pylib_eprots.eprotocol(file, True);
f = p.filter(pattern);
self.data.extend(f.collect_times())
self.statistics = False
def compute_statistics(self):
"""
Compute max, min, average, median, standard deviation and
number of elements.
"""
if not self.statistics:
self.data_sorted = self.data[:]
self.data_sorted.sort()
self._min = self.data_sorted[0]
self._max = self.data_sorted[-1]
self._median = self.data_sorted[int(len(self.data)/2)]
self._mean = pylib_basics.mean(self.data)
self._sd = pylib_basics.standard_deviation(self.data)
self.statistics = True
def minimum(self):
self.compute_statistics()
return self._min
def maximum(self):
self.compute_statistics()
return self._max
def median(self):
self.compute_statistics()
return self._median
def mean(self):
self.compute_statistics()
return self._mean
def standard_deviation(self):
self.compute_statistics()
return self._sd
def compute_distribution(self, delta):
delta = float(delta)
entries = int(self.maximum()/delta)+2
indices = [int(i/delta) for i in self.data]
indices.sort()
print "Values:", entries, indices[-1]
dist = [0] * entries
for i in indices:
dist[i] = dist[i]+1
self._dist = [(i*delta+delta/2, dist[i]) for i in xrange(len(dist))]
return self._dist
def gnuplot_settings(self, file):
res = """
set xlabel 'Run time (s)'
set ylabel 'Number of instances'
set style fill
set key left
set size square
set xrange [0:3.4]
"""
if file:
res = res+self.file_options
res = res+"set output \""+file+"\"\n";
return res;
def plot_command(self):
res = """
plot '-' title \"Distribution of run times\" with boxes
"""
data = [str(i[0])+" "+str(i[1])+"\n" for i in self._dist]
res = res+"".join(data)+"e\n"
return res
def gnuplot(self, file=False):
"""
Call gnuplot and do the plotting.
"""
filename = None;
if file:
filename = self.filename(".eps")
pipe=os.popen("gnuplot", "w")
#pipe=sys.stdout
pipe.write(self.gnuplot_settings(filename))
pipe.write(self.plot_command())
pipe.flush()
if not file:
print " Return to continue!"
sys.stdin.readline()
pipe.close() # Has to come first to make gnuplot actually
# and reliably write the file!
if file:
pylib_psfixbb.fixbb(filename)
|