aboutsummaryrefslogtreecommitdiff
path: root/src/argaze.test/GazeFeatures.py
blob: cbe6056fd935a523b53b392b322dd39e7b0b8778 (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env python

import unittest

from argaze import GazeFeatures

import numpy

def random_gaze_positions(size):
    """ Generate random TimeStampedGazePsoitions for testing purpose.
        Timestamps are current time.
        GazePositions are random values.
    """

    import random
    import time

    ts_gaze_positions = GazeFeatures.TimeStampedGazePositions()
    
    for i in range(0, size):

        # Edit gaze position
        random_gaze_position = GazeFeatures.GazePosition((random.random(), random.random()))

        # Store gaze position
        ts_gaze_positions[time.time()] = random_gaze_position

    return ts_gaze_positions

class TestGazePositionClass(unittest.TestCase):
    """Test GazePosition class."""

    def test_new(self):
        """Test GazePosition creation."""
        
        # Check empty GazePosition
        empty_gaze_position = GazeFeatures.GazePosition()

        self.assertEqual(empty_gaze_position.value, (0, 0))
        self.assertEqual(empty_gaze_position[0], 0)
        self.assertEqual(empty_gaze_position[1], 0)

        for v in empty_gaze_position:
            self.assertEqual(v, 0)

        self.assertEqual(empty_gaze_position.precision, 0.)
        self.assertEqual(len(empty_gaze_position), 2)
        self.assertEqual(empty_gaze_position.valid, True)
        self.assertEqual(numpy.array(empty_gaze_position).shape, (2,))

        # Check integer GazePosition
        int_gaze_position = GazeFeatures.GazePosition((123, 456), precision=55)

        self.assertEqual(int_gaze_position.value, (123, 456))
        self.assertEqual(int_gaze_position[0], 123)
        self.assertEqual(int_gaze_position[1], 456)
        self.assertEqual(int_gaze_position.precision, 55)
        self.assertEqual(len(int_gaze_position), 2)
        self.assertEqual(int_gaze_position.valid, True)
        self.assertEqual(numpy.array(empty_gaze_position).shape, (2,))

        # Check float GazePosition
        float_gaze_position = GazeFeatures.GazePosition((1.23, 4.56), precision=5.5)

        self.assertEqual(float_gaze_position.value, (1.23, 4.56))
        self.assertEqual(float_gaze_position[0], 1.23)
        self.assertEqual(float_gaze_position[1], 4.56)
        self.assertEqual(float_gaze_position.precision, 5.5)
        self.assertEqual(len(float_gaze_position), 2)
        self.assertEqual(float_gaze_position.valid, True)
        self.assertEqual(numpy.array(empty_gaze_position).shape, (2,))

    def test_properties(self):
        """Test GazePosition properties cannot be modified after creation."""

        gaze_position = GazeFeatures.GazePosition()

        # Check that gaze position value setting fails
        with self.assertRaises(AttributeError):

            gaze_position.value = (123, 456)

        self.assertNotEqual(gaze_position.value, (123, 456))
        self.assertEqual(gaze_position.value, (0, 0))

    def test_overlapping(self):
        """Test GazePosition overlap method."""

        gaze_position_A = GazeFeatures.GazePosition((0, 0), precision=10)
        gaze_position_B = GazeFeatures.GazePosition((10, 0), precision=10)
        gaze_position_C = GazeFeatures.GazePosition((5, 0), precision=5)
        gaze_position_D = GazeFeatures.GazePosition((4, 0), precision=5)

        self.assertFalse(gaze_position_A.overlap(gaze_position_B))
        self.assertTrue(gaze_position_A.overlap(gaze_position_C))
        self.assertFalse(gaze_position_A.overlap(gaze_position_C, both=True))

        self.assertFalse(gaze_position_B.overlap(gaze_position_A))
        self.assertTrue(gaze_position_B.overlap(gaze_position_C))
        self.assertFalse(gaze_position_B.overlap(gaze_position_C, both=True))

        self.assertFalse(gaze_position_C.overlap(gaze_position_A))
        self.assertFalse(gaze_position_C.overlap(gaze_position_B))

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

        # Check empty GazePosition representation
        self.assertEqual(repr(GazeFeatures.GazePosition()), "{\"value\": [0, 0], \"precision\": 0.0}")

class TestUnvalidGazePositionClass(unittest.TestCase):
    """Test UnvalidGazePosition class."""

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

        import math
        
        unvalid_gaze_position = GazeFeatures.UnvalidGazePosition()

        self.assertEqual(unvalid_gaze_position.value, (None, None))
        self.assertEqual(unvalid_gaze_position.precision, None)
        self.assertEqual(unvalid_gaze_position.valid, False)

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

        self.assertEqual(repr(GazeFeatures.UnvalidGazePosition()), "{\"message\": null, \"value\": [null, null], \"precision\": null}")

class TestTimeStampedGazePositionsClass(unittest.TestCase):
    """Test TimeStampedGazePositions class."""

    def test___setitem__(self):
        """Test __setitem__ method."""

        ts_gaze_positions = GazeFeatures.TimeStampedGazePositions()
        ts_gaze_positions[0] = GazeFeatures.GazePosition()
        ts_gaze_positions[1] = GazeFeatures.UnvalidGazePosition()
        ts_gaze_positions[2] = {"value": (0, 0), "precision": 0.}

        # Check GazePosition is correctly stored and accessible as a GazePosition
        self.assertIsInstance(ts_gaze_positions[0], GazeFeatures.GazePosition)
        self.assertEqual(ts_gaze_positions[0].valid, True)

        # Check UnvalidGazePosition is correctly stored and accessible as a UnvalidGazePosition
        self.assertIsInstance(ts_gaze_positions[1], GazeFeatures.UnvalidGazePosition)
        self.assertEqual(ts_gaze_positions[1].valid, False)

        # Check dict with "value" and "precision" keys is correctly stored and accessible as a GazePosition
        self.assertIsInstance(ts_gaze_positions[2], GazeFeatures.GazePosition)
        self.assertEqual(ts_gaze_positions[2].valid, True)

        # Check that bad data type insertion fails
        with self.assertRaises(AssertionError):

            ts_gaze_positions[3] = "This string is not a gaze position value."

        # Check that dict with bad keys insertion fails
        with self.assertRaises(AssertionError):

            ts_gaze_positions[4] = {"bad_key": (0, 0), "precision": 0.}

        # Check final lenght
        self.assertEqual(len(ts_gaze_positions), 3)

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

        ts_gaze_positions = GazeFeatures.TimeStampedGazePositions()

        self.assertEqual(repr(GazeFeatures.TimeStampedGazePositions()), "{}")

        ts_gaze_positions[0] = GazeFeatures.GazePosition()
        
        self.assertEqual(repr(ts_gaze_positions), "{\"0\": {\"value\": [0, 0], \"precision\": 0.0}}")

        ts_gaze_positions[0] = GazeFeatures.UnvalidGazePosition()

        self.assertEqual(repr(ts_gaze_positions), "{\"0\": {\"message\": null, \"value\": [null, null], \"precision\": null}}")

    def test_as_dataframe(self):
        """Test inherited as_dataframe method."""

        ts_gaze_positions_dataframe = random_gaze_positions(10).as_dataframe()

        # Check dataframe conversion
        self.assertEqual(ts_gaze_positions_dataframe.index.name, "timestamp")
        self.assertEqual(ts_gaze_positions_dataframe.index.size, 10)

        self.assertEqual(ts_gaze_positions_dataframe.columns.size, 2)
        self.assertEqual(ts_gaze_positions_dataframe.columns[0], "value")
        self.assertEqual(ts_gaze_positions_dataframe.columns[1], "precision")

        self.assertEqual(ts_gaze_positions_dataframe["value"].dtype, 'object')
        self.assertEqual(ts_gaze_positions_dataframe["precision"].dtype, 'float64')

        # Check unvalid position conversion
        ts_gaze_positions = GazeFeatures.TimeStampedGazePositions()
        ts_gaze_positions[0] = GazeFeatures.UnvalidGazePosition()
        ts_gaze_positions_dataframe = ts_gaze_positions.as_dataframe()

        self.assertEqual(ts_gaze_positions_dataframe.index.name, "timestamp")
        self.assertEqual(ts_gaze_positions_dataframe.index.size, 1)

        self.assertEqual(ts_gaze_positions_dataframe.columns.size, 2)
        self.assertEqual(ts_gaze_positions_dataframe.columns[0], "value")
        self.assertEqual(ts_gaze_positions_dataframe.columns[1], "precision")

        self.assertEqual(ts_gaze_positions_dataframe["value"].dtype, 'object')
        self.assertEqual(ts_gaze_positions_dataframe["precision"].dtype, 'O') # Python object type

if __name__ == '__main__':

    unittest.main()