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