Ejemplo n.º 1
0
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)

        self.recipeModel = recipeModel.Model(self.recipeTable)
        self.instructionModel = instructionModel.Model(self.instructionTable)

        self.windowManager = WindowManager(self.windowsMenu)

        self.recipe_manager = RecipeManager("kitchenModel_1.5.pck")

        self.recipeBrowser = recipeBrowser.MainWindow(self.recipe_manager)
        self.recipeBrowser.show()
        self.windowManager.addWindow(self.recipeBrowser)

        self.costFunctionBrowser = costFunctionBrowser.MainWindow(
            [0, 10, 0, 10], self.recipe_manager.task_planner)
        self.costFunctionBrowser.save_state_tree = True
        self.costFunctionBrowser.allow_null_action = False

        self.windowManager.addWindow(self.costFunctionBrowser)

        self.connect(
            self.recipeTable.selectionModel(),
            SIGNAL("selectionChanged ( QItemSelection, QItemSelection )"),
            self.selectRecipe)

        self.connect(self.sendInstructionButton, SIGNAL("clicked()"),
                     self.sendInstruction)

        self.connect(self.sendRecipeButton, SIGNAL("clicked()"),
                     self.sendRecipe)
Ejemplo n.º 2
0
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.figure = mpl.figure()
        self.axes = self.figure.gca()
        self.axes.set_aspect("equal")
        self.oldParent = self.figure.canvas.parent()
        self.figure.canvas.setParent(self)
        self.matplotlibFrame.layout().addWidget(self.figure.canvas)
        self.toolbar = NavigationToolbar2QT(self.figure.canvas, self)
        self.addToolBar(self.toolbar)
        self.windowManager = WindowManager(self.windowsMenu)

        self.limits = [-2, 8, -2, 8]

        self.contextWindow = context3d.MainWindow()
        self.contextWindow.setWindowTitle(self.contextWindow.windowTitle() + 
                                          " - State Action Browser")
        self.contextWindow.setVisible(True)
        self.windowManager.addWindow(self.contextWindow)


        self.state = DiaperState.example_state()
        self.contextWindow.setContext(self.state.to_context())
        self.draw()
Ejemplo n.º 3
0
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)

        self.nodeModel = nodeModel.Model(self.nodeTable)
        self.nodeResultsModel = nodeResultsModel.Model(self.nodeResultsTable)
        self.factorResultsModel = factorResultsModel.Model(
            self.factorResultsTable)
        self.featureModel = featureModel.Model(self.featureTable)
        self.entropyMetricModel = entropyMetricModel.Model(
            self.entropyMetricTable)

        self.windowManager = WindowManager(self.windowsMenu)
        self.contextWindow = context3d.MainWindow()
        #self.context3dFrame.layout().addWidget(self.contextWindow.centralWidget())
        self.contextWindow.setWindowTitle(self.contextWindow.windowTitle() +
                                          " - Node Feature Weights")
        #self.contextWindow.show()
        self.windowManager.addWindow(self.contextWindow)

        self.figure = mpl.figure()
        self.axes = self.figure.gca()
        self.axes.set_aspect("equal")
        self.oldParent = self.figure.canvas.parent()
        self.figure.canvas.setParent(self)
        self.matplotlibFrame.layout().addWidget(self.figure.canvas)
        self.toolbar = NavigationToolbar2QT(self.figure.canvas, self)
        self.addToolBar(self.toolbar)
        self.limits = [-20, 30, 10, 51]
        self.restoreLimits()
        self.figure.canvas.mpl_connect('draw_event', self.updateLimits)

        self.cf_obj = None

        self.connect(
            self.nodeTable.selectionModel(),
            SIGNAL("selectionChanged ( QItemSelection, QItemSelection )"),
            self.selectNode)
        self.connect(
            self.nodeResultsTable.selectionModel(),
            SIGNAL("selectionChanged ( QItemSelection, QItemSelection )"),
            self.selectNodeResult)

        self.connect(
            self.factorResultsTable.selectionModel(),
            SIGNAL("selectionChanged ( QItemSelection, QItemSelection )"),
            self.selectFactorResult)
