aboutsummaryrefslogtreecommitdiff
path: root/src/argaze.test/PupillFeatures.py
diff options
context:
space:
mode:
authorThéo de la Hogue2023-08-08 17:14:49 +0200
committerThéo de la Hogue2023-08-08 17:14:49 +0200
commit77f906729e46336b8e860b311cafdcf42087007c (patch)
tree5b6b13427b679a1b6fc4fb10eea5c47e33fa4264 /src/argaze.test/PupillFeatures.py
parent0e8ca119eea54e464450946c01d6b63e4e4ec1d1 (diff)
downloadargaze-77f906729e46336b8e860b311cafdcf42087007c.zip
argaze-77f906729e46336b8e860b311cafdcf42087007c.tar.gz
argaze-77f906729e46336b8e860b311cafdcf42087007c.tar.bz2
argaze-77f906729e46336b8e860b311cafdcf42087007c.tar.xz
Renaming Pupil by Pupill.
Diffstat (limited to 'src/argaze.test/PupillFeatures.py')
-rw-r--r--src/argaze.test/PupillFeatures.py167
1 files changed, 167 insertions, 0 deletions
diff --git a/src/argaze.test/PupillFeatures.py b/src/argaze.test/PupillFeatures.py
new file mode 100644
index 0000000..f0e8e1b
--- /dev/null
+++ b/src/argaze.test/PupillFeatures.py
@@ -0,0 +1,167 @@
+#!/usr/bin/env python
+
+""" """
+
+__author__ = "Théo de la Hogue"
+__credits__ = []
+__copyright__ = "Copyright 2023, Ecole Nationale de l'Aviation Civile (ENAC)"
+__license__ = "BSD"
+
+import unittest
+
+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.value, 0.)
+ self.assertEqual(empty_pupill_diameter.valid, False)
+
+ # Check float PupillDiameter
+ float_pupill_diameter = PupillFeatures.PupillDiameter(1.23)
+
+ self.assertEqual(float_pupill_diameter.value, 1.23)
+ self.assertEqual(float_pupill_diameter.valid, True)
+
+ 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.value = 123
+
+ self.assertNotEqual(pupill_diameter.value, 123)
+ self.assertEqual(pupill_diameter.value, 0.)
+
+ def test___repr__(self):
+ """Test PupillDiameter string representation."""
+
+ # Check empty PupillDiameter representation
+ self.assertEqual(repr(PupillFeatures.PupillDiameter()), "{\"value\": 0.0}")
+
+class TestUnvalidPupillDiameterClass(unittest.TestCase):
+ """Test UnvalidPupillDiameter class."""
+
+ def test_new(self):
+ """Test UnvalidPupillDiameter creation."""
+
+ unvalid_pupill_diameter = PupillFeatures.UnvalidPupillDiameter()
+
+ self.assertEqual(unvalid_pupill_diameter.value, 0.)
+ self.assertEqual(unvalid_pupill_diameter.valid, False)
+
+ def test___repr__(self):
+ """Test UnvalidPupillDiameter string representation."""
+
+ self.assertEqual(repr(PupillFeatures.UnvalidPupillDiameter()), "{\"message\": null, \"value\": 0.0}")
+
+class TestTimeStampedPupillDiametersClass(unittest.TestCase):
+ """Test TimeStampedPupillDiameters class."""
+
+ def test___setitem__(self):
+ """Test __setitem__ method."""
+
+ ts_pupill_diameters = PupillFeatures.TimeStampedPupillDiameters()
+ ts_pupill_diameters[0] = PupillFeatures.PupillDiameter()
+ ts_pupill_diameters[1] = PupillFeatures.UnvalidPupillDiameter()
+ ts_pupill_diameters[2] = {"value": 1.23}
+
+ # Check PupillDiameter is correctly stored and accessible as a PupillDiameter
+ self.assertIsInstance(ts_pupill_diameters[0], PupillFeatures.PupillDiameter)
+ self.assertEqual(ts_pupill_diameters[0].valid, False)
+
+ # Check UnvalidPupillDiameter is correctly stored and accessible as a UnvalidPupillDiameter
+ self.assertIsInstance(ts_pupill_diameters[1], PupillFeatures.UnvalidPupillDiameter)
+ self.assertEqual(ts_pupill_diameters[1].valid, False)
+
+ # Check dict with "value" and "precision" keys is correctly stored and accessible as a PupillDiameter
+ self.assertIsInstance(ts_pupill_diameters[2], PupillFeatures.PupillDiameter)
+ self.assertEqual(ts_pupill_diameters[2].valid, True)
+
+ # 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[0] = PupillFeatures.PupillDiameter()
+
+ self.assertEqual(repr(ts_pupill_diameters), "{\"0\": {\"value\": 0.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() \ No newline at end of file