display.py revision 2665:a124942bacb8
1# Copyright (c) 2003-2004 The Regents of The University of Michigan
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met: redistributions of source code must retain the above copyright
7# notice, this list of conditions and the following disclaimer;
8# redistributions in binary form must reproduce the above copyright
9# notice, this list of conditions and the following disclaimer in the
10# documentation and/or other materials provided with the distribution;
11# neither the name of the copyright holders nor the names of its
12# contributors may be used to endorse or promote products derived from
13# this software without specific prior written permission.
14#
15# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26#
27# Authors: Nathan Binkert
28
29class Value:
30    def __init__(self, value, precision, percent = False):
31        self.value = float(value)
32        self.precision = precision
33        self.percent = percent
34    def __str__(self):
35        if isinstance(self.value, str):
36            if self.value.lower() == 'nan':
37                value = 'NaN'
38            if self.value.lower() == 'inf':
39                value = 'Inf'
40        else:
41            if self.precision >= 0:
42                format = "%%.%df" % self.precision
43            elif self.value == 0.0:
44                format = "%.0f"
45            elif self.value % 1.0 == 0.0:
46                format = "%.0f"
47            else:
48                format = "%f"
49            value = self.value
50            if self.percent:
51                value = value * 100.0
52            value = format % value
53
54        if self.percent:
55            value = value + "%"
56
57        return value
58
59class Print:
60    def __init__(self, **vals):
61        self.__dict__.update(vals)
62
63    def __str__(self):
64        value = Value(self.value, self.precision)
65        pdf = ''
66        cdf = ''
67        if self.__dict__.has_key('pdf'):
68            pdf = Value(self.pdf, 2, True)
69        if self.__dict__.has_key('cdf'):
70            cdf = Value(self.cdf, 2, True)
71
72        output = "%-40s %12s %8s %8s" % (self.name, value, pdf, cdf)
73
74        if descriptions and self.__dict__.has_key('desc') and self.desc:
75            output = "%s # %s" % (output, self.desc)
76
77        return output
78
79    def doprint(self):
80        if display_all:
81            return True
82        if self.value == 0.0 and (self.flags & flags_nozero):
83            return False
84        if isinstance(self.value, str):
85            if self.value == 'NaN' and (self.flags & flags_nonan):
86                return False
87        return True
88
89    def display(self):
90        if self.doprint():
91            print self
92
93class VectorDisplay:
94    def display(self):
95        if not self.value:
96            return
97
98        p = Print()
99        p.flags = self.flags
100        p.precision = self.precision
101
102        if not isinstance(self.value, (list, tuple)):
103            p.name = self.name
104            p.desc = self.desc
105            p.value = self.value
106            p.display()
107            return
108
109        mytotal = reduce(lambda x,y: float(x) + float(y), self.value)
110        mycdf = 0.0
111
112        value = self.value
113
114        if display_all:
115            subnames = [ '[%d]' % i for i in range(len(value)) ]
116        else:
117            subnames = [''] * len(value)
118
119        if self.__dict__.has_key('subnames'):
120            for i,each in enumerate(self.subnames):
121                if len(each) > 0:
122                    subnames[i] = '.%s' % each
123
124        subdescs = [self.desc]*len(value)
125        if self.__dict__.has_key('subdescs'):
126            for i in xrange(min(len(value), len(self.subdescs))):
127                subdescs[i] = self.subdescs[i]
128
129        for val,sname,sdesc in map(None, value, subnames, subdescs):
130            if mytotal > 0.0:
131                mypdf = float(val) / float(mytotal)
132                mycdf += mypdf
133                if (self.flags & flags_pdf):
134                    p.pdf = mypdf
135                    p.cdf = mycdf
136
137            if len(sname) == 0:
138                continue
139
140            p.name = self.name + sname
141            p.desc = sdesc
142            p.value = val
143            p.display()
144
145        if (self.flags & flags_total):
146            if (p.__dict__.has_key('pdf')): del p.__dict__['pdf']
147            if (p.__dict__.has_key('cdf')): del p.__dict__['cdf']
148            p.name = self.name + '.total'
149            p.desc = self.desc
150            p.value = mytotal
151            p.display()
152