Ejemplo n.º 4
0
class MainWindow(QMainWindow, state_action_browser_ui.Ui_MainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.figure = mpl.figure()
        self.axes = self.figure.gca()
        self.axes.set_aspect("equal")
        self.oldParent = self.figure.canvas.parent()
        self.figure.canvas.setParent(self)
        self.matplotlibFrame.layout().addWidget(self.figure.canvas)
        self.toolbar = NavigationToolbar2QT(self.figure.canvas, self)
        self.addToolBar(self.toolbar)
        self.windowManager = WindowManager(self.windowsMenu)

        self.limits = [-2, 8, -2, 8]

        self.contextWindow = context3d.MainWindow()
        self.contextWindow.setWindowTitle(self.contextWindow.windowTitle() + 
                                          " - State Action Browser")
        self.contextWindow.setVisible(True)
        self.windowManager.addWindow(self.contextWindow)


        self.state = DiaperState.example_state()
        self.contextWindow.setContext(self.state.to_context())
        self.draw()

    def draw(self):
        self.axes.clear()
        self.artists = []

        for g in self.state.to_context().objects:
                drawObject(self.axes, g)

        self.restoreLimits()
        self.figure.canvas.draw()

                
    def updateLimits(self, mplEvent):
        self.saveLimits()
    def saveLimits(self):
        self.limits = self.axes.axis()
    def restoreLimits(self):
        if self.limits != None:
            self.axes.axis(self.limits)
Ejemplo n.º 5
0
    def __init__(self, taskPlanner=None, initialState=True):
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.featureModel = featureModel.Model(self.featureTable)
        self.candidateModel = candidateModel.Model(self.factorCandidateTable)
        self.groundingsModel = groundingsModel.Model(self.groundingTable)
        self.windowManager = WindowManager(self.windowsMenu)
        self.contextWindow = context3d.MainWindow()
        self.contextWindow.setWindowTitle(self.contextWindow.windowTitle() +
                                          " - CRF Feature Weights")
        self.windowManager.addWindow(self.contextWindow)

        self.connect(
            self.factorCandidateTable.selectionModel(),
            SIGNAL("selectionChanged ( QItemSelection, QItemSelection )"),
            self.selectCandidate)

        self.connect(
            self.groundingTable.selectionModel(),
            SIGNAL("selectionChanged ( QItemSelection, QItemSelection )"),
            self.draw)
        self.connect(self.shouldDrawTextCheckBox, SIGNAL("stateChanged(int)"),
                     self.draw)
        self.connect(self.shouldDrawIconsCheckBox, SIGNAL("stateChanged(int)"),
                     self.draw)
        self.connect(self.shouldDrawAgentCheckBox, SIGNAL("stateChanged(int)"),
                     self.draw)
        self.connect(self.shouldDrawObjectGeomCheckBox,
                     SIGNAL("stateChanged(int)"), self.draw)

        self.figure = mpl.figure()
        self.axes = self.figure.gca()
        self.axes.set_aspect("equal")
        self.oldParent = self.figure.canvas.parent()
        self.figure.canvas.setParent(self)
        self.matplotlibFrame.layout().addWidget(self.figure.canvas)
        self.toolbar = NavigationToolbar2QT(self.figure.canvas, self)
        self.addToolBar(self.toolbar)
        self.limits = [-20, 30, 10, 51]
        #self.limits = [10, 40, 10, 40]
        self.restoreLimits()
        self.figure.canvas.mpl_connect('draw_event', self.updateLimits)
        self.figure.canvas.mpl_connect('button_press_event', self.mapClick)
        self.costImage = None
Ejemplo n.º 6
0
    def __init__(self,
                 limits,
                 taskPlanner=None,
                 initialState=True,
                 show_gui=True,
                 merging_mode="merge_none",
                 start_command=None):

        QMainWindow.__init__(self)
        self.setupUi(self)

        self.windowManager = WindowManager(self.windowsMenu)
        self.artists = []
        self.extractor = None
        self.taskPlanner = taskPlanner
        self.plansDict = None
        self.currAnnotation = None
        self.save_state_tree = True
        self.allow_null_action = False

        self.command = ""
        if start_command:
            self.command = start_command
            self.commandEdit.setPlainText(start_command)

        self.figure = mpl.figure()
        self.axes = self.figure.gca()
        self.axes.set_aspect("equal")
        self.oldParent = self.figure.canvas.parent()
        self.figure.canvas.setParent(self)
        self.matplotlibFrame.layout().addWidget(self.figure.canvas)
        self.toolbar = NavigationToolbar2QT(self.figure.canvas, self)
        self.addToolBar(self.toolbar)

        self.limits = limits

        self.restoreLimits()
        self.figure.canvas.mpl_connect('draw_event', self.updateLimits)

        self.esdcFeatureBrowser = esdcFeatureBrowser.MainWindow()
        self.esdcFeatureBrowser.setWindowTitle(
            str(self.esdcFeatureBrowser.windowTitle()) +
            " (ESDC Feature Browser)")

        self.contextWindow = context3d.MainWindow()
        self.contextWindow.setWindowTitle(self.contextWindow.windowTitle() +
                                          " - Cost Function Browser")

        self.windowManager.addWindow(self.contextWindow)

        self.gggWindow = gggBrowser.MainWindow()
        self.gggWindow.setWindowTitle(self.gggWindow.windowTitle() +
                                      "(GGG Visualizer)")
        self.windowManager.addWindow(self.gggWindow)

        self.crfFeatureWeights = crfFeatureWeights.MainWindow()
        self.crfFeatureWeights.setWindowTitle(
            str(self.crfFeatureWeights.windowTitle()) + " (CFB)")
        self.windowManager.addWindow(self.crfFeatureWeights)

        self.nodeFeatureWeights = nodeFeatureWeights.MainWindow()
        self.nodeFeatureWeights.setWindowTitle(
            str(self.nodeFeatureWeights.windowTitle()) + " (CFB)")
        self.windowManager.addWindow(self.nodeFeatureWeights)

        self.esdcModel = esdcTreeModel.Model(self.esdcView)
        self.plansModel = plansModel.Model(self.plansView)

        self.connect(self.submitButton, SIGNAL("clicked()"),
                     self.followCommandSelf)

        self.connect(
            self.esdcView.selectionModel(),
            SIGNAL("selectionChanged ( QItemSelection, QItemSelection )"),
            self.selectEsdc)

        self.connect(
            self.plansView.selectionModel(),
            SIGNAL("selectionChanged ( QItemSelection, QItemSelection )"),
            self.selectPlan)

        self.connect(self.esdcParserComboBox,
                     SIGNAL("currentIndexChanged( QString)"),
                     self.selectEsdcExtractor)

        self.connect(self.esdcParserModelButton, SIGNAL("clicked()"),
                     self.askForEsdcParserModel)

        self.connect(self.actionLoadContext, SIGNAL("triggered()"),
                     self.loadContext)
        self.connect(self.actionSaveState, SIGNAL("triggered()"),
                     self.saveState)
        self.connect(self.actionLoadState, SIGNAL("triggered()"),
                     self.loadState)

        self.connect(self.actionGroundingProbabilityGraph,
                     SIGNAL("triggered()"), self.groundingProbabilityGraph)

        self.esdcParserModelButton.setText(
            "%s/data/directions/direction_training/annotation/models/crf_discrete_esdcs_1.5.pck"
            % SLU_HOME)

        self.esdcs = []
        self.factors_to_esdcs = None
        self.gggs = None

        #self.selectEsdcExtractor()

        self.merging_mode = merging_mode
Ejemplo n.º 7
0
class MainWindow(QMainWindow, costFunctionBrowser_ui.Ui_MainWindow):
    def mpl_draw(self):
        self.restoreLimits()
        self.figure.canvas.draw()

    def __init__(self,
                 limits,
                 taskPlanner=None,
                 initialState=True,
                 show_gui=True,
                 merging_mode="merge_none",
                 start_command=None):

        QMainWindow.__init__(self)
        self.setupUi(self)

        self.windowManager = WindowManager(self.windowsMenu)
        self.artists = []
        self.extractor = None
        self.taskPlanner = taskPlanner
        self.plansDict = None
        self.currAnnotation = None
        self.save_state_tree = True
        self.allow_null_action = False

        self.command = ""
        if start_command:
            self.command = start_command
            self.commandEdit.setPlainText(start_command)

        self.figure = mpl.figure()
        self.axes = self.figure.gca()
        self.axes.set_aspect("equal")
        self.oldParent = self.figure.canvas.parent()
        self.figure.canvas.setParent(self)
        self.matplotlibFrame.layout().addWidget(self.figure.canvas)
        self.toolbar = NavigationToolbar2QT(self.figure.canvas, self)
        self.addToolBar(self.toolbar)

        self.limits = limits

        self.restoreLimits()
        self.figure.canvas.mpl_connect('draw_event', self.updateLimits)

        self.esdcFeatureBrowser = esdcFeatureBrowser.MainWindow()
        self.esdcFeatureBrowser.setWindowTitle(
            str(self.esdcFeatureBrowser.windowTitle()) +
            " (ESDC Feature Browser)")

        self.contextWindow = context3d.MainWindow()
        self.contextWindow.setWindowTitle(self.contextWindow.windowTitle() +
                                          " - Cost Function Browser")

        self.windowManager.addWindow(self.contextWindow)

        self.gggWindow = gggBrowser.MainWindow()
        self.gggWindow.setWindowTitle(self.gggWindow.windowTitle() +
                                      "(GGG Visualizer)")
        self.windowManager.addWindow(self.gggWindow)

        self.crfFeatureWeights = crfFeatureWeights.MainWindow()
        self.crfFeatureWeights.setWindowTitle(
            str(self.crfFeatureWeights.windowTitle()) + " (CFB)")
        self.windowManager.addWindow(self.crfFeatureWeights)

        self.nodeFeatureWeights = nodeFeatureWeights.MainWindow()
        self.nodeFeatureWeights.setWindowTitle(
            str(self.nodeFeatureWeights.windowTitle()) + " (CFB)")
        self.windowManager.addWindow(self.nodeFeatureWeights)

        self.esdcModel = esdcTreeModel.Model(self.esdcView)
        self.plansModel = plansModel.Model(self.plansView)

        self.connect(self.submitButton, SIGNAL("clicked()"),
                     self.followCommandSelf)

        self.connect(
            self.esdcView.selectionModel(),
            SIGNAL("selectionChanged ( QItemSelection, QItemSelection )"),
            self.selectEsdc)

        self.connect(
            self.plansView.selectionModel(),
            SIGNAL("selectionChanged ( QItemSelection, QItemSelection )"),
            self.selectPlan)

        self.connect(self.esdcParserComboBox,
                     SIGNAL("currentIndexChanged( QString)"),
                     self.selectEsdcExtractor)

        self.connect(self.esdcParserModelButton, SIGNAL("clicked()"),
                     self.askForEsdcParserModel)

        self.connect(self.actionLoadContext, SIGNAL("triggered()"),
                     self.loadContext)
        self.connect(self.actionSaveState, SIGNAL("triggered()"),
                     self.saveState)
        self.connect(self.actionLoadState, SIGNAL("triggered()"),
                     self.loadState)

        self.connect(self.actionGroundingProbabilityGraph,
                     SIGNAL("triggered()"), self.groundingProbabilityGraph)

        self.esdcParserModelButton.setText(
            "%s/data/directions/direction_training/annotation/models/crf_discrete_esdcs_1.5.pck"
            % SLU_HOME)

        self.esdcs = []
        self.factors_to_esdcs = None
        self.gggs = None

        #self.selectEsdcExtractor()

        self.merging_mode = merging_mode

    def loadContext(self):
        fname = QFileDialog.getOpenFileName(self)
        context = Context.fromYaml(yaml.load(open(fname)))

    def saveState(self):
        fname = QFileDialog.getSaveFileName(self)
        pickle_util.save(fname, self.state)

    def loadState(self):
        fname = QFileDialog.getOpenFileName(self)
        state = pickle_util.load(fname)
        self.setState(state)

    def show(self):
        QMainWindow.show(self)

    def askForEsdcParserModel(self):
        existingFname = str(self.esdcParserModelButton.text())
        fname = str(
            QFileDialog.getOpenFileName(self,
                                        directory=dirname(existingFname)))
        print "fname", fname, fname.__class__
        if fname != "":
            self.esdcParserModelButton.setText(fname)

    def selectEsdcExtractor(self):

        esdc_extractor = str(self.esdcParserComboBox.currentText())
        esdc_extractor_model = str(self.esdcParserModelButton.text())
        self.extractor = extractor_utils.make_extractor_func(
            esdc_extractor, esdc_extractor_model)

    def updateEsdcs(self, esdcs, gggs=None):
        self.esdcs = esdcs
        self.esdcModel.setData(self.esdcs)
        self.esdcView.expandAll()
        self.updateCommand(self.esdcs.text)
        self.gggs = gggs

    def setState(self, state):
        self.state = state
        self.contextWindow.setContext(state.to_context())
        self.drawStateOnly()

    def setContext(self, context):
        state = self.state.from_context(context)
        self.setState(state)

    def updateCommand(self, command):
        self.command = command
        self.commandEdit.setPlainText(command)

    def followCommandSelf(self):
        command = str(self.commandEdit.toPlainText())
        state = self.state
        esdcs = None

        self.followCommand(command, state, esdcs)

    def followCommand(self,
                      command=None,
                      state=None,
                      esdcs=None,
                      first_esdc_only=False,
                      verbose=True,
                      input_gggs=None):
        if command != None:
            self.updateCommand(command)

        if state != None:
            self.state = state

        command = str(self.commandEdit.toPlainText())

        if input_gggs != None:
            self.esdcs = input_gggs[0].esdcs
        elif esdcs != None and command == self.command:
            self.esdcs = esdcs
        else:

            class AnnotationStub:
                def __init__(self, command):
                    self.entireText = command

            self.esdcs = self.extractor(AnnotationStub(command))

            if len(self.esdcs) == 0:
                return [], []
            if first_esdc_only:
                self.esdcs = ExtendedSdcGroup([self.esdcs[0]])
                self.flattenedEsdcs = self.esdcs.flattenedEsdcs
        print "esdcs", esdcIo.toYaml(self.esdcs)
        #print "flattened esdcs", self.flattenedEsdcs

        self.esdcModel.setData(self.esdcs)
        self.esdcView.expandAll()

        #convert to a list of plans
        if input_gggs != None:
            gggs = input_gggs
        elif self.gggs != None:
            gggs = self.gggs
        else:
            if self.merging_mode == "merge_coreferences":
                from coreference.bag_of_words_resolver import BagOfWordsResolver
                from coreference.merge_coreferences import merge_coreferences
                resolver = BagOfWordsResolver(
                    "%s/tools/coreference/models/coref_1.5.pck" % SLU_HOME)
                gggs = merge_coreferences(self.esdcs, resolver)
                if len(gggs) > 1:
                    gggs = [gggs[0]]
            elif self.merging_mode == "merge_events":
                from coreference.event_resolver import EventResolver
                from coreference.merge_coreferences import merge_coreferences
                resolver = EventResolver()
                gggs = merge_coreferences(self.esdcs, resolver)
                gggs = gggs[0:1]
                for i, ggg in enumerate(gggs):
                    #ggg.to_latex("ggg_%d.pdf" % i)
                    pass

                # export parses, when saving ground truth parses from asr evaluation run
                # -- stefie10 7/19/2012
                #from esdcs.esdcIo import toYaml
                #import yaml
                #import os
                #with open("esdcs_%d.txt" % os.getpid(), "a") as f:
                #    f.write(yaml.dump([toYaml(self.esdcs)]))

                if len(self.esdcs) != 1:
                    raise ValueError("ESDCs didn't parse right: " +
                                     ` self.esdcs `)
                # print "merging events", self.esdcs.entireText
                # from coreference.merge_coreferences import merge_toplevel_events
                # #resolver = EventResolver()
                # #gggs = merge_coreferences(self.esdcs, resolver,
                # #                          verbose=False)
                # gggs = gggs_from_esdc_group(self.esdcs)
                # gggs = [merge_toplevel_events(gggs)]

                assert len(gggs) == 1, (len(gggs), self.esdcs.text)
            elif self.merging_mode == "merge_none":
                gggs = gggs_from_esdc_group(self.esdcs)
            else:
                raise ValueError("Bad merging mode: " + ` self.merging_mode `)

            if gggs[0].context == None:
                gggs[0].context = self.state.to_context()
            assert gggs[0].context.agent != None

        def run():
            try:
                assert gggs[0].context.agent != None
                self.plansList = self.taskPlanner.find_plan(
                    self.state,
                    gggs,
                    beam_width=self.beamWidthBox.value(),
                    beam_width_sequence=self.seqBeamWidthBox.value(),
                    search_depth_event=self.searchDepthBox.value(),
                    beam_width_event=self.beamWidthEventBox.value(),
                    save_state_tree=self.save_state_tree,
                    allow_null_action=self.allow_null_action)
            except:
                print "excption on gggs"
                #for i, ggg in enumerate(gggs):
                #    ggg.to_latex("ggg_%d.pdf" % i)
                raise

        start = time.time()
        cProfile.runctx("run()", globals(), locals(), "out.prof")
        end = time.time()

        if verbose:
            print "Cost Function Browser took", (end - start), "seconds."

        plansList = self.plansList
        if len(plansList) == 0:
            return [], []
        else:

            self.plans = plansModel.Plan.from_inference_result(
                self.taskPlanner, plansList)
            self.plansModel.setData(self.plans)
            self.nodeFeatureWeights.load(self.taskPlanner.cf_obj,
                                         gggs,
                                         self.plans,
                                         context=self.state.to_context())
            self.plansView.selectRow(0)
            self.gggWindow.load(plansList[0][2],
                                groundingSpace=self.state.objects)
            return self.esdcs, self.plans

    def drawState(self):
        artists = []

        #index of manipulated object, e.g. pallet for forklift
        #        index = self.browserTools.manipulated_object_index(self.state)
        # TODO remove browser tools
        index = -1

        if self.state != None:
            objects = self.state.getObjectsSet()
            #            print "list of objects: ", objects
            for i, ob in enumerate(objects):
                obj = self.state.getGroundableById(ob)
                oldTags = obj.tags
                #newTags = tuple([t for t in oldTags if len(t) > 4][0:1])
                #obj.tags = newTags
                if i == index:  #plot the manipulated object in red
                    self.artists.extend(
                        drawObject(self.axes, obj, plotArgs=dict(color="red")))
                else:  #plot everything else in blue
                    self.artists.extend(
                        drawObject(self.axes, obj,
                                   plotArgs=dict(color="blue")))
                obj.tags = oldTags

        return artists

    def drawStateOnly(self):
        self.axes.clear()

        self.artists = []
        self.artists.extend(self.drawState())
        self.artists.extend(self.drawTopologicalMap())

        x, y = self.state.getPosition()
        theta = self.state.orientation
        self.artists.extend(drawRobot(self.axes, x, y, theta, facecolor="red"))

        self.restoreLimits()
        self.figure.canvas.draw()

    def draw(self):
        self.axes.clear()

        self.artists = []
        self.artists.extend(self.drawTopologicalMap())
        self.artists.extend(self.drawAgentPath())
        self.artists.extend(self.drawState())
        self.artists.extend(self.drawBoundObjects())
        self.artists.extend(self.drawActualPath())

        x, y = self.state.getPosition()
        theta = self.state.orientation
        self.artists.extend(drawRobot(self.axes, x, y, theta, facecolor="red"))

        self.restoreLimits()
        self.figure.canvas.draw()

    def drawBoundObjects(self):

        ggg = self.plansModel.selectedData().ggg

        artists = []
        selectedEsdc = self.esdcModel.selectedEsdc()

        def callback(esdc):
            if selectedEsdc == esdc:
                selected = True
            else:
                selected = False

            artists.extend(self.drawGroundingsForEsdc(esdc, ggg, selected))

        breadthFirstTraverse(self.esdcs, callback)

        return artists

    def drawGroundingsForEsdc(self, esdc, ggg, selected=False):
        artists = []

        factor = ggg.esdc_to_factor(esdc)
        if factor == None:
            return []
        #print "***** groundings"
        #print "esdc is:", esdc

        groundings = ggg.evidences[factor.nodes_for_link("top")[0].id]
        #print repr(groundings)
        #print "-----lambdas"
        #print ggg.evidences[factor.nodes_with_type("lambda")[0].id]
        if len(groundings) == 0:
            print 'no groundings', esdc
            return artists

        value = groundings[0]
        #print "value", value, value.__class__
        if isinstance(value, PhysicalObject) or isinstance(value, Place):
            #print "drawing obj", value
            if selected:
                plotArgs = dict(color="yellow", linewidth=3)
            else:
                plotArgs = dict(color="black")

            artists.extend(drawObject(self.axes, value, plotArgs))
        elif isinstance(value, Path):
            X, Y = value.points_xy
            args = dict(color="green", linewidth=1)
            artists.extend(self.axes.plot(X, Y, "ro-", markersize=1, **args))
        elif isinstance(value, list):  #then value is a sequence of actions
            raise ValueError("no longer supported")

        self.mpl_draw()
        return artists

    def drawTopologicalMap(self):
        args = dict(linewidth=1, color="red")
        artists = []
        nodeLocs = []

        nodeLocs = self.state.topological_locations

        for loc in nodeLocs:
            x, y = loc
            artists.extend(
                self.axes.plot([x], [y], "ro-", markersize=3, **args))

        return artists

    def drawAgentPath(self):
        plan = self.plansModel.selectedData()

        artists = []
        if plan != None:
            state = plan.state
            agent = state.getGroundableById(state.getAgentId())
            artists.extend(
                drawObject(self.axes, agent, shouldDrawStartAndEnd=True))
        return artists

    def drawActualPath(self):
        artists = []
        if (self.currAnnotation):
            path = self.currAnnotation.agent.path

            plotArgs = dict(color="red", linewidth=3)
            artists.extend(drawPath(self.axes, path, plotArgs))

        return artists

    def selectEsdc(self):
        #        if self.esdcModel.selectedEsdc() in self.plansDict:
        #            self.plansModel.setData(self.plansDict[self.esdcModel.selectedEsdc()])
        #            self.esdcFeatureBrowser.selectEsdc(self.esdcModel.selectedEsdc())
        self.draw()
        esdc = self.esdcModel.selectedEsdc()
        plan = self.plansModel.selectedData()
        ggg = plan.ggg
        factor = ggg.esdc_to_factor(esdc)
        print "factor", factor, esdc
        if factor != None:
            print "loading crf feature weights"
            self.crfFeatureWeights.load(ggg.context, self.taskPlanner.cf_obj,
                                        factor)

    def selectPlan(self):
        #selectedAnnotation = self.plansModel.selectedPlan()
        #print selectedAnnotation.toYaml()
        #self.esdcFeatureBrowser.loadAnnotations([selectedAnnotation])

        state, ggg = self.plansModel.selectedPlan()
        self.contextWindow.setContext(state.to_context())
        self.contextWindow.highlightGroundings(ggg.groundings)

        print self.plansModel.selectedData().plan_string

        self.draw()

    def updateLimits(self, mplEvent):
        self.saveLimits()

    def saveLimits(self):
        self.limits = self.axes.axis()

    def restoreLimits(self):
        if self.limits != None:
            self.axes.axis(self.limits)

    def groundingProbabilityGraph(self):
        print "graph"
        mpl.figure()
        selectedEsdc = self.esdcModel.selectedEsdc()
        if selectedEsdc == None:
            return
        values_to_prob = {}
        values_to_groundings = {}
        for plan in self.plansModel._data:
            factor = plan.ggg.esdc_to_factor(selectedEsdc)
            assert len(factor.gamma_nodes) == 1
            node = factor.gamma_nodes[0]
            node_value = plan.ggg.evidence_for_node(node)[0].withoutPath()

            node_value_hash = tuple(node_value.hash_string)
            cost = plan.ggg.cost_for_factor(factor)
            prob = math.exp(-cost)
            if node_value_hash in values_to_prob:
                assert values_to_prob[node_value_hash] == prob
            else:
                values_to_prob[node_value_hash] = prob
            values_to_groundings[node_value_hash] = node_value

        X = []
        heights = []
        labels = []
        for i, (key, prob) in enumerate(values_to_prob.iteritems()):
            grounding = values_to_groundings[key]

            X.append(i)
            heights.append(prob)
            labels.append(grounding.tags)
        mpl.bar(X, heights)
        mpl.xticks(na.array(X) + 0.4, labels)
        mpl.ylabel("Probability")
        mpl.ylim(0, 1)
        mpl.show()
Ejemplo n.º 8
0
class MainWindow(QMainWindow, recipeCorpusBrowser_ui.Ui_MainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)

        self.recipeModel = recipeModel.Model(self.recipeTable)
        self.instructionModel = instructionModel.Model(self.instructionTable)

        self.windowManager = WindowManager(self.windowsMenu)

        self.recipe_manager = RecipeManager("kitchenModel_1.5.pck")

        self.recipeBrowser = recipeBrowser.MainWindow(self.recipe_manager)
        self.recipeBrowser.show()
        self.windowManager.addWindow(self.recipeBrowser)

        self.costFunctionBrowser = costFunctionBrowser.MainWindow(
            [0, 10, 0, 10], self.recipe_manager.task_planner)
        self.costFunctionBrowser.save_state_tree = True
        self.costFunctionBrowser.allow_null_action = False

        self.windowManager.addWindow(self.costFunctionBrowser)

        self.connect(
            self.recipeTable.selectionModel(),
            SIGNAL("selectionChanged ( QItemSelection, QItemSelection )"),
            self.selectRecipe)

        self.connect(self.sendInstructionButton, SIGNAL("clicked()"),
                     self.sendInstruction)

        self.connect(self.sendRecipeButton, SIGNAL("clicked()"),
                     self.sendRecipe)

    def sendRecipe(self):
        ar = self.recipeModel.selectedEntry()
        self.recipeBrowser.load(ar)

    def sendInstruction(self):
        entry = self.instructionModel.selectedEntry()
        recipe = self.recipeModel.selectedEntry()

        esdc, ggg = self.recipe_manager.make_ggg_for_instruction(entry.text)
        self.costFunctionBrowser.gggs = [ggg]
        self.costFunctionBrowser.esdcs = [esdc]
        start_state = recipe.idx_to_start_state(entry.idx)
        print 'start', start_state
        self.costFunctionBrowser.followCommand(entry.text,
                                               state=start_state,
                                               esdcs=[esdc])

        for plan in self.costFunctionBrowser.plansList[0:10]:
            result = self.recipe_manager.sequence(plan)
            string = planningLanguage.decompile(result)
            print "planning language", string

    def selectRecipe(self):
        ar = self.recipeModel.selectedEntry()
        self.recipeTextEdit.setPlainText(ar.recipe_text)
        self.instructionModel.setRecipe(ar)
        self.instructionTable.selectRow(0)

    def load(self, fname):

        self.recipeTable.selectRow(0)
        self.instructionTable.selectRow(0)

        training_set = pickle_util.load("training.pck")
        print "training", training_set
        recipes = Corpus(fname, training_set)

        self.recipeModel.setData(recipes)
        self.recipeTable.sortByColumn(recipeModel.COL_IS_TRAINING_SET,
                                      Qt.AscendingOrder)
        self.recipeTable.selectRow(0)
Ejemplo n.º 9
0
class MainWindow(QMainWindow, nodeFeatureWeights_ui.Ui_MainWindow):
    def mpl_draw(self):
        self.restoreLimits()
        self.figure.canvas.draw()

    def updateLimits(self, mplEvent):
        self.saveLimits()

    def saveLimits(self):
        self.limits = self.axes.axis()

    def restoreLimits(self):
        if self.limits != None:
            self.axes.axis(self.limits)

    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)

        self.nodeModel = nodeModel.Model(self.nodeTable)
        self.nodeResultsModel = nodeResultsModel.Model(self.nodeResultsTable)
        self.factorResultsModel = factorResultsModel.Model(
            self.factorResultsTable)
        self.featureModel = featureModel.Model(self.featureTable)
        self.entropyMetricModel = entropyMetricModel.Model(
            self.entropyMetricTable)

        self.windowManager = WindowManager(self.windowsMenu)
        self.contextWindow = context3d.MainWindow()
        #self.context3dFrame.layout().addWidget(self.contextWindow.centralWidget())
        self.contextWindow.setWindowTitle(self.contextWindow.windowTitle() +
                                          " - Node Feature Weights")
        #self.contextWindow.show()
        self.windowManager.addWindow(self.contextWindow)

        self.figure = mpl.figure()
        self.axes = self.figure.gca()
        self.axes.set_aspect("equal")
        self.oldParent = self.figure.canvas.parent()
        self.figure.canvas.setParent(self)
        self.matplotlibFrame.layout().addWidget(self.figure.canvas)
        self.toolbar = NavigationToolbar2QT(self.figure.canvas, self)
        self.addToolBar(self.toolbar)
        self.limits = [-20, 30, 10, 51]
        self.restoreLimits()
        self.figure.canvas.mpl_connect('draw_event', self.updateLimits)

        self.cf_obj = None

        self.connect(
            self.nodeTable.selectionModel(),
            SIGNAL("selectionChanged ( QItemSelection, QItemSelection )"),
            self.selectNode)
        self.connect(
            self.nodeResultsTable.selectionModel(),
            SIGNAL("selectionChanged ( QItemSelection, QItemSelection )"),
            self.selectNodeResult)

        self.connect(
            self.factorResultsTable.selectionModel(),
            SIGNAL("selectionChanged ( QItemSelection, QItemSelection )"),
            self.selectFactorResult)

    def selectNode(self):
        node = self.nodeModel.selectedNode()
        node_results = []
        for i, plan in enumerate(self.plans):
            try:
                nr = NodeResult(i, plan.ggg, node, plan.ggg)
                nr.state = plan.state
            except:
                print "couldn't get top factors for node", node
                print plan.ggg.node_to_top_esdc(node)
                raise
            node_results.append(nr)

        #self.entropyMetricModel.setData(entropy_metrics.metrics.values(), node_results)
        self.nodeResultsModel.setData(node_results)

    def selectNodeResult(self):
        r = []
        for nr in self.nodeResultsModel.selectedEntries():
            print "selected nr", nr
            r.extend([
                factorResultsModel.Entry(i, nr.end_ggg, factor)
                for i, factor in enumerate(nr.node.factors)
            ])
        self.factorResultsModel.setData(r)

        overall_cost = sum(x.cost for x in r)
        self.overallCostLabel.setText("%.3e" % overall_cost)
        self.overallProbLabel.setText("%.3e" % math.exp(-overall_cost))

        nr = self.nodeResultsModel.selectedEntry()
        print 'highlight'
        self.contextWindow.setContext(nr.state.to_context())

        self.contextWindow.highlightGroundings([nr.inferred_pobj],
                                               color="yellow")

        groundings = []
        for other_node in nr.node.factor_for_link("top").nodes:
            if other_node.is_object:
                groundings.extend(nr.ggg.evidence_for_node(other_node))

        self.contextWindow.highlightGroundings(groundings, color="green")

    def selectFactorResult(self):

        factors = self.factorResultsModel.selectedEntries()
        if len(factors) != 0:
            fr = factors[0]
            entry = fr.ggg.entry_for_factor(fr.factor)
            self.featureModel.setData(self.cf_obj.lccrf, entry.dobs)

    def load(self, cf_obj, gggs, plans, context=None):
        self.cf_obj = cf_obj
        self.ggg = gggs[0]
        self.plans = plans
        self.nodeModel.setData(self.ggg.gamma_nodes, self.ggg)
        if context != None:
            self.contextWindow.setContext(context)

        self.selectNode()
        self.selectNodeResult()
        self.nodeResultsTable.selectRow(0)
        self.selectFactorResult()

    def draw(self):
        self.axes.clear()
        self.restoreLimits()
        self.figure.canvas.draw()
