blob: dfb7005fa65fae6732b63f9b061cbe3413d3dfe6 (
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
from typing import TypeVar
from dataclasses import dataclass, field
import math
from argaze import PupilFeatures
import numpy
@dataclass
class PupilDiameterAnalyzer(PupilFeatures.PupilDiameterAnalyzer):
"""Periodic average of pupil diameter variations to pupil diameter reference value."""
reference: PupilFeatures.PupilDiameter
""" """
period: int | float = field(default=1)
"""Identification period length."""
def __post_init__(self):
assert(self.reference.valid)
self.__variations_sum = 0.
self.__variations_number = 0
self.__last_ts = 0
def analyze(self, ts, pupil_diameter) -> float:
"""Analyze workload index from successive timestamped pupil diameters."""
# Ignore non valid pupil diameter
if not pupil_diameter.valid:
return None
if ts - 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 = ts
return workload_index
else:
self.__variations_sum += pupil_diameter.value - self.reference.value
self.__variations_number += 1
|