def simulate_samples(scenes, features, step_size, params):
    """Does a few random simulations and prints an R 
    script for visualization."""

    # Need to set the parameters so that all the instances are kept.
    params.outlierHandling = OutlierHandling.WEIGHTING
    params.uniformSamplingRate = 1.0

    for _ in range(20):
        scene = random.choice(scenes)
        instances = []

        # Always sweep right to make things easier. So invert the focus
        # measures half the time.
        if random.random() < 0.5:
            scene = scene.inverse_copy()

        lens_pos = random.randint(step_size * 2 + 1, scene.step_count - 1)

        initial_lens_positions = first_three_lens_pos(lens_pos, step_size)
        lens_positions = simulate_sweep(scene, features, instances,
            initial_lens_positions, +1, get_move_right_classification, params)

        # Don't want the initial three positions we used to decide whether
        # to move left or right anymore.
        lens_positions = lens_positions[3:]

        print_R_script(scene, lens_positions, instances)
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 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 get_instances(scenes, classifier, position_selector, features, step_size):
    """Create an array of instances in ARFF string representation."""
    lines = []

    for scene in scenes:
        for lens_pos in position_selector(scene):
            # Data at this lens position.
            initial_positions = featuresfirststep.first_three_lens_pos(
                lens_pos, step_size)
            first, second, third = scene.get_focus_values(initial_positions)
            normalized_lens_pos = float(lens_pos) / (scene.step_count - 1)

            # Evaluate features on data at this lens position.
            values = [feature(first=first, second=second, third=third,
                              lens_pos=normalized_lens_pos)
                      for _, _, feature in features]

            classification = (",left" if classifier(scene, lens_pos)
                                      else ",right")
            instance = ','.join(str(convert_true_false(value))
                                  for value in values) + classification
            lines.append(instance)

    return lines
def get_instances(scenes, classifier, features, step_size):
    """Create an array of instances in ARFF string representation."""
    lines = []

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

            go_left = classifier(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:
                    classification = ",fine"
                else:
                    classification = ",coarse"
            else:
                last_pos = initial_positions[-1]
                if scene.distance_to_closest_right_peak(last_pos) <= 15:
                    classification = ",fine"
                else:
                    classification = ",coarse"

            first, second, third = scene.get_focus_values(initial_positions)
            normalized_lens_pos = float(lens_pos) / (scene.step_count - 1)
            values = [feature(first=first, second=second, third=third,
                              lens_pos=normalized_lens_pos)
                      for _, _, feature in features]

            instance = ','.join(str(convert_true_false(value))
                                  for value in values) + classification
            lines.append(instance)

    return lines