Ejemplo n.º 10
0
class MainWindow(QMainWindow, crfFeatureWeights_ui.Ui_MainWindow):
    def mpl_draw(self):
        self.restoreLimits()
        self.figure.canvas.draw()

    def updateLimits(self, mplEvent):
        self.saveLimits()

    def saveLimits(self):
        self.limits = self.axes.axis()

    def restoreLimits(self):
        if self.limits != None:
            self.axes.axis(self.limits)

    def __init__(self, taskPlanner=None, initialState=True):
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.featureModel = featureModel.Model(self.featureTable)
        self.candidateModel = candidateModel.Model(self.factorCandidateTable)
        self.groundingsModel = groundingsModel.Model(self.groundingTable)
        self.windowManager = WindowManager(self.windowsMenu)
        self.contextWindow = context3d.MainWindow()
        self.contextWindow.setWindowTitle(self.contextWindow.windowTitle() +
                                          " - CRF Feature Weights")
        self.windowManager.addWindow(self.contextWindow)

        self.connect(
            self.factorCandidateTable.selectionModel(),
            SIGNAL("selectionChanged ( QItemSelection, QItemSelection )"),
            self.selectCandidate)

        self.connect(
            self.groundingTable.selectionModel(),
            SIGNAL("selectionChanged ( QItemSelection, QItemSelection )"),
            self.draw)
        self.connect(self.shouldDrawTextCheckBox, SIGNAL("stateChanged(int)"),
                     self.draw)
        self.connect(self.shouldDrawIconsCheckBox, SIGNAL("stateChanged(int)"),
                     self.draw)
        self.connect(self.shouldDrawAgentCheckBox, SIGNAL("stateChanged(int)"),
                     self.draw)
        self.connect(self.shouldDrawObjectGeomCheckBox,
                     SIGNAL("stateChanged(int)"), self.draw)

        self.figure = mpl.figure()
        self.axes = self.figure.gca()
        self.axes.set_aspect("equal")
        self.oldParent = self.figure.canvas.parent()
        self.figure.canvas.setParent(self)
        self.matplotlibFrame.layout().addWidget(self.figure.canvas)
        self.toolbar = NavigationToolbar2QT(self.figure.canvas, self)
        self.addToolBar(self.toolbar)
        self.limits = [-20, 30, 10, 51]
        #self.limits = [10, 40, 10, 40]
        self.restoreLimits()
        self.figure.canvas.mpl_connect('draw_event', self.updateLimits)
        self.figure.canvas.mpl_connect('button_press_event', self.mapClick)
        self.costImage = None

    def load(self, context, cf_obj, factor):
        self.cf_obj = cf_obj
        self.candidateModel.setData(cf_obj.factor_to_cost[factor], cf_obj)
        self.factorCandidateTable.selectRow(0)
        if context == None:
            context = Context.empty_context()
        self.contextWindow.setContext(context)

    def selectCandidate(self):
        entry = self.candidateModel.selectedEntry()
        if entry != None:
            self.featureModel.setData(self.cf_obj.lccrf, entry.candidate.dobs)
            self.featureTable.sortByColumn(featureModel.COL_WEIGHT_ABS)
            self.groundingsModel.setData(list(entry.groundings))
            self.groundingTable.selectAll()
            import math
            for phi in [True, False]:
                ev = entry.ggg.evidences.add(entry.factor.phi_node.id, phi)
                ggg = GGG.from_ggg_and_evidence(entry.ggg, ev)
                cost, cobs, dobs = self.cf_obj.compute_factor_cost(
                    entry.factor, ggg, entry.states)

                prob = math.exp(-cost)
                print phi, prob
            entry.ggg.set_evidence_for_node(entry.factor.phi_node, phi)
            print "highlighting", entry.groundings
            self.contextWindow.highlightGroundings(entry.all_groundings)
        self.draw()

    def draw(self):
        self.axes.clear()
        #self.axes.set_xticks([])
        #self.axes.set_yticks([])

        if self.costImage:
            self.axes.imshow(
                self.costImage.costs,
                origin="lower",
                extent=(self.costImage.xmin, self.costImage.xmax,
                        self.costImage.ymin, self.costImage.ymax),
                #                             cmap=mpl.cm.gray)
                cmap=mpl.cm.jet)
            #norm=matplotlib.colors.Normalize(vmin=0.0, vmax=1.0))

        for grounding in self.groundingsModel.selectedGroundings():
            drawGrounding(self.axes,
                          grounding,
                          shouldDrawIcon=self.shouldDrawIcons,
                          shouldDrawText=self.shouldDrawText,
                          plotArgs=dict(color="black", lw=4))

        if self.shouldDrawAgent:
            for candidate in self.candidateModel.selectedEntries():
                if candidate.ggg.context != None:
                    agent = candidate.ggg.context.agent
                    if agent != None:
                        ax, ay, az, ath = agent.path.locationAtT(0)
                        drawRobot(self.axes, ax, ay, ath)
                        drawObject(self.axes, agent, shouldDrawPath=True)
        self.restoreLimits()
        self.figure.canvas.draw()

    def mapClick(self, mouseevent):
        if not self.costImage or mouseevent.key != 'shift':
            return
        x = mouseevent.xdata
        y = mouseevent.ydata

        for (xe, ye), entries in self.costImage.costEntries:
            if x > xe:
                continue
            if y > ye:
                continue
            break
        for i in range(self.candidateModel.rowCount(None)):
            if self.candidateModel.get(i).candidate in entries:
                self.factorCandidateTable.selectRow(i)
                break

    def setCostImage(self, costs, xmin, xmax, ymin, ymax):
        self.costImage = CostImage(costs, xmin, xmax, ymin, ymax)
        self.limits = [xmin, xmax, ymin, ymax]

    @property
    def shouldDrawIcons(self):
        return qt_utils.isChecked(self.shouldDrawIconsCheckBox)

    @property
    def shouldDrawAgent(self):
        return qt_utils.isChecked(self.shouldDrawAgentCheckBox)

    @property
    def shouldDrawObjectGeom(self):
        return qt_utils.isChecked(self.shouldDrawObjectGeomCheckBox)

    @property
    def shouldDrawText(self):
        return qt_utils.isChecked(self.shouldDrawIconsCheckBox)
