aboutsummaryrefslogtreecommitdiff
path: root/src/argaze.test
diff options
context:
space:
mode:
authorThéo de la Hogue2022-11-28 13:23:11 +0100
committerThéo de la Hogue2022-11-28 13:23:11 +0100
commit81170eac950fe1db167b11547c1ab7bf5f7dc233 (patch)
tree3c1398d1cb1df9cecbab2bcc27dfd8e23fe67137 /src/argaze.test
parent214a057db794ced4fed7722912a571297d2a64de (diff)
parent5f3a94b4ca85aef6f7a51318f7d53a0fd00cf836 (diff)
downloadargaze-81170eac950fe1db167b11547c1ab7bf5f7dc233.zip
argaze-81170eac950fe1db167b11547c1ab7bf5f7dc233.tar.gz
argaze-81170eac950fe1db167b11547c1ab7bf5f7dc233.tar.bz2
argaze-81170eac950fe1db167b11547c1ab7bf5f7dc233.tar.xz
Merging last master changes.
Diffstat (limited to 'src/argaze.test')
-rw-r--r--src/argaze.test/GazeAnalysis/DispersionBasedGazeMovementIdentifier.py156
-rw-r--r--src/argaze.test/GazeFeatures.py14
2 files changed, 168 insertions, 2 deletions
diff --git a/src/argaze.test/GazeAnalysis/DispersionBasedGazeMovementIdentifier.py b/src/argaze.test/GazeAnalysis/DispersionBasedGazeMovementIdentifier.py
new file mode 100644
index 0000000..01b2aad
--- /dev/null
+++ b/src/argaze.test/GazeAnalysis/DispersionBasedGazeMovementIdentifier.py
@@ -0,0 +1,156 @@
+#!/usr/bin/env python
+
+import unittest
+import random
+import time
+
+from argaze import GazeFeatures
+from argaze.GazeAnalysis import DispersionBasedGazeMovementIdentifier
+
+import numpy
+
+def build_gaze_fixation(size: int, center: tuple, dispersion: float, start_time: float, min_time: float, max_time: float, validity: list = []):
+ """ Generate N TimeStampedGazePsoitions dispersed around a center point for testing purpose.
+ Timestamps are current time after random sleep (second).
+ GazePositions are random values.
+ """
+ ts_gaze_positions = GazeFeatures.TimeStampedGazePositions()
+
+ for i in range(0, size):
+
+ # Check position validity
+ valid = True
+ if len(validity) > i:
+
+ valid = validity[i]
+
+ if valid:
+
+ # Edit gaze position
+ random_x = center[0] + dispersion * (random.random() - 0.5)
+ random_y = center[1] + dispersion * (random.random() - 0.5)
+ gaze_position = GazeFeatures.GazePosition((random_x, random_y))
+
+ else:
+
+ gaze_position = GazeFeatures.UnvalidGazePosition()
+
+ # Store gaze position
+ ts = time.time() - start_time
+ ts_gaze_positions[ts] = gaze_position
+
+ # Sleep a random time
+ sleep_time = random.random() * (max_time - min_time) + min_time
+ time.sleep(sleep_time)
+
+ return ts_gaze_positions
+
+class TestDispersionBasedGazeMovementIdentifierClass(unittest.TestCase):
+ """Test DispersionBasedGazeMovementIdentifier class."""
+
+ def test_fixation_identification(self):
+ """Test DispersionBasedGazeMovementIdentifier fixation identification."""
+
+ size = 10
+ center = (0, 0)
+ dispersion = 10
+ start_time = time.time()
+ min_time = 0.01
+ max_time = 0.1
+
+ ts_gaze_positions = build_gaze_fixation(size, center, dispersion, start_time, min_time, max_time)
+ gaze_movement_identifier = DispersionBasedGazeMovementIdentifier.GazeMovementIdentifier(dispersion_threshold=dispersion, duration_threshold=min_time*2)
+ ts_fixations, ts_saccades, ts_status = gaze_movement_identifier.identify(ts_gaze_positions)
+
+ # Check result size
+ self.assertEqual(len(ts_fixations), 1)
+ self.assertEqual(len(ts_saccades), 0)
+ self.assertEqual(len(ts_status), size)
+
+ # Check fixation
+ ts, fixation = ts_fixations.pop_first()
+
+ self.assertEqual(len(fixation.positions.keys()), size)
+ self.assertLessEqual(fixation.dispersion, dispersion)
+ self.assertGreaterEqual(fixation.duration, size * min_time)
+ self.assertLessEqual(fixation.duration, size * max_time)
+
+ def test_fixation_and_saccade_identification(self):
+ """Test DispersionBasedGazeMovementIdentifier fixation and saccade identification."""
+
+ size = 10
+ center_A = (0, 0)
+ center_B = (50, 50)
+ dispersion = 10
+ start_time = time.time()
+ min_time = 0.01
+ max_time = 0.1
+
+ ts_gaze_positions_A = build_gaze_fixation(size, center_A, dispersion, start_time, min_time, max_time)
+ ts_gaze_positions_B = build_gaze_fixation(size, center_B, dispersion, start_time, min_time, max_time)
+
+ ts_gaze_positions = ts_gaze_positions_A.append(ts_gaze_positions_B)
+
+ gaze_movement_identifier = DispersionBasedGazeMovementIdentifier.GazeMovementIdentifier(dispersion_threshold=dispersion, duration_threshold=min_time*2)
+ ts_fixations, ts_saccades, ts_status = gaze_movement_identifier.identify(ts_gaze_positions)
+
+ # Check result size
+ self.assertEqual(len(ts_fixations), 2)
+ self.assertEqual(len(ts_saccades), 1)
+ self.assertEqual(len(ts_status), size*2)
+
+ # Check first fixation
+ ts, fixation = ts_fixations.pop_first()
+
+ self.assertEqual(len(fixation.positions.keys()), size)
+ self.assertLessEqual(fixation.dispersion, dispersion)
+ self.assertGreaterEqual(fixation.duration, size * min_time)
+ self.assertLessEqual(fixation.duration, size * max_time)
+
+ # Check first saccade
+ ts, saccade = ts_saccades.pop_first()
+
+ self.assertEqual(len(saccade.positions.keys()), 2)
+ self.assertGreaterEqual(saccade.duration, min_time)
+ self.assertLessEqual(saccade.duration, max_time)
+
+ # Check second fixation
+ ts, fixation = ts_fixations.pop_first()
+
+ self.assertEqual(len(fixation.positions.keys()), size)
+ self.assertLessEqual(fixation.dispersion, dispersion)
+ self.assertGreaterEqual(fixation.duration, size * min_time)
+ self.assertLessEqual(fixation.duration, size * max_time)
+
+ def test_invalid_gaze_position(self):
+ """Test DispersionBasedGazeMovementIdentifier fixation and saccade identification with invalid gaze position."""
+
+ size = 10
+ center = (0, 0)
+ dispersion = 10
+ start_time = time.time()
+ min_time = 0.01
+ max_time = 0.1
+ validity = [True, True, True, True, False, False, True, True, True, True]
+
+ ts_gaze_positions = build_gaze_fixation(size, center, dispersion, start_time, min_time, max_time, validity)
+
+ gaze_movement_identifier = DispersionBasedGazeMovementIdentifier.GazeMovementIdentifier(dispersion_threshold=dispersion, duration_threshold=min_time*2)
+ ts_fixations, ts_saccades, ts_status = gaze_movement_identifier.identify(ts_gaze_positions)
+
+ # Check result size
+ self.assertEqual(len(ts_fixations), 1)
+ self.assertEqual(len(ts_saccades), 0)
+ self.assertEqual(len(ts_status), size-2)
+
+ # Check fixation
+ ts, fixation = ts_fixations.pop_first()
+
+ self.assertEqual(len(fixation.positions.keys()), size-2)
+ self.assertLessEqual(fixation.dispersion, dispersion)
+ self.assertGreaterEqual(fixation.duration, size * min_time)
+ self.assertLessEqual(fixation.duration, size * max_time)
+
+if __name__ == '__main__':
+
+ unittest.main() \ No newline at end of file
diff --git a/src/argaze.test/GazeFeatures.py b/src/argaze.test/GazeFeatures.py
index 5a3c2d9..dd0717c 100644
--- a/src/argaze.test/GazeFeatures.py
+++ b/src/argaze.test/GazeFeatures.py
@@ -117,6 +117,7 @@ class TestTimeStampedGazePositionsClass(unittest.TestCase):
ts_gaze_positions = GazeFeatures.TimeStampedGazePositions()
ts_gaze_positions[0] = GazeFeatures.GazePosition()
ts_gaze_positions[1] = GazeFeatures.UnvalidGazePosition()
+ ts_gaze_positions[2] = {"value": (0, 0), "accuracy": 0.}
# Check GazePosition is correctly stored and accessible as a GazePosition
self.assertIsInstance(ts_gaze_positions[0], GazeFeatures.GazePosition)
@@ -126,13 +127,22 @@ class TestTimeStampedGazePositionsClass(unittest.TestCase):
self.assertIsInstance(ts_gaze_positions[1], GazeFeatures.UnvalidGazePosition)
self.assertEqual(ts_gaze_positions[1].valid, False)
+ # Check dict with "value" and "accuracy" 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[2] = "This string is not a gaze position value."
+ 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), "accuracy": 0.}
# Check final lenght
- self.assertEqual(len(ts_gaze_positions), 2)
+ self.assertEqual(len(ts_gaze_positions), 3)
def test___repr__(self):
"""Test inherited string representation."""