예제 #1
0
    def __init__(self, puzzle_location):
        """

        Args:
            puzzle_location (PuzzleLocation): Puzzle location information in the PuzzleLocation class format.
        """
        self._puzzle_location = puzzle_location
        self._neighbor_side_list = [None for _ in xrange(0, PuzzlePieceSide.get_numb_sides())]
        self._numb_neighbors = 0
예제 #2
0
    def test_accuracy_calculator(self):

        # Build a known test puzzle.
        dummy_puzzle = PuzzleTester.build_dummy_puzzle()
        for piece in dummy_puzzle.pieces:
            piece._assign_to_original_location()
            piece._set_id_number_to_original_id()
            piece.rotation = PuzzlePieceRotation.degree_0

        # Test different conditions
        for i in xrange(0, 2):

            if i == 0:
                # Set all piece rotation to the default
                for piece in dummy_puzzle.pieces:
                    piece.rotation = PuzzlePieceRotation.degree_0
            elif i == 1:
                dummy_puzzle.pieces[0].rotation = PuzzlePieceRotation.degree_90

            # Build the results information collection
            results_information = PuzzleResultsCollection([dummy_puzzle.pieces], "." + os.sep + "dummy.jpg")
            results_information.calculate_accuracies([dummy_puzzle])

            # Verify the neighbor results
            numb_pieces = len(dummy_puzzle.pieces)
            results = results_information.results[0]

            if i == 0:
                assert results.standard_direct_accuracy.numb_correct_placements == numb_pieces
                assert results.standard_direct_accuracy.numb_wrong_rotation == 0

                assert results.modified_direct_accuracy.numb_correct_placements == numb_pieces
                assert results.modified_direct_accuracy.numb_wrong_rotation == 0
            elif i == 1:
                assert results.standard_direct_accuracy.numb_correct_placements == numb_pieces - 1
                assert results.standard_direct_accuracy.numb_wrong_rotation == 1

                assert results.modified_direct_accuracy.numb_correct_placements == numb_pieces - 1
                assert results.modified_direct_accuracy.numb_wrong_rotation == 1

            assert results.standard_direct_accuracy.numb_different_puzzle == 0
            assert results.standard_direct_accuracy.numb_wrong_location == 0
            assert results.standard_direct_accuracy.total_numb_pieces_in_solved_puzzle == numb_pieces

            assert results.modified_direct_accuracy.numb_wrong_location == 0
            assert results.modified_direct_accuracy.total_numb_pieces_in_solved_puzzle == numb_pieces

            # Check the neighbor accuracy
            numb_sides = PuzzlePieceSide.get_numb_sides()
            if i == 0:
                assert results.modified_neighbor_accuracy.correct_neighbor_count == numb_sides * numb_pieces
                assert results.modified_neighbor_accuracy.wrong_neighbor_count == 0
            elif i == 1:
                assert results.modified_neighbor_accuracy.correct_neighbor_count == numb_sides * numb_pieces - 5
                assert results.modified_neighbor_accuracy.wrong_neighbor_count == 5
            assert results.modified_neighbor_accuracy.total_numb_pieces_in_solved_puzzle == numb_pieces
예제 #3
0
    def density(self):
        """
        Calculates the best buddy density for the original image.  It is defined as the total number of best
        buddies divided by the total number of possible best buddies (i.e. number of pieces multiplied by the number
        of sides per piece).

        Returns (float):
            Best buddy density across the entire image.

        """
        return 1.0 * self.total_number_of_best_buddies / (self.numb_pieces * PuzzlePieceSide.get_numb_sides())
예제 #4
0
    def __init__(self, image_file_path, piece_width, puzzle_type, distance_function):

        # Store the information about the input image
        self._filename_root = Puzzle.get_filename_without_extension(image_file_path)
        self._file_extension = Puzzle.get_file_extension(image_file_path)
        # File extension should not include the period
        assert "." not in self._file_extension

        logging.info("Performing best buddy analysis for image: %s" % self._filename_root)

        self.puzzle_type = puzzle_type

        # Consider both interior and exterior best buddies.
        self._numb_wrong_exterior_bb = 0
        self._total_numb_interior_bb = 0
        self._numb_wrong_interior_bb = 0

        # Build a puzzle
        self._puzzle = Puzzle(0, image_file_path, piece_width)

        self.numb_pieces = self._puzzle.numb_pieces

        # Get the piece IDs
        self._puzzle.assign_all_piece_id_numbers_to_original_id()
        self._puzzle.assign_all_pieces_to_original_location()
        self._puzzle.assign_all_pieces_to_same_rotation(PuzzlePieceRotation.degree_0)

        # Calculate the inter-piece distance
        self._interpiece_distance = InterPieceDistance(self._puzzle.pieces, distance_function, puzzle_type)

        # Get the link between number of test buddies and accuracy
        self._numb_best_buddies_versus_accuracy = np.zeros((PuzzlePieceSide.get_numb_sides() + 1,
                                                            PuzzlePieceSide.get_numb_sides() + 1,
                                                            PuzzlePieceSide.get_numb_sides() + 1),
                                                           np.uint32)
        # Store the location of each piece
        self._piece_locations, _ = self._puzzle.build_placed_piece_info()
