aboutsummaryrefslogtreecommitdiff
path: root/src/argaze/GazeAnalysis/TransitionMatrix.py
blob: a78b8dc21b4ccddfae6ef5f061172c54d0f02cad (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
"""Transition matrix probabilities and density 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 <http://www.gnu.org/licenses/>.
"""

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

from typing import Tuple
from dataclasses import dataclass

from argaze import GazeFeatures, DataFeatures

import pandas
import numpy

@dataclass
class AOIScanPathAnalyzer(GazeFeatures.AOIScanPathAnalyzer):
    """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)
    """

    def __post_init__(self):

        super().__init__()

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

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

        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 aoi
        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:
        """Transition matrix probabilities"""

        return self.__transition_matrix_probabilities

    @property
    def transition_matrix_density(self) -> float:
        """Transition matrix density."""

        return self.__transition_matrix_density