Advertisement
froleyks

experiment.py

Oct 20th, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.08 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import pandas as pd
  3. import ntpath
  4. import sys
  5. import os
  6. from subprocess import getstatusoutput as cmd
  7. from comment_parser import comment_parser
  8.  
  9.  
  10. def getCurrentOpt(sourceFiles):
  11.     comments = []
  12.     for file in sourceFiles:
  13.         print(file)
  14.         comments += comment_parser.extract_comments(file)
  15.     comments = [c for c in comments if c.text().strip()[0] == 'O']
  16.  
  17.     currentMax = 0
  18.     currentOpt = "none"
  19.     for c in comments:
  20.         comString = c.text().strip()
  21.         # print(comString)
  22.         strNum = comString.partition(' ')[0][1:].strip()
  23.         if strNum != "":
  24.             optNum = int(comString.partition(' ')[0][1:].strip())
  25.             if optNum > currentMax:
  26.                 currentMax = optNum
  27.                 currentOpt = str(
  28.                     c.line_number()) + ": " + comString.partition(' ')[1:][1]
  29.  
  30.     return currentMax, currentOpt
  31.  
  32.  
  33. def runExperiment(executableName):
  34.     return cmd("./" + executableName)[1]
  35.  
  36.  
  37. def backupResult(currentOpt):
  38.     tempResultFile = currentOpt[0] + ".txt"
  39.     backupResultFile = sys.argv[2] + "/" + str(currentOpt[1]) + ".res"
  40.     os.rename(tempResultFile, backupResultFile)
  41.  
  42.     f = open(backupResultFile, "a")
  43.     f.write("; " + currentOpt[2])
  44.     f.close()
  45.  
  46.     f = open(sys.argv[2] + "/" + "incumbent", "w")
  47.     f.write(str(currentOpt[1]))
  48.     f.close()
  49.  
  50.  
  51. def readData():
  52.     dfs = []
  53.     dir = os.path.dirname(__file__)  #<-- absolute dir the script is in
  54.     dir = os.path.join(dir, sys.argv[2])
  55.     for file in os.listdir(dir):
  56.         if file.endswith(".res"):
  57.             df = pd.read_fwf(os.path.join(dir, file), comment=';')
  58.             agregated = df.groupby("sec").mean()
  59.             agregated = agregated.drop(columns=["#it", "deg", "n_start", "n_end"])
  60.             # agregated.columns = [file + "_" + col for col in agregated.columns]
  61.             print(agregated)
  62.             dfs.append([agregated, file])
  63.     return dfs
  64.  
  65.  
  66. def plot():
  67.     dfs = readData()
  68.     agregated, file = dfs[0]
  69.     print(file)
  70.     # print(agregated)
  71.  
  72.     for objective in dfs[0][0].columns[:-1]:
  73.         print("obj: " + objective)
  74.  
  75.         currentPlot = dfs[0][0][[objective]]
  76.         currentPlot.columns = [dfs[0][1] for col in currentPlot.columns]
  77.         # print(currentPlot)
  78.         for agregated, file in dfs[1:]:
  79.             agregated = agregated[[objective]]
  80.             agregated.columns = [file for col in agregated.columns]
  81.             # print(agregated)
  82.             currentPlot = currentPlot.join(agregated, on="sec")
  83.         print(currentPlot)
  84.         currentPlot.plot()
  85.         plt.title(sys.argv[2] + "\n" + objective)
  86.         plt.xlabel("section")
  87.         plt.ylabel("time    [ns]")
  88.         plt.show()
  89.  
  90.  
  91. if __name__ == '__main__':
  92.     recompute = sys.argv[1]
  93.     if recompute != "False":
  94.         executable = sys.argv[3]
  95.         sourceFiles = sys.argv[4:]
  96.  
  97.         currentOpt = getCurrentOpt(sourceFiles)
  98.         currentOptimization = [executable, currentOpt[0], currentOpt[1]]
  99.         experimentResult = runExperiment(executable)
  100.         print(experimentResult)
  101.         print(currentOptimization)
  102.         backupResult(currentOptimization)
  103.     plot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement