aboutsummaryrefslogtreecommitdiff
path: root/src/argaze/GazeAnalysis/KCoefficient.py
blob: 40e3ddd2a08918be5990f7d9c4fc9e127bec3527 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python

"""K coefficient and K-modified coefficient module.
"""

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

from dataclasses import dataclass

from argaze import GazeFeatures, DataFeatures

import numpy

@dataclass
class ScanPathAnalyzer(GazeFeatures.ScanPathAnalyzer):
    """Implementation of the K coefficient algorithm as described in:

        **Krejtz K., Duchowski A., Krejtz I., Szarkowska A., & Kopacz A. (2016).**  
        *Discerning ambient/focal attention with coefficient K.*  
        ACM Transactions on Applied Perception (TAP, 1–20).  
        [https://doi.org/10.1145/2896452](https://doi.org/10.1145/2896452)
    """

    def __post_init__(self):

        super().__init__()

        self.__K = 0

    @DataFeatures.PipelineStepMethod
    def analyze(self, timestamp: int|float, scan_path: GazeFeatures.ScanPathType):

        assert(len(scan_path) > 1)

        durations = []
        amplitudes = []

        for scan_step in scan_path:

            durations.append(scan_step.duration)
            amplitudes.append(scan_step.last_saccade.amplitude)

        durations = numpy.array(durations)
        amplitudes = numpy.array(amplitudes)

        duration_mean = numpy.mean(durations)
        amplitude_mean = numpy.mean(amplitudes)

        duration_std = numpy.std(durations)
        amplitude_std = numpy.std(amplitudes)

        if duration_std > 0. and amplitude_std > 0.:

            Ks = []
            for scan_step in scan_path:

                Ks.append((abs(scan_step.duration - duration_mean) / duration_std) - (abs(scan_step.last_saccade.amplitude - amplitude_mean) / amplitude_std))

            self.__K = numpy.array(Ks).mean()

        else:

            self.__K = 0.

    @property
    def K(self) -> float:
        """K coefficient."""

        return self.__K

@dataclass
class AOIScanPathAnalyzer(GazeFeatures.AOIScanPathAnalyzer):
    """Implementation of the K-modified coefficient algorithm as described in:

        **Lounis, C. A., Hassoumi, A., Lefrancois, O., Peysakhovich, V., & Causse, M. (2020, June).**  
        *Detecting ambient/focal visual attention in professional airline pilots with a modified Coefficient K: a full flight simulator study.*  
        ACM Symposium on Eye Tracking Research and Applications (ETRA'20, 1-6).  
        [https://doi.org/10.1145/3379157.3391412](https://doi.org/10.1145/3379157.3391412)
    """

    def __post_init__(self):

        super().__init__()

        self.__K = 0

    @DataFeatures.PipelineStepMethod
    def analyze(self, timestamp: int|float, aoi_scan_path: GazeFeatures.AOIScanPathType) -> float:

        assert(len(aoi_scan_path) > 1)

        durations = []
        amplitudes = []

        for aoi_scan_step in aoi_scan_path:

            durations.append(aoi_scan_step.duration)
            amplitudes.append(aoi_scan_step.last_saccade.amplitude)

        durations = numpy.array(durations)
        amplitudes = numpy.array(amplitudes)

        duration_mean = numpy.mean(durations)
        amplitude_mean = numpy.mean(amplitudes)

        duration_std = numpy.std(durations)
        amplitude_std = numpy.std(amplitudes)

        if duration_std > 0. and amplitude_std > 0.:

            Ks = []
            for aoi_scan_step in aoi_scan_path:

                Ks.append((abs(aoi_scan_step.duration - duration_mean) / duration_std) - (abs(aoi_scan_step.last_saccade.amplitude - amplitude_mean) / amplitude_std))

            self.__K = numpy.array(Ks).mean()

        else:

            self.__K = 0.

    @property
    def K(self) -> float:
        """K coefficient."""

        return self.__K