Ejemplo n.º 11
0
    def __init__(self, model):
        QMainWindow.__init__(self)
        self.setupUi(self)

        self.windowManager = WindowManager(self.windowsMenu)

        self.taskPlanner = nodeSearch.BeamSearch(
            cf.CostFnCrf.from_mallet(model))

        self.cfBrowser = costFunctionBrowser.MainWindow(
            limits=[10, 40, 10, 40],
            taskPlanner=self.taskPlanner,
            initialState=False)
        self.cfBrowser.setWindowTitle(
            str(self.cfBrowser.windowTitle()) +
            " (Evaluation Results Browser)")
        self.windowManager.addWindow(self.cfBrowser)

        self.nodeFeatureWeights = nodeFeatureWeights.MainWindow()

        self.gggBrowser = gggBrowser.MainWindow()
        self.gggBrowser.setWindowTitle(self.gggBrowser.windowTitle() +
                                       " (Evaluation Results Browser)")
        self.windowManager.addWindow(self.gggBrowser)

        self.contextWindow = context3d.MainWindow()
        self.contextWindow.setWindowTitle(self.contextWindow.windowTitle() +
                                          " - Annotation")
        self.windowManager.addWindow(self.contextWindow)

        self.contextWindowInferred = context3d.MainWindow()
        self.contextWindowInferred.setWindowTitle(
            self.contextWindow.windowTitle() + " - Inferred")
        self.windowManager.addWindow(self.contextWindowInferred)

        self.original_commands_fname = None

        self.figure = mpl.figure()
        self.axes = self.figure.gca()

        self.limits = [10, 40, 10, 40]
        self.restoreLimits()
        self.figure.canvas.mpl_connect('draw_event', self.updateLimits)

        self.resultsModel = resultsModel.Model(self.resultsTable)
        self.resultListModel = resultsModel.Model(self.resultListTable)
        self.nodeResultsModel = nodeResultsModel.Model(self.nodeResultsTable)

        self.model = None
        self.dataset = None

        self.connect(
            self.resultsTable.selectionModel(),
            SIGNAL("selectionChanged ( QItemSelection, QItemSelection )"),
            self.selectResult)

        self.connect(
            self.nodeResultsTable.selectionModel(),
            SIGNAL("selectionChanged ( QItemSelection, QItemSelection )"),
            self.selectNodeResult)

        self.connect(self.sendToCfbButton, SIGNAL("clicked()"), self.sendToCfb)

        self.connect(self.recomputeEntropyButton, SIGNAL("clicked()"),
                     self.recomputeEntropy)
        self.connect(self.saveNodeResultsButton, SIGNAL("clicked()"),
                     self.saveNodeResults)

        self.connect(self.plotEntropyButton, SIGNAL("clicked()"),
                     self.plotEntropy)

        self.connect(self.sendToCfbNodeButton, SIGNAL("clicked()"),
                     self.sendToCfbNode)

        self.connect(self.esdcFilter, SIGNAL("editingFinished()"),
                     self.filterEsdcs)

        self.connect(self.nodeFilter, SIGNAL("editingFinished()"),
                     self.filterNodes)

        print "adding export button"
        self.connect(self.exportResultsButton, SIGNAL("clicked()"),
                     self.sendToClipboard)

        print "adding export button"
        self.connect(self.exportNodeResultsButton, SIGNAL("clicked()"),
                     self.sendToClipboardNodeResults)

        self.filterEsdcs()
        self.filterNodes()
        self.resultsTable.selectRow(0)

        signal.signal(signal.SIGINT, lambda *s: self.cleanup())
