aboutsummaryrefslogtreecommitdiff
path: root/src/argaze/GazeFeatures.py
blob: 677e9bb6fa7b2ce316aa9594fed2a01176bbe161 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
"""Generic gaze data and class definitions.

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 TypeVar, Tuple, Any
from dataclasses import dataclass, field
import math
import ast
import json
import importlib
from inspect import getmembers

from argaze import DataFeatures
from argaze.AreaOfInterest import AOIFeatures
from argaze.utils import UtilsFeatures # DEBUG

import numpy
import pandas
import cv2

GazePositionType = TypeVar('GazePosition', bound="GazePosition")
# Type definition for type annotation convenience

class GazePosition(tuple, DataFeatures.TimestampedObject):
    """Define gaze position as a tuple of coordinates with precision.

    Parameters:
        precision: the radius of a circle around value where other same gaze position measurements could be.
        message: a string to describe why the the position is what it is.
    """

    def __new__(cls, position: tuple = (), precision: int|float = None, message: str = None, timestamp: int|float = math.nan):

        return tuple.__new__(cls, position)

    def __init__(self, position: tuple = (), precision: int|float = None, message: str = None, timestamp: int|float = math.nan):

        DataFeatures.TimestampedObject.__init__(self, timestamp)
        self.__precision = precision
        self.__message = message

    @property
    def value(self):
        """Get position's tuple value."""
        return tuple(self)

    @property
    def precision(self):
        """Get position's precision."""
        return self.__precision

    @property
    def message(self):
        """Get position's message."""
        return self.__message

    @classmethod
    def from_dict(self, position_data: dict) -> GazePositionType:

        if 'value' in position_data.keys():

            value = position_data.pop('value')
            return GazePosition(value, **position_data)

        else:

            return GazePosition(**position_data)

    def __bool__(self) -> bool:
        """Is the position value valid?"""
        return len(self) > 0

    def __repr__(self):
        """String representation"""

        return json.dumps(DataFeatures.as_dict(self))

    def __add__(self, position: GazePositionType) -> GazePositionType:
        """Add position.

        !!! note
            The returned position precision is the maximal precision.

        !!! note
            The returned position timestamp is the self object timestamp.
        """
        if self.__precision is not None and position.precision is not None:

            return GazePosition(numpy.array(self) + numpy.array(position), precision = max(self.__precision, position.precision), timestamp=self.timestamp)

        else:

            return GazePosition(numpy.array(self) + numpy.array(position), timestamp=self.timestamp)

    __radd__ = __add__

    def __sub__(self, position: GazePositionType) -> GazePositionType:
        """Substract position.

        !!! note
            The returned position precision is the maximal precision.

        !!! note
            The returned position timestamp is the self object timestamp.
        """
        if self.__precision is not None and position.precision is not None:

            return GazePosition(numpy.array(self) - numpy.array(position), precision = max(self.__precision, position.precision), timestamp=self.timestamp)

        else:

            return GazePosition(numpy.array(self) - numpy.array(position), timestamp=self.timestamp)

    def __rsub__(self, position: GazePositionType) -> GazePositionType:
        """Reversed substract position.

        !!! note
            The returned position precision is the maximal precision.

        !!! note
            The returned position timestamp is the self object timestamp.
        """
        if self.__precision is not None and position.precision is not None:
        
            return GazePosition(numpy.array(position) - numpy.array(self), precision = max(self.__precision, position.precision), timestamp=self.timestamp)

        else:

            return GazePosition(numpy.array(position) - numpy.array(self), timestamp=self.timestamp)

    def __mul__(self, factor: int|float) -> GazePositionType:
        """Multiply position by a factor.

        !!! note
            The returned position precision is also multiplied by the factor.

        !!! note
            The returned position timestamp is the self object timestamp.
        """
        return GazePosition(numpy.array(self) * factor, precision = self.__precision * factor if self.__precision is not None else None, timestamp=self.timestamp)

    def __pow__(self, factor: int|float) -> GazePositionType:
        """Power position by a factor.

        !!! note
            The returned position precision is also powered by the factor.

        !!! note
            The returned position timestamp is the self object timestamp.
        """
        return GazePosition(numpy.array(self) ** factor, precision = self.__precision ** factor if self.__precision is not None else None, timestamp=self.timestamp)

    def distance(self, gaze_position) -> float:
        """Distance to another gaze positions."""

        distance = (self[0] - gaze_position[0])**2 + (self[1] - gaze_position[1])**2
        distance = numpy.sqrt(distance)

        return distance

    def overlap(self, gaze_position, both=False) -> float:
        """Does this gaze position overlap another gaze position considering its precision?
        Set both to True to test if the other gaze position overlaps this one too."""

        distance = numpy.sqrt(numpy.sum((self - gaze_position)**2))

        if both:
            return distance < min(self.__precision, gaze_position.precision)
        else:
            return distance < self.__precision

    def draw(self, image: numpy.array, color: tuple = None, size: int = None, draw_precision=True):
        """Draw gaze position point and precision circle."""

        if self:

            int_value = (int(self[0]), int(self[1]))

            # Draw point at position if required
            if color is not None:
                cv2.circle(image, int_value, size, color, -1)

            # Draw precision circle
            if self.__precision is not None and draw_precision:
                cv2.circle(image, int_value, round(self.__precision), color, 1)

