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

"""Stationary and transition entropy module.
"""

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

from typing import Tuple
from dataclasses import dataclass, field

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

import pandas
import numpy

@dataclass
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)
    """

    transition_matrix_analyzer: TransitionMatrix.AOIScanPathAnalyzer = field(default_factory=TransitionMatrix.AOIScanPathAnalyzer)
    """To get its transition_matrix_probabilities result.

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

    def __post_init__(self):

        super().__init__()

        self.__stationary_entropy = -1
        self.__transition_entropy = -1

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

        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_probalities = {aoi: count/scan_fixations_count for aoi, count in aoi_fixations_count.items()}

        # Stationary entropy
        self.__stationary_entropy = 0

        for aoi, p in stationary_probalities.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_probalities[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