NeoPZ
stats.py
Go to the documentation of this file.
1 #! /usr/bin/env python2.7
2 
3 # import libraries
4 import sys
5 import os.path
6 import resource
7 import getopt
8 import math #sqrt
9 
10 # read a RDT file into a dictionary
11 # the dictionary is indexed by the column header
12 # and the list of values at the column
13 def read(filename):
14  # check if the file exists
15  if not os.path.isfile(filename):
16  raise rdt_error(filename + 'is not a valid file.')
17  d={}
18  with open(filename) as f:
19  # process the first row to setup headers
20  hlist = f.readline().split(',')
21  hi=1
22  for h in hlist:
23  header = h.strip()
24  d[hi] = (header,[])
25  hi = hi + 1
26  # process the remaining rows
27  for line in f:
28  vlist = line.split(',')
29  first = vlist[1]
30  if first.strip() != "#":
31  hi = 1
32  for v in vlist:
33  d[hi][1].append(float(v.strip()))
34  hi = hi + 1
35  return d
36 # find the column by field and return
37 # the list of values
38 def get_column_values(rdt_d, field):
39  for k, v in rdt_d.iteritems():
40  if v[0] == field:
41  return v[1]
42  return []
43 # calculates the average of the values
44 # in the vlist
45 def average(vlist):
46  sz = len(vlist)
47  if sz == 0:
48  return 0.0
49  else:
50  s=reduce(lambda x, y : float(x) + float(y), vlist, 0.0)
51  return s/sz
52 # calculates the variance of the values
53 # in the vlist
54 def variance(vlist) :
55  size=len(vlist)
56  if size == 1 or size == 0:
57  raise StatsError("Cannot calculate variance for a sample of size "+str(size))
58  sum1=reduce(lambda x,y : float(x)+float(y), vlist, 0.0)
59  sum2=reduce(lambda x,y : float(x)+(float(y)*float(y)), vlist, 0.0)
60  v = (sum2-(pow(sum1,2)/float(size)))/float(size-1)
61  if v < 0.0 :
62  if v > -1.e-15 : # tolerate floating point rounding errors
63  return 0.0
64  else :
65  raise StatsError("Something went wrong while calculating the variance (result < 0.0)")
66  return v
67 # calculate the standard deviation
68 # using the function variance of the values
69 # int the vlist
70 def stdev(vlist) :
71  var = variance(vlist)
72  return math.sqrt(var)
73 # main
74 if __name__ == "__main__":
75  # process arguments
76  try:
77  opts, extra_args = getopt.getopt(sys.argv[1:], 'f:')
78  except:
79  error(str(e), 1)
80  for f, v in opts:
81  if f == '-f':
82  filename = v
83  stats_res = read(filename)
84  elapsed_lst = get_column_values(stats_res, "ELAPSED")
85  self_lst = get_column_values(stats_res, "SELF_RU_UTIME")
86  print '{0:7s} \t {1:7s} \t {2:7s} \t {3:7s}'.format('User Time', 'User Error', 'Wall Time', 'Wall Error')
87  elapsed_avg = average(elapsed_lst)
88  self_avg = average(self_lst)
89  elapsed_err = stdev(elapsed_lst)
90  self_err = stdev(self_lst)
91  print '{0:5.2f} \t {1:5.2f} \t {2:5.2f} \t {3:5.2f}'.format(self_avg, self_err, elapsed_avg, elapsed_err)
def stdev(vlist)
Definition: stats.py:70
def get_column_values(rdt_d, field)
Definition: stats.py:38
void error(char *string)
Definition: testShape.cc:7
def read(filename)
Definition: stats.py:13
def average(vlist)
Definition: stats.py:45
def variance(vlist)
Definition: stats.py:54
TPZFlopCounter pow(const TPZFlopCounter &orig, const TPZFlopCounter &xp)
Returns the power and increments the counter of the power.
Definition: pzreal.h:487