Ejemplo n.º 1
0
    def test_grouping_two_objects(self):
        obj_a = Object(1, (1, 0), [
            (0, 0),
            (1, 0),
        ], 0)
        obj_b = Object(1, (0, 4), [
            (0, 0),
            (1, 0),
        ], 1)
        frame_model = FrameModel(4, 3, 0, [
            obj_a,
            obj_b,
        ])
        expected_grouped_objects: Dict[ObjectKind, List[ObjectId]] = {
            obj_a.get_object_kind(): [obj_a.id, obj_b.id]
        }
        expected_object_group = expected_grouped_objects[
            obj_a.get_object_kind()]

        actual_grouped_objects = frame_model.group_same_objects()
        actual_object_group = actual_grouped_objects[obj_a.get_object_kind()]

        self.assertEqual(1, len(list(actual_grouped_objects.keys())))
        self.assertEqual(2, len(actual_object_group))

        self.assertEqual(list(expected_grouped_objects.keys()),
                         list(actual_grouped_objects.keys()))
        self.assertEqual(expected_object_group, actual_object_group)
Ejemplo n.º 2
0
    def get_frame_similarity(frame_model_a: FrameModel,
                             frame_model_b: FrameModel) -> float:
        """
        Returns how similar frame A is to frame B
        Similarity is the percentage of squares that are the same in each frame
        - the number of correct squares / the total number of squares

        If the numbers of rows or columns don't match, this returns 0 - might be too harsh

        :param frame_model_a:
        :param frame_model_b:
        :return: the percentage of squares that are the same in each frame
        """
        if (  # might be too harsh
                frame_model_a.number_of_rows != frame_model_b.number_of_rows
                or frame_model_a.number_of_columns !=
                frame_model_b.number_of_columns):
            return 0

        grid_a_array = frame_model_a.to_grid().grid_array
        grid_b = frame_model_b.to_grid()

        number_of_correct_squares = 0
        total_number_of_squares = 0
        for y, row in enumerate(grid_a_array):
            for x, square_colour_a in enumerate(row):
                if square_colour_a == grid_b.get_colour(x, y):
                    number_of_correct_squares = number_of_correct_squares + 1

                total_number_of_squares = total_number_of_squares + 1

        return number_of_correct_squares / total_number_of_squares
Ejemplo n.º 3
0
    def test_making_frame_model_from_grid_with_one_object(self):
        g = [
            [0, 0, 0, 0, 0, 0],
            [0, 0, 3, 0, 0, 0],
            [0, 3, 0, 3, 0, 0],
            [0, 0, 3, 0, 3, 0],
            [0, 0, 0, 3, 0, 0],
        ]
        grid = Grid(g)

        frame_model = FrameModel.create_from_grid(grid)
        self.assertEqual(
            6,
            frame_model.number_of_columns
        )
        self.assertEqual(
            5,
            frame_model.number_of_rows
        )

        self.assertEqual(
            0,
            frame_model.background_colour
        )

        self.assertEqual(
            1,
            len(frame_model.objects)
        )
Ejemplo n.º 4
0
    def test_identical_frames(self):
        frame_model_a = FrameModel.create_from_grid(
            Grid([
                [0, 0, 0],
                [0, 0, 0],
                [0, 0, 0],
            ]))
        frame_model_b = FrameModel.create_from_grid(
            Grid([
                [0, 0, 0],
                [0, 0, 0],
                [0, 0, 0],
            ]))

        self.assertEqual(
            1, ARCDriver.get_frame_similarity(frame_model_a, frame_model_b))
Ejemplo n.º 5
0
    def test_frames_of_different_sizes(self):
        frame_model_a = FrameModel.create_from_grid(
            Grid([
                [1, 1, 3],
                [3, 2, 2],
                [3, 2, 2],
            ]))
        frame_model_b = FrameModel.create_from_grid(
            Grid([
                [0, 0, 0],
                [0, 2, 2],
                [0, 2, 2],
                [0, 2, 2],
            ]))

        self.assertEqual(
            0, ARCDriver.get_frame_similarity(frame_model_a, frame_model_b))
Ejemplo n.º 6
0
    def test_grouping_one_object(self):
        obj = Object(1, (1, 0), [
            (0, 0),
            (1, 0),
        ], 0)
        frame_model = FrameModel(3, 3, 0, [obj])
        expected_grouped_objects: Dict[ObjectKind, List[ObjectId]] = {
            obj.get_object_kind(): [obj.id]
        }
        expected_object_group = expected_grouped_objects[obj.get_object_kind()]

        actual_grouped_objects = frame_model.group_same_objects()
        actual_object_group = actual_grouped_objects[obj.get_object_kind()]
        self.assertEqual(1, len(list(actual_grouped_objects.keys())))
        self.assertEqual(1, len(actual_object_group))

        self.assertEqual(list(expected_grouped_objects.keys()),
                         list(actual_grouped_objects.keys()))
        self.assertEqual(expected_object_group, actual_object_group)
