aboutsummaryrefslogtreecommitdiff
path: root/src/argaze.test/PupillFeatures.py
blob: f1abb6600d4a5ff759fe47b05020d6e2a9c30dd8 (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
""" 

This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
"""

__author__ = "Théo de la Hogue"
__credits__ = []
__copyright__ = "Copyright 2023, Ecole Nationale de l'Aviation Civile (ENAC)"
__license__ = "GPLv3"

import unittest
import math

from argaze import PupillFeatures

import numpy

def random_pupill_diameters(size):
    """ Generate random TimeStampedPupillDiameters for testing purpose.
        Timestamps are current time.
        PupillDiameters are random values.
    """

    import random
    import time

    ts_pupill_diameters = PupillFeatures.TimeStampedPupillDiameters()
    
    for i in range(0, size):

        # Edit pupill diameter
        random_pupill_diameter = PupillFeatures.PupillDiameter(random.random())

        # Store pupill diameter
        ts_pupill_diameters[time.time()] = random_pupill_diameter

    return ts_pupill_diameters

class TestPupillDiameterClass(unittest.TestCase):
    """Test PupillDiameter class."""

    def test_new(self):
        """Test PupillDiameter creation."""
        
        # Check empty PupillDiameter
        empty_pupill_diameter = PupillFeatures.PupillDiameter()

        self.assertEqual(empty_pupill_diameter, math.nan)

        # Check float PupillDiameter
        float_pupill_diameter = PupillFeatures.PupillDiameter(1.23)

        self.assertEqual(float_pupill_diameter, 1.23)

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

        pupill_diameter = PupillFeatures.PupillDiameter()

        # Check that pupill diameter value setting fails
        with self.assertRaises(AttributeError):

            pupill_diameter = 123

        self.assertNotEqual(pupill_diameter, 123)
        self.assertEqual(pupill_diameter, math.nan)

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

        # Check empty PupillDiameter representation
        self.assertEqual(repr(PupillFeatures.PupillDiameter()), "{\"value\": NaN}")

class TestTimeStampedPupillDiametersClass(unittest.TestCase):
    """Test TimeStampedPupillDiameters class."""

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

        ts_pupill_diameters = PupillFeatures.TimeStampedPupillDiameters([
            PupillFeatures.PupillDiameter(),
            PupillFeatures.PupillDiameter(0.63),
            {"value": 1.23}
        ])

        # Check empty PupillDiameter is correctly stored and accessible as a PupillDiameter
        self.assertIsInstance(ts_pupill_diameters[0], PupillFeatures.PupillDiameter)
        self.assertEqual(ts_pupill_diameters[0], math.nan)

        # Check PupillDiameter is correctly stored and accessible as a PupillDiameter
        self.assertIsInstance(ts_pupill_diameters[1], PupillFeatures.PupillDiameter)
        self.assertEqual(ts_pupill_diameters[0], 0.63)

        # Check dict with "value" key is correctly stored and accessible as a PupillDiameter
        self.assertIsInstance(ts_pupill_diameters[2], PupillFeatures.PupillDiameter)
        self.assertEqual(ts_pupill_diameters[0], 1.23)

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

            ts_pupill_diameters[3] = "This string is not a pupill diameter value."

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

            ts_pupill_diameters[4] = {"bad_key": 0.}

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

        ts_pupill_diameters = PupillFeatures.TimeStampedPupillDiameters()

        self.assertEqual(repr(PupillFeatures.TimeStampedPupillDiameters()), "[]")

        ts_pupill_diameters.append(PupillFeatures.PupillDiameter())
        
        self.assertEqual(repr(ts_pupill_diameters), "[{\"value\": NaN, \"timestamp\": 0}]")

        ts_pupill_diameters[0] = PupillFeatures.UnvalidPupillDiameter()

        self.assertEqual(repr(ts_pupill_diameters), "{\"0\": {\"message\": null, \"value\": 0.0}}")

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

        ts_pupill_diameters_dataframe = random_pupill_diameters(10).as_dataframe()

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

        self.assertEqual(ts_pupill_diameters_dataframe.columns.size, 1)
        self.assertEqual(ts_pupill_diameters_dataframe.columns[0], "value")

        self.assertEqual(ts_pupill_diameters_dataframe["value"].dtype, 'float64')

        # Check unvalid diameter conversion
        ts_pupill_diameters = PupillFeatures.TimeStampedPupillDiameters()
        ts_pupill_diameters[0] = PupillFeatures.UnvalidPupillDiameter()
        ts_pupill_diameters_dataframe = ts_pupill_diameters.as_dataframe()

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

        self.assertEqual(ts_pupill_diameters_dataframe.columns.size, 1)
        self.assertEqual(ts_pupill_diameters_dataframe.columns[0], "value")

        self.assertEqual(ts_pupill_diameters_dataframe["value"].dtype, 'float64')

if __name__ == '__main__':

    unittest.main()