aboutsummaryrefslogtreecommitdiff
path: root/src/argaze.test/AreaOfInterest/AOIFeatures.py
blob: 4313500104666cf3fa2b5127c0f65b18007caab3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env python

import unittest
import math

from argaze.AreaOfInterest import AOIFeatures

import numpy

class TestAreaOfInterestClass(unittest.TestCase):
    """Test AreaOfInterest class."""

    def test_new(self):
        """Test AreaOfInterest creation."""
        
        # Check that 0D AreaOfInterest creation is considered as empty
        aoi_0D = AOIFeatures.AreaOfInterest()

        self.assertTrue(aoi_0D.empty)

        # Check 1 point 1D AreaOfInterest creation
        aoi_1D = AOIFeatures.AreaOfInterest([[0]])

        self.assertFalse(aoi_1D.empty)
        self.assertEqual(aoi_1D.dimension, 1)
        self.assertEqual(aoi_1D.size, 1)
        self.assertEqual(aoi_1D.center, [0])

        with self.assertRaises(AssertionError):
            self.assertEqual(aoi_1D.bounding_box, 1)

        # Check 2 points 1D AreaOfInterest creation
        aoi_1D = AOIFeatures.AreaOfInterest([[0], [1]])

        self.assertFalse(aoi_1D.empty)
        self.assertEqual(aoi_1D.dimension, 1)
        self.assertEqual(aoi_1D.size, 2)
        self.assertEqual(aoi_1D.center, [0.5])

        with self.assertRaises(AssertionError):
            self.assertEqual(aoi_1D.bounding_box, 1)

        # Check 4 points 2D AreaOfInterest creation
        aoi_2D = AOIFeatures.AreaOfInterest([[0, 0], [0, 1], [1, 0], [1, 1]])

        self.assertFalse(aoi_2D.empty)
        self.assertEqual(aoi_2D.dimension, 2)
        self.assertEqual(aoi_2D.size, 4)
        self.assertIsNone(numpy.testing.assert_array_equal(aoi_2D.center, [0.5, 0.5]))
        self.assertIsNone(numpy.testing.assert_array_equal(aoi_2D.bounding_box, [[0, 0], [1, 0], [1, 1], [0, 1]]))

        # Check 8 points 3D AreaOfInterest creation
        aoi_3D = AOIFeatures.AreaOfInterest([[0, 0, 0], [0, 1, 0], [1, 0, 0], [1, 1, 0], [0, 0, 1], [0, 1, 1], [1, 0, 1], [1, 1, 1]])

        self.assertFalse(aoi_3D.empty)
        self.assertEqual(aoi_3D.dimension, 3)
        self.assertEqual(aoi_3D.size, 8)

        with self.assertRaises(AssertionError):
            self.assertEqual(aoi_3D.bounding_box, 1)

    def test___repr__(self):
        """Test AreaOfInterest string representation."""

        aoi_2D_int = AOIFeatures.AreaOfInterest([[0, 0], [0, 1], [1, 0], [1, 1]])

        self.assertEqual(repr(aoi_2D_int), "[[0, 0], [0, 1], [1, 0], [1, 1]]")

        aoi_2D_float = AOIFeatures.AreaOfInterest([[0, 0], [0, math.pi], [math.pi, 0], [math.pi, math.pi]])

        self.assertEqual(repr(aoi_2D_float), f'[[0.0, 0.0], [0.0, {repr(math.pi)}], [{repr(math.pi)}, 0.0], [{repr(math.pi)}, {repr(math.pi)}]]')

    def test___str__(self):
        """Test AreaOfInterest string display."""

        aoi_2D_int = AOIFeatures.AreaOfInterest([[0, 0], [0, 1], [1, 0], [1, 1]])

        self.assertEqual(str(aoi_2D_int), "[[0, 0], [0, 1], [1, 0], [1, 1]]")

        aoi_2D_float = AOIFeatures.AreaOfInterest([[0, 0], [0, math.pi], [math.pi, 0], [math.pi, math.pi]])

        self.assertEqual(str(aoi_2D_float), f'[[0.0, 0.0], [0.0, {str(math.pi)}], [{str(math.pi)}, 0.0], [{str(math.pi)}, {str(math.pi)}]]')
    
    def test_clockwise(self):
        """Test AreaOfInterest clockwise method."""

        aoi_2D_clockwise = AOIFeatures.AreaOfInterest([[0, 0], [0, 1], [1, 0], [1, 1]]).clockwise()

        self.assertEqual(type(aoi_2D_clockwise), AOIFeatures.AreaOfInterest)
        self.assertIsNone(numpy.testing.assert_array_equal(aoi_2D_clockwise, [[0, 0], [1, 0], [1, 1], [0, 1]]))

    def test_contains_point(self):
        """Test AreaOfInterest contains_point method."""

        aoi_2D = AOIFeatures.AreaOfInterest([[0, 0], [0, 1], [1, 1], [1, 0]])

        self.assertTrue(aoi_2D.contains_point((0.1, 0.9)))
        self.assertFalse(aoi_2D.contains_point((1.1, 0.9)))

    def test_inner_axis(self):

        aoi_2D = AOIFeatures.AreaOfInterest([[0, 0], [0, 2], [2, 2], [2, 0]])

        self.assertEqual(aoi_2D.inner_axis((1, 1)), (0.5, 0.5))

    def test_outter_axis(self):

        aoi_2D = AOIFeatures.AreaOfInterest([[0, 0], [0, 2], [2, 2], [2, 0]])

        self.assertEqual(aoi_2D.outter_axis((0.5, 0.5)), (1, 1))

    def test_circle_intersection(self):

        aoi_2D = AOIFeatures.AreaOfInterest([[0, 0], [0, 1], [1, 1], [1, 0]])
        intersection, aoi_ratio, circle_ratio = aoi_2D.circle_intersection((0, 0), 1)

        self.assertTrue(math.isclose(aoi_ratio, math.pi / 4, abs_tol=1e-2))
        self.assertTrue(math.isclose(circle_ratio, 1 / 4, abs_tol=1e-3))

