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

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

__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

from argaze import GazeFeatures

import pandas
import numpy

@dataclass
class AOIScanPathAnalyzer(GazeFeatures.AOIScanPathAnalyzer):

    def __post_init__(self):

        pass

    def analyze(self, aoi_scan_path: GazeFeatures.AOIScanPathType, transition_matrix_probabilities: pandas.DataFrame) -> Tuple[float, float]:
        """Analyze aoi scan path.

        * **Returns:**
            - stationary entropy
            - transition entropy"""

        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
        stationary_entropy = 0

        for aoi, p in stationary_probalities.items():

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

        stationary_entropy *= -1

        # Transition entropy
        transition_entropy = 0

        destination_p_log_sum = 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():

            transition_entropy += s * stationary_probalities[aoi]

        transition_entropy *= -1

        return stationary_entropy, transition_entropy