TimeStampedGazePositionsType = TypeVar('TimeStampedGazePositions', bound="TimeStampedGazePositions")
# Type definition for type annotation convenience

class TimeStampedGazePositions(DataFeatures.TimestampedObjectsList):
    """Handle timestamped gaze positions into a list."""
    
    def __init__(self, gaze_positions: list = []):

        DataFeatures.TimestampedObjectsList.__init__(self, GazePosition, gaze_positions)

    def values(self) -> list:
        """Get all timestamped position values as list of tuple."""
        return [tuple(ts_position) for ts_position in self]

    ''' Is it still needed as there is a TimestampedObjectsList.from_json method?
    @classmethod
    def from_json(self, json_filepath: str) -> TimeStampedGazePositionsType:
        """Create a TimeStampedGazePositionsType from .json file."""

        with open(json_filepath, encoding='utf-8') as ts_positions_file:

            json_positions = json.load(ts_positions_file)

            return TimeStampedGazePositions({ast.literal_eval(ts_str): json_positions[ts_str] for ts_str in json_positions})
    '''

    @classmethod
    def from_dataframe(self, dataframe: pandas.DataFrame, timestamp: str, x: str, y: str, precision: str = None, message: str = None) -> TimeStampedGazePositionsType:
        """Create a TimeStampedGazePositions from [Pandas DataFrame](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html).

        Parameters:
            timestamp: specific timestamp column label.
            x: specific x column label.
            y: specific y column label.
            precision: specific precision column label if exist.
            message: specific message column label if exist.
        """

        # Copy columns
        columns = (timestamp, x, y)

        if precision is not None:

            columns += (precision,)

        if message is not None:

            columns += (message,)

        df = dataframe.loc[:, columns]

        # Merge x and y columns into one 'value' column
        df['value'] = tuple(zip(df[x], df[y]))
        df.drop(columns=[x, y], inplace=True, axis=1)

        # Replace tuple values containing NaN values by ()
        df['value'] = df.apply(lambda row: () if pandas.isnull(list(row.value)).any() else row.value, axis=True)

        # Handle precision data
        if precision:

            # Rename precision column into 'precision' column
            df.rename(columns={precision: 'precision'}, inplace=True)

        else:

            # Append a None precision column
            df['precision'] = df.apply(lambda row: None, axis=True)

        # Handle message data
        if message:

            # Rename message column into 'message' column
            df.rename(columns={precision: 'message'}, inplace=True)

        else:

            # Append a None message column
            df['message'] = df.apply(lambda row: None, axis=True)

        # Rename timestamp column into 'timestamp' column
        df.rename(columns={timestamp: 'timestamp'}, inplace=True)
        
        # Filter duplicate timestamps
        df = df[df.timestamp.duplicated() == False]

        # Create timestamped gaze positions
        return TimeStampedGazePositions(df.apply(lambda row: GazePosition(row.value, precision=row.precision, message=row.message, timestamp=row.timestamp), axis=True))

class GazePositionCalibrationFailed(Exception):
    """Exception raised by GazePositionCalibrator."""

    def __init__(self, message):  

        super().__init__(message)

GazePositionCalibratorType = TypeVar('GazePositionCalibrator', bound="GazePositionCalibrator")
# Type definition for type annotation convenience