예제 #5
0
 def __init__(self):
     # Store the location of the open locations
     self._open_locations = {}
     # Depending on the number of best buddies, you are placed in a different tier of dictionary
     self._multiside_open_slots_lists = [None for _ in xrange(0, PuzzlePieceSide.get_numb_sides())]
예제 #6
0
    def analyze_piece_best_buddy_info(self, piece):
        """
        Analyze the best buddy information for a single piece.

        Args:
            piece (PuzzlePiece): Puzzle piece whose best buddy info will be analyzed
        """

        # Get the neighbor location and sides
        neighbor_loc_and_sides = piece.get_neighbor_locations_and_sides()
        original_neighbor_id_and_side = piece.original_neighbor_id_numbers_and_sides

        # Initialize the counters for the piece on the total number of best best buddies and how many are wrong
        numb_piece_bb = 0
        numb_wrong_interior_bb = 0
        numb_wrong_exterior_bb = 0

        # Reset the image coloring
        piece.reset_image_coloring_for_polygons()

        # Iterate through all sides
        for i in xrange(PuzzlePieceSide.get_numb_sides()):

            # Get the neighbor location
            (neighbor_loc, piece_side) = neighbor_loc_and_sides[i]
            neighbor_id_and_side = original_neighbor_id_and_side[i]

            # Assert neighbor and piece side are complementary
            if neighbor_id_and_side is not None:
                (neighbor_id, neighbor_side) = neighbor_id_and_side
                assert piece_side == neighbor_side
            else:
                neighbor_id = -sys.maxint

            # Get the best buddy information for the piece.
            bb_info = self._interpiece_distance.best_buddies(piece.id_number, piece_side)
            if not bb_info:
                piece.results_image_polygon_coloring(piece_side, PieceSideBestBuddyAccuracyResult.no_best_buddy)
                continue
            # Increment the best buddy count
            numb_piece_bb += 1

            # Check if there is a neighbor
            if (neighbor_loc[0] < 0 or neighbor_loc[0] >= self._puzzle.grid_size[0]
                    or neighbor_loc[1] < 0 or neighbor_loc[1] >= self._puzzle.grid_size[1]
                    or self._piece_locations[neighbor_loc] == Puzzle.MISSING_PIECE_PUZZLE_INFO_VALUE):
                # If the neighboring cell is empty and it has a best buddy, it is wrong
                self._numb_wrong_exterior_bb += 1
                numb_wrong_exterior_bb += 1
                piece.results_image_polygon_coloring(piece_side, PieceSideBestBuddyAccuracyResult.wrong_best_buddy)

            # Piece has a neighbor
            else:
                # Increment interior best buddy count
                self._total_numb_interior_bb += 1
                # TODO currently only supports single best buddy
                bb_info = bb_info[0]
                if neighbor_id != bb_info[0] or piece_side.complementary_side != bb_info[1]:
                    numb_wrong_interior_bb += 1
                    self._numb_wrong_interior_bb += 1
                    piece.results_image_polygon_coloring(piece_side, PieceSideBestBuddyAccuracyResult.wrong_best_buddy)
                else:
                    piece.results_image_polygon_coloring(piece_side, PieceSideBestBuddyAccuracyResult.correct_best_buddy)
        # Update the master data structure showing the best buddy distribution
        numpy_index = ImageBestBuddyStatistics.best_buddies_versus_accuracy_tuple(numb_piece_bb,
                                                                                  numb_wrong_interior_bb,
                                                                                  numb_wrong_exterior_bb)
        # Increment the statistics
        self._numb_best_buddies_versus_accuracy[numpy_index] += 1