00001 from pgmagick import Image, Color, Geometry, DrawableArc, DrawableLine, DrawableList, DrawableFillColor
00002 from LogReader import LogReader
00003 import pickle
00004 import math
00005
00006
00007 class LogAnalyzer:
00008 def __init__(self):
00009 self.rdrLog = LogReader()
00010 self.arrColors = ["white", "red", "blue", "yellow", "black"]
00011
00012 def analyzeLog(self, strPath):
00013 log = self.rdrLog.loadLog(strPath)
00014 data = log.getOwlData()["task-tree"]
00015 tti = log.getOwlData()["task-tree-individuals"]
00016
00017
00018
00019
00020
00021 data = self.correctTime(data)
00022
00023 imgTaskPie = Image(Geometry(700, 700), Color("white"))
00024
00025 imgTaskPie.strokeColor("#000000")
00026 imgTaskPie.strokeWidth(2.5)
00027 imgTaskPie.fillColor("transparent")
00028
00029 self.drawTaskPie(imgTaskPie, data, -1, -1, 5)
00030 imgTaskPie.write("out.png")
00031
00032 toTasks = self.timelyOrderedTasks(data)
00033
00034 dicClassTimes = {}
00035 for dicTask in toTasks:
00036 owlTask = tti[dicTask["name"]]
00037 strType = owlTask.type()
00038
00039 if not strType in dicClassTimes:
00040 dicClassTimes[strType] = int(dicTask["time"])
00041 else:
00042 dicClassTimes[strType] += int(dicTask["time"])
00043
00044 nEarliestTS = -1
00045 nLatestTS = -1
00046
00047 for dicTask in toTasks:
00048 owlTask = tti[dicTask["name"]]
00049 TS = owlTask.timeSpan()
00050
00051 if TS:
00052 if nEarliestTS == -1 or int(TS[0]) < nEarliestTS:
00053 nEarliestTS = int(TS[0])
00054 if nLatestTS == -1 or int(TS[1]) > nLatestTS:
00055 nLatestTS = int(TS[1])
00056
00057 nOverallTime = nLatestTS - nEarliestTS
00058
00059 with open("classtimes.pkl", "wb") as f:
00060 pickle.dump(dicClassTimes, f, pickle.HIGHEST_PROTOCOL)
00061
00062 print "Longest Task: ", toTasks[len(toTasks) - 1]
00063
00064 for strItem in dicClassTimes:
00065 print strItem, dicClassTimes[strItem]
00066
00067 print ""
00068 if not "MotionPlanning" in dicClassTimes:
00069 print "Picking Up Objects: " + str(dicClassTimes["PickingUpAnObject"])
00070 print "Placing Objects: " + str(dicClassTimes["PuttingDownAnObject"])
00071 print "Path Planning + Motion Execution: " + str(dicClassTimes["ArmMovement"])
00072 print "Navigation: " + str(dicClassTimes["BaseMovement"])
00073 print "Head Movement: " + str(dicClassTimes["HeadMovement"])
00074 print "Perception Queries: " + str(dicClassTimes["UIMAPerception"])
00075 print "Object Identity Resolution + Belief State Updates: " + str(dicClassTimes["PerceivingObjects"] - dicClassTimes["UIMAPerception"])
00076 else:
00077 print "--- General ---"
00078 print "Overall : " + str(nOverallTime)
00079 print "--- High Level ---"
00080 print "Picking Up Objects : " + str(dicClassTimes["PickingUpAnObject"] +
00081 dicClassTimes["CarryingAnObject"] +
00082 dicClassTimes["LiftingAnObject"])
00083 print "Placing Objects : " + str(dicClassTimes["PuttingDownAnObject"])
00084 print "Finding Objects : " + str(dicClassTimes["FindingObjects"])
00085 print "Other Activities : " + str(nOverallTime -
00086 (dicClassTimes["PickingUpAnObject"] +
00087 dicClassTimes["CarryingAnObject"] +
00088 dicClassTimes["LiftingAnObject"] +
00089 dicClassTimes["PuttingDownAnObject"] +
00090 dicClassTimes["FindingObjects"] -
00091 dicClassTimes["UIMAPerception"]))
00092 print "--- Low Level ---"
00093 print "Path Planning : " + str(dicClassTimes["MotionPlanning"])
00094 print "Motion Execution : " + str(dicClassTimes["MotionExecution"])
00095 print "Navigation : " + str(dicClassTimes["BaseMovement"])
00096 print "Head Movement : " + str(dicClassTimes["HeadMovement"])
00097 print "Perception Queries : " + str(dicClassTimes["UIMAPerception"])
00098 print "Object Identity Resolution : " + str(dicClassTimes["ObjectIdentityResolution"])
00099 print "Belief State Updates : " + str(dicClassTimes["BeliefStateUpdate"])
00100
00101 def timelyOrderedTasks(self, data):
00102 dicLinear = self.linearizeTaskTree(data)
00103 arrItems = []
00104
00105 for strItem in dicLinear:
00106 arrItems.append({"name": strItem,
00107 "time": dicLinear[strItem]})
00108
00109 return sorted(arrItems, key=lambda item: item["time"])
00110
00111 def linearizeTaskTree(self, tree):
00112 dicLinear = {}
00113
00114 for strBranch in tree:
00115 dicLinear[strBranch] = tree[strBranch]["time"]
00116 dicSub = self.linearizeTaskTree(tree[strBranch]["children"])
00117 dicLinear = dict(dicLinear, **dicSub)
00118
00119 return dicLinear
00120
00121 def correctTime(self, data):
00122 for strBranchName in data:
00123 data[strBranchName]["children"] = self.correctTime(data[strBranchName]["children"])
00124
00125 nTimeSum = 0
00126 for strChild in data[strBranchName]["children"]:
00127 nTimeSum += data[strBranchName]["children"][strChild]["time"]
00128
00129 if data[strBranchName]["time"] < nTimeSum:
00130 data[strBranchName]["time"] = nTimeSum
00131
00132 return data
00133
00134 def drawTaskPie(self, imgPie, dicTaskTree, globalTimespan = -1, parentTimespan = -1, radiusDelta = 10, radiusInner = 0, angleStart = 0, angleEnd = 360):
00135 if globalTimespan == -1:
00136 globalTimespan = 0
00137 for strBranchName in dicTaskTree:
00138 globalTimespan += dicTaskTree[strBranchName]["time"]
00139
00140 if parentTimespan == -1:
00141 parentTimespan = 0
00142 for strBranchName in dicTaskTree:
00143 parentTimespan += dicTaskTree[strBranchName]["time"]
00144
00145 if parentTimespan > 0:
00146 nSegments = len(dicTaskTree)
00147
00148 radiusOuter = radiusInner + radiusDelta
00149
00150 nCenterX = imgPie.columns() / 2
00151 nCenterY = imgPie.rows() / 2
00152
00153 nStartXOuter = nCenterX - radiusOuter
00154 nStartYOuter = nCenterY - radiusOuter
00155 nEndXOuter = nCenterX + radiusOuter
00156 nEndYOuter = nCenterY + radiusOuter
00157
00158 nStartXInner = nCenterX - radiusInner
00159 nStartYInner = nCenterY - radiusInner
00160 nEndXInner = nCenterX + radiusInner
00161 nEndYInner = nCenterY + radiusInner
00162
00163 dAngleOffset = 0
00164
00165 for strBranchName in dicTaskTree:
00166 dAngleWidth = float(dicTaskTree[strBranchName]["time"]) / float(parentTimespan) * (angleEnd - angleStart)
00167
00168 if dAngleWidth > 0:
00169 dStartingAngle = angleStart + dAngleOffset
00170 dEndingAngle = dStartingAngle + dAngleWidth
00171 dAngleOffset += dAngleWidth
00172
00173 if "children" in dicTaskTree[strBranchName]:
00174 if len(dicTaskTree[strBranchName]["children"]) > 0:
00175 self.drawTaskPie(imgPie, dicTaskTree[strBranchName]["children"], globalTimespan, dicTaskTree[strBranchName]["time"], radiusDelta, radiusOuter, dStartingAngle, dEndingAngle)
00176
00177 dTimeSpanDegree = float(dicTaskTree[strBranchName]["time"]) / float(globalTimespan)
00178 imgPie.strokeColor(Color(int(255 * dTimeSpanDegree), 0, int(255 * (1.0 - dTimeSpanDegree))))
00179
00180 lstDrawables = DrawableList()
00181 lstDrawables.append(DrawableLine(nCenterX + radiusInner * math.cos(math.radians(dStartingAngle)),
00182 nCenterY + radiusInner * math.sin(math.radians(dStartingAngle)),
00183 nCenterX + radiusOuter * math.cos(math.radians(dStartingAngle)),
00184 nCenterY + radiusOuter * math.sin(math.radians(dStartingAngle))))
00185 lstDrawables.append(DrawableArc(nStartXOuter, nStartYOuter, nEndXOuter, nEndYOuter, dStartingAngle, dEndingAngle))
00186 lstDrawables.append(DrawableLine(nCenterX + radiusInner * math.cos(math.radians(dEndingAngle)),
00187 nCenterY + radiusInner * math.sin(math.radians(dEndingAngle)),
00188 nCenterX + radiusOuter * math.cos(math.radians(dEndingAngle)),
00189 nCenterY + radiusOuter * math.sin(math.radians(dEndingAngle))))
00190 lstDrawables.append(DrawableArc(nStartXInner, nStartYInner, nEndXInner, nEndYInner, dStartingAngle, dEndingAngle))
00191
00192 imgPie.draw(lstDrawables)