display.py revision 1758
11758Ssaidi@eecs.umich.edu
21758Ssaidi@eecs.umich.edu# Copyright (c) 2003-2004 The Regents of The University of Michigan
31758Ssaidi@eecs.umich.edu# All rights reserved.
41758Ssaidi@eecs.umich.edu#
51758Ssaidi@eecs.umich.edu# Redistribution and use in source and binary forms, with or without
61758Ssaidi@eecs.umich.edu# modification, are permitted provided that the following conditions are
71758Ssaidi@eecs.umich.edu# met: redistributions of source code must retain the above copyright
81758Ssaidi@eecs.umich.edu# notice, this list of conditions and the following disclaimer;
91758Ssaidi@eecs.umich.edu# redistributions in binary form must reproduce the above copyright
101758Ssaidi@eecs.umich.edu# notice, this list of conditions and the following disclaimer in the
111758Ssaidi@eecs.umich.edu# documentation and/or other materials provided with the distribution;
121758Ssaidi@eecs.umich.edu# neither the name of the copyright holders nor the names of its
131758Ssaidi@eecs.umich.edu# contributors may be used to endorse or promote products derived from
141758Ssaidi@eecs.umich.edu# this software without specific prior written permission.
151758Ssaidi@eecs.umich.edu#
161758Ssaidi@eecs.umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
171758Ssaidi@eecs.umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
181758Ssaidi@eecs.umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
191758Ssaidi@eecs.umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
201758Ssaidi@eecs.umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
211758Ssaidi@eecs.umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
221758Ssaidi@eecs.umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
231758Ssaidi@eecs.umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
241758Ssaidi@eecs.umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
251758Ssaidi@eecs.umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
261758Ssaidi@eecs.umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
271758Ssaidi@eecs.umich.edu
281758Ssaidi@eecs.umich.edu#Permission is granted to use, copy, create derivative works and
291758Ssaidi@eecs.umich.edu#redistribute this software and such derivative works for any purpose,
301758Ssaidi@eecs.umich.edu#so long as the copyright notice above, this grant of permission, and
311758Ssaidi@eecs.umich.edu#the disclaimer below appear in all copies made; and so long as the
321758Ssaidi@eecs.umich.edu#name of The University of Michigan is not used in any advertising or
331758Ssaidi@eecs.umich.edu#publicity pertaining to the use or distribution of this software
341758Ssaidi@eecs.umich.edu#without specific, written prior authorization.
351758Ssaidi@eecs.umich.edu#
361758Ssaidi@eecs.umich.edu#THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION FROM THE
371758Ssaidi@eecs.umich.edu#UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY PURPOSE, AND WITHOUT
381758Ssaidi@eecs.umich.edu#WARRANTY BY THE UNIVERSITY OF MICHIGAN OF ANY KIND, EITHER EXPRESS OR
391758Ssaidi@eecs.umich.edu#IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF
401758Ssaidi@eecs.umich.edu#MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE REGENTS OF
411758Ssaidi@eecs.umich.edu#THE UNIVERSITY OF MICHIGAN SHALL NOT BE LIABLE FOR ANY DAMAGES,
421758Ssaidi@eecs.umich.edu#INCLUDING DIRECT, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
431758Ssaidi@eecs.umich.edu#DAMAGES, WITH RESPECT TO ANY CLAIM ARISING OUT OF OR IN CONNECTION
441758Ssaidi@eecs.umich.edu#WITH THE USE OF THE SOFTWARE, EVEN IF IT HAS BEEN OR IS HEREAFTER
451758Ssaidi@eecs.umich.edu#ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
461758Ssaidi@eecs.umich.edu
471049Sbinkertn@umich.educlass Value:
481049Sbinkertn@umich.edu    def __init__(self, value, precision, percent = False):
491049Sbinkertn@umich.edu        self.value = value
501049Sbinkertn@umich.edu        self.precision = precision
511049Sbinkertn@umich.edu        self.percent = percent
521049Sbinkertn@umich.edu    def __str__(self):
531049Sbinkertn@umich.edu        if isinstance(self.value, str):
541049Sbinkertn@umich.edu            if self.value.lower() == 'nan':
551049Sbinkertn@umich.edu                value = 'NaN'
561049Sbinkertn@umich.edu            if self.value.lower() == 'inf':
571049Sbinkertn@umich.edu                value = 'Inf'
581049Sbinkertn@umich.edu        else:
591049Sbinkertn@umich.edu            if self.precision >= 0:
601049Sbinkertn@umich.edu                format = "%%.%df" % self.precision
611049Sbinkertn@umich.edu            elif self.value == 0.0:
621049Sbinkertn@umich.edu                format = "%.0f"
631049Sbinkertn@umich.edu            elif self.value % 1.0 == 0.0:
641049Sbinkertn@umich.edu                format = "%.0f"
651049Sbinkertn@umich.edu            else:
661049Sbinkertn@umich.edu                format = "%f"
671049Sbinkertn@umich.edu            value = self.value
681049Sbinkertn@umich.edu            if self.percent:
691049Sbinkertn@umich.edu                value = value * 100.0
701049Sbinkertn@umich.edu            value = format % value
711049Sbinkertn@umich.edu
721049Sbinkertn@umich.edu        if self.percent:
731049Sbinkertn@umich.edu            value = value + "%"
741049Sbinkertn@umich.edu
751049Sbinkertn@umich.edu        return value
761049Sbinkertn@umich.edu
771049Sbinkertn@umich.educlass Print:
781049Sbinkertn@umich.edu    def __init__(self, **vals):
791049Sbinkertn@umich.edu        self.__dict__.update(vals)
801049Sbinkertn@umich.edu
811049Sbinkertn@umich.edu    def __str__(self):
821049Sbinkertn@umich.edu        value = Value(self.value, self.precision)
831049Sbinkertn@umich.edu        pdf = ''
841049Sbinkertn@umich.edu        cdf = ''
851049Sbinkertn@umich.edu        if self.__dict__.has_key('pdf'):
861049Sbinkertn@umich.edu            pdf = Value(self.pdf, 2, True)
871049Sbinkertn@umich.edu        if self.__dict__.has_key('cdf'):
881049Sbinkertn@umich.edu            cdf = Value(self.cdf, 2, True)
891049Sbinkertn@umich.edu
901049Sbinkertn@umich.edu        output = "%-40s %12s %8s %8s" % (self.name, value, pdf, cdf)
911049Sbinkertn@umich.edu
921049Sbinkertn@umich.edu        if descriptions and self.__dict__.has_key('desc') and self.desc:
931049Sbinkertn@umich.edu            output = "%s # %s" % (output, self.desc)
941049Sbinkertn@umich.edu
951049Sbinkertn@umich.edu        return output
961049Sbinkertn@umich.edu
971049Sbinkertn@umich.edu    def doprint(self):
981049Sbinkertn@umich.edu        if display_all:
991049Sbinkertn@umich.edu            return True
1001049Sbinkertn@umich.edu        if self.value == 0.0 and (self.flags & flags_nozero):
1011049Sbinkertn@umich.edu            return False
1021049Sbinkertn@umich.edu        if isinstance(self.value, str):
1031049Sbinkertn@umich.edu            if self.value == 'NaN' and (self.flags & flags_nonan):
1041049Sbinkertn@umich.edu                return False
1051049Sbinkertn@umich.edu        return True
1061049Sbinkertn@umich.edu
1071049Sbinkertn@umich.edu    def display(self):
1081049Sbinkertn@umich.edu        if self.doprint():
1091049Sbinkertn@umich.edu            print self
1101049Sbinkertn@umich.edu
1111049Sbinkertn@umich.educlass VectorDisplay:
1121049Sbinkertn@umich.edu    def display(self):
1131049Sbinkertn@umich.edu        p = Print()
1141049Sbinkertn@umich.edu        p.flags = self.flags
1151049Sbinkertn@umich.edu        p.precision = self.precision
1161049Sbinkertn@umich.edu
1171547Sbinkertn@umich.edu        if isinstance(self.value, (list, tuple)):
1181049Sbinkertn@umich.edu            if not len(self.value):
1191049Sbinkertn@umich.edu                return
1201049Sbinkertn@umich.edu
1211049Sbinkertn@umich.edu            mytotal = reduce(lambda x,y: float(x) + float(y), self.value)
1221049Sbinkertn@umich.edu            mycdf = 0.0
1231049Sbinkertn@umich.edu
1241049Sbinkertn@umich.edu            value = self.value
1251049Sbinkertn@umich.edu
1261049Sbinkertn@umich.edu            if display_all:
1271049Sbinkertn@umich.edu                subnames = [ '[%d]' % i for i in range(len(value)) ]
1281049Sbinkertn@umich.edu            else:
1291049Sbinkertn@umich.edu                subnames = [''] * len(value)
1301049Sbinkertn@umich.edu
1311049Sbinkertn@umich.edu            if self.__dict__.has_key('subnames'):
1321049Sbinkertn@umich.edu                for i,each in enumerate(self.subnames):
1331049Sbinkertn@umich.edu                    if len(each) > 0:
1341049Sbinkertn@umich.edu                        subnames[i] = '.%s' % each
1351049Sbinkertn@umich.edu
1361049Sbinkertn@umich.edu            subdescs = [self.desc]*len(value)
1371049Sbinkertn@umich.edu            if self.__dict__.has_key('subdescs'):
1381049Sbinkertn@umich.edu                for i in xrange(min(len(value), len(self.subdescs))):
1391049Sbinkertn@umich.edu                    subdescs[i] = self.subdescs[i]
1401049Sbinkertn@umich.edu
1411049Sbinkertn@umich.edu            for val,sname,sdesc in map(None, value, subnames, subdescs):
1421049Sbinkertn@umich.edu                if mytotal > 0.0:
1431049Sbinkertn@umich.edu                    mypdf = float(val) / float(mytotal)
1441049Sbinkertn@umich.edu                    mycdf += mypdf
1451049Sbinkertn@umich.edu                    if (self.flags & flags_pdf):
1461049Sbinkertn@umich.edu                        p.pdf = mypdf
1471049Sbinkertn@umich.edu                        p.cdf = mycdf
1481049Sbinkertn@umich.edu
1491049Sbinkertn@umich.edu                if len(sname) == 0:
1501049Sbinkertn@umich.edu                    continue
1511049Sbinkertn@umich.edu
1521049Sbinkertn@umich.edu                p.name = self.name + sname
1531049Sbinkertn@umich.edu                p.desc = sdesc
1541049Sbinkertn@umich.edu                p.value = val
1551049Sbinkertn@umich.edu                p.display()
1561049Sbinkertn@umich.edu
1571049Sbinkertn@umich.edu            if (self.flags & flags_total):
1581049Sbinkertn@umich.edu                if (p.__dict__.has_key('pdf')): del p.__dict__['pdf']
1591049Sbinkertn@umich.edu                if (p.__dict__.has_key('cdf')): del p.__dict__['cdf']
1601049Sbinkertn@umich.edu                p.name = self.name + '.total'
1611049Sbinkertn@umich.edu                p.desc = self.desc
1621049Sbinkertn@umich.edu                p.value = mytotal
1631049Sbinkertn@umich.edu                p.display()
1641049Sbinkertn@umich.edu
1651049Sbinkertn@umich.edu        else:
1661049Sbinkertn@umich.edu            p.name = self.name
1671049Sbinkertn@umich.edu            p.desc = self.desc
1681049Sbinkertn@umich.edu            p.value = self.value
1691049Sbinkertn@umich.edu            p.display()
1701049Sbinkertn@umich.edu
171