class GazePositionCalibrator(DataFeatures.PipelineStepObject):
    """Abstract class to define what should provide a gaze position calibrator algorithm."""

    @DataFeatures.PipelineStepInit
    def __init__(self, **kwargs):

        super().__init__(**kwargs)

    def store(self, observed_gaze_position: GazePosition, expected_gaze_position: GazePosition):
        """Store observed and expected gaze positions.

        Parameters:
            observed_gaze_position: where gaze position actually is
            expected_gaze_position: where gaze position should be
        """

        raise NotImplementedError('calibrate() method not implemented')

    def reset(self):
        """Reset observed and expected gaze positions."""

        raise NotImplementedError('reset() method not implemented')

    def calibrate(self) -> Any:
        """Process calibration from observed and expected gaze positions.

        Returns:
            calibration outputs: any data returned to assess calibration
        """

        raise NotImplementedError('terminate() method not implemented')

    def apply(self, observed_gaze_position: GazePosition) -> GazePositionType:
        """Apply calibration onto observed gaze position.

        Parameters:
            observed_gaze_position: where gaze position actually is

        Returns:
            expected_gaze_position: where gaze position should be if the calibrator is ready else, observed gaze position
        """

        raise NotImplementedError('apply() method not implemented')

    def draw(self, image: numpy.array):
        """Draw calibration into image.
        
        Parameters:
            image: where to draw
        """

        raise NotImplementedError('draw() method not implemented')

    def is_calibrating(self) -> bool:
        """Is the calibration running?"""

        raise NotImplementedError('ready getter not implemented')

GazeMovementType = TypeVar('GazeMovement', bound="GazeMovement")
# Type definition for type annotation convenience

class GazeMovement(TimeStampedGazePositions, DataFeatures.TimestampedObject):
    """Define abstract gaze movement class as timestamped gaze positions list.

    !!! note
        Gaze movement timestamp is always equal to its first position timestamp.

    Parameters:
        positions: timestamp gaze positions.
        finished: is the movement finished?
        message: a string to describe why the movement is what it is.
    """

    def __new__(cls, positions: TimeStampedGazePositions = TimeStampedGazePositions(), finished: bool = False, message: str = None, timestamp: int|float = math.nan):

        return TimeStampedGazePositions.__new__(cls, positions)

    def __init__(self, positions: TimeStampedGazePositions = TimeStampedGazePositions(), finished: bool = False, message: str = None, timestamp: int|float = math.nan):
        """Initialize GazeMovement"""

        TimeStampedGazePositions.__init__(self, positions)
        DataFeatures.TimestampedObject.__init__(self, timestamp)

        self.__finished = finished
        self.__message = message

    @property
    def timestamp(self) -> int|float:
        """Get first position timestamp."""
        if self:
            return self[0].timestamp

    def is_timestamped(self) -> bool:
        """If first position exist, the movement is timestamped."""
        return bool(self)

    @timestamp.setter
    def timestamp(self, timestamp: int|float):
        """Block gaze movement timestamp setting."""
        raise('GazeMovement timestamp is first positon timestamp.')

    def is_finished(self) -> bool:
        """Is the movement finished?"""
        return self.__finished

    def finish(self) -> GazeMovementType:
        """Set gaze movement as finished"""
        self.__finished = True
        return self

    @property
    def message(self):
        """Get movement's message."""
        return self.__message

    @property
    def amplitude(self):
        """Get inferred amplitude from first and last positions."""
        if self:

            return numpy.linalg.norm(self[0] - self[-1])

        else:

            return 0

    def __str__(self) -> str:
        """String display"""

        if self:

            output = f'{type(self)}:\n\tduration={self.duration}\n\tsize={len(self)}\n\tfinished={self.is_finished()}'

            for position in self:

                output += f'\n\t{position.timestamp}:\n\t\tvalue={position},\n\t\tprecision={position.precision}'

        else:

            output = f'{type(self)}'

        return output

    def draw_positions(self, image: numpy.array, position_color: tuple = None, line_color: tuple = None):
        """Draw gaze movement positions with line between each position.
        
        Parameters:
            position_color: color of position point
            line_color: color of line between each position
        """

        positions = self.copy()

        while len(positions) >= 2:

            start_gaze_position = positions.pop(0)
            next_gaze_position = positions[0]

            # Draw line between positions if required
            if line_color is not None:

                cv2.line(image, (int(start_gaze_position[0]), int(start_gaze_position[1])), (int(next_gaze_position[0]), int(next_gaze_position[1])), line_color, 1)

            # Draw position if required
            if position_color is not None:

                start_gaze_position.draw(image, position_color, draw_precision=False)

    def draw(self, image: numpy.array, **kwargs):
        """Draw gaze movement into image."""

        raise NotImplementedError('draw() method not implemented')

