aboutsummaryrefslogtreecommitdiff
path: root/src/argaze/GazeAnalysis/NearestNeighborIndex.py
blob: 104bb30ed7a620477e0ff47c642c9c91c017b86d (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
#!/usr/bin/env python

""" """

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

from typing import TypeVar, Tuple, Any
from dataclasses import dataclass, field

from argaze import GazeFeatures

import numpy
from scipy.spatial.distance import cdist

@dataclass
class ScanPathAnalyzer(GazeFeatures.ScanPathAnalyzer):
    """Implementation of Nearest Neighbor Index (NNI) as described in Di Nocera et al., 2006
    """

    def __post_init__(self):

        pass

    def analyze(self, scan_path: GazeFeatures.ScanPathType, screen_dimension: tuple[float, float]) -> float:
        """Analyze scan path."""

        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(screen_dimension[0] * screen_dimension[1] / len(fixations_focus))

        return dNN / dran