Ejemplo n.º 7
0
    def make_frames_from_task(
            self, train_or_test: str) -> List[Dict[str, FrameModel]]:
        frames: List[Dict[str, FrameModel]] = []

        pairs = self.task[train_or_test]
        number_of_pairs = len(pairs)
        for pair_number in range(number_of_pairs):
            input_matrix = pairs[pair_number]['input']
            input_grid = Grid(input_matrix)
            input_frame_model = FrameModel.create_from_grid(input_grid)

            output_matrix = pairs[pair_number]['output']
            output_grid = Grid(output_matrix)
            output_frame_model = FrameModel.create_from_grid(output_grid)
            frames.append({
                "input": input_frame_model,
                "output": output_frame_model,
            })

        return frames
Ejemplo n.º 8
0
    def test_grouping_two_objects_where_one_is_rotated_270_degrees(self):
        g = [
            [0, 1, 1, 1],
            [0, 0, 0, 1],
            [0, 1, 0, 0],
            [0, 1, 0, 0],
            [1, 1, 0, 0],
        ]
        obj_a = Object(1, (1, 0), [
            (0, 0),
            (1, 0),
            (2, 0),
            (2, 1),
        ], 0)
        obj_b = Object(1, (2, 3), [
            (1, 0),
            (1, 1),
            (1, 2),
            (0, 2),
        ], 1)
        frame_model = FrameModel(5, 4, 0, [
            obj_a,
            obj_b,
        ])
        expected_grouped_objects: Dict[ObjectKind, List[ObjectId]] = {
            obj_a.get_object_kind(): [obj_a.id, obj_b.id]
        }
        expected_object_group = expected_grouped_objects[
            obj_a.get_object_kind()]

        actual_grouped_objects = frame_model.group_same_objects()
        actual_object_group = actual_grouped_objects[obj_a.get_object_kind()]

        self.assertEqual(1, len(list(actual_grouped_objects.keys())))
        self.assertEqual(2, len(actual_object_group))

        self.assertEqual(list(expected_grouped_objects.keys()),
                         list(actual_grouped_objects.keys()))
        self.assertEqual(expected_object_group, actual_object_group)
Ejemplo n.º 9
0
    def test_making_grid_from_frame_model_with_one_object(self):
        g = [
            [0, 0, 0, 0, 0, 0],
            [0, 0, 3, 0, 0, 0],
            [0, 3, 0, 3, 0, 0],
            [0, 0, 3, 0, 3, 0],
            [0, 0, 0, 3, 0, 0],
        ]
        grid = Grid(g)

        frame_model = FrameModel.create_from_grid(grid)
        grid2 = frame_model.to_grid()
        self.assertSequenceEqual(grid.grid_array, grid2.grid_array)
Ejemplo n.º 10
0
 def test_making_grid_from_frame_model_with_an_occluded_object(self):
     frame_model = FrameModel(
         3, 3, 0, {
             1: Object(1, (0, 0), [
                 (0, 0),
                 (1, 0),
                 (0, 1),
                 (1, 1),
             ], 0),
             2: Object(2, (1, 1), [
                 (0, 0),
                 (1, 0),
                 (0, 1),
                 (1, 1),
             ], -1)
         }, [])
     grid = frame_model.to_grid()
     self.assertSequenceEqual([
         [1, 1, 0],
         [1, 1, 2],
         [0, 2, 2],
     ], grid.grid_array)
Ejemplo n.º 11
0
    def test_matching_one_object_out_of_three_across_frames(self):
        obj_1 = Object(
            1,
            (1, 1),
            [
                (0, 0),
                (1, 0),
                (2, 0),
                (2, 1),
            ],
            0,
            1
        )
        obj_2 = Object(
            2,
            (1, 1),
            [
                (0, 0),
                (1, 0),
                (2, 0),
                (2, 1),
            ],
            0,
            2
        )
        obj_3 = Object(
            1,
            (1, 1),
            [
                (0, 0),
                (1, 0),
                (2, 0),
                (2, 2),
            ],
            0,
            3
        )

        obj_1b = Object(
            1,
            (1, 1),
            [
                (0, 0),
                (1, 0),
                (2, 0),
                (2, 1),
            ],
            0,
            4
        )
        obj_2b = Object(
            3,
            (1, 1),
            [
                (0, 0),
                (1, 0),
                (2, 0),
                (2, 1),
            ],
            0,
            5
        )
        obj_3b = Object(
            1,
            (1, 1),
            [
                (0, 1),
                (1, 0),
                (2, 0),
                (2, 1),
            ],
            0,
            6
        )

        original_frame_model = FrameModel(
            5,
            5,
            0,
            [
                obj_1,
                obj_2,
                obj_3,
            ]
        )
        second_frame_model = FrameModel(
            5,
            5,
            0,
            [
                obj_1b,
                obj_2b,
                obj_3b,
            ]
        )

        self.assertSequenceEqual(
            [
                4,
                5,
                6
            ],
            list(second_frame_model.objects.keys())
        )

        second_frame_model_after_match = ObjectRuntime.match_objects_in_second_frame_to_those_in_first(
            original_frame_model, second_frame_model
        )
        matched_second_object = second_frame_model_after_match.objects[obj_1.id]

        self.assertSequenceEqual(
            [
                1,
                5,
                6
            ],
            list(second_frame_model_after_match.objects.keys())
        )
        self.assertEqual(
            obj_1.colour,
            matched_second_object.colour,
        )
        self.assertEqual(
            obj_1.top_left_offset,
            matched_second_object.top_left_offset,
        )
        self.assertSequenceEqual(
            obj_1.relative_positions,
            matched_second_object.relative_positions,
        )