FixationType = TypeVar('Fixation', bound="Fixation")
# Type definition for type annotation convenience

class Fixation(GazeMovement):
    """Define abstract fixation as gaze movement."""

    def __init__(self, positions: TimeStampedGazePositions = TimeStampedGazePositions(), finished: bool = False, message: str = None, **kwargs):

        super().__init__(positions, finished, message, **kwargs)

        self._focus = ()

    @property
    def focus(self) -> tuple:
        """Get representative position of the fixation."""
        return self._focus

    @focus.setter
    def focus(self, focus: tuple):
        """Set representative position of the fixation."""
        self._focus = focus

    def merge(self, fixation) -> FixationType:
        """Merge another fixation into this fixation."""

        raise NotImplementedError('merge() method not implemented')

def is_fixation(gaze_movement):
    """Is a gaze movement a fixation?"""

    return type(gaze_movement).__bases__[0] == Fixation or type(gaze_movement) == Fixation

SaccadeType = TypeVar('Saccade', bound="Saccade")
# Type definition for type annotation convenience

class Saccade(GazeMovement):
    """Define abstract saccade as gaze movement."""

    def __init__(self, positions: TimeStampedGazePositions = TimeStampedGazePositions(), finished: bool = False, message: str = None, **kwargs):

        super().__init__(positions, finished, message, **kwargs)

def is_saccade(gaze_movement):
    """Is a gaze movement a saccade?"""

    return type(gaze_movement).__bases__[0] == Saccade or type(gaze_movement) == Saccade

TimeStampedGazeMovementsType = TypeVar('TimeStampedGazeMovements', bound="TimeStampedGazeMovements")
# Type definition for type annotation convenience

class TimeStampedGazeMovements(DataFeatures.TimestampedObjectsList):
    """Handle timestamped gaze movements into a list"""

    def __init__(self, gaze_movements: list = []):

        DataFeatures.TimestampedObjectsList.__init__(self, GazeMovement, gaze_movements)

GazeStatusType = TypeVar('GazeStatus', bound="GazeStatus")
# Type definition for type annotation convenience

class GazeStatus(list, DataFeatures.TimestampedObject):
    """Define gaze status as a list of 1 or 2 (index, GazeMovementType) tuples.

    Parameters:
        position: the position that the status represents.
    """

    def __init__(self, position: GazePosition):

        DataFeatures.TimestampedObject.__init__(self, timestamp=position.timestamp)

        self.__position = position

    @property
    def position(self) -> GazePosition:
        """Get gaze status position."""
        return self.__position

    def append(self, movement_index: int, movement_type:type):
        """Append movement index and type."""

        super().append((movement_index, movement_type))

TimeStampedGazeStatusType = TypeVar('TimeStampedGazeStatus', bound="TimeStampedGazeStatus")
# Type definition for type annotation convenience

class TimeStampedGazeStatus(DataFeatures.TimestampedObjectsList):
    """Handle timestamped gaze status into a list."""

    def __init__(self):

        super().__init__(GazeStatus)

