def print_R_script(scene, tree, classifier, step_size):

    print "# " + scene.filename + "\n"

    # Some R functions for plotting.
    print "library(scales)" # for alpha blending
    rtools.print_plot_focus_measures(scene.fvalues)

    # The correct classifications.
    classes = [ "left" if classifier(scene, lens_pos) else "right"
                for lens_pos in range(2 * step_size, scene.step_count) ]

    # What we actually get.
    results = []
    for lens_pos in range(2 * step_size, scene.step_count):
        initial_positions = featuresfirststep.first_three_lens_pos(
            lens_pos, step_size)
        first, second, third = scene.get_focus_values(initial_positions)
        norm_lens_pos = float(lens_pos) / (scene.step_count - 1)

        evaluator = featuresfirststep.firststep_feature_evaluator(
            first, second, third, norm_lens_pos)

        evaluation = evaluatetree.evaluate_tree(tree, evaluator)
        results.append(evaluation)

    rtools.print_classification_points(classes, results, ["left", "right"])

    print "# Plot me!\n"
    def _get_first_direction(self):
        """Direction in which we should start sweeping initially."""
        first, second, third = self.camera.get_fvalues(
            self.camera.visited_positions[-3:])
        norm_lens_pos = float(self.initial_pos) / (self.scene.step_count - 1)

        evaluator = featuresfirststep.firststep_feature_evaluator(
            first, second, third, norm_lens_pos)
        return Direction(evaluatetree.evaluate_tree(
            self.params.left_right_tree, evaluator))
def print_R_script(scene, tree, step_size):

    print "# " + scene.filename + "\n"

    # Some R functions for plotting.
    print "library(scales)" # for alpha blending
    rtools.print_plot_focus_measures(scene.fvalues)

    classes = []
    results = []

    for lens_pos in range(2 * step_size, scene.step_count):

        # The correct classification.
        go_left = featuresfirststep.nearest_on_left(scene, lens_pos)
        initial_positions = featuresfirststep.first_three_lens_pos(
            lens_pos, step_size)
        if go_left:
            initial_positions.reverse()
            last_pos = initial_positions[-1]
            if scene.distance_to_closest_left_peak(last_pos) <= 15:
                classes.append("fine")
            else:
                classes.append("coarse")
        else:
            last_pos = initial_positions[-1]
            if scene.distance_to_closest_right_peak(last_pos) <= 15:
                classes.append("fine")
            else:
                classes.append("coarse")

        # The classification obtained by evaluating the decision tree.
        first, second, third = scene.get_focus_values(initial_positions)
        norm_lens_pos = float(lens_pos) / (scene.step_count - 1)


        evaluator = featuresfirststep.firststep_feature_evaluator(
            first, second, third, norm_lens_pos)

        evaluation = evaluatetree.evaluate_tree(tree, evaluator)
        results.append(evaluation)

    rtools.print_classification_points(classes, results, ["coarse", "fine"])

    print "# Plot me!\n"
    def _sweep(self, direction):
        """Sweep the lens in one direction and return a
        tuple (success state, number of steps taken) along the way.
        """
        initial_position = self.camera.last_position()
        sweep_fvalues = [ self.camera.last_fmeasure() ]

        while not self.camera.will_hit_edge(direction):
            # Move the lens forward.
            self.camera.move_coarse(direction)
            sweep_fvalues.append(self.camera.last_fmeasure())

            # Take at least two steps before we allow turning back.
            if len(sweep_fvalues) < 3:
                continue
       
            if self.perfect_classification is None:
                # Obtain the ML classification at the new lens position.
                evaluator = featuresturn.action_feature_evaluator(
                    sweep_fvalues, self.scene.step_count)
                classification = evaluatetree.evaluate_tree(
                    self.params.action_tree, evaluator)
            else:
                key = featuresturn.make_key(str(direction), initial_position, 
                                            self.camera.last_position())
                classification = self.perfect_classification[key]

            if classification != "continue":
                assert (classification == "turn_peak" or
                        classification == "backtrack")
                return classification, len(sweep_fvalues) - 1

        # We've reached an edge, but the decision tree still does not want
        # to turn back, so what do we do now?
        # After thinking a lot about it, I think the best thing to do is to
        # introduce a condition manually. It's a bit ad-hoc, but we really need
        # to be able to handle this case robustly, as there are lot of cases
        # (i.e., landscape shots) where peaks will be at the edge.
        min_val = min(self.camera.get_fvalues(self.camera.visited_positions))
        max_val = max(self.camera.get_fvalues(self.camera.visited_positions))
        if float(min_val) / max_val > 0.8:
            return "backtrack", len(sweep_fvalues) - 1
        else:
            return "turn_peak", len(sweep_fvalues) - 1