display.py revision 1758
1892SN/A
21762SN/A# Copyright (c) 2003-2004 The Regents of The University of Michigan
3892SN/A# All rights reserved.
4892SN/A#
5892SN/A# Redistribution and use in source and binary forms, with or without
6892SN/A# modification, are permitted provided that the following conditions are
7892SN/A# met: redistributions of source code must retain the above copyright
8892SN/A# notice, this list of conditions and the following disclaimer;
9892SN/A# redistributions in binary form must reproduce the above copyright
10892SN/A# notice, this list of conditions and the following disclaimer in the
11892SN/A# documentation and/or other materials provided with the distribution;
12892SN/A# neither the name of the copyright holders nor the names of its
13892SN/A# contributors may be used to endorse or promote products derived from
14892SN/A# this software without specific prior written permission.
15892SN/A#
16892SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17892SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18892SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19892SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20892SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21892SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22892SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23892SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24892SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25892SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26892SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu
282665Ssaidi@eecs.umich.edu#Permission is granted to use, copy, create derivative works and
29892SN/A#redistribute this software and such derivative works for any purpose,
30802SN/A#so long as the copyright notice above, this grant of permission, and
311722SN/A#the disclaimer below appear in all copies made; and so long as the
32802SN/A#name of The University of Michigan is not used in any advertising or
33802SN/A#publicity pertaining to the use or distribution of this software
34802SN/A#without specific, written prior authorization.
35802SN/A#
36802SN/A#THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION FROM THE
37802SN/A#UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY PURPOSE, AND WITHOUT
38802SN/A#WARRANTY BY THE UNIVERSITY OF MICHIGAN OF ANY KIND, EITHER EXPRESS OR
39802SN/A#IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF
40802SN/A#MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE REGENTS OF
411310SN/A#THE UNIVERSITY OF MICHIGAN SHALL NOT BE LIABLE FOR ANY DAMAGES,
422542SN/A#INCLUDING DIRECT, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
434762Snate@binkert.org#DAMAGES, WITH RESPECT TO ANY CLAIM ARISING OUT OF OR IN CONNECTION
44802SN/A#WITH THE USE OF THE SOFTWARE, EVEN IF IT HAS BEEN OR IS HEREAFTER
45802SN/A#ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
46802SN/A
472107SN/Aclass Value:
48802SN/A    def __init__(self, value, precision, percent = False):
492539SN/A        self.value = value
504762Snate@binkert.org        self.precision = precision
51802SN/A        self.percent = percent
523846Shsul@eecs.umich.edu    def __str__(self):
53802SN/A        if isinstance(self.value, str):
54802SN/A            if self.value.lower() == 'nan':
55909SN/A                value = 'NaN'
563349Sbinkertn@umich.edu            if self.value.lower() == 'inf':
57909SN/A                value = 'Inf'
582539SN/A        else:
593918Ssaidi@eecs.umich.edu            if self.precision >= 0:
602539SN/A                format = "%%.%df" % self.precision
612539SN/A            elif self.value == 0.0:
622539SN/A                format = "%.0f"
633349Sbinkertn@umich.edu            elif self.value % 1.0 == 0.0:
642539SN/A                format = "%.0f"
652539SN/A            else:
663918Ssaidi@eecs.umich.edu                format = "%f"
67909SN/A            value = self.value
68802SN/A            if self.percent:
694762Snate@binkert.org                value = value * 100.0
704762Snate@binkert.org            value = format % value
71802SN/A
724762Snate@binkert.org        if self.percent:
73802SN/A            value = value + "%"
74
75        return value
76
77class Print:
78    def __init__(self, **vals):
79        self.__dict__.update(vals)
80
81    def __str__(self):
82        value = Value(self.value, self.precision)
83        pdf = ''
84        cdf = ''
85        if self.__dict__.has_key('pdf'):
86            pdf = Value(self.pdf, 2, True)
87        if self.__dict__.has_key('cdf'):
88            cdf = Value(self.cdf, 2, True)
89
90        output = "%-40s %12s %8s %8s" % (self.name, value, pdf, cdf)
91
92        if descriptions and self.__dict__.has_key('desc') and self.desc:
93            output = "%s # %s" % (output, self.desc)
94
95        return output
96
97    def doprint(self):
98        if display_all:
99            return True
100        if self.value == 0.0 and (self.flags & flags_nozero):
101            return False
102        if isinstance(self.value, str):
103            if self.value == 'NaN' and (self.flags & flags_nonan):
104                return False
105        return True
106
107    def display(self):
108        if self.doprint():
109            print self
110
111class VectorDisplay:
112    def display(self):
113        p = Print()
114        p.flags = self.flags
115        p.precision = self.precision
116
117        if isinstance(self.value, (list, tuple)):
118            if not len(self.value):
119                return
120
121            mytotal = reduce(lambda x,y: float(x) + float(y), self.value)
122            mycdf = 0.0
123
124            value = self.value
125
126            if display_all:
127                subnames = [ '[%d]' % i for i in range(len(value)) ]
128            else:
129                subnames = [''] * len(value)
130
131            if self.__dict__.has_key('subnames'):
132                for i,each in enumerate(self.subnames):
133                    if len(each) > 0:
134                        subnames[i] = '.%s' % each
135
136            subdescs = [self.desc]*len(value)
137            if self.__dict__.has_key('subdescs'):
138                for i in xrange(min(len(value), len(self.subdescs))):
139                    subdescs[i] = self.subdescs[i]
140
141            for val,sname,sdesc in map(None, value, subnames, subdescs):
142                if mytotal > 0.0:
143                    mypdf = float(val) / float(mytotal)
144                    mycdf += mypdf
145                    if (self.flags & flags_pdf):
146                        p.pdf = mypdf
147                        p.cdf = mycdf
148
149                if len(sname) == 0:
150                    continue
151
152                p.name = self.name + sname
153                p.desc = sdesc
154                p.value = val
155                p.display()
156
157            if (self.flags & flags_total):
158                if (p.__dict__.has_key('pdf')): del p.__dict__['pdf']
159                if (p.__dict__.has_key('cdf')): del p.__dict__['cdf']
160                p.name = self.name + '.total'
161                p.desc = self.desc
162                p.value = mytotal
163                p.display()
164
165        else:
166            p.name = self.name
167            p.desc = self.desc
168            p.value = self.value
169            p.display()
170
171