class TestAOISceneClass(unittest.TestCase):
    """Test AOIScene class."""

    def test_new(self):
        """Test AOIScene creation."""

        # Check that 0D AOIScene creation fails
        with self.assertRaises(AssertionError):
            aoi_0D_scene = AOIFeatures.AOIScene(0)

        # Check empty 2D AOIScene creation
        aoi_2d_scene = AOIFeatures.AOIScene(2)

        self.assertEqual(aoi_2d_scene.dimension, 2)
        self.assertEqual(len(aoi_2d_scene.items()), 0)

        # Check 2D AOIScene creation
        aoi_2D = AOIFeatures.AreaOfInterest([[0, 0], [0, 1], [1, 1], [1, 0]])
        aoi_2d_scene = AOIFeatures.AOIScene(2, {"A": aoi_2D})

        self.assertEqual(aoi_2d_scene.dimension, 2)
        self.assertEqual(len(aoi_2d_scene.items()), 1)
        self.assertEqual(list(aoi_2d_scene.keys()), ["A"])
        self.assertEqual(len(vars(aoi_2d_scene)), 3)

    def test___set_item__(self):
        """Test AOIScene __set_item__ method."""

        aoi_2d_scene = AOIFeatures.AOIScene(2,)

        # Check AOI with same dimension can be added
        aoi_2d_scene["A"] = AOIFeatures.AreaOfInterest([[0, 0], [0, 1], [1, 1], [1, 0]])

        # Check that AOI with different dimension fails
        with self.assertRaises(AssertionError):
            aoi_2d_scene["B"] = AOIFeatures.AreaOfInterest([[0, 0, 0], [0, 1, 0], [1, 1, 0], [1, 0, 0]])

    def test___repr__(self):
        """Test AOIScene string representation."""

        # Check empty 2D AOIScene representation
        self.assertEqual(repr(AOIFeatures.AOIScene(2)), "{}")

        # Check 2D AOIScene representation with integer AOI
        aoi_2D_int = AOIFeatures.AreaOfInterest([[0, 0], [0, 1], [1, 1], [1, 0]])
        aoi_2d_scene = AOIFeatures.AOIScene(2, {"A": aoi_2D_int})

        self.assertEqual(repr(aoi_2d_scene), "{'A': [[0, 0], [0, 1], [1, 1], [1, 0]]}")

        # Check 2D AOIScene representation with float AOI
        aoi_2D_float = AOIFeatures.AreaOfInterest([[0, 0], [0, math.pi], [math.pi, 0], [math.pi, math.pi]])
        aoi_2d_scene = AOIFeatures.AOIScene(2, {"A": aoi_2D_float})

        expected_representation = "{'A': " + f'[[0.0, 0.0], [0.0, {repr(math.pi)}], [{repr(math.pi)}, 0.0], [{repr(math.pi)}, {repr(math.pi)}]]' + "}"
        self.assertEqual(repr(aoi_2d_scene), expected_representation)

    def test_properties(self):
        """Test AOIScene properties."""

        aoi_2D_A = AOIFeatures.AreaOfInterest([[0, 0], [0, 1], [1, 1], [1, 0]])
        aoi_2D_B = AOIFeatures.AreaOfInterest([[1, 1], [1, 2], [2, 2], [2, 1]])
        aoi_2d_scene = AOIFeatures.AOIScene(2, {"A": aoi_2D_A, "B": aoi_2D_B})

        self.assertEqual(aoi_2d_scene.dimension, 2)
        self.assertEqual(len(aoi_2d_scene.items()), 2)
        self.assertEqual(list(aoi_2d_scene.keys()), ["A", "B"])
        self.assertIsNone(numpy.testing.assert_array_equal(aoi_2d_scene.bounds, [[0, 0], [2, 2]]))
        self.assertIsNone(numpy.testing.assert_array_equal(aoi_2d_scene.center, [1, 1]))
        self.assertIsNone(numpy.testing.assert_array_equal(aoi_2d_scene.size, [2, 2]))

if __name__ == '__main__':

    unittest.main()