class GazeMovementIdentifier(DataFeatures.PipelineStepObject):
    """Abstract class to define what should provide a gaze movement identifier."""

    @DataFeatures.PipelineStepInit
    def __init__(self, **kwargs):

        super().__init__(**kwargs)

    @DataFeatures.PipelineStepMethod
    def identify(self, timestamped_gaze_position: GazePosition, terminate:bool=False) -> GazeMovementType:
        """Identify gaze movement from successive timestamped gaze positions.

        !!! warning "Mandatory"
            Each identified gaze movement have to share its first/last gaze position with previous/next gaze movement.

        Parameters:
            timestamped_gaze_position: new gaze position from where identification have to be done considering former gaze positions.
            terminate: allows to notify identification algorithm that given gaze position will be the last one.
        
        Returns:
            gaze_movement: identified gaze movement once it is finished otherwise it returns empty gaze movement at least.
        """

        raise NotImplementedError('identify() method not implemented')

    def current_gaze_movement(self) -> GazeMovementType:
        """Get the current identified gaze movement (finished or in progress) if it exists otherwise, an empty gaze movement."""

        raise NotImplementedError('current_gaze_movement getter not implemented')

    def current_fixation(self) -> FixationType:
        """Get the current identified fixation (finished or in progress) if it exists otherwise, an empty gaze movement."""

        raise NotImplementedError('current_fixation getter not implemented')

    def current_saccade(self) -> SaccadeType:
        """Get the current identified saccade (finished or in progress) if it exists otherwise, an empty gaze movement."""

        raise NotImplementedError('current_saccade getter not implemented')

    def browse(self, ts_gaze_positions: TimeStampedGazePositions) -> Tuple[TimeStampedGazeMovementsType, TimeStampedGazeMovementsType, TimeStampedGazeStatusType]:
        """Identify fixations and saccades browsing timestamped gaze positions.

        Returns:
            timestamped_fixations: all fixations stored by timestamped.
            timestamped_saccades: all saccades stored by timestamped.
            timestamped_gaze_status: all gaze status stored by timestamped.
        """

        assert(type(ts_gaze_positions) == TimeStampedGazePositions)

        ts_fixations = TimeStampedGazeMovements()
        ts_saccades = TimeStampedGazeMovements()
        ts_status = TimeStampedGazeStatus()

        # Get last ts to terminate identification on last gaze position
        last_ts = ts_gaze_positions[-1].timestamp

        # Iterate on gaze positions
        for gaze_position in ts_gaze_positions:

            gaze_movement = self.identify(gaze_position, terminate=(gaze_position.timestamp == last_ts))

            if gaze_movement:

                # First gaze movement position is always shared with previous gaze movement
                for movement_position in gaze_movement:

                     # Is a status already exist for this position?
                    gaze_status = ts_status.look_for(movement_position.timestamp)

                    if not gaze_status:
                            
                        gaze_status = GazeStatus(movement_position)
                        ts_status.append(gaze_status)

                    gaze_status.append(len(ts_fixations), type(gaze_movement))

                # Store gaze movment into the appropriate list
                if is_fixation(gaze_movement):

                    ts_fixations.append(gaze_movement)

                elif is_saccade(gaze_movement):

                    ts_saccades.append(gaze_movement)

        return ts_fixations, ts_saccades, ts_status

    def __call__(self, ts_gaze_positions: TimeStampedGazePositions) -> Tuple[int|float, GazeMovementType]:
        """GazeMovement generator.

        Parameters:
            ts_gaze_positions: timestamped gaze positions to process.

        Returns:
            timestamp: first gaze position date of identified gaze movement
            gaze_movement: identified gaze movement once it is finished
        """

        assert(type(ts_gaze_positions) == TimeStampedGazePositions)

        # Get last ts to terminate identification on last gaze position
        last_ts = ts_gaze_positions[-1]

        # Iterate on gaze positions
        for gaze_position in ts_gaze_positions:

            gaze_movement = self.identify(gaze_position, terminate=(gaze_position.timestamp == last_ts))

            if gaze_movement:

                yield gaze_movement

ScanStepType = TypeVar('ScanStep', bound="ScanStep")
# Type definition for type annotation convenience

class ScanStepError(Exception):
    """Exception raised at ScanStep creation if a aoi scan step doesn't start by a fixation or doesn't end by a saccade."""

    def __init__(self, message):  

        super().__init__(message)

class ScanStep():
    """Define a scan step as a fixation and a consecutive saccade.

    Parameters:
        first_fixation: a fixation that comes before the next saccade.
        last_saccade: a saccade that comes after the previous fixation.
    
    !!! warning
        Scan step have to start by a fixation and then end by a saccade.
    """

    def __init__(self, first_fixation: Fixation, last_saccade: Saccade):

        self.__first_fixation = first_fixation
        self.__last_saccade = last_saccade

        # First movement have to be a fixation
        if not is_fixation(self.__first_fixation):

            raise ScanStepError('First step movement is not a fixation')

        # Last movement have to be a saccade
        if not is_saccade(self.__last_saccade):
            
            raise ScanStepError('Last step movement is not a saccade')

    @property
    def first_fixation(self):
        """Get scan step first fixation."""
        return self.__first_fixation

    @property
    def last_saccade(self):
        """Get scan step last saccade."""
        return self.__last_saccade
    
    @property
    def fixation_duration(self) -> int|float:
        """Time spent on AOI

        Returns:
            fixation duration
        """

        return self.__first_fixation.duration

    @property
    def duration(self) -> int|float:
        """Time spent on AOI and time spent to go to next AOI

        Returns:
            duration
        """

        return self.__first_fixation.duration + self.__last_saccade.duration

