aboutsummaryrefslogtreecommitdiff
path: root/src/argaze/PupilAnalysis/WorkloadIndex.py
blob: e450d520b985791e49630c12eff64cd2e8887d6d (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
""" """

"""
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__ = ["Jean-Paul Imbert"]
__copyright__ = "Copyright 2023, Ecole Nationale de l'Aviation Civile (ENAC)"
__license__ = "GPLv3"

import math

from argaze import DataFeatures, PupilFeatures


class PupilDiameterAnalyzer(PupilFeatures.PupilDiameterAnalyzer):
    """Periodic average of pupil diameter variations to pupil diameter reference value.

    Parameters:
        reference: base line value.
        period: identification period length.
    """

    @DataFeatures.PipelineStepInit
    def __init__(self, reference: PupilFeatures.PupilDiameter, period: int|float = 1):

        # Init PupilDiameterAnalyzer class
        super().__init__()

        assert(not math.isnan(reference))

        self.__reference = reference
        self.__period = period

        self.__variations_sum = 0.
        self.__variations_number = 0
        self.__last_ts = 0

    @property
    def reference(self) -> PupilFeatures.PupilDiameter:
        """Get workload index reference."""
        return self.__reference
    
    @property
    def period(self) -> int|float:
        """Get workload index period."""
        return self.__period
    
    @DataFeatures.PipelineStepMethod
    def analyze(self, pupil_diameter: PupilFeatures.PupilDiameter) -> float:
        """Analyze workload index from successive timestamped pupil diameters."""

        # Ignore non valid pupil diameter
        if not math.isnan(pupil_diameter):

            return math.nan

        if pupil_diameter.timestamp - self.__last_ts >= self.__period:

            if self.__variations_number > 0 and self.__reference.value > 0.:

                workload_index = (self.__variations_sum / self.__variations_number) / self.__reference.value

            else: 

                workload_index = 0.

            self.__variations_sum = pupil_diameter.value - self.__reference.value
            self.__variations_number = 1
            self.__last_ts = pupil_diameter.timestamp

            return workload_index

        else:

            self.__variations_sum += pupil_diameter.value - self.__reference.value
            self.__variations_number += 1