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

import math

from argaze import DataStructures

import numpy

FIXATION_MAX_DURATION = 1000

class GazePosition(DataStructures.DictObject):
    """Define gaze position"""

    def __init__(self, x, y):

        super().__init__(type(self).__name__, **{'x': x, 'y': y})

class TimeStampedGazePositions(DataStructures.TimeStampedBuffer):
    """Define timestamped buffer to store gaze positions"""

    def __setitem__(self, key, value: GazePosition):
        """Force value to be a GazePosition"""
        if type(value) != GazePosition:
            raise ValueError('value must be a GazePosition')

        super().__setitem__(key, value)

class Fixation(DataStructures.DictObject):
    """Define fixation"""

    def __init__(self, duration, dispersion, cx, cy):

        super().__init__(type(self).__name__, **{'duration': duration, 'dispersion': dispersion, 'centroid': [cx, cy]})

class TimeStampedFixations(DataStructures.TimeStampedBuffer):
    """Define timestamped buffer to store fixations"""

    def __setitem__(self, key, value: Fixation):
        """Force value to be a Fixation"""
        if type(value) != Fixation:
            raise ValueError('value must be a Fixation')

        super().__setitem__(key, value)

class FixationIdentifier():
    """Abstract class to define what should provide a fixation identifier"""

    fixations = TimeStampedFixations()
    saccades = DataStructures.TimeStampedBuffer()

    def identify(self, ts_gaze_positions):
        raise NotImplementedError('identify() method not implemented')

    def __init__(self, ts_gaze_positions: TimeStampedGazePositions):

        if type(ts_gaze_positions) != TimeStampedGazePositions:
            raise ValueError('argument must be a TimeStampedGazePositions')

        # do identification on a copy
        self.identify(ts_gaze_positions.copy())

class DispersionBasedFixationIdentifier(FixationIdentifier):
    """Implementation of the I-DT algorithm as described in:
        
            Dario D. Salvucci and Joseph H. Goldberg. 2000. Identifying fixations and
            saccades in eye-tracking protocols. In Proceedings of the 2000 symposium
            on Eye tracking research & applications (ETRA '00). ACM, New York, NY, USA,
            71-78. DOI=http://dx.doi.org/10.1145/355017.355028
    """

    def __init__(self, ts_gaze_positions, dispersion_threshold = 10, duration_threshold = 100):

        self.__dispersion_threshold = dispersion_threshold
        self.__duration_threshold = duration_threshold

        super().__init__(ts_gaze_positions)

    # euclidian dispersion
    def __getEuclideanDispersion(self, ts_gaze_positions_list):

        x_list = [gp.x for (ts, gp) in ts_gaze_positions_list]
        y_list = [gp.y for (ts, gp) in ts_gaze_positions_list]

        cx = numpy.mean(x_list)
        cy = numpy.mean(y_list)
        c = [cx, cy]

        points = numpy.column_stack([x_list, y_list])

        dist = (points - c)**2
        dist = numpy.sum(dist, axis=1)
        dist = numpy.sqrt(dist)

        return max(dist), cx, cy

    # basic dispersion
    def __getDispersion(self, ts_gaze_positions_list):

        x_list = [gp.x for (ts, gp) in ts_gaze_positions_list]
        y_list = [gp.y for (ts, gp) in ts_gaze_positions_list]

        return (max(x_list) - min(x_list)) + (max(y_list) - min(y_list))

    def identify(self, ts_gaze_positions):

        # while there are 2 gaze positions at least
        while len(ts_gaze_positions) >= 2:

            # copy remaining timestamped gaze positions
            remaining_ts_gaze_positions = ts_gaze_positions.copy()

            # select timestamped gaze position until a duration threshold
            (ts_start, gaze_position_start) = remaining_ts_gaze_positions.pop_first()
            (ts_current, gaze_position_current) = remaining_ts_gaze_positions.pop_first()

            ts_gaze_positions_list = [(ts_start, gaze_position_start)]

            while (ts_current - ts_start) < self.__duration_threshold:

                ts_gaze_positions_list.append( (ts_current, gaze_position_current) )

                if len(remaining_ts_gaze_positions) > 0:
                    (ts_current, gaze_position_current) = remaining_ts_gaze_positions.pop_first()
                else:
                    break

            # how much gaze is dispersed ?
            dispersion, cx, cy = self.__getEuclideanDispersion(ts_gaze_positions_list)


            # little dispersion
            if dispersion <= self.__dispersion_threshold:

                # remove selected gaze positions
                for gp in ts_gaze_positions_list:
                    ts_gaze_positions.pop_first()

                # are next gaze positions not too dispersed ?
                while len(remaining_ts_gaze_positions) > 0:

                    # select next gaze position
                    ts_gaze_positions_list.append(remaining_ts_gaze_positions.pop_first())

                    new_dispersion, new_cx, new_cy = self.__getEuclideanDispersion(ts_gaze_positions_list)

                    # dispersion too wide
                    if new_dispersion > self.__dispersion_threshold:

                        # remove last gaze position
                        ts_gaze_positions_list.pop(-1)
                        break

                    # store new dispersion data
                    dispersion = new_dispersion
                    cx = new_cx
                    cy = new_cy

                    # remove selected gaze position
                    ts_gaze_positions.pop_first()

                # we have a new fixation
                ts_list = [ts for (ts, gp) in ts_gaze_positions_list]
                duration = ts_list[-1] - ts_list[0]

                if duration > FIXATION_MAX_DURATION:
                    duration = FIXATION_MAX_DURATION

                if duration > 0:

                    # append fixation to timestamped buffer
                    self.fixations[ts_list[0]] = Fixation(duration, dispersion, cx, cy)

            # dispersion too wide : consider next gaze position
            else:
                ts_gaze_positions.pop_first()