ScanPathType = TypeVar('ScanPathType', bound="ScanPathType")
# Type definition for type annotation convenience

class ScanPath(list):
    """List of scan steps.

    Parameters:
        duration_max: duration from which older scan steps are removed each time new scan steps are added. 0 means no maximal duration.
    """

    def __init__(self, duration_max: int|float = 0):

        super().__init__()

        self.duration_max = duration_max

        self.__last_fixation = None
        self.__duration = 0

    @property
    def duration(self) -> int|float:
        """Sum of all scan steps duration

        Returns:
            duration
        """

        return self.__duration

    def __check_duration(self):
        """Constrain path duration to maximal duration."""

        if self.duration_max > 0:

            while self.__duration > self.duration_max:

                oldest_step = self.pop(0)

                self.__duration -= oldest_step.duration

    def append_saccade(self, saccade) -> ScanStepType:
        """Append new saccade to scan path and return last new scan step if one have been created."""

        # Ignore saccade if no fixation came before
        if self.__last_fixation != None:

            try: 

                # Edit new step
                new_step = ScanStep(self.__last_fixation, saccade)

                # Append new step
                super().append(new_step)

                # Update duration
                self.__duration += new_step.duration

                # Constrain path duration to maximal duration
                self.__check_duration()

                # Return new step
                return new_step

            finally:

                # Clear last fixation
                self.__last_fixation = None

    def append_fixation(self, fixation):
        """Append new fixation to scan path.
        !!! warning
            Consecutives fixations are ignored keeping the last fixation"""

        self.__last_fixation = fixation

    def draw(self, image: numpy.array, draw_fixations: dict = None, draw_saccades: dict = None, deepness: int = 0):
        """Draw scan path into image.

        Parameters:
            draw_fixations: Fixation.draw parameters (which depends of the loaded gaze movement identifier module, if None, no fixation is drawn)
            draw_saccades: Saccade.draw parameters (which depends of the loaded gaze movement identifier module, if None, no saccade is drawn)
            deepness: number of steps back to draw
        """

        for step in self[-deepness:]:

            # Draw fixation if required
            if draw_fixations is not None:

                step.first_fixation.draw(image, **draw_fixations)

            # Draw saccade if required
            if draw_saccades is not None:

                step.last_saccade.draw(image, **draw_saccades)

class ScanPathAnalyzer(DataFeatures.PipelineStepObject):
    """Abstract class to define what should provide a scan path analyzer."""

    @DataFeatures.PipelineStepInit
    def __init__(self, **kwargs):

        super().__init__(**kwargs)

        self.__properties = [name for (name, value) in self.__class__.__dict__.items() if isinstance(value, property)]

    def analysis(self) -> DataFeatures.DataDictionary:
        """Get all scan path analyzer analysis as data dictionary."""

        analysis = {}

        for p in self.__properties:

            analysis[p] = getattr(self, p)

        return DataFeatures.DataDictionary(analysis)

    @DataFeatures.PipelineStepMethod
    def analyze(self, scan_path: ScanPathType):
        """Analyze scan path."""

        raise NotImplementedError('analyze() method not implemented')

class AOIMatcher(DataFeatures.PipelineStepObject):
    """Abstract class to define what should provide an AOI matcher algorithm."""

    @DataFeatures.PipelineStepInit
    def __init__(self, **kwargs):

        super().__init__(**kwargs)

        self.__exclude = []

    @property
    def exclude(self):
        """List of AOI to exclude from matching."""
        return self.__exclude

    @exclude.setter
    def exclude(self, exclude: list[str]):

        self.__exclude = exclude
    
    def match(self, aoi_scene: AOIFeatures.AOIScene, gaze_movement: GazeMovement) -> Tuple[str, AOIFeatures.AreaOfInterest]:
        """Which AOI is looked in the scene?"""

        raise NotImplementedError('match() method not implemented')

    def draw(self, image: numpy.array, aoi_scene: AOIFeatures.AOIScene):
        """Draw matching into image.
        
        Parameters:
            image: where to draw
            aoi_scene: to refresh looked aoi if required
        """

        raise NotImplementedError('draw() method not implemented')

    def looked_aoi(self) -> AOIFeatures.AreaOfInterest:
        """Get most likely looked aoi."""

        raise NotImplementedError('looked_aoi() method not implemented')

    def looked_aoi_name(self) -> str:
        """Get most likely looked aoi name."""
        raise NotImplementedError('looked_aoi_name() method not implemented')

