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

"""Implementation of transition matrix probabilities and density 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):

        super().__init__()

        self.__transition_matrix_probabilities = pandas.DataFrame()
        self.__transition_matrix_density = 0.

    def analyze(self, aoi_scan_path: GazeFeatures.AOIScanPathType):
        """Analyze aoi scan path."""

        assert(len(aoi_scan_path) > 1)

        # Sum transitions starting from each aoi
        row_sum = aoi_scan_path.transition_matrix.apply(lambda row: row.sum(), axis=1)

        # Editing transition matrix probabilities
        # Note: when no transiton starts from an aoi, destination probabilites is equal to 1/S where S is the number of aois
        self.__transition_matrix_probabilities = aoi_scan_path.transition_matrix.apply(lambda row: row.apply(lambda p: p / row_sum[row.name] if row_sum[row.name] > 0 else 1 / row_sum.size), axis=1)

        # Calculate matrix density
        self.__transition_matrix_density = (self.__transition_matrix_probabilities != 0.).astype(int).sum(axis=1).sum() / self.__transition_matrix_probabilities.size

    @property
    def transition_matrix_probabilities(self) -> pandas.DataFrame:

        return self.__transition_matrix_probabilities

    @property
    def transition_matrix_density(self) -> float:

        return self.__transition_matrix_density