Ejemplo n.º 12
0
class MainWindow(QMainWindow, evaluationResultsBrowser_ui.Ui_MainWindow):
    def cleanup(self):
        sys.exit(1)

    def mpl_draw(self):
        self.restoreLimits()
        self.figure.canvas.draw()

    def __init__(self, model):
        QMainWindow.__init__(self)
        self.setupUi(self)

        self.windowManager = WindowManager(self.windowsMenu)

        self.taskPlanner = nodeSearch.BeamSearch(
            cf.CostFnCrf.from_mallet(model))

        self.cfBrowser = costFunctionBrowser.MainWindow(
            limits=[10, 40, 10, 40],
            taskPlanner=self.taskPlanner,
            initialState=False)
        self.cfBrowser.setWindowTitle(
            str(self.cfBrowser.windowTitle()) +
            " (Evaluation Results Browser)")
        self.windowManager.addWindow(self.cfBrowser)

        self.nodeFeatureWeights = nodeFeatureWeights.MainWindow()

        self.gggBrowser = gggBrowser.MainWindow()
        self.gggBrowser.setWindowTitle(self.gggBrowser.windowTitle() +
                                       " (Evaluation Results Browser)")
        self.windowManager.addWindow(self.gggBrowser)

        self.contextWindow = context3d.MainWindow()
        self.contextWindow.setWindowTitle(self.contextWindow.windowTitle() +
                                          " - Annotation")
        self.windowManager.addWindow(self.contextWindow)

        self.contextWindowInferred = context3d.MainWindow()
        self.contextWindowInferred.setWindowTitle(
            self.contextWindow.windowTitle() + " - Inferred")
        self.windowManager.addWindow(self.contextWindowInferred)

        self.original_commands_fname = None

        self.figure = mpl.figure()
        self.axes = self.figure.gca()

        self.limits = [10, 40, 10, 40]
        self.restoreLimits()
        self.figure.canvas.mpl_connect('draw_event', self.updateLimits)

        self.resultsModel = resultsModel.Model(self.resultsTable)
        self.resultListModel = resultsModel.Model(self.resultListTable)
        self.nodeResultsModel = nodeResultsModel.Model(self.nodeResultsTable)

        self.model = None
        self.dataset = None

        self.connect(
            self.resultsTable.selectionModel(),
            SIGNAL("selectionChanged ( QItemSelection, QItemSelection )"),
            self.selectResult)

        self.connect(
            self.nodeResultsTable.selectionModel(),
            SIGNAL("selectionChanged ( QItemSelection, QItemSelection )"),
            self.selectNodeResult)

        self.connect(self.sendToCfbButton, SIGNAL("clicked()"), self.sendToCfb)

        self.connect(self.recomputeEntropyButton, SIGNAL("clicked()"),
                     self.recomputeEntropy)
        self.connect(self.saveNodeResultsButton, SIGNAL("clicked()"),
                     self.saveNodeResults)

        self.connect(self.plotEntropyButton, SIGNAL("clicked()"),
                     self.plotEntropy)

        self.connect(self.sendToCfbNodeButton, SIGNAL("clicked()"),
                     self.sendToCfbNode)

        self.connect(self.esdcFilter, SIGNAL("editingFinished()"),
                     self.filterEsdcs)

        self.connect(self.nodeFilter, SIGNAL("editingFinished()"),
                     self.filterNodes)

        print "adding export button"
        self.connect(self.exportResultsButton, SIGNAL("clicked()"),
                     self.sendToClipboard)

        print "adding export button"
        self.connect(self.exportNodeResultsButton, SIGNAL("clicked()"),
                     self.sendToClipboardNodeResults)

        self.filterEsdcs()
        self.filterNodes()
        self.resultsTable.selectRow(0)

        signal.signal(signal.SIGINT, lambda *s: self.cleanup())

    def sendToClipboard(self):
        self.resultsModel.sendToClipboard()

    def sendToClipboardNodeResults(self):
        self.nodeResultsModel.sendToClipboard()

    def sendToCfbNode(self):
        result = self.nodeResultsModel.selectedEntry().best_result
        self.sendResultToCfb(result)

    def sendResultToCfb(self, result):
        self.cfBrowser.updateEsdcs(result.esdcs, result.start_gggs)
        self.cfBrowser.currAnnotation = result.annotation
        self.cfBrowser.setState(result.start_state)

    def sendToCfb(self):
        '''send a command to the cost function browser'''

        selectedResults = self.resultListModel.selectedData()
        selectedResult = selectedResults[0]

        self.sendResultToCfb(selectedResult)

    def filterNodes(self):
        expr = self.nodeFilter.text()
        print expr
        try:
            filterFunction = eval(str(expr))
        except:
            raise
        self.nodeResultsModel.setFilter(filterFunction)
        self.updateNodeLabels()

    def filterEsdcs(self):
        expr = self.esdcFilter.text()
        print expr
        try:
            filterFunction = eval(str(expr))
        except:
            raise
        self.resultsModel.setFilter(filterFunction)

    def plotEntropy(self):
        print "making figure"
        entries = self.nodeResultsModel.selectedEntries()
        plot_entropy_curves(entries, "%d entries " % len(entries))
        mpl.show()

    def saveNodeResults(self):
        entries = self.nodeResultsModel.entries
        for e in entries:
            e.ggg = None
            e.annotation = None
            #e.results = [e.results[0]] + random.sample(e.results[1:], 50)
            for r in e.results:
                r.end_ggg.remove_cost_entries()

        fname = "node_results.pck"
        print "saving", fname, "..."
        pickle_util.save("node_results.pck", entries)
        print "done"

    def recomputeEntropy(self):
        print "making figure"
        fname = "%s/tools/g3/python/g3/evaluator/test_metric.py" % SLU_HOME

        locals_dict = {}
        execfile(fname, globals(), locals_dict)
        metric = locals_dict["metric"]

        entries = self.nodeResultsModel.entries
        for entry in entries:
            entry.entropy = metric(entry.results, entry.node)

        self.nodeResultsModel.reset()
        plot_entropy_curves(entries, "%d entries " % len(entries))
        mpl.show()

    def selectNodeResult(self, newSelection, oldSelection):
        result = self.nodeResultsModel.selectedEntry()
        self.contextWindowInferred.setContext(
            result.annotation.context.withoutPaths())
        self.contextWindowInferred.highlightGroundings([result.labeled_pobj],
                                                       color="green")
        if result.inferred_pobj != None:
            self.contextWindowInferred.highlightGroundings(
                [result.inferred_pobj], color="red")

        highlightTextLabel(self.entireEsdcTextNode, result.node_esdc)

        self.updateNodeLabels()
        self.gggBrowser.load(result.ggg)

    def updateNodeLabels(self):
        self.updateAccuracyLabel(self.nodeResultsModel.selectedEntries(),
                                 self.selectedNodeAccuracyLabel)
        self.updateAccuracyLabel(self.nodeResultsModel.allData(),
                                 self.overallNodeAccuracyLabel)

    def updateAccuracyLabel(self, data, label):
        if len(data) == 0:
            accuracy = 0
            num = 0
            denom = 0
        else:
            num = float(len([r for r in data if r.correct]))
            denom = len(data)
            accuracy = num / denom
        label.setText("%.3f (%d of %d)" % (accuracy, num, denom))

    def selectResult(self, newSelection, oldSelection):
        selectedResults = self.resultsModel.selectedData()
        result = selectedResults[0]
        self.contextWindow.setContext(result.annotation.context)
        self.contextWindowInferred.setContext(
            result.annotation.context.withoutPaths())
        self.contextWindowInferred.highlightGroundings(
            result.end_ggg.groundings, color="cyan")

        highlightTextLabel(self.entireEsdcText, result.esdcs)

        self.resultListModel.loadResults(result)
        self.resultListTable.selectRow(0)
        self.nodeResultsModel.setData(
            compute_node_results(selectedResults,
                                 self.original_commands_fname))
        self.updateNodeLabels()

    def load(self, dataset_fname, results_dir, original_commands_fname):
        self.resultsFile = ResultsFile(results_dir)
        self.original_commands_fname = original_commands_fname
        with open("%s/parameters.yaml" % results_dir, "r") as f:
            self.parameters = yaml.load(f)
            self.cfBrowser.beamWidthBox.setValue(self.parameters['beam_width'])
            self.cfBrowser.seqBeamWidthBox.setValue(
                self.parameters['beam_width_sequence'])
            self.cfBrowser.searchDepthBox.setValue(
                self.parameters['search_depth_event'])
            self.cfBrowser.beamWidthEventBox.setValue(
                self.parameters['beam_width_event'])

        self.agileRunSummaryLog = "%s/agile_run_summary_log.txt" % results_dir
        if (os.path.exists(self.agileRunSummaryLog) == False):
            f = open(self.agileRunSummaryLog, 'w')
            f.write('# ' + results_dir + '\n')
            f.write('# EvaluationTimeSec LCMCommandLog\n')
            f.close()
        self.setWindowTitle("Evaluation Results Browser: " + results_dir)
        self.resultsModel.loadResultsFile(self.resultsFile)

    def updateLimits(self, mplEvent):
        self.saveLimits()

    def saveLimits(self):
        self.limits = self.axes.axis()

    def restoreLimits(self):
        if self.limits != None:
            self.axes.axis(self.limits)
