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
|
"""Define eye tracking data file context"""
"""
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 <https://www.gnu.org/licenses/>.
"""
__author__ = "Théo de la Hogue"
__credits__ = []
__copyright__ = "Copyright 2023, Ecole Nationale de l'Aviation Civile (ENAC)"
__license__ = "GPLv3"
import logging
import time
import math
import threading
import ast
import pandas
import numpy
from argaze import ArFeatures, DataFeatures, GazeFeatures
from argaze.utils import UtilsFeatures
class CSV(ArFeatures.DataPlaybackContext):
@DataFeatures.PipelineStepInit
def __init__(self, **kwargs):
# Init ArContext class
super().__init__()
# Init private attributes
self.__path = None
self.__separator = ','
self.__timestamp_column = None
self.__x_column = None
self.__y_column = None
self.__xy_column = None
self.__left_eye_x_column = None
self.__left_eye_y_column = None
self.__left_eye_validity_column = None
self.__right_eye_x_column = None
self.__right_eye_y_column = None
self.__right_eye_validity_column = None
self.__rescale_to_pipeline_size = False
self.__start = math.nan
self.__end = math.nan
self.__duration = 0.
self.__progression = 0.
@property
def path(self) -> str:
"""Path to data file."""
return self.__path
@path.setter
def path(self, path: str):
self.__path = path
@property
def separator(self) -> str:
"""Value delimiter character"""
return self.__separator
@separator.setter
def separator(self, separator: str):
self.__separator = separator
@property
def timestamp_column(self) -> str:
"""Timestamp column name in file.
!!! warning
This column name is mandatory.
"""
return self.__timestamp_column
@timestamp_column.setter
def timestamp_column(self, timestamp_column: str):
self.__timestamp_column = timestamp_column
@property
def x_column(self) -> str:
"""X coordinate column name.
!!! note
This column name is required in the case where gaze position coordinates are splitted in two separated columns.
"""
return self.__x_column
@x_column.setter
def x_column(self, x_column: str):
self.__x_column = x_column
@property
def y_column(self) -> str:
"""Y coordinate column name.
!!! note
This column name is required in the case where gaze position coordinates are splitted in two separated columns.
"""
return self.__y_column
@y_column.setter
def y_column(self, y_column: str):
self.__y_column = y_column
@property
def xy_column(self) -> str:
"""X and Y coordinate column name.
!!! note
This column name is required in the case where gaze position coordinates are joined as a list in one single column.
"""
return self.__xy_column
@xy_column.setter
def xy_column(self, xy_column: str):
self.__xy_column = xy_column
@property
def left_eye_x_column(self) -> str:
"""X coordinate for left eye column name.
!!! note
This column name is required in the case where gaze position coordinates and validity are given for each eye in six separated columns.
"""
return self.__left_eye_x_column
@left_eye_x_column.setter
def left_eye_x_column(self, left_eye_x_column: str):
self.__left_eye_x_column = left_eye_x_column
@property
def left_eye_y_column(self) -> str:
"""Y coordinate for left eye column name.
!!! note
This column name is required in the case where gaze position coordinates and validity are given for each eye in six separated columns.
"""
return self.__left_eye_y_column
@left_eye_y_column.setter
def left_eye_y_column(self, left_eye_y_column: str):
self.__left_eye_y_column = left_eye_y_column
@property
def left_eye_validity_column(self) -> str:
"""Validity of left eye coordinate column name.
!!! note
This column name is required in the case where gaze position coordinates and validity are given for each eye in six separated columns.
"""
return self.__left_eye_validity_column
@left_eye_validity_column.setter
def left_eye_validity_column(self, left_eye_validity_column: str):
self.__left_eye_validity_column = left_eye_validity_column
@property
def right_eye_x_column(self) -> str:
"""X coordinate for right eye column name.
!!! note
This column name is required in the case where gaze position coordinates and validity are given for each eye in six separated columns.
"""
return self.__right_eye_x_column
@right_eye_x_column.setter
def right_eye_x_column(self, right_eye_x_column: str):
self.__right_eye_x_column = right_eye_x_column
@property
def right_eye_y_column(self) -> str:
"""Y coordinate for right eye column name.
!!! note
This column name is required in the case where gaze position coordinates and validity are given for each eye in six separated columns.
"""
return self.__right_eye_y_column
@right_eye_y_column.setter
def right_eye_y_column(self, right_eye_y_column: str):
self.__right_eye_y_column = right_eye_y_column
@property
def right_eye_validity_column(self) -> str:
"""Validity of right eye coordinate column name.
!!! note
This column name is required in the case where gaze position coordinates and validity are given for each eye in six separated columns.
"""
return self.__right_eye_validity_column
@right_eye_validity_column.setter
def right_eye_validity_column(self, right_eye_validity_column: str):
self.__right_eye_validity_column = right_eye_validity_column
@property
def rescale_to_pipeline_size(self) -> bool:
"""Rescale gaze positions to pipeline size."""
return self.__rescale_to_pipeline_size
@rescale_to_pipeline_size.setter
def rescale_to_pipeline_size(self, rescale: bool):
self.__rescale_to_pipeline_size = rescale
@property
def start(self) -> int|float:
"""Start reading timestamp."""
return self.__start
@start.setter
def start(self, start: int|float):
self.__start = start
@property
def end(self) -> int|float:
"""End reading timestamp."""
return self.__end
@end.setter
def end(self, end: int|float):
self.__end = end
@property
def duration(self) -> int|float:
"""Get data duration."""
return self.__duration
@property
def progression(self) -> float:
"""Get data processing progression between 0 and 1."""
return self.__progression
@DataFeatures.PipelineStepEnter
def __enter__(self):
logging.info('CSV file context starts...')
# Select data to load from CSV file
data_columns = []
data_types = {}
data_converters = {}
# Select timestamp column
if self.__timestamp_column:
data_columns.append(self.__timestamp_column)
data_types[self.__timestamp_column] = numpy.float64
else:
logging.error('Missing timestamp column name')
# Case where gaze position coordinates are splitted in two separated columns
if self.__x_column and self.__y_column:
data_columns.append(self.__x_column)
data_types[self.__x_column] = numpy.float64
data_columns.append(self.__y_column)
data_types[self.__y_column] = numpy.float64
# Case where gaze position coordinates are joined as a list in one single column
elif self.__xy_column:
data_columns.append(self.__xy_column)
data_converters[self.__xy_column] = ast.literal_eval
# Case where gaze position coordinates and validity are given for each eye in six separated columns.
elif self.__left_eye_x_column and self.__left_eye_y_column and self.__left_eye_validity_column and self.__right_eye_x_column and self.__right_eye_y_column and self.__right_eye_validity_column:
data_columns.append(self.__left_eye_x_column)
data_types[self.__left_eye_x_column] = numpy.float64
data_columns.append(self.__left_eye_y_column)
data_types[self.__left_eye_y_column] = numpy.float64
data_columns.append(self.__left_eye_validity_column)
data_types[self.__left_eye_validity_column] = numpy.bool_
data_columns.append(self.__right_eye_x_column)
data_types[self.__right_eye_x_column] = numpy.float64
data_columns.append(self.__right_eye_y_column)
data_types[self.__right_eye_y_column] = numpy.float64
data_columns.append(self.__right_eye_validity_column)
data_types[self.__right_eye_validity_column] = numpy.bool_
dataframe = pandas.read_csv(self.__path, delimiter = self.__separator, low_memory = False, usecols=data_columns, dtype=data_types, converters=data_converters)
# Optionnaly select a time range
if not math.isnan(self.start):
dataframe = dataframe.loc[(dataframe[self.__timestamp_column] >= self.start)]
if not math.isnan(self.end):
dataframe = dataframe.loc[(dataframe[self.__timestamp_column] <= self.end)]
# Transform dataframe to a get timestamp, x, y, validity columns
# First, rename timestamp column
dataframe.rename(columns={self.__timestamp_column: 'timestamp'}, inplace=True)
# Case where gaze position coordinates are splitted in two separated columns
if self.__x_column and self.__y_column:
dataframe.rename(columns={self.__x_column: 'x',self.__y_column: 'y'}, inplace=True)
# Case where gaze position coordinates are joined as a list in one single column
elif self.__xy_column:
dataframe[['x','y']] = pandas.DataFrame(dataframe[self.__xy_column].to_list(), columns=['x','y'])
# Case where gaze position coordinates and validity are given for each eye in six separated columns.
elif self.__left_eye_x_column and self.__left_eye_y_column and self.__left_eye_validity_column and self.__right_eye_x_column and self.__right_eye_y_column and self.__right_eye_validity_column:
# Edit gaze position X coordinate as the mean of left and right eyes positions if both are valid else select the valid one
dataframe['x'] = numpy.where(
dataframe[self.__left_eye_validity_column] & dataframe[self.__right_eye_validity_column],
(dataframe[self.__left_eye_x_column] + dataframe[self.__right_eye_x_column]) / 2,
numpy.where(
dataframe[self.__left_eye_validity_column],
dataframe[self.__left_eye_x_column],
numpy.where(
dataframe[self.__right_eye_validity_column],
dataframe[self.__right_eye_x_column],
None
)
)
)
# Edit gaze position Y coordinate as the mean of left and right eyes positions if both are valid else select the valid one
dataframe['y'] = numpy.where(
dataframe[self.__left_eye_validity_column] & dataframe[self.__right_eye_validity_column],
(dataframe[self.__left_eye_y_column] + dataframe[self.__right_eye_y_column]) / 2,
numpy.where(
dataframe[self.__left_eye_validity_column],
dataframe[self.__left_eye_y_column],
numpy.where(
dataframe[self.__right_eye_validity_column],
dataframe[self.__right_eye_y_column],
None
)
)
)
# Mark unvalid gaze positions
dataframe[['validity']] = pandas.DataFrame(dataframe['x'].notna() & dataframe['y'].notna())
unvalid_count = len(dataframe[dataframe['validity'] == False])
if unvalid_count:
logging.info('%i unvalid gaze positions have been detected', unvalid_count)
# Scale valid gaze positions to pipeline size
if self.__rescale_to_pipeline_size:
dataframe.loc[dataframe['validity'], 'x'] *= self.pipeline.size[0]
dataframe.loc[dataframe['validity'], 'y'] *= self.pipeline.size[1]
logging.info('gaze positions have been scaled to pipeline size')
# Open reading thread
self.__reading_thread = threading.Thread(target=self.__read, kwargs={'dataframe': dataframe})
logging.debug('> starting reading thread...')
self.__reading_thread.start()
@DataFeatures.PipelineStepExit
def __exit__(self, exception_type, exception_value, exception_traceback):
logging.info('CSV file context stops...')
# Close data stream
self.stop()
# Stop reading thread
threading.Thread.join(self.__reading_thread)
def __read(self, dataframe: pandas.DataFrame):
"""Read and process gaze positions from dataframe."""
# Setup start and end dates
start_ts = dataframe.iloc[0].timestamp if math.isnan(self.start) else self.start
end_ts = dataframe.iloc[-1].timestamp if math.isnan(self.end) else self.end
self.__duration = end_ts - start_ts
self.__progression = 0.
logging.info('Reading %i gaze positions from %f to %f', len(dataframe), start_ts, end_ts)
for index, row in dataframe.iterrows():
# Stop reading
if not self.is_running():
break
# Pause reading
while self.is_paused() and self.is_running():
time.sleep(0.1)
# Process gaze position
self._process_gaze_position(x = row['x'], y = row['y'], timestamp = row['timestamp'])
# Update progression
self.__progression = (row['timestamp'] - start_ts) / self.__duration
|