info.py revision 1331
11049Sbinkertn@umich.edufrom __future__ import division
21049Sbinkertn@umich.eduimport operator, re, types
31049Sbinkertn@umich.edu
41049Sbinkertn@umich.edusource = None
51049Sbinkertn@umich.edudisplay_run = 0
61329Ssaidi@eecs.umich.eduglobal globalTicks
71329Ssaidi@eecs.umich.eduglobalTicks = None
81049Sbinkertn@umich.edu
91049Sbinkertn@umich.edudef issequence(t):
101049Sbinkertn@umich.edu    return isinstance(t, types.TupleType) or isinstance(t, types.ListType)
111049Sbinkertn@umich.edu
121049Sbinkertn@umich.edudef total(f):
131049Sbinkertn@umich.edu    if isinstance(f, FormulaStat):
141049Sbinkertn@umich.edu        v = f.value
151049Sbinkertn@umich.edu    else:
161049Sbinkertn@umich.edu        v = f
171049Sbinkertn@umich.edu
181049Sbinkertn@umich.edu    f = FormulaStat()
191049Sbinkertn@umich.edu    if issequence(v):
201049Sbinkertn@umich.edu        f.value = reduce(operator.add, v)
211049Sbinkertn@umich.edu    else:
221049Sbinkertn@umich.edu        f.value = v
231049Sbinkertn@umich.edu
241049Sbinkertn@umich.edu    return f
251049Sbinkertn@umich.edu
261049Sbinkertn@umich.edudef unaryop(op, f):
271049Sbinkertn@umich.edu    if isinstance(f, FormulaStat):
281049Sbinkertn@umich.edu        v = f.value
291049Sbinkertn@umich.edu    else:
301049Sbinkertn@umich.edu        v = f
311049Sbinkertn@umich.edu
321049Sbinkertn@umich.edu    if issequence(v):
331049Sbinkertn@umich.edu        return map(op, v)
341049Sbinkertn@umich.edu    else:
351049Sbinkertn@umich.edu        return op(v)
361049Sbinkertn@umich.edu
371049Sbinkertn@umich.edudef zerodiv(lv, rv):
381049Sbinkertn@umich.edu    if rv == 0.0:
391049Sbinkertn@umich.edu        return 0.0
401049Sbinkertn@umich.edu    else:
411049Sbinkertn@umich.edu        return operator.truediv(lv, rv)
421049Sbinkertn@umich.edu
431049Sbinkertn@umich.edudef wrapop(op, lv, rv):
441049Sbinkertn@umich.edu    if isinstance(lv, str):
451049Sbinkertn@umich.edu        return lv
461049Sbinkertn@umich.edu
471049Sbinkertn@umich.edu    if isinstance(rv, str):
481049Sbinkertn@umich.edu        return rv
491049Sbinkertn@umich.edu
501049Sbinkertn@umich.edu    return op(lv, rv)
511049Sbinkertn@umich.edu
521269Sbinkertn@umich.edudef same(lrun, rrun):
531269Sbinkertn@umich.edu    for lx,rx in zip(lrun.keys(),rrun.keys()):
541269Sbinkertn@umich.edu        if lx != rx:
551269Sbinkertn@umich.edu            print 'lx != rx'
561269Sbinkertn@umich.edu            print lx, rx
571269Sbinkertn@umich.edu            print lrun.keys()
581269Sbinkertn@umich.edu            print rrun.keys()
591049Sbinkertn@umich.edu            return False
601269Sbinkertn@umich.edu        for ly,ry in zip(lrun[lx].keys(),rrun[rx].keys()):
611269Sbinkertn@umich.edu            if ly != ry:
621269Sbinkertn@umich.edu                print 'ly != ry'
631269Sbinkertn@umich.edu                print ly, ry
641269Sbinkertn@umich.edu                print lrun[lx].keys()
651269Sbinkertn@umich.edu                print rrun[rx].keys()
661049Sbinkertn@umich.edu                return False
671049Sbinkertn@umich.edu    return True
681049Sbinkertn@umich.edu
691049Sbinkertn@umich.edu
701049Sbinkertn@umich.edudef binaryop(op, lf, rf):
711049Sbinkertn@umich.edu    result = {}
721049Sbinkertn@umich.edu
731049Sbinkertn@umich.edu    if isinstance(lf, FormulaStat) and isinstance(rf, FormulaStat):
741049Sbinkertn@umich.edu        lv = lf.value
751049Sbinkertn@umich.edu        rv = rf.value
761049Sbinkertn@umich.edu
771269Sbinkertn@umich.edu        theruns = []
781269Sbinkertn@umich.edu        for r in lv.keys():
791269Sbinkertn@umich.edu            if rv.has_key(r):
801269Sbinkertn@umich.edu                if same(lv[r], rv[r]):
811269Sbinkertn@umich.edu                    theruns.append(r)
821269Sbinkertn@umich.edu                else:
831269Sbinkertn@umich.edu                    raise AttributeError
841049Sbinkertn@umich.edu
851269Sbinkertn@umich.edu        for run in theruns:
861049Sbinkertn@umich.edu            result[run] = {}
871049Sbinkertn@umich.edu            for x in lv[run].keys():
881049Sbinkertn@umich.edu                result[run][x] = {}
891049Sbinkertn@umich.edu                for y in lv[run][x].keys():
901049Sbinkertn@umich.edu                    result[run][x][y] = wrapop(op, lv[run][x][y],
911049Sbinkertn@umich.edu                                               rv[run][x][y])
921049Sbinkertn@umich.edu    elif isinstance(lf, FormulaStat):
931049Sbinkertn@umich.edu        lv = lf.value
941049Sbinkertn@umich.edu        for run in lv.keys():
951049Sbinkertn@umich.edu            result[run] = {}
961049Sbinkertn@umich.edu            for x in lv[run].keys():
971049Sbinkertn@umich.edu                result[run][x] = {}
981049Sbinkertn@umich.edu                for y in lv[run][x].keys():
991049Sbinkertn@umich.edu                    result[run][x][y] = wrapop(op, lv[run][x][y], rf)
1001049Sbinkertn@umich.edu    elif isinstance(rf, FormulaStat):
1011049Sbinkertn@umich.edu        rv = rf.value
1021049Sbinkertn@umich.edu        for run in rv.keys():
1031049Sbinkertn@umich.edu            result[run] = {}
1041049Sbinkertn@umich.edu            for x in rv[run].keys():
1051049Sbinkertn@umich.edu                result[run][x] = {}
1061049Sbinkertn@umich.edu                for y in rv[run][x].keys():
1071049Sbinkertn@umich.edu                    result[run][x][y] = wrapop(op, lf, rv[run][x][y])
1081049Sbinkertn@umich.edu
1091049Sbinkertn@umich.edu    return result
1101049Sbinkertn@umich.edu
1111049Sbinkertn@umich.edudef sums(x, y):
1121049Sbinkertn@umich.edu    if issequence(x):
1131049Sbinkertn@umich.edu        return map(lambda x, y: x + y, x, y)
1141049Sbinkertn@umich.edu    else:
1151049Sbinkertn@umich.edu        return x + y
1161049Sbinkertn@umich.edu
1171049Sbinkertn@umich.edudef alltrue(list):
1181049Sbinkertn@umich.edu    return reduce(lambda x, y: x and y, list)
1191049Sbinkertn@umich.edu
1201049Sbinkertn@umich.edudef allfalse(list):
1211049Sbinkertn@umich.edu    return not reduce(lambda x, y: x or y, list)
1221049Sbinkertn@umich.edu
1231049Sbinkertn@umich.edudef enumerate(list):
1241049Sbinkertn@umich.edu    return map(None, range(len(list)), list)
1251049Sbinkertn@umich.edu
1261049Sbinkertn@umich.edudef cmp(a, b):
1271049Sbinkertn@umich.edu    if a < b:
1281049Sbinkertn@umich.edu        return -1
1291049Sbinkertn@umich.edu    elif a == b:
1301049Sbinkertn@umich.edu        return 0
1311049Sbinkertn@umich.edu    else:
1321049Sbinkertn@umich.edu        return 1
1331049Sbinkertn@umich.edu
1341049Sbinkertn@umich.educlass Statistic(object):
1351329Ssaidi@eecs.umich.edu
1361049Sbinkertn@umich.edu    def __init__(self, data):
1371049Sbinkertn@umich.edu        self.__dict__.update(data.__dict__)
1381049Sbinkertn@umich.edu        if not self.__dict__.has_key('value'):
1391049Sbinkertn@umich.edu            self.__dict__['value'] = None
1401049Sbinkertn@umich.edu        if not self.__dict__.has_key('bins'):
1411049Sbinkertn@umich.edu            self.__dict__['bins'] = None
1421049Sbinkertn@umich.edu        if not self.__dict__.has_key('ticks'):
1431049Sbinkertn@umich.edu            self.__dict__['ticks'] = None
1441329Ssaidi@eecs.umich.edu        if 'vc' not in self.__dict__:
1451329Ssaidi@eecs.umich.edu            self.vc = {}
1461049Sbinkertn@umich.edu
1471049Sbinkertn@umich.edu    def __getattribute__(self, attr):
1481329Ssaidi@eecs.umich.edu        if attr == 'ticks':
1491329Ssaidi@eecs.umich.edu            if self.__dict__['ticks'] != globalTicks:
1501329Ssaidi@eecs.umich.edu                self.__dict__['value'] = None
1511329Ssaidi@eecs.umich.edu                self.__dict__['ticks'] = globalTicks
1521329Ssaidi@eecs.umich.edu            return self.__dict__['ticks']
1531049Sbinkertn@umich.edu        if attr == 'value':
1541329Ssaidi@eecs.umich.edu            if self.__dict__['ticks'] != globalTicks:
1551329Ssaidi@eecs.umich.edu                if self.__dict__['ticks'] != None and \
1561329Ssaidi@eecs.umich.edu                                    len(self.__dict__['ticks']) == 1:
1571329Ssaidi@eecs.umich.edu                    self.vc[self.__dict__['ticks'][0]] = self.__dict__['value']
1581329Ssaidi@eecs.umich.edu                self.__dict__['ticks'] = globalTicks
1591329Ssaidi@eecs.umich.edu                if len(globalTicks) == 1 and self.vc.has_key(globalTicks[0]):
1601329Ssaidi@eecs.umich.edu                    self.__dict__['value'] = self.vc[globalTicks[0]]
1611329Ssaidi@eecs.umich.edu                else:
1621329Ssaidi@eecs.umich.edu                    self.__dict__['value'] = None
1631049Sbinkertn@umich.edu            if self.__dict__['value'] == None:
1641049Sbinkertn@umich.edu                self.__dict__['value'] = self.getValue()
1651049Sbinkertn@umich.edu            return self.__dict__['value']
1661049Sbinkertn@umich.edu        else:
1671049Sbinkertn@umich.edu            return super(Statistic, self).__getattribute__(attr)
1681049Sbinkertn@umich.edu
1691049Sbinkertn@umich.edu    def __setattr__(self, attr, value):
1701049Sbinkertn@umich.edu        if attr == 'bins' or attr == 'ticks':
1711049Sbinkertn@umich.edu            if attr == 'bins':
1721049Sbinkertn@umich.edu                if value is not None:
1731165Sbinkertn@umich.edu                    value = source.getBin(value)
1741329Ssaidi@eecs.umich.edu            #elif attr == 'ticks' and type(value) is str:
1751329Ssaidi@eecs.umich.edu            #    value = [ int(x) for x in value.split() ]
1761049Sbinkertn@umich.edu
1771049Sbinkertn@umich.edu            self.__dict__[attr] = value
1781049Sbinkertn@umich.edu            self.__dict__['value'] = None
1791329Ssaidi@eecs.umich.edu            self.vc = {}
1801049Sbinkertn@umich.edu        else:
1811049Sbinkertn@umich.edu            super(Statistic, self).__setattr__(attr, value)
1821049Sbinkertn@umich.edu
1831049Sbinkertn@umich.edu    def getValue(self):
1841049Sbinkertn@umich.edu        raise AttributeError, 'getValue() must be defined'
1851049Sbinkertn@umich.edu
1861049Sbinkertn@umich.edu    def zero(self):
1871049Sbinkertn@umich.edu        return False
1881049Sbinkertn@umich.edu
1891049Sbinkertn@umich.edu    def __ne__(self, other):
1901049Sbinkertn@umich.edu        return not (self == other)
1911049Sbinkertn@umich.edu
1921049Sbinkertn@umich.edu    def __str__(self):
1931049Sbinkertn@umich.edu        return '%f' % (float(self))
1941049Sbinkertn@umich.edu
1951049Sbinkertn@umich.educlass FormulaStat(object):
1961049Sbinkertn@umich.edu    def __add__(self, other):
1971049Sbinkertn@umich.edu        f = FormulaStat()
1981049Sbinkertn@umich.edu        f.value = binaryop(operator.add, self, other)
1991049Sbinkertn@umich.edu        return f
2001049Sbinkertn@umich.edu    def __sub__(self, other):
2011049Sbinkertn@umich.edu        f = FormulaStat()
2021049Sbinkertn@umich.edu        f.value = binaryop(operator.sub, self, other)
2031049Sbinkertn@umich.edu        return f
2041049Sbinkertn@umich.edu    def __mul__(self, other):
2051049Sbinkertn@umich.edu        f = FormulaStat()
2061049Sbinkertn@umich.edu        f.value = binaryop(operator.mul, self, other)
2071049Sbinkertn@umich.edu        return f
2081049Sbinkertn@umich.edu    def __truediv__(self, other):
2091049Sbinkertn@umich.edu        f = FormulaStat()
2101049Sbinkertn@umich.edu        f.value = binaryop(zerodiv, self, other)
2111049Sbinkertn@umich.edu        return f
2121049Sbinkertn@umich.edu    def __mod__(self, other):
2131049Sbinkertn@umich.edu        f = FormulaStat()
2141049Sbinkertn@umich.edu        f.value = binaryop(operator.mod, self, other)
2151049Sbinkertn@umich.edu        return f
2161049Sbinkertn@umich.edu    def __radd__(self, other):
2171049Sbinkertn@umich.edu        f = FormulaStat()
2181049Sbinkertn@umich.edu        f.value = binaryop(operator.add, other, self)
2191049Sbinkertn@umich.edu        return f
2201049Sbinkertn@umich.edu    def __rsub__(self, other):
2211049Sbinkertn@umich.edu        f = FormulaStat()
2221049Sbinkertn@umich.edu        f.value = binaryop(operator.sub, other, self)
2231049Sbinkertn@umich.edu        return f
2241049Sbinkertn@umich.edu    def __rmul__(self, other):
2251049Sbinkertn@umich.edu        f = FormulaStat()
2261049Sbinkertn@umich.edu        f.value = binaryop(operator.mul, other, self)
2271049Sbinkertn@umich.edu        return f
2281049Sbinkertn@umich.edu    def __rtruediv__(self, other):
2291049Sbinkertn@umich.edu        f = FormulaStat()
2301049Sbinkertn@umich.edu        f.value = binaryop(zerodiv, other, self)
2311049Sbinkertn@umich.edu        return f
2321049Sbinkertn@umich.edu    def __rmod__(self, other):
2331049Sbinkertn@umich.edu        f = FormulaStat()
2341049Sbinkertn@umich.edu        f.value = binaryop(operator.mod, other, self)
2351049Sbinkertn@umich.edu        return f
2361049Sbinkertn@umich.edu    def __neg__(self):
2371049Sbinkertn@umich.edu        f = FormulaStat()
2381049Sbinkertn@umich.edu        f.value = unaryop(operator.neg, self)
2391049Sbinkertn@umich.edu        return f
2401049Sbinkertn@umich.edu    def __getitem__(self, idx):
2411049Sbinkertn@umich.edu        f = FormulaStat()
2421049Sbinkertn@umich.edu        f.value = {}
2431049Sbinkertn@umich.edu        for key in self.value.keys():
2441049Sbinkertn@umich.edu            f.value[key] = {}
2451049Sbinkertn@umich.edu            f.value[key][0] = {}
2461049Sbinkertn@umich.edu            f.value[key][0][0] = self.value[key][idx][0]
2471049Sbinkertn@umich.edu        return f
2481049Sbinkertn@umich.edu
2491049Sbinkertn@umich.edu    def __float__(self):
2501049Sbinkertn@umich.edu        if isinstance(self.value, FormulaStat):
2511049Sbinkertn@umich.edu            return float(self.value)
2521049Sbinkertn@umich.edu        if not self.value.has_key(display_run):
2531049Sbinkertn@umich.edu            return (1e300*1e300)
2541049Sbinkertn@umich.edu        if len(self.value[display_run]) == 1:
2551049Sbinkertn@umich.edu            return self.value[display_run][0][0]
2561049Sbinkertn@umich.edu        else:
2571049Sbinkertn@umich.edu            #print self.value[display_run]
2581049Sbinkertn@umich.edu            return self.value[display_run][4][0]
2591049Sbinkertn@umich.edu            #raise ValueError
2601049Sbinkertn@umich.edu
2611049Sbinkertn@umich.edu    def display(self):
2621049Sbinkertn@umich.edu        import display
2631049Sbinkertn@umich.edu        d = display.VectorDisplay()
2641049Sbinkertn@umich.edu        d.flags = 0
2651049Sbinkertn@umich.edu        d.precision = 1
2661049Sbinkertn@umich.edu        d.name = 'formula'
2671049Sbinkertn@umich.edu        d.desc = 'formula'
2681049Sbinkertn@umich.edu        val = self.value[display_run]
2691049Sbinkertn@umich.edu        d.value = [ val[x][0] for x in val.keys() ]
2701049Sbinkertn@umich.edu        d.display()
2711049Sbinkertn@umich.edu
2721049Sbinkertn@umich.edu
2731049Sbinkertn@umich.educlass Scalar(Statistic,FormulaStat):
2741049Sbinkertn@umich.edu    def getValue(self):
2751301Ssaidi@eecs.umich.edu        return source.data(self, self.bins, self.ticks)
2761049Sbinkertn@umich.edu
2771049Sbinkertn@umich.edu    def display(self):
2781049Sbinkertn@umich.edu        import display
2791049Sbinkertn@umich.edu        p = display.Print()
2801049Sbinkertn@umich.edu        p.name = self.name
2811049Sbinkertn@umich.edu        p.desc = self.desc
2821049Sbinkertn@umich.edu        p.value = float(self)
2831049Sbinkertn@umich.edu        p.flags = self.flags
2841049Sbinkertn@umich.edu        p.precision = self.precision
2851049Sbinkertn@umich.edu        if display.all or (self.flags & flags.printable):
2861049Sbinkertn@umich.edu            p.display()
2871049Sbinkertn@umich.edu
2881049Sbinkertn@umich.edu    def comparable(self, other):
2891049Sbinkertn@umich.edu        return self.name == other.name
2901049Sbinkertn@umich.edu
2911049Sbinkertn@umich.edu    def __eq__(self, other):
2921049Sbinkertn@umich.edu        return self.value == other.value
2931049Sbinkertn@umich.edu
2941049Sbinkertn@umich.edu    def __isub__(self, other):
2951049Sbinkertn@umich.edu        self.value -= other.value
2961049Sbinkertn@umich.edu        return self
2971049Sbinkertn@umich.edu
2981049Sbinkertn@umich.edu    def __iadd__(self, other):
2991049Sbinkertn@umich.edu        self.value += other.value
3001049Sbinkertn@umich.edu        return self
3011049Sbinkertn@umich.edu
3021049Sbinkertn@umich.edu    def __itruediv__(self, other):
3031049Sbinkertn@umich.edu        if not other:
3041049Sbinkertn@umich.edu            return self
3051049Sbinkertn@umich.edu        self.value /= other
3061049Sbinkertn@umich.edu        return self
3071049Sbinkertn@umich.edu
3081049Sbinkertn@umich.educlass Vector(Statistic,FormulaStat):
3091049Sbinkertn@umich.edu    def getValue(self):
3101331Ssaidi@eecs.umich.edu        return source.data(self, self.bins, self.ticks);
3111049Sbinkertn@umich.edu
3121049Sbinkertn@umich.edu    def display(self):
3131049Sbinkertn@umich.edu        import display
3141049Sbinkertn@umich.edu        if not display.all and not (self.flags & flags.printable):
3151049Sbinkertn@umich.edu            return
3161049Sbinkertn@umich.edu
3171049Sbinkertn@umich.edu        d = display.VectorDisplay()
3181049Sbinkertn@umich.edu        d.__dict__.update(self.__dict__)
3191049Sbinkertn@umich.edu        d.display()
3201049Sbinkertn@umich.edu
3211049Sbinkertn@umich.edu    def comparable(self, other):
3221049Sbinkertn@umich.edu        return self.name == other.name and \
3231049Sbinkertn@umich.edu               len(self.value) == len(other.value)
3241049Sbinkertn@umich.edu
3251049Sbinkertn@umich.edu    def __eq__(self, other):
3261049Sbinkertn@umich.edu        if issequence(self.value) != issequence(other.value):
3271049Sbinkertn@umich.edu            return false
3281049Sbinkertn@umich.edu
3291049Sbinkertn@umich.edu        if issequence(self.value):
3301049Sbinkertn@umich.edu            if len(self.value) != len(other.value):
3311049Sbinkertn@umich.edu                return False
3321049Sbinkertn@umich.edu            else:
3331049Sbinkertn@umich.edu                for v1,v2 in zip(self.value, other.value):
3341049Sbinkertn@umich.edu                    if v1 != v2:
3351049Sbinkertn@umich.edu                        return False
3361049Sbinkertn@umich.edu                return True
3371049Sbinkertn@umich.edu        else:
3381049Sbinkertn@umich.edu            return self.value == other.value
3391049Sbinkertn@umich.edu
3401049Sbinkertn@umich.edu    def __isub__(self, other):
3411049Sbinkertn@umich.edu        self.value = binaryop(operator.sub, self.value, other.value)
3421049Sbinkertn@umich.edu        return self
3431049Sbinkertn@umich.edu
3441049Sbinkertn@umich.edu    def __iadd__(self, other):
3451049Sbinkertn@umich.edu        self.value = binaryop(operator.add, self.value, other.value)
3461049Sbinkertn@umich.edu        return self
3471049Sbinkertn@umich.edu
3481049Sbinkertn@umich.edu    def __itruediv__(self, other):
3491049Sbinkertn@umich.edu        if not other:
3501049Sbinkertn@umich.edu            return self
3511049Sbinkertn@umich.edu        if issequence(self.value):
3521049Sbinkertn@umich.edu            for i in xrange(len(self.value)):
3531049Sbinkertn@umich.edu                self.value[i] /= other
3541049Sbinkertn@umich.edu        else:
3551049Sbinkertn@umich.edu            self.value /= other
3561049Sbinkertn@umich.edu        return self
3571049Sbinkertn@umich.edu
3581049Sbinkertn@umich.educlass Formula(Vector):
3591049Sbinkertn@umich.edu    def getValue(self):
3601049Sbinkertn@umich.edu        formula = re.sub(':', '__', self.formula)
3611049Sbinkertn@umich.edu        x = eval(formula, source.stattop)
3621049Sbinkertn@umich.edu        return x.value
3631049Sbinkertn@umich.edu
3641049Sbinkertn@umich.edu    def comparable(self, other):
3651049Sbinkertn@umich.edu        return self.name == other.name and \
3661049Sbinkertn@umich.edu               compare(self.dist, other.dist)
3671049Sbinkertn@umich.edu
3681049Sbinkertn@umich.edu    def __eq__(self, other):
3691049Sbinkertn@umich.edu        return self.value == other.value
3701049Sbinkertn@umich.edu
3711049Sbinkertn@umich.edu    def __isub__(self, other):
3721049Sbinkertn@umich.edu        return self
3731049Sbinkertn@umich.edu
3741049Sbinkertn@umich.edu    def __iadd__(self, other):
3751049Sbinkertn@umich.edu        return self
3761049Sbinkertn@umich.edu
3771049Sbinkertn@umich.edu    def __itruediv__(self, other):
3781049Sbinkertn@umich.edu        if not other:
3791049Sbinkertn@umich.edu            return self
3801049Sbinkertn@umich.edu        return self
3811049Sbinkertn@umich.edu
3821049Sbinkertn@umich.educlass SimpleDist(object):
3831049Sbinkertn@umich.edu    def __init__(self, sums, squares, samples):
3841049Sbinkertn@umich.edu        self.sums = sums
3851049Sbinkertn@umich.edu        self.squares = squares
3861049Sbinkertn@umich.edu        self.samples = samples
3871049Sbinkertn@umich.edu
3881049Sbinkertn@umich.edu    def getValue(self):
3891049Sbinkertn@umich.edu        return 0.0
3901049Sbinkertn@umich.edu
3911049Sbinkertn@umich.edu    def display(self, name, desc, flags, precision):
3921049Sbinkertn@umich.edu        import display
3931049Sbinkertn@umich.edu        p = display.Print()
3941049Sbinkertn@umich.edu        p.flags = flags
3951049Sbinkertn@umich.edu        p.precision = precision
3961049Sbinkertn@umich.edu
3971049Sbinkertn@umich.edu        if self.samples > 0:
3981049Sbinkertn@umich.edu            p.name = name + ".mean"
3991049Sbinkertn@umich.edu            p.value = self.sums / self.samples
4001049Sbinkertn@umich.edu            p.display()
4011049Sbinkertn@umich.edu
4021049Sbinkertn@umich.edu            p.name = name + ".stdev"
4031049Sbinkertn@umich.edu            if self.samples > 1:
4041049Sbinkertn@umich.edu                var = (self.samples * self.squares - self.sums ** 2) \
4051049Sbinkertn@umich.edu                      / (self.samples * (self.samples - 1))
4061049Sbinkertn@umich.edu                if var >= 0:
4071049Sbinkertn@umich.edu                    p.value = math.sqrt(var)
4081049Sbinkertn@umich.edu                else:
4091049Sbinkertn@umich.edu                    p.value = 'NaN'
4101049Sbinkertn@umich.edu            else:
4111049Sbinkertn@umich.edu                p.value = 0.0
4121049Sbinkertn@umich.edu            p.display()
4131049Sbinkertn@umich.edu
4141049Sbinkertn@umich.edu        p.name = name + ".samples"
4151049Sbinkertn@umich.edu        p.value = self.samples
4161049Sbinkertn@umich.edu        p.display()
4171049Sbinkertn@umich.edu
4181049Sbinkertn@umich.edu    def comparable(self, other):
4191049Sbinkertn@umich.edu        return True
4201049Sbinkertn@umich.edu
4211049Sbinkertn@umich.edu    def __eq__(self, other):
4221049Sbinkertn@umich.edu        return self.sums == other.sums and self.squares == other.squares and \
4231049Sbinkertn@umich.edu               self.samples == other.samples
4241049Sbinkertn@umich.edu
4251049Sbinkertn@umich.edu    def __isub__(self, other):
4261049Sbinkertn@umich.edu        self.sums -= other.sums
4271049Sbinkertn@umich.edu        self.squares -= other.squares
4281049Sbinkertn@umich.edu        self.samples -= other.samples
4291049Sbinkertn@umich.edu        return self
4301049Sbinkertn@umich.edu
4311049Sbinkertn@umich.edu    def __iadd__(self, other):
4321049Sbinkertn@umich.edu        self.sums += other.sums
4331049Sbinkertn@umich.edu        self.squares += other.squares
4341049Sbinkertn@umich.edu        self.samples += other.samples
4351049Sbinkertn@umich.edu        return self
4361049Sbinkertn@umich.edu
4371049Sbinkertn@umich.edu    def __itruediv__(self, other):
4381049Sbinkertn@umich.edu        if not other:
4391049Sbinkertn@umich.edu            return self
4401049Sbinkertn@umich.edu        self.sums /= other
4411049Sbinkertn@umich.edu        self.squares /= other
4421049Sbinkertn@umich.edu        self.samples /= other
4431049Sbinkertn@umich.edu        return self
4441049Sbinkertn@umich.edu
4451049Sbinkertn@umich.educlass FullDist(SimpleDist):
4461049Sbinkertn@umich.edu    def __init__(self, sums, squares, samples, minval, maxval,
4471049Sbinkertn@umich.edu                 under, vec, over, min, max, bsize, size):
4481049Sbinkertn@umich.edu        self.sums = sums
4491049Sbinkertn@umich.edu        self.squares = squares
4501049Sbinkertn@umich.edu        self.samples = samples
4511049Sbinkertn@umich.edu        self.minval = minval
4521049Sbinkertn@umich.edu        self.maxval = maxval
4531049Sbinkertn@umich.edu        self.under = under
4541049Sbinkertn@umich.edu        self.vec = vec
4551049Sbinkertn@umich.edu        self.over = over
4561049Sbinkertn@umich.edu        self.min = min
4571049Sbinkertn@umich.edu        self.max = max
4581049Sbinkertn@umich.edu        self.bsize = bsize
4591049Sbinkertn@umich.edu        self.size = size
4601049Sbinkertn@umich.edu
4611049Sbinkertn@umich.edu    def getValue(self):
4621049Sbinkertn@umich.edu        return 0.0
4631049Sbinkertn@umich.edu
4641049Sbinkertn@umich.edu    def display(self, name, desc, flags, precision):
4651049Sbinkertn@umich.edu        import display
4661049Sbinkertn@umich.edu        p = display.Print()
4671049Sbinkertn@umich.edu        p.flags = flags
4681049Sbinkertn@umich.edu        p.precision = precision
4691049Sbinkertn@umich.edu
4701049Sbinkertn@umich.edu        p.name = name + '.min_val'
4711049Sbinkertn@umich.edu        p.value = self.minval
4721049Sbinkertn@umich.edu        p.display()
4731049Sbinkertn@umich.edu
4741049Sbinkertn@umich.edu        p.name = name + '.max_val'
4751049Sbinkertn@umich.edu        p.value = self.maxval
4761049Sbinkertn@umich.edu        p.display()
4771049Sbinkertn@umich.edu
4781049Sbinkertn@umich.edu        p.name = name + '.underflow'
4791049Sbinkertn@umich.edu        p.value = self.under
4801049Sbinkertn@umich.edu        p.display()
4811049Sbinkertn@umich.edu
4821049Sbinkertn@umich.edu        i = self.min
4831049Sbinkertn@umich.edu        for val in self.vec[:-1]:
4841049Sbinkertn@umich.edu            p.name = name + '[%d:%d]' % (i, i + self.bsize - 1)
4851049Sbinkertn@umich.edu            p.value = val
4861049Sbinkertn@umich.edu            p.display()
4871049Sbinkertn@umich.edu            i += self.bsize
4881049Sbinkertn@umich.edu
4891049Sbinkertn@umich.edu        p.name = name + '[%d:%d]' % (i, self.max)
4901049Sbinkertn@umich.edu        p.value = self.vec[-1]
4911049Sbinkertn@umich.edu        p.display()
4921049Sbinkertn@umich.edu
4931049Sbinkertn@umich.edu
4941049Sbinkertn@umich.edu        p.name = name + '.overflow'
4951049Sbinkertn@umich.edu        p.value = self.over
4961049Sbinkertn@umich.edu        p.display()
4971049Sbinkertn@umich.edu
4981049Sbinkertn@umich.edu        SimpleDist.display(self, name, desc, flags, precision)
4991049Sbinkertn@umich.edu
5001049Sbinkertn@umich.edu    def comparable(self, other):
5011049Sbinkertn@umich.edu        return self.min == other.min and self.max == other.max and \
5021049Sbinkertn@umich.edu               self.bsize == other.bsize and self.size == other.size
5031049Sbinkertn@umich.edu
5041049Sbinkertn@umich.edu    def __eq__(self, other):
5051049Sbinkertn@umich.edu        return self.sums == other.sums and self.squares == other.squares and \
5061049Sbinkertn@umich.edu               self.samples == other.samples
5071049Sbinkertn@umich.edu
5081049Sbinkertn@umich.edu    def __isub__(self, other):
5091049Sbinkertn@umich.edu        self.sums -= other.sums
5101049Sbinkertn@umich.edu        self.squares -= other.squares
5111049Sbinkertn@umich.edu        self.samples -= other.samples
5121049Sbinkertn@umich.edu
5131049Sbinkertn@umich.edu        if other.samples:
5141049Sbinkertn@umich.edu            self.minval = min(self.minval, other.minval)
5151049Sbinkertn@umich.edu            self.maxval = max(self.maxval, other.maxval)
5161049Sbinkertn@umich.edu            self.under -= under
5171049Sbinkertn@umich.edu            self.vec = map(lambda x,y: x - y, self.vec, other.vec)
5181049Sbinkertn@umich.edu            self.over -= over
5191049Sbinkertn@umich.edu        return self
5201049Sbinkertn@umich.edu
5211049Sbinkertn@umich.edu    def __iadd__(self, other):
5221049Sbinkertn@umich.edu        if not self.samples and other.samples:
5231049Sbinkertn@umich.edu            self = other
5241049Sbinkertn@umich.edu            return self
5251049Sbinkertn@umich.edu
5261049Sbinkertn@umich.edu        self.sums += other.sums
5271049Sbinkertn@umich.edu        self.squares += other.squares
5281049Sbinkertn@umich.edu        self.samples += other.samples
5291049Sbinkertn@umich.edu
5301049Sbinkertn@umich.edu        if other.samples:
5311049Sbinkertn@umich.edu            self.minval = min(self.minval, other.minval)
5321049Sbinkertn@umich.edu            self.maxval = max(self.maxval, other.maxval)
5331049Sbinkertn@umich.edu            self.under += other.under
5341049Sbinkertn@umich.edu            self.vec = map(lambda x,y: x + y, self.vec, other.vec)
5351049Sbinkertn@umich.edu            self.over += other.over
5361049Sbinkertn@umich.edu        return self
5371049Sbinkertn@umich.edu
5381049Sbinkertn@umich.edu    def __itruediv__(self, other):
5391049Sbinkertn@umich.edu        if not other:
5401049Sbinkertn@umich.edu            return self
5411049Sbinkertn@umich.edu        self.sums /= other
5421049Sbinkertn@umich.edu        self.squares /= other
5431049Sbinkertn@umich.edu        self.samples /= other
5441049Sbinkertn@umich.edu
5451049Sbinkertn@umich.edu        if self.samples:
5461049Sbinkertn@umich.edu            self.under /= other
5471049Sbinkertn@umich.edu            for i in xrange(len(self.vec)):
5481049Sbinkertn@umich.edu                self.vec[i] /= other
5491049Sbinkertn@umich.edu            self.over /= other
5501049Sbinkertn@umich.edu        return self
5511049Sbinkertn@umich.edu
5521049Sbinkertn@umich.educlass Dist(Statistic):
5531049Sbinkertn@umich.edu    def getValue(self):
5541049Sbinkertn@umich.edu        return 0.0
5551049Sbinkertn@umich.edu
5561049Sbinkertn@umich.edu    def display(self):
5571049Sbinkertn@umich.edu        import display
5581049Sbinkertn@umich.edu        if not display.all and not (self.flags & flags.printable):
5591049Sbinkertn@umich.edu            return
5601049Sbinkertn@umich.edu
5611049Sbinkertn@umich.edu        self.dist.display(self.name, self.desc, self.flags, self.precision)
5621049Sbinkertn@umich.edu
5631049Sbinkertn@umich.edu    def comparable(self, other):
5641049Sbinkertn@umich.edu        return self.name == other.name and \
5651049Sbinkertn@umich.edu               self.dist.compareable(other.dist)
5661049Sbinkertn@umich.edu
5671049Sbinkertn@umich.edu    def __eq__(self, other):
5681049Sbinkertn@umich.edu        return self.dist == other.dist
5691049Sbinkertn@umich.edu
5701049Sbinkertn@umich.edu    def __isub__(self, other):
5711049Sbinkertn@umich.edu        self.dist -= other.dist
5721049Sbinkertn@umich.edu        return self
5731049Sbinkertn@umich.edu
5741049Sbinkertn@umich.edu    def __iadd__(self, other):
5751049Sbinkertn@umich.edu        self.dist += other.dist
5761049Sbinkertn@umich.edu        return self
5771049Sbinkertn@umich.edu
5781049Sbinkertn@umich.edu    def __itruediv__(self, other):
5791049Sbinkertn@umich.edu        if not other:
5801049Sbinkertn@umich.edu            return self
5811049Sbinkertn@umich.edu        self.dist /= other
5821049Sbinkertn@umich.edu        return self
5831049Sbinkertn@umich.edu
5841049Sbinkertn@umich.educlass VectorDist(Statistic):
5851049Sbinkertn@umich.edu    def getValue(self):
5861049Sbinkertn@umich.edu        return 0.0
5871049Sbinkertn@umich.edu
5881049Sbinkertn@umich.edu    def display(self):
5891049Sbinkertn@umich.edu        import display
5901049Sbinkertn@umich.edu        if not display.all and not (self.flags & flags.printable):
5911049Sbinkertn@umich.edu            return
5921049Sbinkertn@umich.edu
5931049Sbinkertn@umich.edu        if isinstance(self.dist, SimpleDist):
5941049Sbinkertn@umich.edu            return
5951049Sbinkertn@umich.edu
5961049Sbinkertn@umich.edu        for dist,sn,sd,i in map(None, self.dist, self.subnames, self.subdescs,
5971049Sbinkertn@umich.edu                                range(len(self.dist))):
5981049Sbinkertn@umich.edu            if len(sn) > 0:
5991049Sbinkertn@umich.edu                name = '%s.%s' % (self.name, sn)
6001049Sbinkertn@umich.edu            else:
6011049Sbinkertn@umich.edu                name = '%s[%d]' % (self.name, i)
6021049Sbinkertn@umich.edu
6031049Sbinkertn@umich.edu            if len(sd) > 0:
6041049Sbinkertn@umich.edu                desc = sd
6051049Sbinkertn@umich.edu            else:
6061049Sbinkertn@umich.edu                desc = self.desc
6071049Sbinkertn@umich.edu
6081049Sbinkertn@umich.edu            dist.display(name, desc, self.flags, self.precision)
6091049Sbinkertn@umich.edu
6101049Sbinkertn@umich.edu        if (self.flags & flags.total) or 1:
6111049Sbinkertn@umich.edu            if isinstance(self.dist[0], SimpleDist):
6121049Sbinkertn@umich.edu                disttotal = SimpleDist( \
6131049Sbinkertn@umich.edu                    reduce(sums, [d.sums for d in self.dist]),
6141049Sbinkertn@umich.edu                    reduce(sums, [d.squares for d in self.dist]),
6151049Sbinkertn@umich.edu                    reduce(sums, [d.samples for d in self.dist]))
6161049Sbinkertn@umich.edu            else:
6171049Sbinkertn@umich.edu                disttotal = FullDist( \
6181049Sbinkertn@umich.edu                    reduce(sums, [d.sums for d in self.dist]),
6191049Sbinkertn@umich.edu                    reduce(sums, [d.squares for d in self.dist]),
6201049Sbinkertn@umich.edu                    reduce(sums, [d.samples for d in self.dist]),
6211049Sbinkertn@umich.edu                    min([d.minval for d in self.dist]),
6221049Sbinkertn@umich.edu                    max([d.maxval for d in self.dist]),
6231049Sbinkertn@umich.edu                    reduce(sums, [d.under for d in self.dist]),
6241049Sbinkertn@umich.edu                    reduce(sums, [d.vec for d in self.dist]),
6251049Sbinkertn@umich.edu                    reduce(sums, [d.over for d in self.dist]),
6261049Sbinkertn@umich.edu                    dist[0].min,
6271049Sbinkertn@umich.edu                    dist[0].max,
6281049Sbinkertn@umich.edu                    dist[0].bsize,
6291049Sbinkertn@umich.edu                    dist[0].size)
6301049Sbinkertn@umich.edu
6311049Sbinkertn@umich.edu            name = '%s.total' % (self.name)
6321049Sbinkertn@umich.edu            desc = self.desc
6331049Sbinkertn@umich.edu            disttotal.display(name, desc, self.flags, self.precision)
6341049Sbinkertn@umich.edu
6351049Sbinkertn@umich.edu    def comparable(self, other):
6361049Sbinkertn@umich.edu        return self.name == other.name and \
6371049Sbinkertn@umich.edu               alltrue(map(lambda x, y : x.comparable(y),
6381049Sbinkertn@umich.edu                           self.dist,
6391049Sbinkertn@umich.edu                           other.dist))
6401049Sbinkertn@umich.edu
6411049Sbinkertn@umich.edu    def __eq__(self, other):
6421049Sbinkertn@umich.edu        return alltrue(map(lambda x, y : x == y, self.dist, other.dist))
6431049Sbinkertn@umich.edu
6441049Sbinkertn@umich.edu    def __isub__(self, other):
6451049Sbinkertn@umich.edu        if issequence(self.dist) and issequence(other.dist):
6461049Sbinkertn@umich.edu            for sd,od in zip(self.dist, other.dist):
6471049Sbinkertn@umich.edu                sd -= od
6481049Sbinkertn@umich.edu        else:
6491049Sbinkertn@umich.edu            self.dist -= other.dist
6501049Sbinkertn@umich.edu        return self
6511049Sbinkertn@umich.edu
6521049Sbinkertn@umich.edu    def __iadd__(self, other):
6531049Sbinkertn@umich.edu        if issequence(self.dist) and issequence(other.dist):
6541049Sbinkertn@umich.edu            for sd,od in zip(self.dist, other.dist):
6551049Sbinkertn@umich.edu                sd += od
6561049Sbinkertn@umich.edu        else:
6571049Sbinkertn@umich.edu            self.dist += other.dist
6581049Sbinkertn@umich.edu        return self
6591049Sbinkertn@umich.edu
6601049Sbinkertn@umich.edu    def __itruediv__(self, other):
6611049Sbinkertn@umich.edu        if not other:
6621049Sbinkertn@umich.edu            return self
6631049Sbinkertn@umich.edu        if issequence(self.dist):
6641049Sbinkertn@umich.edu            for dist in self.dist:
6651049Sbinkertn@umich.edu                dist /= other
6661049Sbinkertn@umich.edu        else:
6671049Sbinkertn@umich.edu            self.dist /= other
6681049Sbinkertn@umich.edu        return self
6691049Sbinkertn@umich.edu
6701049Sbinkertn@umich.educlass Vector2d(Statistic):
6711049Sbinkertn@umich.edu    def getValue(self):
6721049Sbinkertn@umich.edu        return 0.0
6731049Sbinkertn@umich.edu
6741049Sbinkertn@umich.edu    def display(self):
6751049Sbinkertn@umich.edu        import display
6761049Sbinkertn@umich.edu        if not display.all and not (self.flags & flags.printable):
6771049Sbinkertn@umich.edu            return
6781049Sbinkertn@umich.edu
6791049Sbinkertn@umich.edu        d = display.VectorDisplay()
6801049Sbinkertn@umich.edu        d.__dict__.update(self.__dict__)
6811049Sbinkertn@umich.edu
6821049Sbinkertn@umich.edu        if self.__dict__.has_key('ysubnames'):
6831049Sbinkertn@umich.edu            ysubnames = list(self.ysubnames)
6841049Sbinkertn@umich.edu            slack = self.x - len(ysubnames)
6851049Sbinkertn@umich.edu            if slack > 0:
6861049Sbinkertn@umich.edu                ysubnames.extend(['']*slack)
6871049Sbinkertn@umich.edu        else:
6881049Sbinkertn@umich.edu            ysubnames = range(self.x)
6891049Sbinkertn@umich.edu
6901049Sbinkertn@umich.edu        for x,sname in enumerate(ysubnames):
6911049Sbinkertn@umich.edu            o = x * self.y
6921049Sbinkertn@umich.edu            d.value = self.value[o:o+self.y]
6931049Sbinkertn@umich.edu            d.name = '%s[%s]' % (self.name, sname)
6941049Sbinkertn@umich.edu            d.display()
6951049Sbinkertn@umich.edu
6961049Sbinkertn@umich.edu        if self.flags & flags.total:
6971049Sbinkertn@umich.edu            d.value = []
6981049Sbinkertn@umich.edu            for y in range(self.y):
6991049Sbinkertn@umich.edu                xtot = 0.0
7001049Sbinkertn@umich.edu                for x in range(self.x):
7011049Sbinkertn@umich.edu                    xtot += self.value[y + x * self.x]
7021049Sbinkertn@umich.edu                d.value.append(xtot)
7031049Sbinkertn@umich.edu
7041049Sbinkertn@umich.edu            d.name = self.name + '.total'
7051049Sbinkertn@umich.edu            d.display()
7061049Sbinkertn@umich.edu
7071049Sbinkertn@umich.edu    def comparable(self, other):
7081049Sbinkertn@umich.edu        return self.name == other.name and self.x == other.x and \
7091049Sbinkertn@umich.edu               self.y == other.y
7101049Sbinkertn@umich.edu
7111049Sbinkertn@umich.edu    def __eq__(self, other):
7121049Sbinkertn@umich.edu        return True
7131049Sbinkertn@umich.edu
7141049Sbinkertn@umich.edu    def __isub__(self, other):
7151049Sbinkertn@umich.edu        return self
7161049Sbinkertn@umich.edu
7171049Sbinkertn@umich.edu    def __iadd__(self, other):
7181049Sbinkertn@umich.edu        return self
7191049Sbinkertn@umich.edu
7201049Sbinkertn@umich.edu    def __itruediv__(self, other):
7211049Sbinkertn@umich.edu        if not other:
7221049Sbinkertn@umich.edu            return self
7231049Sbinkertn@umich.edu        return self
7241049Sbinkertn@umich.edu
7251049Sbinkertn@umich.edudef NewStat(data):
7261049Sbinkertn@umich.edu    stat = None
7271049Sbinkertn@umich.edu    if data.type == 'SCALAR':
7281049Sbinkertn@umich.edu        stat = Scalar(data)
7291049Sbinkertn@umich.edu    elif data.type == 'VECTOR':
7301049Sbinkertn@umich.edu        stat = Vector(data)
7311049Sbinkertn@umich.edu    elif data.type == 'DIST':
7321049Sbinkertn@umich.edu        stat = Dist(data)
7331049Sbinkertn@umich.edu    elif data.type == 'VECTORDIST':
7341049Sbinkertn@umich.edu        stat = VectorDist(data)
7351049Sbinkertn@umich.edu    elif data.type == 'VECTOR2D':
7361049Sbinkertn@umich.edu        stat = Vector2d(data)
7371049Sbinkertn@umich.edu    elif data.type == 'FORMULA':
7381049Sbinkertn@umich.edu        stat = Formula(data)
7391049Sbinkertn@umich.edu
7401049Sbinkertn@umich.edu    return stat
7411049Sbinkertn@umich.edu
742