Ejemplo n.º 13
0
class MainWindow(QMainWindow, recipeBrowser_ui.Ui_MainWindow):
    def __init__(self, recipe_manager):
        QMainWindow.__init__(self)
        self.setupUi(self)

        self.recipe_manager = recipe_manager
        self.instructionModel = instructionModel.Model(self.instructionTable)

        self.windowManager = WindowManager(self.windowsMenu)
        self.recipe_manager = RecipeManager("kitchenModel_1.5.pck")
        self.costFunctionBrowser = costFunctionBrowser.MainWindow(
            [0, 10, 0, 10], self.recipe_manager.task_planner)
        self.costFunctionBrowser.save_state_tree = True
        self.costFunctionBrowser.allow_null_action = False

        self.windowManager.addWindow(self.costFunctionBrowser)

        self.connect(
            self.instructionTable.selectionModel(),
            SIGNAL("selectionChanged ( QItemSelection, QItemSelection )"),
            self.selectInstruction)

        self.connect(self.inferPlanButton, SIGNAL("clicked()"), self.inferPlan)

        self.connect(self.sendToRobotButton, SIGNAL("clicked()"),
                     self.sendToRobot)

        self.connect(self.sendInstructionToCfbButton, SIGNAL("clicked()"),
                     self.sendInstructionToCfb)

        self.start_state = None

    def selectInstruction(self):
        print "select instruction"

    def sendInstructionToCfb(self):
        entry = self.instructionModel.selectedEntry()
        esdc, ggg = self.recipe_manager.make_ggg_for_instruction(entry.text)
        self.costFunctionBrowser.gggs = [ggg]
        self.costFunctionBrowser.esdcs = [esdc]

        self.costFunctionBrowser.followCommand(entry.text,
                                               state=entry.start_state,
                                               esdcs=[esdc])

        for plan in self.costFunctionBrowser.plansList:
            result = self.recipe_manager.sequence(plan)
            string = planningLanguage.decompile(result)
            print "planning language", string

    def sendToRobot(self):

        ingredients_str = "\n".join(i.ros_str()
                                    for i in self.start_state.ingredients)

        plan_string = "\n".join(
            [e.annotation for e in self.instructionModel.entries])
        print "send to robot."
        print "ingredients"
        print ingredients_str
        print "plan"
        print plan_string
        self.sendToRos(ingredients_str + '\n' + plan_string)

    def sendToRos(self, string):
        import roslib
        roslib.load_manifest('bakebot')
        from services.bakebot_recipe_service import BakebotClient
        status = BakebotClient.execute_recipe(string, debug=True)
        print "ros status", status

    def inferPlan(self):
        print "follow command"
        recipe_text = str(self.recipeTextEdit.toPlainText())
        sequence = self.recipe_manager.find_plan(recipe_text, self.start_state)
        entries = []
        for i, (instruction, states, cost) in enumerate(sequence):
            if i == 0:
                start_state = self.start_state
            else:
                start_state = sequence[i - 1][1][-1][1]
            annotation = planningLanguage.decompile(states)

            entry = instructionModel.Entry(i, instruction.text, annotation,
                                           start_state, cost)
            entries.append(entry)
        self.instructionModel.setData(entries)

    def load(self, annotated_recipe):
        self.recipeTextEdit.setPlainText(annotated_recipe.instruction_text)
        self.recipeTitleLabel.setText(annotated_recipe.name)
        self.start_state = annotated_recipe.start_state
        self.annotated_recipe = annotated_recipe