1# Process the test results
2# Test status (like passed, or failed with error code)
3
4import argparse
5import re
6import TestScripts.NewParser as parse
7import TestScripts.CodeGen
8from collections import deque
9import os.path
10import numpy as np
11import pandas as pd
12import statsmodels.api as sm
13import statsmodels.formula.api as smf
14import csv
15import TestScripts.Deprecate as d
16
17def findItem(root,path):
18        """ Find a node in a tree
19
20        Args:
21          path (list) : A list of node ID
22            This list is describing a path in the tree.
23            By starting from the root and following this path,
24            we can find the node in the tree.
25        Raises:
26          Nothing
27        Returns:
28          TreeItem : A node
29        """
30        # The list is converted into a queue.
31        q = deque(path)
32        q.popleft()
33        c = root
34        while q:
35            n = q.popleft()
36            # We get the children based on its ID and continue
37            c = c[n-1]
38        return(c)
39
40
41
42NORMAL = 1
43INTEST = 2
44TESTPARAM = 3
45
46def joinit(iterable, delimiter):
47    it = iter(iterable)
48    yield next(it)
49    for x in it:
50        yield delimiter
51        yield x
52
53def formatProd(a,b):
54  if a == "Intercept":
55     return("%.3f" % b)
56  return("%s * %.3f" % (a,b))
57
58def log2(x):
59  return(np.log2(x))
60
61def log(x):
62  return(np.log(x))
63
64def summaryBenchmark(resultPath,elem,path):
65   regressionPath=os.path.join(os.path.dirname(path),"regression.csv")
66
67   if os.path.isfile(path):
68      print("  Generating %s" % regressionPath)
69      full=pd.read_csv(path,dtype={'OLDID': str} ,keep_default_na = False)
70      #print(full)
71
72      csvheaders = []
73      with open(os.path.join(resultPath,'currentConfig.csv'), 'r') as f:
74           reader = csv.reader(f)
75           csvheaders = next(reader, None)
76
77      groupList = list(set(elem.params.full) - set(elem.params.summary))
78      #grouped=full.groupby(list(elem.params.summary) + ['ID','CATEGORY']).max()
79      #grouped.reset_index(level=grouped.index.names, inplace=True)
80      #print(grouped)
81      #print(grouped.columns)
82
83
84      def reg(d):
85       m=d["CYCLES"].max()
86       #print( elem.params.formula)
87
88       results = smf.ols('CYCLES ~ ' + elem.params.formula, data=d).fit()
89
90       f=joinit([formatProd(a,b) for (a,b) in zip(results.params.index,results.params.values)]," + ")
91       f="".join(f)
92       f = re.sub(r':','*',f)
93       #print(results.summary())
94       return(pd.Series({'Regression':"%s" % f,'MAX' : m,'MAXREGCOEF' : results.params.values[-1]}))
95
96      regList = ['ID','OLDID','CATEGORY','TESTNAME','NAME'] + csvheaders + groupList
97
98      regression=full.groupby(regList).apply(reg)
99      regression.reset_index(level=regression.index.names, inplace=True)
100      renamingDict = { a : b for (a,b) in zip(elem.params.full,elem.params.paramNames)}
101      regression = regression.rename(columns=renamingDict)
102      regression.to_csv(regressionPath,index=False,quoting=csv.QUOTE_NONNUMERIC)
103
104
105def extractBenchmarks(resultPath,benchmark,elem):
106  if not elem.data["deprecated"]:
107     if elem.params:
108         benchPath = os.path.join(benchmark,elem.fullPath(),"fullBenchmark.csv")
109         print("Processing %s" % benchPath)
110         summaryBenchmark(resultPath,elem,benchPath)
111
112     for c in elem.children:
113       extractBenchmarks(resultPath,benchmark,c)
114
115
116
117parser = argparse.ArgumentParser(description='Generate summary benchmarks')
118
119parser.add_argument('-f', nargs='?',type = str, default="Output.pickle", help="Test description cache")
120parser.add_argument('-b', nargs='?',type = str, default="FullBenchmark", help="Full Benchmark dir path")
121# Needed to find the currentConfig.csv and know the headers
122parser.add_argument('-r', nargs='?',type = str, default=None, help="Result file path")
123
124parser.add_argument('others', nargs=argparse.REMAINDER)
125
126args = parser.parse_args()
127
128if args.f is not None:
129    #p = parse.Parser()
130    # Parse the test description file
131    #root = p.parse(args.f)
132    root=parse.loadRoot(args.f)
133    d.deprecate(root,args.others)
134    resultPath=os.path.dirname(args.r)
135    extractBenchmarks(resultPath,args.b,root)
136
137else:
138    parser.print_help()