-
Notifications
You must be signed in to change notification settings - Fork 3
/
calculators.py
259 lines (211 loc) · 7.88 KB
/
calculators.py
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
import numpy
import numpy as np
"""
Calculate measures of the detected gaze events, such as:
["t_start", 't_end', 'duration', 'x_start', 'y_start', 'x_end', 'y_end', 'amplitude', 'mean_vel', 'max_vel']
"""
def fixation(x, y, time, events, printing):
"""
Calculate fixation measures
arguments
time - numpy array of EyeTribe timestamps
events - numpy array of of detected gaze events
returns
Fixations - list of lists, each containing [starttime, endtime, duration, endx, endy]
"""
# empty list to contain data
Fixations = []
# check where the missing samples are
idx = numpy.array(events == 'FIX', dtype=int)
# check where the starts and ends are (+1 to counteract shift to left)
diff = numpy.diff(idx)
starts = numpy.where(diff == 1)[0] + 1
ends = numpy.where(diff == -1)[0] + 1
# delete detected starts and ends from before and after recording
if idx[-0] == 1:
starts = starts[0:-1]
if idx[0] == 1:
ends = ends[1::]
if len(starts) > len(ends):
ends = np.append(ends, len(idx) - 1)
# compile fixation starts and ends
for i in range(len(starts)):
# get starting index
s = starts[i]
# get ending index
if i < len(ends):
e = ends[i]
elif len(ends) > 0:
e = ends[-1]
else:
e = -1
# add ending time
xpos = np.mean([x[s], x[e]])
ypos = np.mean([y[s], y[e]])
duration = time[e] - time[s]
Fixations.append([time[s], time[e], duration, xpos, ypos])
Fixations = numpy.array(Fixations)
if Fixations.any() and printing:
numFix = len(Fixations)
avgFix = 1000 * sum(Fixations[:, 2]) / numFix
print('Number of fixations: ' + str(numFix))
print(' Average fixation duration: ' + str(avgFix) + 'ms')
return Fixations
def saccade(x, y, v, time, events, printing):
"""
Calculate Saccade measures
arguments
x - numpy array of x positions
y - numpy array of y positions
v - numpy array of velocities
time - numpy array of trafcker timestamps in milliseconds
events - numpy array of of detected gaze events
returns
Saccades - list of lists, each containing [starttime, endtime, duration, startx, starty, endx, endy, amplitude, meanvel, maxvel])]
"""
# empty list to contain data
Saccades = []
# check where the missing samples are
idx = numpy.array(events == 'SACCADE', dtype=int)
# check where the starts and ends are (+1 to counteract shift to left)
diff = numpy.diff(idx)
starts = numpy.where(diff == 1)[0] + 1
ends = numpy.where(diff == -1)[0] + 1
# delete detected starts and ends from before and after recording
if idx[-0] == 1:
starts = starts[0:-1]
if idx[0] == 1:
ends = ends[1::]
if len(starts) > len(ends):
ends = np.append(ends, len(idx) - 1)
# compile saccade starts and ends
for i in range(len(starts)):
# get starting index
s = starts[i]
# get ending index
if i < len(ends):
e = ends[i]
elif len(ends) > 0:
e = ends[-1]
else:
e = -1
# writing data
duration = time[e] - time[s]
amplitude = ((x[s] - x[e]) ** 2 + (y[s] - y[e]) ** 2) ** 0.5
meanvel = sum(v[s:e]) / len(v[s:e])
maxvel = max(v[s:e])
Saccades.append([time[s], time[e], duration, x[s], y[s], x[e], y[e], amplitude, meanvel, maxvel])
Saccades = numpy.array(Saccades)
if Saccades.any() and printing:
numSac = len(Saccades)
avgSacT = 1000 * sum(Saccades[:, 2]) / numSac
avgSacA = sum(Saccades[:, 7]) / numSac
avgSacV = sum(Saccades[:, 8]) / numSac
avgSacMV = sum(Saccades[:, 9]) / numSac
print('Number of saccades: ' + str(numSac))
print(' Average saccades duration: ' + str(avgSacT) + ' ms')
print(' Average saccades Amplitude: ' + str(avgSacA) + ' deg')
print(' Average saccades velocity: ' + str(avgSacV) + ' deg/s')
print(' Average max saccades velocity: ' + str(avgSacMV) + ' deg/s')
return Saccades
def pursuit(x, y, v, time, events, printing):
"""
Calculates Pursuit measures
arguments
x - numpy array of x positions
y - numpy array of y positions
v - numpy array of velocities
time - numpy array of tracker timestamps in milliseconds
events - numpy array of of detected gaze events
returns
Pursuits - list of lists, each containing [starttime, endtime, duration, startx, starty, endx, endy, amplitude, meanvel, maxvel])]
"""
# empty list to contain data
Pursuits = []
# check where the missing samples are
idx = numpy.array(events == 'SP', dtype=int)
# check where the starts and ends are (+1 to counteract shift to left)
diff = numpy.diff(idx)
starts = numpy.where(diff == 1)[0] + 1
ends = numpy.where(diff == -1)[0] + 1
# delete detected starts and ends from before and after recording
if idx[-0] == 1:
starts = starts[0:-1]
if idx[0] == 1:
ends = ends[1::]
if len(starts) > len(ends):
ends = np.append(ends, len(idx) - 1)
# compile pursuit starts and ends
for i in range(len(starts)):
# get starting index
s = starts[i]
# get ending index
if i < len(ends):
e = ends[i]
elif len(ends) > 0:
e = ends[-1]
else:
e = -1
# writing data
duration = time[e] - time[s]
amplitude = ((x[s] - x[e]) ** 2 + (y[s] - y[e]) ** 2) ** 0.5
meanvel = sum(v[s:e]) / len(v[s:e])
maxvel = max(v[s:e])
Pursuits.append([time[s], time[e], duration, x[s], y[s], x[e], y[e], amplitude, meanvel, maxvel])
Pursuits = numpy.array(Pursuits)
if Pursuits.any() and printing:
numSP = len(Pursuits)
avgSPT = 1000 * sum(Pursuits[:, 2]) / numSP
avgSPA = sum(Pursuits[:, 7]) / numSP
avgSPV = sum(Pursuits[:, 8]) / numSP
avgSPMV = sum(Pursuits[:, 9]) / numSP
print('Number of Smooth Pursuits: ' + str(numSP))
print(' Average pursuit duration: ' + str(avgSPT) + 'ms')
print(' Average pursuit Amplitude: ' + str(avgSPA) + ' deg')
print(' Average pursuit velocity: ' + str(avgSPV) + ' deg/s')
print(' Average max pursuit velocity: ' + str(avgSPMV) + ' deg/s')
return Pursuits
def blink(time, events, printing):
"""
Calculates Blink measures
arguments
time - numpy array of EyeTribe timestamps
events - numpy array of of detected gaze events
returns
Blinks - list of lists, each containing [starttime, endtime, duration]
"""
# empty list to contain data
Blinks = []
# check where the missing samples are
idx = numpy.array(events == 'BLINK', dtype=int)
# check where the starts and ends are (+1 to counteract shift to left)
diff = numpy.diff(idx)
starts = numpy.where(diff == 1)[0] + 1
ends = numpy.where(diff == -1)[0] + 1
# delete detected starts and ends from before and after recording
if idx[-0] == 1:
starts = starts[0:-1]
if idx[0] == 1:
ends = ends[1::]
if len(starts) > len(ends):
ends = np.append(ends, len(idx) - 1)
# compile blink starts and ends
for i in range(len(starts)):
# get starting index
s = starts[i]
# get ending index
if i < len(ends):
e = ends[i]
elif len(ends) > 0:
e = ends[-1]
else:
e = -1
# add ending time
Blinks.append([time[s], time[e], time[e] - time[s]])
Blinks = numpy.array(Blinks)
if Blinks.any() and printing:
numBlk = len(Blinks)
avgBlk = 1000 * sum(Blinks[:, 2]) / numBlk
print('Number of blinks: ' + str(numBlk))
print(' Average blink duration: ' + str(avgBlk) + 'ms')
return Blinks