aboutsummaryrefslogtreecommitdiff
path: root/src/argaze/GazeAnalysis/Entropy.py
blob: 2f98d2cfd483c94fdca2732f6b60949e96e7fad1 (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
"""Stationary and transition entropy 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 <https://www.gnu.org/licenses/>.
"""

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

import numpy

from argaze import GazeFeatures, DataFeatures
from argaze.GazeAnalysis import TransitionMatrix


class AOIScanPathAnalyzer(GazeFeatures.AOIScanPathAnalyzer):
    """Implementation of entropy algorithm as described in:

        **Krejtz K., Szmidt T., Duchowski A.T. (2014).**  
        *Entropy-based statistical analysis of eye movement transitions.*  
        Proceedings of the Symposium on Eye Tracking Research and Applications (ETRA'14, 159-166).  
        [https://doi.org/10.1145/2578153.2578176](https://doi.org/10.1145/2578153.2578176)
    """

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

        # Init AOIScanPathAnalyzer class
        super().__init__()

        self.__transition_matrix_analyzer = None
        self.__stationary_entropy = -1
        self.__transition_entropy = -1

    @property
    def transition_matrix_analyzer(self) -> TransitionMatrix.AOIScanPathAnalyzer:
        """Bind to TransitionMatrix analyzer to get its transition_matrix_probabilities.

        !!! warning "Mandatory"
            TransitionMatrix analyzer have to be loaded before.
        """

        return self.__transition_matrix_analyzer

    @transition_matrix_analyzer.setter
    def transition_matrix_analyzer(self, transition_matrix_analyzer: TransitionMatrix.AOIScanPathAnalyzer):

        self.__transition_matrix_analyzer = transition_matrix_analyzer

    @DataFeatures.PipelineStepMethod
    def analyze(self, aoi_scan_path: GazeFeatures.AOIScanPath):

        assert(len(aoi_scan_path) > 1)

        # Count total number of fixations and how many fixations are there per aoi
        scan_fixations_count, aoi_fixations_count = aoi_scan_path.fixations_count()

        # Probability to have a fixation onto each aoi
        stationary_probabilities = {aoi: count/scan_fixations_count for aoi, count in aoi_fixations_count.items()}

        # Stationary entropy
        self.__stationary_entropy = 0

        for aoi, p in stationary_probabilities.items():

            self.__stationary_entropy += p * numpy.log(p + 1e-9)

        self.__stationary_entropy *= -1

        # Transition entropy
        self.__transition_entropy = 0

        destination_p_log_sum = self.transition_matrix_analyzer.transition_matrix_probabilities.apply(lambda row: row.apply(lambda p: p * numpy.log(p + 1e-9)).sum(), axis=1)

        for aoi, s in destination_p_log_sum.items():

            self.__transition_entropy += s * stationary_probabilities[aoi]

        self.__transition_entropy *= -1

    @property
    def stationary_entropy(self) -> float:
        """Stationary entropy."""

        return self.__stationary_entropy

    @property
    def transition_entropy(self) -> float:
        """Transition entropy."""

        return self.__transition_entropy