From e35e90d161ffb9202459631a0049448cde905b3c Mon Sep 17 00:00:00 2001 From: Théo de la Hogue Date: Wed, 31 May 2023 15:35:28 +0200 Subject: Testing all gaze analysis algorithm. Unifying paper citation. --- src/argaze.test/GazeAnalysis/Entropy.py | 12 ++-- src/argaze.test/GazeAnalysis/KCoefficient.py | 56 +++++++++++++++++ .../GazeAnalysis/LempelZivComplexity.py | 54 ++++++++++++++++ src/argaze.test/GazeAnalysis/NGram.py | 57 +++++++++++++++++ .../GazeAnalysis/NearestNeighborIndex.py | 40 ++++++++++++ src/argaze.test/GazeAnalysis/TransitionMatrix.py | 13 ++-- src/argaze.test/GazeFeatures.py | 72 ++++++++++++++-------- 7 files changed, 268 insertions(+), 36 deletions(-) create mode 100644 src/argaze.test/GazeAnalysis/KCoefficient.py create mode 100644 src/argaze.test/GazeAnalysis/LempelZivComplexity.py create mode 100644 src/argaze.test/GazeAnalysis/NGram.py create mode 100644 src/argaze.test/GazeAnalysis/NearestNeighborIndex.py (limited to 'src/argaze.test') diff --git a/src/argaze.test/GazeAnalysis/Entropy.py b/src/argaze.test/GazeAnalysis/Entropy.py index 47d5556..b69f329 100644 --- a/src/argaze.test/GazeAnalysis/Entropy.py +++ b/src/argaze.test/GazeAnalysis/Entropy.py @@ -18,20 +18,20 @@ GazeFeaturesTest = MiscFeatures.importFromTestPackage('GazeFeatures') class TestAOIScanPathAnalyzer(unittest.TestCase): """Test AOIScanPathAnalyzer class.""" - def test_analyse(self): - """Test analyse method.""" - - aoi_scan_path = GazeFeaturesTest.build_aoi_scan_path(['Foo', 'Bar', 'Shu'], ['Bar', 'Shu', 'Foo', 'Bar', 'Shu', 'Foo', 'Bar', 'Shu', 'Foo']) + def test_analyze(self): + """Test analyze method.""" entropy_analyzer = Entropy.AOIScanPathAnalyzer() transition_matrix_analyser = TransitionMatrix.AOIScanPathAnalyzer() - transition_matrix_probabilities, transition_matrix_density = transition_matrix_analyser.analyze(aoi_scan_path) - stationary_entropy, transition_entropy = entropy_analyzer.analyze(aoi_scan_path, transition_matrix_probabilities) + aoi_scan_path = GazeFeaturesTest.build_aoi_scan_path(['Foo', 'Bar', 'Shu'], ['Bar', 'Shu', 'Foo', 'Bar', 'Shu', 'Foo', 'Bar', 'Shu', 'Foo']) # Check aoi scan path self.assertEqual(len(aoi_scan_path), 9) + transition_matrix_probabilities, transition_matrix_density = transition_matrix_analyser.analyze(aoi_scan_path) + stationary_entropy, transition_entropy = entropy_analyzer.analyze(aoi_scan_path, transition_matrix_probabilities) + # Check entropy analysis self.assertAlmostEqual(stationary_entropy, 1.09, 1) self.assertAlmostEqual(transition_entropy, 0, 1) diff --git a/src/argaze.test/GazeAnalysis/KCoefficient.py b/src/argaze.test/GazeAnalysis/KCoefficient.py new file mode 100644 index 0000000..07dff79 --- /dev/null +++ b/src/argaze.test/GazeAnalysis/KCoefficient.py @@ -0,0 +1,56 @@ +#!/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 GazeFeatures +from argaze.GazeAnalysis import KCoefficient +from argaze.utils import MiscFeatures + +GazeFeaturesTest = MiscFeatures.importFromTestPackage('GazeFeatures') + +class TestScanPathAnalyzer(unittest.TestCase): + """Test ScanPathAnalyzer class.""" + + def test_analyze(self): + """Test analyze method.""" + + kcoeff_analyzer = KCoefficient.AOIScanPathAnalyzer() + + scan_path = GazeFeaturesTest.build_scan_path(10) + + # Check scan path + self.assertEqual(len(scan_path), 10) + + K = kcoeff_analyzer.analyze(scan_path) + + # Check that K coefficient is almost equal to 0 + self.assertAlmostEqual(K, 0) + +class TestAOIScanPathAnalyzer(unittest.TestCase): + """Test AOIScanPathAnalyzer class.""" + + def test_analyze(self): + """Test analyze method.""" + + kcoeff_analyzer = KCoefficient.AOIScanPathAnalyzer() + + aoi_scan_path = GazeFeaturesTest.build_aoi_scan_path(['Foo', 'Bar', 'Shu'], ['Bar', 'Shu', 'Foo', 'Bar']) + + # Check aoi scan path + self.assertEqual(len(aoi_scan_path), 4) + + K = kcoeff_analyzer.analyze(aoi_scan_path) + + # Check that K coefficient is almost equal to 0 + self.assertAlmostEqual(K, 0) + +if __name__ == '__main__': + + unittest.main() \ No newline at end of file diff --git a/src/argaze.test/GazeAnalysis/LempelZivComplexity.py b/src/argaze.test/GazeAnalysis/LempelZivComplexity.py new file mode 100644 index 0000000..75afc4d --- /dev/null +++ b/src/argaze.test/GazeAnalysis/LempelZivComplexity.py @@ -0,0 +1,54 @@ +#!/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 GazeFeatures +from argaze.GazeAnalysis import LempelZivComplexity +from argaze.utils import MiscFeatures + +GazeFeaturesTest = MiscFeatures.importFromTestPackage('GazeFeatures') + +class TestAOIScanPathAnalyzer(unittest.TestCase): + """Test AOIScanPathAnalyzer class.""" + + @unittest.skip("The result is not like in the paper.") + def test_analyze_first_example(self): + """Test analyze method with first example sequence from the paper.""" + + lzc_analyzer = LempelZivComplexity.AOIScanPathAnalyzer() + + aoi_scan_path = GazeFeaturesTest.build_aoi_scan_path(['Foo', 'Bar', 'Shu'], ['Bar', 'Shu', 'Bar', 'Shu', 'Bar', 'Shu', 'Bar', 'Shu', 'Bar', 'Shu', 'Bar', 'Shu', 'Foo']) + + # Check aoi scan path + self.assertEqual(len(aoi_scan_path), 13) + + lzc = lzc_analyzer.analyze(aoi_scan_path) + + # Check LZC coefficient + self.assertEqual(lzc, 6) + + def test_analyze_seconde_example(self): + """Test analyze method with second example sequence from the paper.""" + + lzc_analyzer = LempelZivComplexity.AOIScanPathAnalyzer() + + aoi_scan_path = GazeFeaturesTest.build_aoi_scan_path(['Ade', 'Bar', 'Cob', 'Gno', 'Kel', 'Iop', 'Eca'], ['Bar', 'Cob', 'Ade', 'Kel', 'Ade', 'Cob', 'Kel', 'Gno', 'Kel', 'Ade', 'Kel', 'Iop', 'Eca']) + + # Check aoi scan path + self.assertEqual(len(aoi_scan_path), 13) + + lzc = lzc_analyzer.analyze(aoi_scan_path) + + # Check LZC coefficient + self.assertEqual(lzc, 9) + +if __name__ == '__main__': + + unittest.main() \ No newline at end of file diff --git a/src/argaze.test/GazeAnalysis/NGram.py b/src/argaze.test/GazeAnalysis/NGram.py new file mode 100644 index 0000000..9608b90 --- /dev/null +++ b/src/argaze.test/GazeAnalysis/NGram.py @@ -0,0 +1,57 @@ +#!/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 GazeFeatures +from argaze.GazeAnalysis import NGram +from argaze.utils import MiscFeatures + +GazeFeaturesTest = MiscFeatures.importFromTestPackage('GazeFeatures') + +class TestAOIScanPathAnalyzer(unittest.TestCase): + """Test AOIScanPathAnalyzer class.""" + + def test_analyze(self): + """Test analyze method.""" + + ngram_analyzer = NGram.AOIScanPathAnalyzer() + + aoi_scan_path = GazeFeaturesTest.build_aoi_scan_path(['Foo', 'Bar', 'Shu'], ['Bar', 'Shu', 'Foo', 'Bar', 'Shu', 'Foo']) + + # Check aoi scan path + self.assertEqual(len(aoi_scan_path), 6) + + ngram_analysis = ngram_analyzer.analyze(aoi_scan_path, 2) + + # Check 2-gram analysis + self.assertEqual(len(ngram_analysis), 3) + self.assertEqual(ngram_analysis[('Bar', 'Shu')], 2) + self.assertEqual(ngram_analysis[('Shu', 'Foo')], 2) + self.assertEqual(ngram_analysis[('Foo', 'Bar')], 1) + + ngram_analysis = ngram_analyzer.analyze(aoi_scan_path, 3) + + # Check 3-gram analysis + self.assertEqual(len(ngram_analysis), 3) + self.assertEqual(ngram_analysis[('Bar', 'Shu', 'Foo')], 2) + self.assertEqual(ngram_analysis[('Shu', 'Foo', 'Bar')], 1) + self.assertEqual(ngram_analysis[('Foo', 'Bar', 'Shu')], 1) + + ngram_analysis = ngram_analyzer.analyze(aoi_scan_path, 4) + + # Check 4-gram analysis + self.assertEqual(len(ngram_analysis), 3) + self.assertEqual(ngram_analysis[('Bar', 'Shu', 'Foo', 'Bar')], 1) + self.assertEqual(ngram_analysis[('Shu', 'Foo', 'Bar', 'Shu')], 1) + self.assertEqual(ngram_analysis[('Foo', 'Bar', 'Shu', 'Foo')], 1) + +if __name__ == '__main__': + + unittest.main() \ No newline at end of file diff --git a/src/argaze.test/GazeAnalysis/NearestNeighborIndex.py b/src/argaze.test/GazeAnalysis/NearestNeighborIndex.py new file mode 100644 index 0000000..fb7d4ec --- /dev/null +++ b/src/argaze.test/GazeAnalysis/NearestNeighborIndex.py @@ -0,0 +1,40 @@ +#!/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 GazeFeatures +from argaze.GazeAnalysis import NearestNeighborIndex +from argaze.utils import MiscFeatures + +GazeFeaturesTest = MiscFeatures.importFromTestPackage('GazeFeatures') + +class TestScanPathAnalyzer(unittest.TestCase): + """Test ScanPathAnalyzer class.""" + + def test_analyze(self): + """Test analyze.""" + + nni_analyzer = NearestNeighborIndex.ScanPathAnalyzer() + + screen_dimension = (100, 100) + scan_path = GazeFeaturesTest.build_scan_path(6, screen_dimension) + + # Check aoi scan path + self.assertEqual(len(scan_path), 6) + + nni = nni_analyzer.analyze(scan_path, screen_dimension) + + # Check NNI + self.assertGreaterEqual(nni, 0) + self.assertLessEqual(nni, 1) + +if __name__ == '__main__': + + unittest.main() \ No newline at end of file diff --git a/src/argaze.test/GazeAnalysis/TransitionMatrix.py b/src/argaze.test/GazeAnalysis/TransitionMatrix.py index 14a34ce..997b706 100644 --- a/src/argaze.test/GazeAnalysis/TransitionMatrix.py +++ b/src/argaze.test/GazeAnalysis/TransitionMatrix.py @@ -18,17 +18,18 @@ GazeFeaturesTest = MiscFeatures.importFromTestPackage('GazeFeatures') class TestAOIScanPathAnalyzer(unittest.TestCase): """Test AOIScanPathAnalyzer class.""" - def test_analyse(self): - """Test analyse method.""" - - aoi_scan_path = GazeFeaturesTest.build_aoi_scan_path(['Foo', 'Bar', 'Shu'], ['Bar', 'Shu', 'Foo', 'Bar']) - + def test_analyze(self): + """Test analyze method.""" + transition_matrix_analyser = TransitionMatrix.AOIScanPathAnalyzer() - transition_matrix_probabilities, transition_matrix_density = transition_matrix_analyser.analyze(aoi_scan_path) + + aoi_scan_path = GazeFeaturesTest.build_aoi_scan_path(['Foo', 'Bar', 'Shu'], ['Bar', 'Shu', 'Foo', 'Bar']) # Check aoi scan path self.assertEqual(len(aoi_scan_path), 4) + transition_matrix_probabilities, transition_matrix_density = transition_matrix_analyser.analyze(aoi_scan_path) + # Check transition matrix probabilities ([destination][departure]) self.assertEqual(transition_matrix_probabilities['Foo']['Foo'], 0) self.assertEqual(transition_matrix_probabilities['Bar']['Bar'], 0) diff --git a/src/argaze.test/GazeFeatures.py b/src/argaze.test/GazeFeatures.py index 8206baf..f73eefe 100644 --- a/src/argaze.test/GazeFeatures.py +++ b/src/argaze.test/GazeFeatures.py @@ -8,12 +8,13 @@ __copyright__ = "Copyright 2023, Ecole Nationale de l'Aviation Civile (ENAC)" __license__ = "BSD" import unittest +from dataclasses import dataclass from argaze import GazeFeatures import numpy -def random_gaze_positions(size): +def random_gaze_positions(size, screen_dimension: tuple[float, float] = (1, 1)): """ Generate random TimeStampedGazePsoitions for testing purpose. Timestamps are current time. GazePositions are random values. @@ -27,13 +28,36 @@ def random_gaze_positions(size): for i in range(0, size): # Edit gaze position - random_gaze_position = GazeFeatures.GazePosition((random.random(), random.random())) + random_gaze_position = GazeFeatures.GazePosition((random.random() * screen_dimension[0], random.random() * screen_dimension[1])) # Store gaze position ts_gaze_positions[time.time()] = random_gaze_position return ts_gaze_positions +@dataclass(frozen=True) +class TestFixation(GazeFeatures.Fixation): + """Define basic fixation class for test.""" + + def __post_init__(self): + + super().__post_init__() + + points = self.positions.values() + points_x, points_y = [p[0] for p in points], [p[1] for p in points] + points_array = numpy.column_stack([points_x, points_y]) + centroid_array = numpy.array([numpy.mean(points_x), numpy.mean(points_y)]) + + # Update frozen focus attribute using centroid + object.__setattr__(self, 'focus', (centroid_array[0], centroid_array[1])) + +@dataclass(frozen=True) +class TestSaccade(GazeFeatures.Saccade): + """Define basic saccade for test.""" + + def __post_init__(self): + super().__post_init__() + class TestGazePositionClass(unittest.TestCase): """Test GazePosition class.""" @@ -222,8 +246,8 @@ class TestScanStepClass(unittest.TestCase): def test_new(self): """Test ScanStep creation.""" - fixation = GazeFeatures.Fixation(random_gaze_positions(10)) - saccade = GazeFeatures.Saccade(random_gaze_positions(2)) + fixation = TestFixation(random_gaze_positions(10)) + saccade = TestSaccade(random_gaze_positions(2)) scan_step = GazeFeatures.ScanStep(fixation, saccade) @@ -232,18 +256,18 @@ class TestScanStepClass(unittest.TestCase): self.assertEqual(scan_step.last_saccade, saccade) self.assertGreater(scan_step.duration, 0) -def build_scan_path(size): +def build_scan_path(size, screen_dimension: tuple[float, float] = (1, 1)): """Build scan path""" scan_path = GazeFeatures.ScanPath() for i in range(size): - fixation = GazeFeatures.Fixation(random_gaze_positions(10)) + fixation = TestFixation(random_gaze_positions(10, screen_dimension)) ts, _ = fixation.positions.first scan_path.append_fixation(ts, fixation) - saccade = GazeFeatures.Saccade(random_gaze_positions(2)) + saccade = TestSaccade(random_gaze_positions(2, screen_dimension)) ts, _ = saccade.positions.first scan_path.append_saccade(ts, saccade) @@ -266,7 +290,7 @@ class TestScanPathClass(unittest.TestCase): scan_path = GazeFeatures.ScanPath() # Append a saccade that should be ignored - saccade = GazeFeatures.Saccade(random_gaze_positions(2)) + saccade = TestSaccade(random_gaze_positions(2)) ts, _ = saccade.positions.first new_step = scan_path.append_saccade(ts, saccade) @@ -276,7 +300,7 @@ class TestScanPathClass(unittest.TestCase): self.assertEqual(new_step, None) # Append first fixation - fixation_A = GazeFeatures.Fixation(random_gaze_positions(10)) + fixation_A = TestFixation(random_gaze_positions(10)) ts, _ = fixation_A.positions.first new_step = scan_path.append_fixation(ts, fixation_A) @@ -286,7 +310,7 @@ class TestScanPathClass(unittest.TestCase): self.assertEqual(new_step, None) # Append consecutive saccade - saccade_A = GazeFeatures.Saccade(random_gaze_positions(2)) + saccade_A = TestSaccade(random_gaze_positions(2)) ts, _ = saccade_A.positions.first new_step = scan_path.append_saccade(ts, saccade_A) @@ -297,7 +321,7 @@ class TestScanPathClass(unittest.TestCase): self.assertEqual(new_step.last_saccade, saccade_A) # Append 2 consecutive fixations then a saccade - fixation_B1 = GazeFeatures.Fixation(random_gaze_positions(10)) + fixation_B1 = TestFixation(random_gaze_positions(10)) ts, _ = fixation_B1.positions.first new_step = scan_path.append_fixation(ts, fixation_B1) @@ -306,7 +330,7 @@ class TestScanPathClass(unittest.TestCase): self.assertEqual(len(scan_path), 1) self.assertEqual(new_step, None) - fixation_B2 = GazeFeatures.Fixation(random_gaze_positions(10)) + fixation_B2 = TestFixation(random_gaze_positions(10)) ts, _ = fixation_B2.positions.first new_step = scan_path.append_fixation(ts, fixation_B2) @@ -315,7 +339,7 @@ class TestScanPathClass(unittest.TestCase): self.assertEqual(len(scan_path), 1) self.assertEqual(new_step, None) - saccade_B = GazeFeatures.Saccade(random_gaze_positions(2)) + saccade_B = TestSaccade(random_gaze_positions(2)) ts, _ = saccade_B.positions.first new_step = scan_path.append_saccade(ts, saccade_B) @@ -333,11 +357,11 @@ class TestAOIScanStepClass(unittest.TestCase): movements = GazeFeatures.TimeStampedGazeMovements() - fixation = GazeFeatures.Fixation(random_gaze_positions(10)) + fixation = TestFixation(random_gaze_positions(10)) ts, _ = fixation.positions.first movements[ts] = fixation - saccade = GazeFeatures.Saccade(random_gaze_positions(2)) + saccade = TestSaccade(random_gaze_positions(2)) ts, _ = saccade.positions.first movements[ts] = saccade @@ -355,11 +379,11 @@ class TestAOIScanStepClass(unittest.TestCase): movements = GazeFeatures.TimeStampedGazeMovements() - saccade = GazeFeatures.Saccade(random_gaze_positions(2)) + saccade = TestSaccade(random_gaze_positions(2)) ts, _ = saccade.positions.first movements[ts] = saccade - fixation = GazeFeatures.Fixation(random_gaze_positions(10)) + fixation = TestFixation(random_gaze_positions(10)) ts, _ = fixation.positions.first movements[ts] = fixation @@ -378,11 +402,11 @@ def build_aoi_scan_path(expected_aois, aoi_path): for aoi in aoi_path: - fixation = GazeFeatures.Fixation(random_gaze_positions(10)) + fixation = TestFixation(random_gaze_positions(10)) ts, _ = fixation.positions.first aoi_scan_path.append_fixation(ts, fixation, aoi) - saccade = GazeFeatures.Saccade(random_gaze_positions(2)) + saccade = TestSaccade(random_gaze_positions(2)) ts, _ = saccade.positions.first aoi_scan_path.append_saccade(ts, saccade) @@ -405,7 +429,7 @@ class TestAOIScanPathClass(unittest.TestCase): aoi_scan_path = GazeFeatures.AOIScanPath(['Foo', 'Bar']) # Append fixation on A aoi - fixation = GazeFeatures.Fixation(random_gaze_positions(10)) + fixation = TestFixation(random_gaze_positions(10)) ts, _ = fixation.positions.first new_step = aoi_scan_path.append_fixation(ts, fixation, 'Foo') @@ -415,7 +439,7 @@ class TestAOIScanPathClass(unittest.TestCase): self.assertEqual(new_step, None) # Append saccade - saccade = GazeFeatures.Saccade(random_gaze_positions(2)) + saccade = TestSaccade(random_gaze_positions(2)) ts, _ = saccade.positions.first new_step = aoi_scan_path.append_saccade(ts, saccade) @@ -425,7 +449,7 @@ class TestAOIScanPathClass(unittest.TestCase): self.assertEqual(new_step, None) # Append fixation on B aoi - fixation = GazeFeatures.Fixation(random_gaze_positions(10)) + fixation = TestFixation(random_gaze_positions(10)) ts, _ = fixation.positions.first new_step = aoi_scan_path.append_fixation(ts, fixation, 'Bar') @@ -445,7 +469,7 @@ class TestAOIScanPathClass(unittest.TestCase): aoi_scan_path = GazeFeatures.AOIScanPath(['Foo', 'Bar']) # Append fixation on A aoi - fixation = GazeFeatures.Fixation(random_gaze_positions(10)) + fixation = TestFixation(random_gaze_positions(10)) ts, _ = fixation.positions.first new_step = aoi_scan_path.append_fixation(ts, fixation, 'Foo') @@ -455,7 +479,7 @@ class TestAOIScanPathClass(unittest.TestCase): self.assertEqual(new_step, None) # Append fixation on B aoi - fixation = GazeFeatures.Fixation(random_gaze_positions(10)) + fixation = TestFixation(random_gaze_positions(10)) ts, _ = fixation.positions.first # Check that aoi scan step creation fail when fixation is appened after another fixation -- cgit v1.1