def getLength(strPath): rdrLog = LogReader() log = rdrLog.loadLog(sys.argv[1]) owl = log.getOwlData() meta = owl["metadata"] tti = owl["task-tree-individuals"] root = tti[meta.subActions()[0]] return root.time()
def getLength(strPath): rdrLog = LogReader() log = rdrLog.loadLog(sys.argv[1]) owl = log.getOwlData() meta = owl["metadata"] tti = owl["task-tree-individuals"] root = tti[meta.subActions()[0]] return root.time()
class TimeConfidence: def __init__(self): self.rdrLog = LogReader() def confidence(self, strPath): dataOwl = None log = self.rdrLog.loadLog(strPath) dataOwl = log.getOwlData() self.tti = dataOwl["task-tree-individuals"] owlMeta = dataOwl["metadata"] owlAnnot = dataOwl["annotation"] if owlMeta: toplevel_nodes = owlMeta.subActions() else: print "No meta data in file!" self.timeSpans = {} self.findTimeSpansPerTask(toplevel_nodes) for ctx in self.timeSpans: print ctx, mean_confidence_interval(self.timeSpans[ctx]) def findTimeSpansPerTask(self, nodes): for node in nodes: owlNode = self.tti[node] ctx = owlNode.taskContext() if not ctx in self.timeSpans: self.timeSpans[ctx] = [] self.timeSpans[ctx].append(owlNode.time()) self.findTimeSpansPerTask(owlNode.subActions())
class TimeConfidence: def __init__(self): self.rdrLog = LogReader() def confidence(self, strPath): dataOwl = None log = self.rdrLog.loadLog(strPath) dataOwl = log.getOwlData() self.tti = dataOwl["task-tree-individuals"] owlMeta = dataOwl["metadata"] owlAnnot = dataOwl["annotation"] if owlMeta: toplevel_nodes = owlMeta.subActions() else: print "No meta data in file!" self.timeSpans = {} self.findTimeSpansPerTask(toplevel_nodes) for ctx in self.timeSpans: print ctx, mean_confidence_interval(self.timeSpans[ctx]) def findTimeSpansPerTask(self, nodes): for node in nodes: owlNode = self.tti[node] ctx = owlNode.taskContext() if not ctx in self.timeSpans: self.timeSpans[ctx] = [] self.timeSpans[ctx].append(owlNode.time()) self.findTimeSpansPerTask(owlNode.subActions())
class DataCondenser: def __init__(self): self.rdrLog = LogReader() self.tti = {} def condenseData(self, strOutputFile, arrSourceDirectories): dicToplevelNodes = [] for strSourceDirectory in arrSourceDirectories: logData = self.rdrLog.loadLog(strSourceDirectory) owlData = logData.getOwlData() self.tti = dict(self.tti.items() + owlData["task-tree-individuals"].items()) meta = owlData["metadata"] if meta: dicToplevelNodes += meta.subActions() dicResult = {"Toplevel": self.condenseNodes("", dicToplevelNodes)} with open(strOutputFile, "wb") as fOut: json.dump(dicResult, fOut) def condenseNodes(self, strParentNode, arrNodes, nLevel=0): arrTypes = {} arrIndividuals = {} for strNode in arrNodes: owlNode = self.tti[strNode] nodeclass = owlNode.taskContext() if nLevel < 0: ident = "*" else: ident = nodeclass failures = owlNode.failures() failure = "" if len(failures) > 0: failure = self.tti[failures[0]].type() result = self.condenseNodes(strNode, owlNode.subActions(), nLevel + 1) if not ident in arrTypes: arrTypes[ident] = result else: arrTypes[ident] = self.unifyResults(arrTypes[ident], result) arrTypes[ident]["individuals"][strNode] = { "parameters": owlNode.annotatedParameters(True), "parent": strParentNode, "failure": failure, "class": nodeclass } return {"subTypes": arrTypes, "individuals": {}} def unifyResults(self, res1, res2): resparams = {} if len(res1["individuals"]) > 0: resparams = res1["individuals"] if len(res2["individuals"]) > 0: resparams = dict(resparams.items() + res2["individuals"].items()) unified = {"subTypes": {}, "individuals": resparams} for ressub1 in res1["subTypes"]: if ressub1 in res2["subTypes"]: unified["subTypes"][ressub1] = self.unifyResults( res1["subTypes"][ressub1], res2["subTypes"][ressub1]) else: unified["subTypes"][ressub1] = res1["subTypes"][ressub1] for ressub2 in res2["subTypes"]: if not ressub2 in res1["subTypes"]: unified["subTypes"][ressub2] = res2["subTypes"][ressub2] return unified
#!/usr/bin/python from LogReader import LogReader import sys rdrLog = LogReader() log = rdrLog.loadLog(sys.argv[1]) owl = log.getOwlData() tti = owl["task-tree-individuals"] count_eff = 0 count_eff_fail = 0 def hasSubActionUIMAPerceiveFailed(owlIndiv): if owlIndiv.taskContext() == "UIMA-PERCEIVE": if len( owlIndiv.tagAttributeValues("knowrob:perceptionResult", "rdf:resource")) == 0: return True else: subactions = owlIndiv.subActions() for subaction in subactions: if hasSubActionUIMAPerceiveFailed(tti[subaction]): return True return False
class DataExtractor: def __init__(self): self.rdrLog = LogReader() def extractData(self, strPath): log = self.rdrLog.loadLog(strPath) self.tti = log.getOwlData()["task-tree-individuals"] annot = None meta = None for key in self.tti: owlIndiv = self.tti[key] if owlIndiv.type() == "AnnotationInformation": annot = owlIndiv if annot and meta: break elif owlIndiv.type() == "RobotExperiment": meta = owlIndiv if annot and meta: break if annot and meta: params = annot.tagNodeValues("knowrob:annotatedParameterType") param_set = {} for param in params: param_set[param] = "?" strHeadline = "TASK-CONTEXT, RESULT" for param in params: strHeadline += ", " + param print strHeadline toplevelnodes = meta.tagAttributeValues("knowrob:subAction", "rdf:resource") for toplevelnode in toplevelnodes: node_key = toplevelnode.split("#")[1] self.printNode(node_key, param_set) def printNode(self, node_key, param_set): owlIndiv = self.tti[node_key] subactions = owlIndiv.tagAttributeValues("knowrob:subAction", "rdf:resource") for param in param_set: tnv = owlIndiv.tagNodeValues("knowrob:" + param) if len(tnv) > 0: param_set[param] = tnv[0] strLine = "" strLine += owlIndiv.tagNodeValues("knowrob:taskContext")[0] failures = owlIndiv.tagAttributeValues("knowrob:eventFailure", "rdf:resource") if len(failures) > 0: failure = failures[0].split("#")[1] owlFailure = self.tti[failure] strLine += ", " + owlFailure.type() else: strLine += ", Success" for param in param_set: strLine += ", " + param_set[param] for subaction in subactions: self.printNode(subaction.split("#")[1], param_set) print strLine
#!/usr/bin/python import sys from LogReader import LogReader sys.setrecursionlimit(100000) rdrLog = LogReader() log = rdrLog.loadLog(sys.argv[1]) dataOwl = log.getOwlData() def findDepth(indivkey, lvl = 0): indiv = dataOwl["task-tree-individuals"][indivkey] if len(indiv.subActions()) > 0: for sa in indiv.subActions(): findDepth(sa, lvl + 1) else: print lvl findDepth(dataOwl["metadata"].subActions()[0])
class LogAnalyzer: def __init__(self): self.rdrLog = LogReader() self.arrColors = ["white", "red", "blue", "yellow", "black"] def analyzeLog(self, strPath): log = self.rdrLog.loadLog(strPath) #data = log.getOwlData()["task-tree"] tti = log.getOwlData()["task-tree-individuals"] #with open("data.pkl", "wb") as f: # pickle.dump(data, f, pickle.HIGHEST_PROTOCOL) # data = pickle.load(f) #data = self.correctTime(data) #imgTaskPie = Image(Geometry(700, 700), Color("white")) #imgTaskPie.strokeColor("#000000") #imgTaskPie.strokeWidth(2.5) #imgTaskPie.fillColor("transparent") #self.drawTaskPie(imgTaskPie, data, -1, -1, 5) #imgTaskPie.write("out.png") # toTasks = self.timelyOrderedTasks(data) # dicClassTimes = {} # for dicTask in toTasks: # owlTask = tti[dicTask["name"]] # strType = owlTask.type() # if not strType in dicClassTimes: # dicClassTimes[strType] = int(dicTask["time"]) # else: # dicClassTimes[strType] += int(dicTask["time"]) # nEarliestTS = -1 # nLatestTS = -1 # for dicTask in tti:#toTasks: # owlTask = tti[dicTask] # TS = owlTask.timeSpan() # if TS: # if nEarliestTS == -1 or float(TS[0]) < nEarliestTS: # nEarliestTS = float(TS[0]) # if nLatestTS == -1 or float(TS[1]) > nLatestTS: # nLatestTS = float(TS[1]) # nOverallTime = nLatestTS - nEarliestTS fEarliest = -1 fLatest = -1 dicClassTimes = {} for strTask in tti: owlTask = tti[strTask] TS = owlTask.timeSpan() if TS: if owlTask.type() in dicClassTimes: dicClassTimes[owlTask.type()] += (float(TS[1]) - float(TS[0])) else: dicClassTimes[owlTask.type()] = (float(TS[1]) - float(TS[0])) if float(TS[0]) < fEarliest or fEarliest == -1: fEarliest = float(TS[0]) if float(TS[1]) > fLatest or fLatest == -1: fLatest = float(TS[1]) print "Time =", (fLatest - fEarliest) #with open("classtimes.pkl", "wb") as f: # pickle.dump(dicClassTimes, f, pickle.HIGHEST_PROTOCOL) #print "Longest Task: ", toTasks[len(toTasks) - 1] #for strItem in dicClassTimes: #print strItem, dicClassTimes[strItem] for strClass in dicClassTimes: print strClass, " = ", dicClassTimes[strClass] #print "" # if not "MotionPlanning" in dicClassTimes: # print "Picking Up Objects: " + str(dicClassTimes["PickingUpAnObject"]) # print "Placing Objects: " + str(dicClassTimes["PuttingDownAnObject"]) # print "Path Planning + Motion Execution: " + str(dicClassTimes["ArmMovement"]) # print "Navigation: " + str(dicClassTimes["BaseMovement"]) # print "Head Movement: " + str(dicClassTimes["HeadMovement"]) # print "Perception Queries: " + str(dicClassTimes["UIMAPerception"]) # print "Object Identity Resolution + Belief State Updates: " + str(dicClassTimes["PerceivingObjects"] - dicClassTimes["UIMAPerception"]) # else: # # print "--- General ---" # # print "Overall : " + str(nOverallTime) # print "--- High Level ---" # print "Picking Up Objects : " + str(dicClassTimes["PickingUpAnObject"] + # dicClassTimes["CarryingAnObject"] + # dicClassTimes["LiftingAnObject"]) # print "Placing Objects : " + str(dicClassTimes["PuttingDownAnObject"]) # print "Finding Objects : " + str(dicClassTimes["FindingObjects"]) # # print "Other Activities : " + str(nOverallTime - # # (dicClassTimes["PickingUpAnObject"] + # # dicClassTimes["CarryingAnObject"] + # # dicClassTimes["LiftingAnObject"] + # # dicClassTimes["PuttingDownAnObject"] + # # dicClassTimes["FindingObjects"] - # # dicClassTimes["UIMAPerception"])) # print "--- Low Level ---" # print "Path Planning : " + str(dicClassTimes["MotionPlanning"]) # print "Motion Execution : " + str(dicClassTimes["MotionExecution"]) # print "Navigation : " + str(dicClassTimes["BaseMovement"]) # print "Head Movement : " + str(dicClassTimes["HeadMovement"]) # print "Perception Queries : " + str(dicClassTimes["UIMAPerception"]) # print "Object Identity Resolution : " + str(dicClassTimes["ObjectIdentityResolution"]) # print "Belief State Updates : " + str(dicClassTimes["BeliefStateUpdate"]) def timelyOrderedTasks(self, data): dicLinear = self.linearizeTaskTree(data) arrItems = [] for strItem in dicLinear: arrItems.append({"name": strItem, "time": dicLinear[strItem]}) return sorted(arrItems, key=lambda item: item["time"]) def linearizeTaskTree(self, tree): dicLinear = {} for strBranch in tree: dicLinear[strBranch] = tree[strBranch]["time"] dicSub = self.linearizeTaskTree(tree[strBranch]["children"]) dicLinear = dict(dicLinear, **dicSub) return dicLinear def correctTime(self, data): for strBranchName in data: data[strBranchName]["children"] = self.correctTime( data[strBranchName]["children"]) nTimeSum = 0 for strChild in data[strBranchName]["children"]: nTimeSum += data[strBranchName]["children"][strChild]["time"] if data[strBranchName]["time"] < nTimeSum: data[strBranchName]["time"] = nTimeSum return data def drawTaskPie(self, imgPie, dicTaskTree, globalTimespan=-1, parentTimespan=-1, radiusDelta=10, radiusInner=0, angleStart=0, angleEnd=360): if globalTimespan == -1: globalTimespan = 0 for strBranchName in dicTaskTree: globalTimespan += dicTaskTree[strBranchName]["time"] if parentTimespan == -1: parentTimespan = 0 for strBranchName in dicTaskTree: parentTimespan += dicTaskTree[strBranchName]["time"] if parentTimespan > 0: nSegments = len(dicTaskTree) radiusOuter = radiusInner + radiusDelta nCenterX = imgPie.columns() / 2 nCenterY = imgPie.rows() / 2 nStartXOuter = nCenterX - radiusOuter nStartYOuter = nCenterY - radiusOuter nEndXOuter = nCenterX + radiusOuter nEndYOuter = nCenterY + radiusOuter nStartXInner = nCenterX - radiusInner nStartYInner = nCenterY - radiusInner nEndXInner = nCenterX + radiusInner nEndYInner = nCenterY + radiusInner dAngleOffset = 0 for strBranchName in dicTaskTree: dAngleWidth = float( dicTaskTree[strBranchName]["time"]) / float( parentTimespan) * (angleEnd - angleStart) if dAngleWidth > 0: dStartingAngle = angleStart + dAngleOffset dEndingAngle = dStartingAngle + dAngleWidth dAngleOffset += dAngleWidth if "children" in dicTaskTree[strBranchName]: if len(dicTaskTree[strBranchName]["children"]) > 0: self.drawTaskPie( imgPie, dicTaskTree[strBranchName]["children"], globalTimespan, dicTaskTree[strBranchName]["time"], radiusDelta, radiusOuter, dStartingAngle, dEndingAngle) dTimeSpanDegree = float(dicTaskTree[strBranchName] ["time"]) / float(globalTimespan) imgPie.strokeColor( Color(int(255 * dTimeSpanDegree), 0, int(255 * (1.0 - dTimeSpanDegree)))) lstDrawables = DrawableList() lstDrawables.append( DrawableLine( nCenterX + radiusInner * math.cos(math.radians(dStartingAngle)), nCenterY + radiusInner * math.sin(math.radians(dStartingAngle)), nCenterX + radiusOuter * math.cos(math.radians(dStartingAngle)), nCenterY + radiusOuter * math.sin(math.radians(dStartingAngle)))) lstDrawables.append( DrawableArc(nStartXOuter, nStartYOuter, nEndXOuter, nEndYOuter, dStartingAngle, dEndingAngle)) lstDrawables.append( DrawableLine( nCenterX + radiusInner * math.cos(math.radians(dEndingAngle)), nCenterY + radiusInner * math.sin(math.radians(dEndingAngle)), nCenterX + radiusOuter * math.cos(math.radians(dEndingAngle)), nCenterY + radiusOuter * math.sin(math.radians(dEndingAngle)))) lstDrawables.append( DrawableArc(nStartXInner, nStartYInner, nEndXInner, nEndYInner, dStartingAngle, dEndingAngle)) imgPie.draw(lstDrawables)
class LogAnalyzer: def __init__(self): self.rdrLog = LogReader() self.arrColors = ["white", "red", "blue", "yellow", "black"] def analyzeLog(self, strPath): log = self.rdrLog.loadLog(strPath) #data = log.getOwlData()["task-tree"] tti = log.getOwlData()["task-tree-individuals"] #with open("data.pkl", "wb") as f: # pickle.dump(data, f, pickle.HIGHEST_PROTOCOL) # data = pickle.load(f) #data = self.correctTime(data) #imgTaskPie = Image(Geometry(700, 700), Color("white")) #imgTaskPie.strokeColor("#000000") #imgTaskPie.strokeWidth(2.5) #imgTaskPie.fillColor("transparent") #self.drawTaskPie(imgTaskPie, data, -1, -1, 5) #imgTaskPie.write("out.png") # toTasks = self.timelyOrderedTasks(data) # dicClassTimes = {} # for dicTask in toTasks: # owlTask = tti[dicTask["name"]] # strType = owlTask.type() # if not strType in dicClassTimes: # dicClassTimes[strType] = int(dicTask["time"]) # else: # dicClassTimes[strType] += int(dicTask["time"]) # nEarliestTS = -1 # nLatestTS = -1 # for dicTask in tti:#toTasks: # owlTask = tti[dicTask] # TS = owlTask.timeSpan() # if TS: # if nEarliestTS == -1 or float(TS[0]) < nEarliestTS: # nEarliestTS = float(TS[0]) # if nLatestTS == -1 or float(TS[1]) > nLatestTS: # nLatestTS = float(TS[1]) # nOverallTime = nLatestTS - nEarliestTS fEarliest = -1 fLatest = -1 dicClassTimes = {} for strTask in tti: owlTask = tti[strTask] TS = owlTask.timeSpan() if TS: if owlTask.type() in dicClassTimes: dicClassTimes[owlTask.type()] += (float(TS[1]) - float(TS[0])) else: dicClassTimes[owlTask.type()] = (float(TS[1]) - float(TS[0])) if float(TS[0]) < fEarliest or fEarliest == -1: fEarliest = float(TS[0]) if float(TS[1]) > fLatest or fLatest == -1: fLatest = float(TS[1]) print "Time =", (fLatest - fEarliest) #with open("classtimes.pkl", "wb") as f: # pickle.dump(dicClassTimes, f, pickle.HIGHEST_PROTOCOL) #print "Longest Task: ", toTasks[len(toTasks) - 1] #for strItem in dicClassTimes: #print strItem, dicClassTimes[strItem] for strClass in dicClassTimes: print strClass, " = ", dicClassTimes[strClass] #print "" # if not "MotionPlanning" in dicClassTimes: # print "Picking Up Objects: " + str(dicClassTimes["PickingUpAnObject"]) # print "Placing Objects: " + str(dicClassTimes["PuttingDownAnObject"]) # print "Path Planning + Motion Execution: " + str(dicClassTimes["ArmMovement"]) # print "Navigation: " + str(dicClassTimes["BaseMovement"]) # print "Head Movement: " + str(dicClassTimes["HeadMovement"]) # print "Perception Queries: " + str(dicClassTimes["UIMAPerception"]) # print "Object Identity Resolution + Belief State Updates: " + str(dicClassTimes["PerceivingObjects"] - dicClassTimes["UIMAPerception"]) # else: # # print "--- General ---" # # print "Overall : " + str(nOverallTime) # print "--- High Level ---" # print "Picking Up Objects : " + str(dicClassTimes["PickingUpAnObject"] + # dicClassTimes["CarryingAnObject"] + # dicClassTimes["LiftingAnObject"]) # print "Placing Objects : " + str(dicClassTimes["PuttingDownAnObject"]) # print "Finding Objects : " + str(dicClassTimes["FindingObjects"]) # # print "Other Activities : " + str(nOverallTime - # # (dicClassTimes["PickingUpAnObject"] + # # dicClassTimes["CarryingAnObject"] + # # dicClassTimes["LiftingAnObject"] + # # dicClassTimes["PuttingDownAnObject"] + # # dicClassTimes["FindingObjects"] - # # dicClassTimes["UIMAPerception"])) # print "--- Low Level ---" # print "Path Planning : " + str(dicClassTimes["MotionPlanning"]) # print "Motion Execution : " + str(dicClassTimes["MotionExecution"]) # print "Navigation : " + str(dicClassTimes["BaseMovement"]) # print "Head Movement : " + str(dicClassTimes["HeadMovement"]) # print "Perception Queries : " + str(dicClassTimes["UIMAPerception"]) # print "Object Identity Resolution : " + str(dicClassTimes["ObjectIdentityResolution"]) # print "Belief State Updates : " + str(dicClassTimes["BeliefStateUpdate"]) def timelyOrderedTasks(self, data): dicLinear = self.linearizeTaskTree(data) arrItems = [] for strItem in dicLinear: arrItems.append({"name": strItem, "time": dicLinear[strItem]}) return sorted(arrItems, key=lambda item: item["time"]) def linearizeTaskTree(self, tree): dicLinear = {} for strBranch in tree: dicLinear[strBranch] = tree[strBranch]["time"] dicSub = self.linearizeTaskTree(tree[strBranch]["children"]) dicLinear = dict(dicLinear, **dicSub) return dicLinear def correctTime(self, data): for strBranchName in data: data[strBranchName]["children"] = self.correctTime(data[strBranchName]["children"]) nTimeSum = 0 for strChild in data[strBranchName]["children"]: nTimeSum += data[strBranchName]["children"][strChild]["time"] if data[strBranchName]["time"] < nTimeSum: data[strBranchName]["time"] = nTimeSum return data def drawTaskPie(self, imgPie, dicTaskTree, globalTimespan = -1, parentTimespan = -1, radiusDelta = 10, radiusInner = 0, angleStart = 0, angleEnd = 360): if globalTimespan == -1: globalTimespan = 0 for strBranchName in dicTaskTree: globalTimespan += dicTaskTree[strBranchName]["time"] if parentTimespan == -1: parentTimespan = 0 for strBranchName in dicTaskTree: parentTimespan += dicTaskTree[strBranchName]["time"] if parentTimespan > 0: nSegments = len(dicTaskTree) radiusOuter = radiusInner + radiusDelta nCenterX = imgPie.columns() / 2 nCenterY = imgPie.rows() / 2 nStartXOuter = nCenterX - radiusOuter nStartYOuter = nCenterY - radiusOuter nEndXOuter = nCenterX + radiusOuter nEndYOuter = nCenterY + radiusOuter nStartXInner = nCenterX - radiusInner nStartYInner = nCenterY - radiusInner nEndXInner = nCenterX + radiusInner nEndYInner = nCenterY + radiusInner dAngleOffset = 0 for strBranchName in dicTaskTree: dAngleWidth = float(dicTaskTree[strBranchName]["time"]) / float(parentTimespan) * (angleEnd - angleStart) if dAngleWidth > 0: dStartingAngle = angleStart + dAngleOffset dEndingAngle = dStartingAngle + dAngleWidth dAngleOffset += dAngleWidth if "children" in dicTaskTree[strBranchName]: if len(dicTaskTree[strBranchName]["children"]) > 0: self.drawTaskPie(imgPie, dicTaskTree[strBranchName]["children"], globalTimespan, dicTaskTree[strBranchName]["time"], radiusDelta, radiusOuter, dStartingAngle, dEndingAngle) dTimeSpanDegree = float(dicTaskTree[strBranchName]["time"]) / float(globalTimespan) imgPie.strokeColor(Color(int(255 * dTimeSpanDegree), 0, int(255 * (1.0 - dTimeSpanDegree)))) lstDrawables = DrawableList() lstDrawables.append(DrawableLine(nCenterX + radiusInner * math.cos(math.radians(dStartingAngle)), nCenterY + radiusInner * math.sin(math.radians(dStartingAngle)), nCenterX + radiusOuter * math.cos(math.radians(dStartingAngle)), nCenterY + radiusOuter * math.sin(math.radians(dStartingAngle)))) lstDrawables.append(DrawableArc(nStartXOuter, nStartYOuter, nEndXOuter, nEndYOuter, dStartingAngle, dEndingAngle)) lstDrawables.append(DrawableLine(nCenterX + radiusInner * math.cos(math.radians(dEndingAngle)), nCenterY + radiusInner * math.sin(math.radians(dEndingAngle)), nCenterX + radiusOuter * math.cos(math.radians(dEndingAngle)), nCenterY + radiusOuter * math.sin(math.radians(dEndingAngle)))) lstDrawables.append(DrawableArc(nStartXInner, nStartYInner, nEndXInner, nEndYInner, dStartingAngle, dEndingAngle)) imgPie.draw(lstDrawables)
class OwlToTrainingDataConverter: def __init__(self): self.tdTrainingData = TrainingData() self.rdrLog = LogReader() self.arrIgnoredTasks = [] self.arrAnnotatedParameters = [] def setTaskIgnored(self, strTask): if not strTask in self.arrIgnoredTasks: self.arrIgnoredTasks.append(strTask) def addTrackedParameter(self, strParameter): self.arrAnnotatedParameters.append(strParameter) def convertOwlToTrainingData(self, arrLogDirectories): self.addTrackedParameter("taskContext") self.setTaskIgnored(u"WITH-FAILURE-HANDLING") self.setTaskIgnored(u"WITH-DESIGNATORS") self.setTaskIgnored(u"TAG") self.setTaskIgnored(u"UIMA-PERCEIVE") self.setTaskIgnored(u"GOAL-MONITOR-ACTION") self.setTaskIgnored(u"GOAL-ACHIEVE") self.setTaskIgnored(u"GOAL-PERFORM") self.setTaskIgnored(u"GOAL-PERFORM-ON-PROCESS-MODULE") self.setTaskIgnored(u"PERFORM-ACTION-DESIGNATOR") self.setTaskIgnored(u"REPLACEABLE-FUNCTION-NAVIGATE") self.setTaskIgnored(u"AT-LOCATION") self.setTaskIgnored(u"VOLUNTARY-BODY-MOVEMENT-ARMS") self.setTaskIgnored(u"MOTION-PLANNING") self.setTaskIgnored(u"MOTION-EXECUTION") self.setTaskIgnored(u"PUTDOWN") self.setTaskIgnored(u"VOLUNTARY-BODY-MOVEMENT-HEAD") self.setTaskIgnored(u"OPEN-GRIPPER") self.setTaskIgnored(u"CLOSE-GRIPPER") self.tdTrainingData.registerAttribute(u"Result") self.tdTrainingData.selectFirstAttribute(u"Result") self.tdTrainingData.addIgnoredParameter("_time_created") self.tdTrainingData.setRelation("PlanExecution") for strLogDirectory in arrLogDirectories: self.logData = self.rdrLog.loadLog(strLogDirectory) self.owlData = self.logData.getOwlData() self.designatorData = self.logData.getDesignatorData() self.tti = self.owlData["task-tree-individuals"] self.di = self.owlData["designator-individuals"] self.meta = self.owlData["metadata"] self.annotation = self.owlData["annotation"] for strParameter in self.annotation.tagNodeValues( "knowrob:annotatedParameterType"): self.addTrackedParameter(strParameter) self.walkTree(self.meta) self.tdTrainingData.writeTrainingDataToFile(sys.argv[1]) def isTaskIgnored(self, strTask): return strTask in self.arrIgnoredTasks def walkTree(self, ndOriginNode, dsOrigin=DataSet()): for strParameter in self.arrAnnotatedParameters: arrParameters = ndOriginNode.tagNodeValues("knowrob:" + strParameter) if len(arrParameters) > 0: dsOrigin.setAttributeValue(strParameter, arrParameters[0]) #dsOrigin.setAttributeValue(u"Duration", unicode(ndOriginNode.time())) arrSubActions = ndOriginNode.subActions() strSubResult = u"Success" for strSubAction in arrSubActions: strSubResultTemp = self.walkTree(self.tti[strSubAction], dsOrigin.copy()) if strSubResultTemp != u"Success": strSubResult = strSubResultTemp arrFailures = ndOriginNode.failures() if len(arrFailures) == 0: dsOrigin.setAttributeValue(u"Result", strSubResult) else: desigFailure = self.tti[arrFailures[0]] dsOrigin.setAttributeValue(u"Result", desigFailure.type()) if not self.isTaskIgnored(dsOrigin.getAttributeValue(u"taskContext")): self.tdTrainingData.addDataSet(dsOrigin) return dsOrigin.getAttributeValue(u"Result")
class GraspExtractor: def __init__(self): self.rdrLog = LogReader() def getDesignatorSuccessor(self, strDesignatorID): desig = self.di[strDesignatorID] if desig: successors = desig.tagAttributeValues("knowrob:successorDesignator", "rdf:resource") if successors and len(successors) > 0: return successors[0].split("#")[1] def getNamedDesignator(self, dataDesignators, strName): for designator in dataDesignators: if designator["designator"]["_id"] == strName: return designator["designator"] def processPerform(self, owlPerform): desigsGraspDetails = owlPerform.tagAttributeValues("knowrob:graspDetails", "rdf:resource") if len(desigsGraspDetails) > 0: desigGraspDetails = self.getNamedDesignator(self.log.getDesignatorData(), desigsGraspDetails[0].split("#")[1])["GRASP"] dicGraspPose = desigGraspDetails["GRASP-POSE"] dicPregraspPose = desigGraspDetails["PREGRASP-POSE"] dicObjectPose = desigGraspDetails["OBJECT-POSE"] strObjectName = desigGraspDetails["OBJECT-NAME"] strSide = desigGraspDetails["ARM"] strEffort = desigGraspDetails["EFFORT"] strGraspType = desigGraspDetails["GRASP-TYPE"]["QUOTE"] print " -- Grasp action --" timeSpan = owlPerform.timeSpan() print "Time elapsed :", (float(timeSpan[1]) - float(timeSpan[0])), "seconds" if owlPerform.taskSuccess(): print "Success : True" else: print "Success : False" print "Side :", strSide print "Grasp Type :", strGraspType print "Object Name :", strObjectName print "Object Pose :" self.printPose(dicObjectPose) print "Grasp Pose :" self.printPose(dicGraspPose) print "Pregrasp Pose :" self.printPose(dicPregraspPose) print def extractGrasps(self, strPath): # Load Log self.log = self.rdrLog.loadLog(strPath) self.tti = self.log.getOwlData()["task-tree-individuals"] self.di = self.log.getOwlData()["designator-individuals"] annot = self.log.getOwlData()["annotation"] meta = self.log.getOwlData()["metadata"] for key in self.tti: owlIndiv = self.tti[key] if owlIndiv.type() == "AnnotationInformation": annot = owlIndiv if annot and meta: break elif owlIndiv.type() == "RobotExperiment": meta = owlIndiv if annot and meta: break if annot and meta: for indiv in self.tti: if self.tti[indiv].taskContext() == "GRASP": self.processPerform(self.tti[indiv]) def printPose(self, pose): print " Frame :", pose["header"]["frame_id"] + "\n" + \ " Position : x =", str(pose["pose"]["position"]["x"]) + "\n" + \ " y =", str(pose["pose"]["position"]["y"]) + "\n" + \ " z =", str(pose["pose"]["position"]["z"]) + "\n" + \ " Orientation : x =", str(pose["pose"]["orientation"]["x"]) + "\n" + \ " y =", str(pose["pose"]["orientation"]["y"]) + "\n" + \ " z =", str(pose["pose"]["orientation"]["z"]) + "\n" + \ " w =", str(pose["pose"]["orientation"]["w"])
#!/usr/bin/python import sys from LogReader import LogReader lr = LogReader() log = lr.loadLog(sys.argv[1]) od = log.getOwlData() tti = od["task-tree-individuals"] printable_types = ["CRAMAchieve"] colors = [ "#ddffff", "#ffddff", "#ffffdd", "#ddddff", "#ffdddd", "#ddffdd", "#dddddd", "#ffffff" ] color_assignments = {} color_index = -1 def printIndividual(individual, parent_name): global color_index global color_assignments strdot = "" strlbl = individual.goalContext() if not strlbl: strlbl = individual.name() # if len(individual.failures()) > 0:
class DataCondenser: def __init__(self): self.rdrLog = LogReader() def condenseData(self, strPath): dataOwl = None log = self.rdrLog.loadLog(strPath) dataOwl = log.getOwlData() self.tti = dataOwl["task-tree-individuals"] owlMeta = dataOwl["metadata"] owlAnnot = dataOwl["annotation"] if owlMeta: result = { "Toplevel": self.condenseNodes("", owlMeta.subActions()) } with open("out.json", "wb") as f: json.dump(result, f) with open("generalized_model.pkl", "wb") as f: pickle.dump( { "model": result, "parameters": owlAnnot.annotatedParameterTypes() }, f, pickle.HIGHEST_PROTOCOL) else: print "No meta data in file!" def condenseNodes(self, strParentNode, arrNodes, nLevel=0): arrTypes = {} arrIndividuals = {} for strNode in arrNodes: owlNode = self.tti[strNode] ident = owlNode.taskContext() #.type() failures = owlNode.failures() failure = "" if len(failures) > 0: failure = self.tti[failures[0]].type() result = self.condenseNodes(strNode, owlNode.subActions(), nLevel + 1) if not ident in arrTypes: arrTypes[ident] = result else: arrTypes[ident] = self.unifyResults(arrTypes[ident], result) arrTypes[ident]["individuals"][strNode] = { "parameters": owlNode.annotatedParameters(True), "parent": strParentNode, "failure": failure } return {"subTypes": arrTypes, "individuals": {}} def unifyResults(self, res1, res2): resparams = {} if len(res1["individuals"]) > 0: resparams = res1["individuals"] if len(res2["individuals"]) > 0: resparams = dict(resparams.items() + res2["individuals"].items()) unified = {"subTypes": {}, "individuals": resparams} for ressub1 in res1["subTypes"]: if ressub1 in res2["subTypes"]: unified["subTypes"][ressub1] = self.unifyResults( res1["subTypes"][ressub1], res2["subTypes"][ressub1]) else: unified["subTypes"][ressub1] = res1["subTypes"][ressub1] for ressub2 in res2["subTypes"]: if not ressub2 in res1["subTypes"]: unified["subTypes"][ressub2] = res2["subTypes"][ressub2] return unified
class DataExtractor: def __init__(self): self.rdrLog = LogReader() def extractData(self, strPath): log = self.rdrLog.loadLog(strPath) self.tti = log.getOwlData()["task-tree-individuals"] annot = None meta = None for key in self.tti: owlIndiv = self.tti[key] if owlIndiv.type() == "AnnotationInformation": annot = owlIndiv if annot and meta: break elif owlIndiv.type() == "RobotExperiment": meta = owlIndiv if annot and meta: break if annot and meta: params = annot.tagNodeValues("knowrob:annotatedParameterType") param_set = {} for param in params: param_set[param] = "?" strHeadline = "TASK-CONTEXT, RESULT" for param in params: strHeadline += ", " + param print strHeadline toplevelnodes = meta.tagAttributeValues("knowrob:subAction", "rdf:resource") for toplevelnode in toplevelnodes: node_key = toplevelnode.split("#")[1] self.printNode(node_key, param_set) def printNode(self, node_key, param_set): owlIndiv = self.tti[node_key] subactions = owlIndiv.tagAttributeValues("knowrob:subAction", "rdf:resource") for param in param_set: tnv = owlIndiv.tagNodeValues("knowrob:" + param) if len(tnv) > 0: param_set[param] = tnv[0] strLine = "" strLine += owlIndiv.tagNodeValues("knowrob:taskContext")[0] failures = owlIndiv.tagAttributeValues("knowrob:eventFailure", "rdf:resource") if len(failures) > 0: failure = failures[0].split("#")[1] owlFailure = self.tti[failure] strLine += ", " + owlFailure.type() else: strLine += ", Success" for param in param_set: strLine += ", " + param_set[param] for subaction in subactions: self.printNode(subaction.split("#")[1], param_set) print strLine
#!/usr/bin/python import sys from LogReader import LogReader lr = LogReader() log = lr.loadLog(sys.argv[1]) od = log.getOwlData() tti = od["task-tree-individuals"] printable_types = ["CRAMAchieve"] colors = ["#ddffff", "#ffddff", "#ffffdd", "#ddddff", "#ffdddd", "#ddffdd", "#dddddd", "#ffffff"] color_assignments = {} color_index = -1 def printIndividual(individual, parent_name): global color_index global color_assignments strdot = "" strlbl = individual.goalContext() if not strlbl: strlbl = individual.name() # if len(individual.failures()) > 0: # color = "#ffdddd" # else: # color = "#ddffdd" distinguisher = individual.goalContext()
class DataCondenser: def __init__(self): self.rdrLog = LogReader() def condenseData(self, strPath): dataOwl = None log = self.rdrLog.loadLog(strPath) dataOwl = log.getOwlData() self.tti = dataOwl["task-tree-individuals"] owlMeta = dataOwl["metadata"] owlAnnot = dataOwl["annotation"] if owlMeta: result = {"Toplevel" : self.condenseNodes("", owlMeta.subActions())}; with open("out.json", "wb") as f: json.dump(result, f) with open("generalized_model.pkl", "wb") as f: pickle.dump({"model" : result, "parameters" : owlAnnot.annotatedParameterTypes()}, f, pickle.HIGHEST_PROTOCOL) else: print "No meta data in file!" def condenseNodes(self, strParentNode, arrNodes, nLevel = 0): arrTypes = {} arrIndividuals = {} for strNode in arrNodes: owlNode = self.tti[strNode] ident = owlNode.taskContext()#.type() failures = owlNode.failures() failure = "" if len(failures) > 0: failure = self.tti[failures[0]].type() result = self.condenseNodes(strNode, owlNode.subActions(), nLevel + 1) if not ident in arrTypes: arrTypes[ident] = result else: arrTypes[ident] = self.unifyResults(arrTypes[ident], result) arrTypes[ident]["individuals"][strNode] = {"parameters" : owlNode.annotatedParameters(True), "parent" : strParentNode, "failure" : failure} return {"subTypes" : arrTypes, "individuals" : {}} def unifyResults(self, res1, res2): resparams = {} if len(res1["individuals"]) > 0: resparams = res1["individuals"] if len(res2["individuals"]) > 0: resparams = dict(resparams.items() + res2["individuals"].items()) unified = {"subTypes" : {}, "individuals" : resparams} for ressub1 in res1["subTypes"]: if ressub1 in res2["subTypes"]: unified["subTypes"][ressub1] = self.unifyResults(res1["subTypes"][ressub1], res2["subTypes"][ressub1]) else: unified["subTypes"][ressub1] = res1["subTypes"][ressub1] for ressub2 in res2["subTypes"]: if not ressub2 in res1["subTypes"]: unified["subTypes"][ressub2] = res2["subTypes"][ressub2] return unified
class GraspExtractor: def __init__(self): self.rdrLog = LogReader() def getDesignatorSuccessor(self, strDesignatorID): desig = self.di[strDesignatorID] if desig: successors = desig.tagAttributeValues( "knowrob:successorDesignator", "rdf:resource") if successors and len(successors) > 0: return successors[0].split("#")[1] def getNamedDesignator(self, dataDesignators, strName): for designator in dataDesignators: if designator["designator"]["_id"] == strName: return designator["designator"] def processPerform(self, owlPerform): desigsGraspDetails = owlPerform.tagAttributeValues( "knowrob:graspDetails", "rdf:resource") if len(desigsGraspDetails) > 0: desigGraspDetails = self.getNamedDesignator( self.log.getDesignatorData(), desigsGraspDetails[0].split("#")[1])["GRASP"] dicGraspPose = desigGraspDetails["GRASP-POSE"] dicPregraspPose = desigGraspDetails["PREGRASP-POSE"] dicObjectPose = desigGraspDetails["OBJECT-POSE"] strObjectName = desigGraspDetails["OBJECT-NAME"] strSide = desigGraspDetails["ARM"] strEffort = desigGraspDetails["EFFORT"] strGraspType = desigGraspDetails["GRASP-TYPE"]["QUOTE"] print " -- Grasp action --" timeSpan = owlPerform.timeSpan() print "Time elapsed :", (float(timeSpan[1]) - float(timeSpan[0])), "seconds" if owlPerform.taskSuccess(): print "Success : True" else: print "Success : False" print "Side :", strSide print "Grasp Type :", strGraspType print "Object Name :", strObjectName print "Object Pose :" self.printPose(dicObjectPose) print "Grasp Pose :" self.printPose(dicGraspPose) print "Pregrasp Pose :" self.printPose(dicPregraspPose) print def extractGrasps(self, strPath): # Load Log self.log = self.rdrLog.loadLog(strPath) self.tti = self.log.getOwlData()["task-tree-individuals"] self.di = self.log.getOwlData()["designator-individuals"] annot = self.log.getOwlData()["annotation"] meta = self.log.getOwlData()["metadata"] for key in self.tti: owlIndiv = self.tti[key] if owlIndiv.type() == "AnnotationInformation": annot = owlIndiv if annot and meta: break elif owlIndiv.type() == "RobotExperiment": meta = owlIndiv if annot and meta: break if annot and meta: for indiv in self.tti: if self.tti[indiv].taskContext() == "GRASP": self.processPerform(self.tti[indiv]) def printPose(self, pose): print " Frame :", pose["header"]["frame_id"] + "\n" + \ " Position : x =", str(pose["pose"]["position"]["x"]) + "\n" + \ " y =", str(pose["pose"]["position"]["y"]) + "\n" + \ " z =", str(pose["pose"]["position"]["z"]) + "\n" + \ " Orientation : x =", str(pose["pose"]["orientation"]["x"]) + "\n" + \ " y =", str(pose["pose"]["orientation"]["y"]) + "\n" + \ " z =", str(pose["pose"]["orientation"]["z"]) + "\n" + \ " w =", str(pose["pose"]["orientation"]["w"])
class OwlToTrainingDataConverter: def __init__(self): self.tdTrainingData = TrainingData() self.rdrLog = LogReader() self.arrIgnoredTasks = [] self.arrAnnotatedParameters = [] def setTaskIgnored(self, strTask): if not strTask in self.arrIgnoredTasks: self.arrIgnoredTasks.append(strTask) def addTrackedParameter(self, strParameter): self.arrAnnotatedParameters.append(strParameter) def convertOwlToTrainingData(self, arrLogDirectories): self.addTrackedParameter("taskContext") self.setTaskIgnored(u"WITH-FAILURE-HANDLING") self.setTaskIgnored(u"WITH-DESIGNATORS") self.setTaskIgnored(u"TAG") self.setTaskIgnored(u"UIMA-PERCEIVE") self.setTaskIgnored(u"GOAL-MONITOR-ACTION") self.setTaskIgnored(u"GOAL-ACHIEVE") self.setTaskIgnored(u"GOAL-PERFORM") self.setTaskIgnored(u"GOAL-PERFORM-ON-PROCESS-MODULE") self.setTaskIgnored(u"PERFORM-ACTION-DESIGNATOR") self.setTaskIgnored(u"REPLACEABLE-FUNCTION-NAVIGATE") self.setTaskIgnored(u"AT-LOCATION") self.setTaskIgnored(u"VOLUNTARY-BODY-MOVEMENT-ARMS") self.setTaskIgnored(u"MOTION-PLANNING") self.setTaskIgnored(u"MOTION-EXECUTION") self.setTaskIgnored(u"PUTDOWN") self.setTaskIgnored(u"VOLUNTARY-BODY-MOVEMENT-HEAD") self.setTaskIgnored(u"OPEN-GRIPPER") self.setTaskIgnored(u"CLOSE-GRIPPER") self.tdTrainingData.registerAttribute(u"Result") self.tdTrainingData.selectFirstAttribute(u"Result") self.tdTrainingData.addIgnoredParameter("_time_created") self.tdTrainingData.setRelation("PlanExecution") for strLogDirectory in arrLogDirectories: self.logData = self.rdrLog.loadLog(strLogDirectory) self.owlData = self.logData.getOwlData() self.designatorData = self.logData.getDesignatorData() self.tti = self.owlData["task-tree-individuals"] self.di = self.owlData["designator-individuals"] self.meta = self.owlData["metadata"] self.annotation = self.owlData["annotation"] for strParameter in self.annotation.tagNodeValues("knowrob:annotatedParameterType"): self.addTrackedParameter(strParameter) self.walkTree(self.meta) self.tdTrainingData.writeTrainingDataToFile(sys.argv[1]) def isTaskIgnored(self, strTask): return strTask in self.arrIgnoredTasks def walkTree(self, ndOriginNode, dsOrigin = DataSet()): for strParameter in self.arrAnnotatedParameters: arrParameters = ndOriginNode.tagNodeValues("knowrob:" + strParameter) if len(arrParameters) > 0: dsOrigin.setAttributeValue(strParameter, arrParameters[0]) #dsOrigin.setAttributeValue(u"Duration", unicode(ndOriginNode.time())) arrSubActions = ndOriginNode.subActions() strSubResult = u"Success" for strSubAction in arrSubActions: strSubResultTemp = self.walkTree(self.tti[strSubAction], dsOrigin.copy()) if strSubResultTemp != u"Success": strSubResult = strSubResultTemp arrFailures = ndOriginNode.failures() if len(arrFailures) == 0: dsOrigin.setAttributeValue(u"Result", strSubResult) else: desigFailure = self.tti[arrFailures[0]] dsOrigin.setAttributeValue(u"Result", desigFailure.type()) if not self.isTaskIgnored(dsOrigin.getAttributeValue(u"taskContext")): self.tdTrainingData.addDataSet(dsOrigin) return dsOrigin.getAttributeValue(u"Result")
class DataCondenser: def __init__(self): self.rdrLog = LogReader() self.tti = {} def condenseData(self, strOutputFile, arrSourceDirectories): dicToplevelNodes = [] for strSourceDirectory in arrSourceDirectories: logData = self.rdrLog.loadLog(strSourceDirectory) owlData = logData.getOwlData() self.tti = dict(self.tti.items() + owlData["task-tree-individuals"].items()) meta = owlData["metadata"] if meta: dicToplevelNodes += meta.subActions() dicResult = {"Toplevel" : self.condenseNodes("", dicToplevelNodes)} with open(strOutputFile, "wb") as fOut: json.dump(dicResult, fOut) def condenseNodes(self, strParentNode, arrNodes, nLevel = 0): arrTypes = {} arrIndividuals = {} for strNode in arrNodes: owlNode = self.tti[strNode] nodeclass = owlNode.taskContext() if nLevel < 0: ident = "*" else: ident = nodeclass failures = owlNode.failures() failure = "" if len(failures) > 0: failure = self.tti[failures[0]].type() result = self.condenseNodes(strNode, owlNode.subActions(), nLevel + 1) if not ident in arrTypes: arrTypes[ident] = result else: arrTypes[ident] = self.unifyResults(arrTypes[ident], result) arrTypes[ident]["individuals"][strNode] = {"parameters" : owlNode.annotatedParameters(True), "parent" : strParentNode, "failure" : failure, "class" : nodeclass} return {"subTypes" : arrTypes, "individuals" : {}} def unifyResults(self, res1, res2): resparams = {} if len(res1["individuals"]) > 0: resparams = res1["individuals"] if len(res2["individuals"]) > 0: resparams = dict(resparams.items() + res2["individuals"].items()) unified = {"subTypes" : {}, "individuals" : resparams} for ressub1 in res1["subTypes"]: if ressub1 in res2["subTypes"]: unified["subTypes"][ressub1] = self.unifyResults(res1["subTypes"][ressub1], res2["subTypes"][ressub1]) else: unified["subTypes"][ressub1] = res1["subTypes"][ressub1] for ressub2 in res2["subTypes"]: if not ressub2 in res1["subTypes"]: unified["subTypes"][ressub2] = res2["subTypes"][ressub2] return unified