print.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.eduall = False
481049Sbinkertn@umich.edudescriptions = False
491049Sbinkertn@umich.edu
501049Sbinkertn@umich.educlass Value:
511049Sbinkertn@umich.edu    def __init__(self, value, precision, percent = False):
521049Sbinkertn@umich.edu        self.value = value
531049Sbinkertn@umich.edu        self.precision = precision
541049Sbinkertn@umich.edu        self.percent = percent
551049Sbinkertn@umich.edu    def __str__(self):
561049Sbinkertn@umich.edu        if isinstance(self.value, str):
571049Sbinkertn@umich.edu            if self.value.lower() == 'nan':
581049Sbinkertn@umich.edu                value = 'NaN'
591049Sbinkertn@umich.edu            if self.value.lower() == 'inf':
601049Sbinkertn@umich.edu                value = 'Inf'
611049Sbinkertn@umich.edu        else:
621049Sbinkertn@umich.edu            if self.precision >= 0:
631049Sbinkertn@umich.edu                format = "%%.%df" % self.precision
641049Sbinkertn@umich.edu            elif self.value == 0.0:
651049Sbinkertn@umich.edu                format = "%.0f"
661049Sbinkertn@umich.edu            elif self.value % 1.0 == 0.0:
671049Sbinkertn@umich.edu                format = "%.0f"
681049Sbinkertn@umich.edu            else:
691049Sbinkertn@umich.edu                format = "%f"
701049Sbinkertn@umich.edu            value = self.value
711049Sbinkertn@umich.edu            if self.percent:
721049Sbinkertn@umich.edu                value = value * 100.0
731049Sbinkertn@umich.edu            value = format % value
741049Sbinkertn@umich.edu
751049Sbinkertn@umich.edu        if self.percent:
761049Sbinkertn@umich.edu            value = value + "%"
771049Sbinkertn@umich.edu
781049Sbinkertn@umich.edu        return value
791049Sbinkertn@umich.edu
801049Sbinkertn@umich.educlass Print:
811049Sbinkertn@umich.edu    def __init__(self, **vals):
821049Sbinkertn@umich.edu        self.__dict__.update(vals)
831049Sbinkertn@umich.edu
841049Sbinkertn@umich.edu    def __str__(self):
851049Sbinkertn@umich.edu        value = Value(self.value, self.precision)
861049Sbinkertn@umich.edu        pdf = ''
871049Sbinkertn@umich.edu        cdf = ''
881049Sbinkertn@umich.edu        if self.__dict__.has_key('pdf'):
891049Sbinkertn@umich.edu            pdf = Value(self.pdf, 2, True)
901049Sbinkertn@umich.edu        if self.__dict__.has_key('cdf'):
911049Sbinkertn@umich.edu            cdf = Value(self.cdf, 2, True)
921049Sbinkertn@umich.edu
931049Sbinkertn@umich.edu        output = "%-40s %12s %8s %8s" % (self.name, value, pdf, cdf)
941049Sbinkertn@umich.edu
951049Sbinkertn@umich.edu        if descriptions and self.__dict__.has_key('desc') and self.desc:
961049Sbinkertn@umich.edu            output = "%s # %s" % (output, self.desc)
971049Sbinkertn@umich.edu
981049Sbinkertn@umich.edu        return output
991049Sbinkertn@umich.edu
1001049Sbinkertn@umich.edu    def doprint(self):
1011049Sbinkertn@umich.edu        if display_all:
1021049Sbinkertn@umich.edu            return True
1031049Sbinkertn@umich.edu        if self.value == 0.0 and (self.flags & flags_nozero):
1041049Sbinkertn@umich.edu            return False
1051049Sbinkertn@umich.edu        if isinstance(self.value, str):
1061049Sbinkertn@umich.edu            if self.value == 'NaN' and (self.flags & flags_nonan):
1071049Sbinkertn@umich.edu                return False
1081049Sbinkertn@umich.edu        return True
1091049Sbinkertn@umich.edu
1101049Sbinkertn@umich.edu    def display(self):
1111049Sbinkertn@umich.edu        if self.doprint():
1121049Sbinkertn@umich.edu            print self
1131049Sbinkertn@umich.edu
1141049Sbinkertn@umich.educlass VectorDisplay:
1151049Sbinkertn@umich.edu    def display(self):
1161049Sbinkertn@umich.edu        p = Print()
1171049Sbinkertn@umich.edu        p.flags = self.flags
1181049Sbinkertn@umich.edu        p.precision = self.precision
1191049Sbinkertn@umich.edu
1201547Sbinkertn@umich.edu        if isinstance(self.value, (list, tuple)):
1211049Sbinkertn@umich.edu            if not len(self.value):
1221049Sbinkertn@umich.edu                return
1231049Sbinkertn@umich.edu
1241049Sbinkertn@umich.edu            mytotal = reduce(lambda x,y: float(x) + float(y), self.value)
1251049Sbinkertn@umich.edu            mycdf = 0.0
1261049Sbinkertn@umich.edu
1271049Sbinkertn@umich.edu            value = self.value
1281049Sbinkertn@umich.edu
1291049Sbinkertn@umich.edu            if display_all:
1301049Sbinkertn@umich.edu                subnames = [ '[%d]' % i for i in range(len(value)) ]
1311049Sbinkertn@umich.edu            else:
1321049Sbinkertn@umich.edu                subnames = [''] * len(value)
1331049Sbinkertn@umich.edu
1341049Sbinkertn@umich.edu            if self.__dict__.has_key('subnames'):
1351049Sbinkertn@umich.edu                for i,each in enumerate(self.subnames):
1361049Sbinkertn@umich.edu                    if len(each) > 0:
1371049Sbinkertn@umich.edu                        subnames[i] = '.%s' % each
1381049Sbinkertn@umich.edu
1391049Sbinkertn@umich.edu            subdescs = [self.desc]*len(value)
1401049Sbinkertn@umich.edu            if self.__dict__.has_key('subdescs'):
1411049Sbinkertn@umich.edu                for i in xrange(min(len(value), len(self.subdescs))):
1421049Sbinkertn@umich.edu                    subdescs[i] = self.subdescs[i]
1431049Sbinkertn@umich.edu
1441049Sbinkertn@umich.edu            for val,sname,sdesc in map(None, value, subnames, subdescs):
1451049Sbinkertn@umich.edu                if mytotal > 0.0:
1461049Sbinkertn@umich.edu                    mypdf = float(val) / float(mytotal)
1471049Sbinkertn@umich.edu                    mycdf += mypdf
1481049Sbinkertn@umich.edu                    if (self.flags & flags_pdf):
1491049Sbinkertn@umich.edu                        p.pdf = mypdf
1501049Sbinkertn@umich.edu                        p.cdf = mycdf
1511049Sbinkertn@umich.edu
1521049Sbinkertn@umich.edu                if len(sname) == 0:
1531049Sbinkertn@umich.edu                    continue
1541049Sbinkertn@umich.edu
1551049Sbinkertn@umich.edu                p.name = self.name + sname
1561049Sbinkertn@umich.edu                p.desc = sdesc
1571049Sbinkertn@umich.edu                p.value = val
1581049Sbinkertn@umich.edu                p.display()
1591049Sbinkertn@umich.edu
1601049Sbinkertn@umich.edu            if (self.flags & flags_total):
1611049Sbinkertn@umich.edu                if (p.__dict__.has_key('pdf')): del p.__dict__['pdf']
1621049Sbinkertn@umich.edu                if (p.__dict__.has_key('cdf')): del p.__dict__['cdf']
1631049Sbinkertn@umich.edu                p.name = self.name + '.total'
1641049Sbinkertn@umich.edu                p.desc = self.desc
1651049Sbinkertn@umich.edu                p.value = mytotal
1661049Sbinkertn@umich.edu                p.display()
1671049Sbinkertn@umich.edu
1681049Sbinkertn@umich.edu        else:
1691049Sbinkertn@umich.edu            p.name = self.name
1701049Sbinkertn@umich.edu            p.desc = self.desc
1711049Sbinkertn@umich.edu            p.value = self.value
1721049Sbinkertn@umich.edu            p.display()
1731049Sbinkertn@umich.edu
174