AOIScanStepType = TypeVar('AOIScanStep', bound="AOIScanStep")
# Type definition for type annotation convenience

class AOIScanStepError(Exception):
    """Exception raised at AOIScanStep creation if a aoi scan step doesn't start by a fixation or doesn't end by a saccade."""

    def __init__(self, message, aoi=''):  

        super().__init__(message)

        self.aoi = aoi

class AOIScanStep():
    """Define an aoi scan step as a set of successive gaze movements onto a same AOI.

    Parameters:
        movements: all movements over an AOI and the last saccade that comes out.
        aoi: AOI name
        letter: AOI unique letter to ease sequence analysis.

    !!! warning
        Aoi scan step have to start by a fixation and then end by a saccade.
    """

    def __init__(self, movements: TimeStampedGazeMovements, aoi: str = '', letter: str = ''):

        self.__movements = movements
        self.__aoi = aoi
        self.__letter = letter

        # First movement have to be a fixation
        if not is_fixation(self.first_fixation):

            raise AOIScanStepError('First step movement is not a fixation', self.aoi)

        # Last movement have to be a saccade
        if not is_saccade(self.last_saccade):
            
            raise AOIScanStepError('Last step movement is not a saccade', self.aoi)

    @property
    def movements(self):
        """Get AOI scan step movements."""
        return self.__movements
    
    @property
    def aoi(self):
        """Get AOI scan step aoi."""
        return self.__aoi
    
    @property
    def letter(self):
        """Get AOI scan step letter."""
        return self.__letter
    
    @property
    def first_fixation(self):
        """First fixation on AOI."""
        return self.movements[0]

    @property
    def last_saccade(self):
        """Last saccade that comes out AOI."""
        return self.movements[-1]

    @property
    def fixation_duration(self) -> int|float:
        """Time spent on AOI

        Returns:
            fixation duration
        """
        return self.last_saccade[0].timestamp - self.first_fixation[0].timestamp

    @property
    def duration(self) -> int|float:
        """Time spent on AOI and time spent to go to next AOI

        Returns:
            duration
        """
        return self.last_saccade[-1].timestamp - self.first_fixation[0].timestamp

AOIScanPathType = TypeVar('AOIScanPathType', bound="AOIScanPathType")
# Type definition for type annotation convenience

# Define strings for outside AOI case
OutsideAOI = 'GazeFeatures.OutsideAOI'

