summaryrefslogtreecommitdiff
blob: 986e6410348dec88f45ead51cbb7526c66cad599 (plain)
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
#=====================================================
# Copyright (C) 2012 Andrea Arteaga <andyspiros@gmail.com>
#=====================================================
#
# 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.
#
from os.path import join as pjoin, basename
import sys, numpy as np
from shutil import copy as fcopy, copytree, rmtree

import benchconfig as cfg
from utils import benchutils as bu
from htmlreport import HTMLreport
from testdescr import testdescr
from benchprint import Print


class Plotter:
    def __init__(self, conf):
        # Set plot function
        self.conf = conf
        if not conf.has_key('type'):
            self.plotf = plt.plot
        elif type(conf['type']) == type(''):
            try:
                self.plotf = plt.__dict__[conf['type']]
            except:
                print >> sys.stderr, \
                  'Plot function "', conf['type'], '" not found. Using plot'
                self.plotf = plt.plot
                return
        else:
            self.plot = conf['type']
        
        # Labels
        self.xlabel = conf.has_key('xlabel') and conf['xlabel'] or ''
        self.ylabel = conf.has_key('ylabel') and conf['ylabel'] or ''
        
        # Initialize markers
        markers = ('-', '--', 'o', 'v', '^', 's', 'p', 'h', '*', '+', 'x', 'D')
        colors = ('k', 'r', 'g', 'b')
        self.linestyles = tuple([c+m for m in markers for c in colors])
        self.curstyle = 0
        
        # Open figure
        plt.figure(figsize=(12,9), dpi=300)
    
    
    def addPlot(self, x, y, label):
        style = self.linestyles[self.curstyle]
        self.curstyle = (self.curstyle+1) % len(self.linestyles)
        self.plotf(x, y, style, label=label, hold=True)
    
    def savePlot(self, fname):
        plt.legend(loc='best')
        plt.xlabel(self.xlabel)
        plt.ylabel(self.ylabel)
        plt.grid(True)
        plt.savefig(fname, format='png', bbox_inches='tight', transparent=True)
        


def saveReport():
    # Check whether pyplot is working
    try:
        plt.figure()
    except:
        Print("Unable to generate plots")
        Print("Please make sure that X is running and $DISPLAY is set")
        return

    # Regenerate report in any case
    rmtree(cfg.reportdir, ignore_errors=True)

    # Open HTML file
    bu.mkdir(cfg.reportdir)
    bu.mkdir(pjoin(cfg.reportdir, 'images'))
    htmlfname = pjoin(cfg.reportdir, 'index.html')
    html = HTMLreport(htmlfname)

    for operation in cfg.mod.getTests():
        
        # Begin plot
        p = Plotter(cfg.mod.reportConf())
        
        for tid,test in cfg.tests.items():
            if test.has_key('implementations'):
                for impl in test['implementations']:
                    
                    implres = test['results'][impl]
                    if implres and implres.has_key(operation):
                        resultsFile = implres[operation]
                        x,y = np.loadtxt(resultsFile, unpack=True)
                        p.addPlot(x, y, tid+'/'+impl)

        imgpath = pjoin('images', operation+'.png')
        fname = pjoin(cfg.reportdir, imgpath)
        p.savePlot(fname)
        html.addFig(testdescr[operation], image=imgpath)
        
    # Copy logs and input file
    copytree(cfg.logdir, pjoin(cfg.reportdir, 'log'))
    fcopy(cfg.inputfile, pjoin(cfg.reportdir, basename(cfg.inputfile)));

    # Close HTML file
    html.close()
    Print('HTML report generated: ' + htmlfname)


# Initialize module
try:
    if not locals().has_key('initialized'):
        initialized = True
        import matplotlib
        matplotlib.use('Agg')
        import matplotlib.pyplot as plt
        import numpy as np
        with_images = True
except ImportError:
    sys.stderr.write('Error: matplotlib and numpy are needed' + \
      'in order to generate the reports!\n')
    sys.stderr.write('Continue anyway.\n\n')
    with_images = False