aboutsummaryrefslogtreecommitdiff
path: root/src/argaze/GazeAnalysis/NearestNeighborIndex.py
blob: b0d7312623c6db71802e909e6a58ee848e986f22 (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
"""Nearest Neighbor Index module.


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"

from argaze import GazeFeatures, DataFeatures

import numpy
from scipy.spatial.distance import cdist

class ScanPathAnalyzer(GazeFeatures.ScanPathAnalyzer):
    """Implementation of Nearest Neighbor Index algorithm as described in:

        **Di Nocera F., Terenzi M., Camilli M. (2006).**  
        *Another look at scanpath: distance to nearest neighbour as a measure of mental workload.*  
        Developments in Human Factors in Transportation, Design, and Evaluation.  
        [https://www.researchgate.net](https://www.researchgate.net/publication/239470608_Another_look_at_scanpath_distance_to_nearest_neighbour_as_a_measure_of_mental_workload)
    """

    @DataFeatures.PipelineStepInit
    def __init__(self, **kwargs):

        super().__init__(**kwargs)

        self.__size = (0, 0)
        self.__nearest_neighbor_index = 0

    @property
    def size(self) -> tuple[float, float]:
        """Frame dimension."""
        return self.__size

    @size.setter
    def size(self, size: tuple[float, float]):

        self.__size = size

    @DataFeatures.PipelineStepMethod
    def analyze(self, scan_path: GazeFeatures.ScanPathType):

        assert(len(scan_path) > 1)

        # Gather fixations focus points
        fixations_focus = []
        for step in scan_path:
            fixations_focus.append(step.first_fixation.focus)

        # Compute inter fixation distances
        distances = cdist(fixations_focus, fixations_focus)

        # Find minimal distances between each fixations
        minimums = numpy.apply_along_axis(lambda row: numpy.min(row[numpy.nonzero(row)]), 1, distances)

        # Average of minimun distances
        dNN = numpy.sum(minimums / len(fixations_focus))

        # Mean random distance
        dran = 0.5 * numpy.sqrt(self.size[0] * self.size[1] / len(fixations_focus))

        self.__nearest_neighbor_index = dNN / dran

    @property
    def nearest_neighbor_index(self) -> float:
        """Nearest Neighbor Index."""
        
        return self.__nearest_neighbor_index