class AOIScanPath(list):
    """List of aoi scan steps over successive aoi.

    Parameters:
        duration_max: duration from which older aoi scan steps are removed each time new aoi scan steps are added. 0 means no maximal duration.
    """

    def __init__(self, expected_aoi: list[str] = [], duration_max: int|float = 0):

        super().__init__()

        self.duration_max = duration_max
        self.expected_aoi = expected_aoi

        self.__duration = 0

    def clear(self):
        """Clear aoi scan steps list, letter sequence and transition matrix."""

        super().clear()

        self.__movements = TimeStampedGazeMovements()
        self.__current_aoi = ''
        self.__index = ord('A')
        self.__aoi_letter = {}
        self.__letter_aoi = {}

        size = len(self.__expected_aoi)
        self.__transition_matrix = pandas.DataFrame(numpy.zeros((size, size)), index=self.__expected_aoi, columns=self.__expected_aoi)

    @property
    def duration(self) -> float:
        """Sum of all scan steps duration"""

        return self.__duration

    def __check_duration(self):
        """Constrain path duration to maximal duration."""

        if self.duration_max > 0:

            while self.__duration > self.duration_max:

                oldest_step = self.pop(0)

                self.__duration -= oldest_step.duration

                # Edit transition matrix
                if len(self) > 0:

                    # Decrement [index: source, columns: destination] value
                    self.__transition_matrix.loc[oldest_step.aoi, self[0].aoi,] -= 1

    def __get_aoi_letter(self, aoi):

        try :

            return self.__aoi_letter[aoi]

        except KeyError:

            letter = chr(self.__index)
            self.__aoi_letter[aoi] = letter
            self.__index += 1
            return letter

    def get_letter_aoi(self, letter):
        """Get which aoi is related to an unique letter."""

        return self.__letter_aoi[letter]

    @property
    def letter_sequence(self) -> str:
        """Convert aoi scan path into a string with unique letter per aoi step."""

        sequence = ''
        for step in self:
            sequence += step.letter

        return sequence

    @property
    def expected_aoi(self):
        """List of all expected aoi."""

        return self.__expected_aoi

    @expected_aoi.setter
    def expected_aoi(self, expected_aoi: list[str] = []):
        """Edit list of all expected aoi.

        !!! warning
                This will clear the AOIScanPath
        """
        
        self.__expected_aoi = [OutsideAOI]
        self.__expected_aoi += expected_aoi

        self.clear()
        
    @property
    def current_aoi(self):
        """AOI name of aoi scan step under construction"""

        return self.__current_aoi

    @property
    def transition_matrix(self) -> pandas.DataFrame:
        """[Pandas DataFrame](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html) where indexes are transition departures and columns are transition destinations."""

        return self.__transition_matrix

    def append_saccade(self, saccade):
        """Append new saccade to aoi scan path."""

        # Ignore saccade if no fixation have been stored before
        if len(self.__movements) > 0:

            self.__movements.append(saccade)

    def append_fixation(self, fixation, looked_aoi: str) -> bool:
        """Append new fixation to aoi scan path and return last new aoi scan step if one have been created.

        !!! warning
            It could raise AOIScanStepError
        """

        # Replace None aoi by generic OutsideAOI name
        if looked_aoi is None:

            looked_aoi = OutsideAOI

        # Raise error when aoi is not expected
        elif looked_aoi not in self.__expected_aoi:

            raise AOIScanStepError('AOI not expected', looked_aoi)

        # Is it fixation onto a new aoi?
        if looked_aoi != self.__current_aoi and len(self.__movements) > 0:

            try: 

                # Edit unique letter per aoi
                letter = self.__get_aoi_letter(self.__current_aoi)

                # Remember which letter identify which aoi
                self.__letter_aoi[letter] = self.__current_aoi

                # Edit new step
                new_step = AOIScanStep(self.__movements, self.__current_aoi, letter)

                # Edit transition matrix
                if len(self) > 0:

                    # Increment [index: source, columns: destination] value
                    self.__transition_matrix.loc[self[-1].aoi, self.__current_aoi,] += 1

                # Append new step
                super().append(new_step)

                # Update duration
                self.__duration += new_step.duration

                # Constrain path duration to maximal duration
                self.__check_duration()

                # Return new step
                return new_step

            finally:

                # Clear movements
                self.__movements = TimeStampedGazeMovements()

                # Append new fixation
                self.__movements.append(fixation)

                # Remember new aoi
                self.__current_aoi = looked_aoi
        else:

            # Append new fixation
            self.__movements.append(fixation)

            # Remember aoi
            self.__current_aoi = looked_aoi

            return None

    def fixations_count(self):
        """Get how many fixations are there in the scan path and how many fixation are there in each aoi."""

        scan_fixations_count = 0
        aoi_fixations_count = {aoi: 0 for aoi in self.__expected_aoi}

        for aoi_scan_step in self:

            step_fixations_count = len(aoi_scan_step.movements) - 1 # -1: to ignore last saccade

            scan_fixations_count += step_fixations_count
            aoi_fixations_count[aoi_scan_step.aoi] += step_fixations_count
            
        return scan_fixations_count, aoi_fixations_count

class AOIScanPathAnalyzer(DataFeatures.PipelineStepObject):
    """Abstract class to define what should provide a aoi scan path analyzer."""

    @DataFeatures.PipelineStepInit
    def __init__(self, **kwargs):

        super().__init__(**kwargs)

        self.__properties = [name for (name, value) in self.__class__.__dict__.items() if isinstance(value, property)]

    def analysis(self) -> dict:
        """Get all aoi scan path analyzer analysis as data dictionary."""

        analysis = {}

        for p in self.__properties:

            analysis[p] = getattr(self, p)

        return DataFeatures.DataDictionary(analysis)

    @DataFeatures.PipelineStepMethod
    def analyze(self, aoi_scan_path: AOIScanPathType):
        """Analyze aoi scan path."""

        raise NotImplementedError('analyze() method not implemented')