Georg Willer
commited on
Commit
·
45856e0
1
Parent(s):
4010ff3
Add missing files
Browse files- constants.py +7 -0
- detectors.py +267 -0
- eyetrack2saccade.py +67 -0
- eyetrack_2_saccade_pipeline.py +2 -4
constants.py
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
TIME_ALIASES = ['time', 'timestamp']
|
2 |
+
LEFT_X_ALIASES = ['l por x [px]']
|
3 |
+
LEFT_Y_ALIASES = ['l por y [px]']
|
4 |
+
RIGHT_X_ALIASES = ['r por x [px]']
|
5 |
+
RIGHT_Y_ALIASES = ['r por y [px]']
|
6 |
+
X_ALIASES = ['por x [px]', 'x']
|
7 |
+
Y_ALIASES = ['por y [px]', 'y']
|
detectors.py
ADDED
@@ -0,0 +1,267 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
#
|
3 |
+
# This file is part of PyGaze - the open-source toolbox for eye tracking
|
4 |
+
#
|
5 |
+
# PyGazeAnalyser is a Python module for easily analysing eye-tracking data
|
6 |
+
# Copyright (C) 2014 Edwin S. Dalmaijer
|
7 |
+
#
|
8 |
+
# This program is free software: you can redistribute it and/or modify
|
9 |
+
# it under the terms of the GNU General Public License as published by
|
10 |
+
# the Free Software Foundation, either version 3 of the License, or
|
11 |
+
# (at your option) any later version.
|
12 |
+
#
|
13 |
+
# This program is distributed in the hope that it will be useful,
|
14 |
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
+
# GNU General Public License for more details.
|
17 |
+
#
|
18 |
+
# You should have received a copy of the GNU General Public License
|
19 |
+
# along with this program. If not, see <http://www.gnu.org/licenses/>
|
20 |
+
|
21 |
+
# EyeTribe Reader
|
22 |
+
#
|
23 |
+
# Reads files as produced by PyTribe (https://github.com/esdalmaijer/PyTribe),
|
24 |
+
# and performs a very crude fixation and blink detection: every sample that
|
25 |
+
# is invalid (usually coded '0.0') is considered to be part of a blink, and
|
26 |
+
# every sample in which the gaze movement velocity is below a threshold is
|
27 |
+
# considered to be part of a fixation. For optimal event detection, it would be
|
28 |
+
# better to use a different algorithm, e.g.:
|
29 |
+
# Nystrom, M., & Holmqvist, K. (2010). An adaptive algorithm for fixation,
|
30 |
+
# saccade, and glissade detection in eyetracking data. Behavior Research
|
31 |
+
# Methods, 42, 188-204. doi:10.3758/BRM.42.1.188
|
32 |
+
#
|
33 |
+
# (C) Edwin Dalmaijer, 2014
|
34 | |
35 |
+
#
|
36 |
+
# version 1 (01-Jul-2014)
|
37 |
+
|
38 |
+
__author__ = "Edwin Dalmaijer"
|
39 |
+
|
40 |
+
import numpy
|
41 |
+
|
42 |
+
|
43 |
+
def blink_detection(x, y, time, missing=0.0, minlen=10):
|
44 |
+
"""Detects blinks, defined as a period of missing data that lasts for at
|
45 |
+
least a minimal amount of samples
|
46 |
+
|
47 |
+
arguments
|
48 |
+
|
49 |
+
x - numpy array of x positions
|
50 |
+
y - numpy array of y positions
|
51 |
+
time - numpy array of EyeTribe timestamps
|
52 |
+
|
53 |
+
keyword arguments
|
54 |
+
|
55 |
+
missing - value to be used for missing data (default = 0.0)
|
56 |
+
minlen - integer indicating the minimal amount of consecutive
|
57 |
+
missing samples
|
58 |
+
|
59 |
+
returns
|
60 |
+
Sblk, Eblk
|
61 |
+
Sblk - list of lists, each containing [starttime]
|
62 |
+
Eblk - list of lists, each containing [starttime, endtime, duration]
|
63 |
+
"""
|
64 |
+
|
65 |
+
# empty list to contain data
|
66 |
+
Sblk = []
|
67 |
+
Eblk = []
|
68 |
+
|
69 |
+
# check where the missing samples are
|
70 |
+
mx = numpy.array(x == missing, dtype=int)
|
71 |
+
my = numpy.array(y == missing, dtype=int)
|
72 |
+
miss = numpy.array((mx + my) == 2, dtype=int)
|
73 |
+
|
74 |
+
# check where the starts and ends are (+1 to counteract shift to left)
|
75 |
+
diff = numpy.diff(miss)
|
76 |
+
starts = numpy.where(diff == 1)[0] + 1
|
77 |
+
ends = numpy.where(diff == -1)[0] + 1
|
78 |
+
|
79 |
+
# compile blink starts and ends
|
80 |
+
for i in range(len(starts)):
|
81 |
+
# get starting index
|
82 |
+
s = starts[i]
|
83 |
+
# get ending index
|
84 |
+
if i < len(ends):
|
85 |
+
e = ends[i]
|
86 |
+
elif len(ends) > 0:
|
87 |
+
e = ends[-1]
|
88 |
+
else:
|
89 |
+
e = -1
|
90 |
+
# append only if the duration in samples is equal to or greater than
|
91 |
+
# the minimal duration
|
92 |
+
if e - s >= minlen:
|
93 |
+
# add starting time
|
94 |
+
Sblk.append([time[s]])
|
95 |
+
# add ending time
|
96 |
+
Eblk.append([time[s], time[e], time[e] - time[s]])
|
97 |
+
|
98 |
+
return Sblk, Eblk
|
99 |
+
|
100 |
+
|
101 |
+
def remove_missing(x, y, time, missing):
|
102 |
+
mx = numpy.array(x == missing, dtype=int)
|
103 |
+
my = numpy.array(y == missing, dtype=int)
|
104 |
+
x = x[(mx + my) != 2]
|
105 |
+
y = y[(mx + my) != 2]
|
106 |
+
time = time[(mx + my) != 2]
|
107 |
+
return x, y, time
|
108 |
+
|
109 |
+
|
110 |
+
def fixation_detection(x, y, time, missing=0.0, maxdist=25, mindur=50):
|
111 |
+
"""Detects fixations, defined as consecutive samples with an inter-sample
|
112 |
+
distance of less than a set amount of pixels (disregarding missing data)
|
113 |
+
|
114 |
+
arguments
|
115 |
+
|
116 |
+
x - numpy array of x positions
|
117 |
+
y - numpy array of y positions
|
118 |
+
time - numpy array of EyeTribe timestamps
|
119 |
+
|
120 |
+
keyword arguments
|
121 |
+
|
122 |
+
missing - value to be used for missing data (default = 0.0)
|
123 |
+
maxdist - maximal inter sample distance in pixels (default = 25)
|
124 |
+
mindur - minimal duration of a fixation in milliseconds; detected
|
125 |
+
fixation cadidates will be disregarded if they are below
|
126 |
+
this duration (default = 100)
|
127 |
+
|
128 |
+
returns
|
129 |
+
Sfix, Efix
|
130 |
+
Sfix - list of lists, each containing [starttime]
|
131 |
+
Efix - list of lists, each containing [starttime, endtime, duration, endx, endy]
|
132 |
+
"""
|
133 |
+
|
134 |
+
x, y, time = remove_missing(x, y, time, missing)
|
135 |
+
|
136 |
+
# empty list to contain data
|
137 |
+
Sfix = []
|
138 |
+
Efix = []
|
139 |
+
|
140 |
+
# loop through all coordinates
|
141 |
+
si = 0
|
142 |
+
fixstart = False
|
143 |
+
for i in range(1, len(x)):
|
144 |
+
# calculate Euclidean distance from the current fixation coordinate
|
145 |
+
# to the next coordinate
|
146 |
+
squared_distance = ((x[si] - x[i]) ** 2 + (y[si] - y[i]) ** 2)
|
147 |
+
dist = 0.0
|
148 |
+
if squared_distance > 0:
|
149 |
+
dist = squared_distance ** 0.5
|
150 |
+
# check if the next coordinate is below maximal distance
|
151 |
+
if dist <= maxdist and not fixstart:
|
152 |
+
# start a new fixation
|
153 |
+
si = 0 + i
|
154 |
+
fixstart = True
|
155 |
+
Sfix.append([time[i]])
|
156 |
+
elif dist > maxdist and fixstart:
|
157 |
+
# end the current fixation
|
158 |
+
fixstart = False
|
159 |
+
# only store the fixation if the duration is ok
|
160 |
+
if time[i - 1] - Sfix[-1][0] >= mindur:
|
161 |
+
Efix.append([Sfix[-1][0], time[i - 1], time[i - 1] - Sfix[-1][0], x[si], y[si]])
|
162 |
+
# delete the last fixation start if it was too short
|
163 |
+
else:
|
164 |
+
Sfix.pop(-1)
|
165 |
+
si = 0 + i
|
166 |
+
elif not fixstart:
|
167 |
+
si += 1
|
168 |
+
# add last fixation end (we can lose it if dist > maxdist is false for the last point)
|
169 |
+
if len(Sfix) > len(Efix):
|
170 |
+
Efix.append([Sfix[-1][0], time[len(x) - 1], time[len(x) - 1] - Sfix[-1][0], x[si], y[si]])
|
171 |
+
return Sfix, Efix
|
172 |
+
|
173 |
+
|
174 |
+
def saccade_detection(x, y, time, missing=0.0, minlen=5, maxvel=40, maxacc=340):
|
175 |
+
"""Detects saccades, defined as consecutive samples with an inter-sample
|
176 |
+
velocity of over a velocity threshold or an acceleration threshold
|
177 |
+
|
178 |
+
arguments
|
179 |
+
|
180 |
+
x - numpy array of x positions
|
181 |
+
y - numpy array of y positions
|
182 |
+
time - numpy array of tracker timestamps in milliseconds
|
183 |
+
|
184 |
+
keyword arguments
|
185 |
+
|
186 |
+
missing - value to be used for missing data (default = 0.0)
|
187 |
+
minlen - minimal length of saccades in milliseconds; all detected
|
188 |
+
saccades with len(sac) < minlen will be ignored
|
189 |
+
(default = 5)
|
190 |
+
maxvel - velocity threshold in pixels/second (default = 40)
|
191 |
+
maxacc - acceleration threshold in pixels / second**2
|
192 |
+
(default = 340)
|
193 |
+
|
194 |
+
returns
|
195 |
+
Ssac, Esac
|
196 |
+
Ssac - list of lists, each containing [starttime]
|
197 |
+
Esac - list of lists, each containing [starttime, endtime, duration, startx, starty, endx, endy]
|
198 |
+
"""
|
199 |
+
x, y, time = remove_missing(x, y, time, missing)
|
200 |
+
|
201 |
+
# CONTAINERS
|
202 |
+
Ssac = []
|
203 |
+
Esac = []
|
204 |
+
|
205 |
+
# INTER-SAMPLE MEASURES
|
206 |
+
# the distance between samples is the square root of the sum
|
207 |
+
# of the squared horizontal and vertical interdistances
|
208 |
+
intdist = (numpy.diff(x) ** 2 + numpy.diff(y) ** 2) ** 0.5
|
209 |
+
# get inter-sample times
|
210 |
+
inttime = numpy.diff(time)
|
211 |
+
# recalculate inter-sample times to seconds
|
212 |
+
inttime = inttime / 1000.0
|
213 |
+
|
214 |
+
# VELOCITY AND ACCELERATION
|
215 |
+
# the velocity between samples is the inter-sample distance
|
216 |
+
# divided by the inter-sample time
|
217 |
+
vel = intdist / inttime
|
218 |
+
# the acceleration is the sample-to-sample difference in
|
219 |
+
# eye movement velocity
|
220 |
+
acc = numpy.diff(vel)
|
221 |
+
|
222 |
+
# SACCADE START AND END
|
223 |
+
t0i = 0
|
224 |
+
stop = False
|
225 |
+
while not stop:
|
226 |
+
# saccade start (t1) is when the velocity or acceleration
|
227 |
+
# surpass threshold, saccade end (t2) is when both return
|
228 |
+
# under threshold
|
229 |
+
|
230 |
+
# detect saccade starts
|
231 |
+
sacstarts = numpy.where((vel[1 + t0i:] > maxvel).astype(int) + (acc[t0i:] > maxacc).astype(int) >= 1)[0]
|
232 |
+
if len(sacstarts) > 0:
|
233 |
+
# timestamp for starting position
|
234 |
+
t1i = t0i + sacstarts[0] + 1
|
235 |
+
if t1i >= len(time) - 1:
|
236 |
+
t1i = len(time) - 2
|
237 |
+
t1 = time[t1i]
|
238 |
+
|
239 |
+
# add to saccade starts
|
240 |
+
Ssac.append([t1])
|
241 |
+
|
242 |
+
# detect saccade endings
|
243 |
+
sacends = numpy.where((vel[1 + t1i:] < maxvel).astype(int) + (acc[t1i:] < maxacc).astype(int) == 2)[0]
|
244 |
+
if len(sacends) > 0:
|
245 |
+
# timestamp for ending position
|
246 |
+
t2i = sacends[0] + 1 + t1i + 2
|
247 |
+
if t2i >= len(time):
|
248 |
+
t2i = len(time) - 1
|
249 |
+
t2 = time[t2i]
|
250 |
+
dur = t2 - t1
|
251 |
+
|
252 |
+
# ignore saccades that did not last long enough
|
253 |
+
if dur >= minlen:
|
254 |
+
# add to saccade ends
|
255 |
+
Esac.append([t1, t2, dur, x[t1i], y[t1i], x[t2i], y[t2i]])
|
256 |
+
else:
|
257 |
+
# remove last saccade start on too low duration
|
258 |
+
Ssac.pop(-1)
|
259 |
+
|
260 |
+
# update t0i
|
261 |
+
t0i = 0 + t2i
|
262 |
+
else:
|
263 |
+
stop = True
|
264 |
+
else:
|
265 |
+
stop = True
|
266 |
+
|
267 |
+
return Ssac, Esac
|
eyetrack2saccade.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import numpy as np
|
3 |
+
from .constants import TIME_ALIASES, LEFT_X_ALIASES, LEFT_Y_ALIASES, RIGHT_X_ALIASES, RIGHT_Y_ALIASES, X_ALIASES, \
|
4 |
+
Y_ALIASES
|
5 |
+
from .detectors import saccade_detection
|
6 |
+
from typing import Union
|
7 |
+
|
8 |
+
class Eye2SacExtractor:
|
9 |
+
data: pd.DataFrame = None
|
10 |
+
x: np.array = None
|
11 |
+
y: np.array = None
|
12 |
+
time: np.array = None
|
13 |
+
|
14 |
+
def _load_data(self, file_path: str):
|
15 |
+
if file_path.endswith('.csv'):
|
16 |
+
return pd.read_csv(file_path, sep=',')
|
17 |
+
elif file_path.endswith('.txt'):
|
18 |
+
return pd.read_csv(file_path, sep='\t')
|
19 |
+
else:
|
20 |
+
raise ValueError('File format not supported. Please provide a csv or txt file.')
|
21 |
+
|
22 |
+
def _clean_data(self):
|
23 |
+
self.data.dropna(inplace=True)
|
24 |
+
|
25 |
+
def _map_relevant_data(self):
|
26 |
+
# convert column names to lowercase
|
27 |
+
self.data.columns = self.data.columns.str.lower()
|
28 |
+
|
29 |
+
# map and extract relevant data
|
30 |
+
try:
|
31 |
+
self.time = self.data[self.data.columns.intersection(TIME_ALIASES)].to_numpy().flatten()
|
32 |
+
|
33 |
+
if self.data.columns.intersection(X_ALIASES).any() and self.data.columns.intersection(Y_ALIASES).any():
|
34 |
+
self.x = self.data[self.data.columns.intersection(X_ALIASES)].to_numpy().flatten()
|
35 |
+
self.y = self.data[self.data.columns.intersection(Y_ALIASES)].to_numpy().flatten()
|
36 |
+
else:
|
37 |
+
left_x = self.data[self.data.columns.intersection(LEFT_X_ALIASES)].to_numpy().flatten()
|
38 |
+
left_y = self.data[self.data.columns.intersection(LEFT_Y_ALIASES)].to_numpy().flatten()
|
39 |
+
right_x = self.data[self.data.columns.intersection(RIGHT_X_ALIASES)].to_numpy().flatten()
|
40 |
+
right_y = self.data[self.data.columns.intersection(RIGHT_Y_ALIASES)].to_numpy().flatten()
|
41 |
+
self._preprocess(left_x, left_y, right_x, right_y)
|
42 |
+
|
43 |
+
except KeyError:
|
44 |
+
raise ValueError('Required data columns are missing or not in the correct naming format.')
|
45 |
+
|
46 |
+
def _preprocess(self, left_x: np.array, left_y: np.array, right_x: np.array, right_y: np.array):
|
47 |
+
# combine left and right eye data into average value
|
48 |
+
self.x = np.mean([left_x, right_x], axis=0)
|
49 |
+
self.y = np.mean([left_y, right_y], axis=0)
|
50 |
+
|
51 |
+
def extract_features(self, data: Union[pd.DataFrame, str]):
|
52 |
+
if isinstance(data, pd.DataFrame):
|
53 |
+
self.data = data
|
54 |
+
elif isinstance(data, str):
|
55 |
+
self.data = self._load_data(data)
|
56 |
+
else:
|
57 |
+
raise ValueError('Data must be a pandas DataFrame or a file path to a csv or txt file.')
|
58 |
+
self._clean_data()
|
59 |
+
self._map_relevant_data()
|
60 |
+
|
61 |
+
return self._extract_features()
|
62 |
+
|
63 |
+
|
64 |
+
def _extract_features(self, missing: float = 0.0, minlen: int = 5, maxvel: int = 40, maxacc: int = 340) -> pd.DataFrame :
|
65 |
+
_, esac = saccade_detection(self.x, self.y, self.time, missing=missing, minlen=minlen, maxvel=maxvel, maxacc=maxacc)
|
66 |
+
esac_df = pd.DataFrame(esac, columns=['starttime', 'endtime', 'duration', 'startx', 'starty', 'endx', 'endy'])
|
67 |
+
return esac_df
|
eyetrack_2_saccade_pipeline.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
from transformers import Pipeline
|
2 |
-
from
|
3 |
|
4 |
class Eye2SacPipeline(Pipeline):
|
5 |
|
@@ -7,11 +7,9 @@ class Eye2SacPipeline(Pipeline):
|
|
7 |
|
8 |
def _sanitize_parameters(self, **kwargs):
|
9 |
preprocess_kwargs = {}
|
10 |
-
if "maybe_arg" in kwargs:
|
11 |
-
preprocess_kwargs["maybe_arg"] = kwargs["maybe_arg"]
|
12 |
return preprocess_kwargs, {}, {}
|
13 |
|
14 |
-
def preprocess(self, inputs
|
15 |
return self.eye2SacExtractor.extract_features(inputs)
|
16 |
|
17 |
def _forward(self, model_inputs):
|
|
|
1 |
from transformers import Pipeline
|
2 |
+
from .eyetrack2saccade import Eye2SacExtractor
|
3 |
|
4 |
class Eye2SacPipeline(Pipeline):
|
5 |
|
|
|
7 |
|
8 |
def _sanitize_parameters(self, **kwargs):
|
9 |
preprocess_kwargs = {}
|
|
|
|
|
10 |
return preprocess_kwargs, {}, {}
|
11 |
|
12 |
+
def preprocess(self, inputs):
|
13 |
return self.eye2SacExtractor.extract_features(inputs)
|
14 |
|
15 |
def _forward(self, model_inputs):
|