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
|
#!/usr/bin/env python
""" """
__author__ = "Théo de la Hogue"
__credits__ = []
__copyright__ = "Copyright 2023, Ecole Nationale de l'Aviation Civile (ENAC)"
__license__ = "BSD"
import unittest
from dataclasses import dataclass
from argaze import GazeFeatures
import numpy
import pandas
def random_gaze_positions(size, frame_dimension: tuple[float, float] = (1, 1)):
""" Generate random TimeStampedGazePsoitions for testing purpose.
Timestamps are current time.
GazePositions are random values.
"""
import random
import time
ts_gaze_positions = GazeFeatures.TimeStampedGazePositions()
for i in range(0, size):
# Edit gaze position
random_gaze_position = GazeFeatures.GazePosition((random.random() * frame_dimension[0], random.random() * frame_dimension[1]))
# Store gaze position
ts_gaze_positions[time.time()] = random_gaze_position
return ts_gaze_positions
@dataclass(frozen=True)
class TestFixation(GazeFeatures.Fixation):
"""Define basic fixation class for test."""
def __post_init__(self):
super().__post_init__()
points = self.positions.values()
points_x, points_y = [p[0] for p in points], [p[1] for p in points]
points_array = numpy.column_stack([points_x, points_y])
centroid_array = numpy.array([numpy.mean(points_x), numpy.mean(points_y)])
# Update frozen focus attribute using centroid
object.__setattr__(self, 'focus', (centroid_array[0], centroid_array[1]))
@dataclass(frozen=True)
class TestSaccade(GazeFeatures.Saccade):
"""Define basic saccade for test."""
def __post_init__(self):
super().__post_init__()
class TestGazePositionClass(unittest.TestCase):
"""Test GazePosition class."""
def test_new(self):
"""Test GazePosition creation."""
# Check empty GazePosition
empty_gaze_position = GazeFeatures.GazePosition()
self.assertEqual(empty_gaze_position.value, (0, 0))
self.assertEqual(empty_gaze_position[0], 0)
self.assertEqual(empty_gaze_position[1], 0)
for v in empty_gaze_position:
self.assertEqual(v, 0)
self.assertEqual(empty_gaze_position.precision, 0.)
self.assertEqual(len(empty_gaze_position), 2)
self.assertEqual(empty_gaze_position.valid, True)
self.assertEqual(numpy.array(empty_gaze_position).shape, (2,))
# Check integer GazePosition
int_gaze_position = GazeFeatures.GazePosition((123, 456), precision=55)
self.assertEqual(int_gaze_position.value, (123, 456))
self.assertEqual(int_gaze_position[0], 123)
self.assertEqual(int_gaze_position[1], 456)
self.assertEqual(int_gaze_position.precision, 55)
self.assertEqual(len(int_gaze_position), 2)
self.assertEqual(int_gaze_position.valid, True)
self.assertEqual(numpy.array(empty_gaze_position).shape, (2,))
# Check float GazePosition
float_gaze_position = GazeFeatures.GazePosition((1.23, 4.56), precision=5.5)
self.assertEqual(float_gaze_position.value, (1.23, 4.56))
self.assertEqual(float_gaze_position[0], 1.23)
self.assertEqual(float_gaze_position[1], 4.56)
self.assertEqual(float_gaze_position.precision, 5.5)
self.assertEqual(len(float_gaze_position), 2)
self.assertEqual(float_gaze_position.valid, True)
self.assertEqual(numpy.array(empty_gaze_position).shape, (2,))
def test_properties(self):
"""Test GazePosition properties cannot be modified after creation."""
gaze_position = GazeFeatures.GazePosition()
# Check that gaze position value setting fails
with self.assertRaises(AttributeError):
gaze_position.value = (123, 456)
self.assertNotEqual(gaze_position.value, (123, 456))
self.assertEqual(gaze_position.value, (0, 0))
def test_overlapping(self):
"""Test GazePosition overlap method."""
gaze_position_A = GazeFeatures.GazePosition((0, 0), precision=10)
gaze_position_B = GazeFeatures.GazePosition((10, 0), precision=10)
gaze_position_C = GazeFeatures.GazePosition((5, 0), precision=5)
gaze_position_D = GazeFeatures.GazePosition((4, 0), precision=5)
self.assertFalse(gaze_position_A.overlap(gaze_position_B))
self.assertTrue(gaze_position_A.overlap(gaze_position_C))
self.assertFalse(gaze_position_A.overlap(gaze_position_C, both=True))
self.assertFalse(gaze_position_B.overlap(gaze_position_A))
self.assertTrue(gaze_position_B.overlap(gaze_position_C))
self.assertFalse(gaze_position_B.overlap(gaze_position_C, both=True))
self.assertFalse(gaze_position_C.overlap(gaze_position_A))
self.assertFalse(gaze_position_C.overlap(gaze_position_B))
def test___repr__(self):
"""Test GazePosition string representation."""
# Check empty GazePosition representation
self.assertEqual(repr(GazeFeatures.GazePosition()), "{\"value\": [0, 0], \"precision\": 0.0}")
class TestUnvalidGazePositionClass(unittest.TestCase):
"""Test UnvalidGazePosition class."""
def test_new(self):
"""Test UnvalidGazePosition creation."""
import math
unvalid_gaze_position = GazeFeatures.UnvalidGazePosition()
self.assertEqual(unvalid_gaze_position.value, (None, None))
self.assertEqual(unvalid_gaze_position.precision, None)
self.assertEqual(unvalid_gaze_position.valid, False)
def test___repr__(self):
"""Test UnvalidGazePosition string representation."""
self.assertEqual(repr(GazeFeatures.UnvalidGazePosition()), "{\"message\": null, \"value\": [null, null], \"precision\": null}")
class TestTimeStampedGazePositionsClass(unittest.TestCase):
"""Test TimeStampedGazePositions class."""
def test___setitem__(self):
"""Test __setitem__ method."""
ts_gaze_positions = GazeFeatures.TimeStampedGazePositions()
ts_gaze_positions[0] = GazeFeatures.GazePosition()
ts_gaze_positions[1] = GazeFeatures.UnvalidGazePosition()
ts_gaze_positions[2] = {"value": (0, 0), "precision": 0.}
# Check GazePosition is correctly stored and accessible as a GazePosition
self.assertIsInstance(ts_gaze_positions[0], GazeFeatures.GazePosition)
self.assertEqual(ts_gaze_positions[0].valid, True)
# Check UnvalidGazePosition is correctly stored and accessible as a UnvalidGazePosition
self.assertIsInstance(ts_gaze_positions[1], GazeFeatures.UnvalidGazePosition)
self.assertEqual(ts_gaze_positions[1].valid, False)
# Check dict with "value" and "precision" keys is correctly stored and accessible as a GazePosition
self.assertIsInstance(ts_gaze_positions[2], GazeFeatures.GazePosition)
self.assertEqual(ts_gaze_positions[2].valid, True)
# Check that bad data type insertion fails
with self.assertRaises(AssertionError):
ts_gaze_positions[3] = "This string is not a gaze position value."
# Check that dict with bad keys insertion fails
with self.assertRaises(AssertionError):
ts_gaze_positions[4] = {"bad_key": (0, 0), "precision": 0.}
# Check final lenght
self.assertEqual(len(ts_gaze_positions), 3)
def test___repr__(self):
"""Test inherited string representation."""
ts_gaze_positions = GazeFeatures.TimeStampedGazePositions()
self.assertEqual(repr(GazeFeatures.TimeStampedGazePositions()), "{}")
ts_gaze_positions[0] = GazeFeatures.GazePosition()
self.assertEqual(repr(ts_gaze_positions), "{\"0\": {\"value\": [0, 0], \"precision\": 0.0}}")
ts_gaze_positions[0] = GazeFeatures.UnvalidGazePosition()
self.assertEqual(repr(ts_gaze_positions), "{\"0\": {\"message\": null, \"value\": [null, null], \"precision\": null}}")
def test_from_dataframe(self):
"""Test from_dataframe classmethod."""
data = {'Specific timestamp label': [0, 1, 2, 3, 4],
'Specific gaze position x label': [0, 10, numpy.nan, 30, 40],
'Specific gaze position y label': [0, 100, numpy.nan, 300, 400]
}
dataframe = pandas.DataFrame().from_dict(data)
ts_gaze_positions = GazeFeatures.TimeStampedGazePositions.from_dataframe(dataframe, timestamp='Specific timestamp label', x='Specific gaze position x label', y='Specific gaze position y label')
# Check buffer length
self.assertEqual(len(ts_gaze_positions), 5)
# Check first gaze position is correctly stored and accessible as a GazePosition
self.assertIsInstance(ts_gaze_positions[0], GazeFeatures.GazePosition)
self.assertEqual(ts_gaze_positions[0].precision, 0)
self.assertEqual(ts_gaze_positions[0].valid, True)
# Check third gaze position is correctly stored and accessible as a UnvalidGazePosition
self.assertIsInstance(ts_gaze_positions[2], GazeFeatures.UnvalidGazePosition)
self.assertEqual(ts_gaze_positions[2].precision, None)
self.assertEqual(ts_gaze_positions[2].valid, False)
data = {'Specific timestamp label': [0, 1, 2, 3, 4],
'Specific gaze position x label': [0, 10, numpy.nan, 30, 40],
'Specific gaze position y label': [0, 100, numpy.nan, 300, 400],
'Specific precisison label': [15, 15, numpy.nan, 15, 15]
}
dataframe = pandas.DataFrame().from_dict(data)
ts_gaze_positions = GazeFeatures.TimeStampedGazePositions.from_dataframe(dataframe, timestamp='Specific timestamp label', x='Specific gaze position x label', y='Specific gaze position y label', precision='Specific precisison label')
# Check buffer length
self.assertEqual(len(ts_gaze_positions), 5)
# Check first gaze position is correctly stored and accessible as a GazePosition
self.assertIsInstance(ts_gaze_positions[0], GazeFeatures.GazePosition)
self.assertEqual(ts_gaze_positions[0].precision, 15)
self.assertEqual(ts_gaze_positions[0].valid, True)
# Check third gaze position is correctly stored and accessible as a UnvalidGazePosition
self.assertIsInstance(ts_gaze_positions[2], GazeFeatures.UnvalidGazePosition)
self.assertEqual(ts_gaze_positions[2].precision, None)
self.assertEqual(ts_gaze_positions[2].valid, False)
def test_as_dataframe(self):
"""Test inherited as_dataframe method."""
ts_gaze_positions_dataframe = random_gaze_positions(10).as_dataframe()
# Check dataframe conversion
self.assertEqual(ts_gaze_positions_dataframe.index.name, "timestamp")
self.assertEqual(ts_gaze_positions_dataframe.index.size, 10)
self.assertEqual(ts_gaze_positions_dataframe.columns.size, 2)
self.assertEqual(ts_gaze_positions_dataframe.columns[0], "value")
self.assertEqual(ts_gaze_positions_dataframe.columns[1], "precision")
self.assertEqual(ts_gaze_positions_dataframe["value"].dtype, 'object')
self.assertEqual(ts_gaze_positions_dataframe["precision"].dtype, 'float64')
# Check unvalid position conversion
ts_gaze_positions = GazeFeatures.TimeStampedGazePositions()
ts_gaze_positions[0] = GazeFeatures.UnvalidGazePosition()
ts_gaze_positions_dataframe = ts_gaze_positions.as_dataframe()
self.assertEqual(ts_gaze_positions_dataframe.index.name, "timestamp")
self.assertEqual(ts_gaze_positions_dataframe.index.size, 1)
self.assertEqual(ts_gaze_positions_dataframe.columns.size, 2)
self.assertEqual(ts_gaze_positions_dataframe.columns[0], "value")
self.assertEqual(ts_gaze_positions_dataframe.columns[1], "precision")
self.assertEqual(ts_gaze_positions_dataframe["value"].dtype, 'object')
self.assertEqual(ts_gaze_positions_dataframe["precision"].dtype, 'O') # Python object type
class TestGazeMovementClass(unittest.TestCase):
"""Test GazeMovement class."""
def test_new(self):
"""Test GazeMovement creation."""
abstract_gaze_movement = GazeFeatures.GazeMovement(random_gaze_positions(0))
# Check abstract GazeMovement
self.assertEqual(len(abstract_gaze_movement.positions), 0)
self.assertEqual(abstract_gaze_movement.duration, -1)
self.assertEqual(abstract_gaze_movement.amplitude, -1)
self.assertEqual(abstract_gaze_movement.valid, False)
self.assertEqual(abstract_gaze_movement.finished, False)
def test_finish(self):
"""Test GazeMovement finishing."""
abstract_gaze_movement = GazeFeatures.GazeMovement(random_gaze_positions(2))
abstract_gaze_movement_ref = abstract_gaze_movement
# Check abstract GazeMovement and its reference
self.assertEqual(abstract_gaze_movement.finished, False)
self.assertEqual(abstract_gaze_movement_ref.finished, False)
# Set gaze movement as finished
abstract_gaze_movement.finish()
# Check abstract GazeMovement and its reference
self.assertEqual(abstract_gaze_movement.finished, True)
self.assertEqual(abstract_gaze_movement_ref.finished, True)
class TestUnvalidGazeMovementClass(unittest.TestCase):
"""Test UnvalidGazeMovement class."""
def test_new(self):
"""Test UnvalidGazeMovement creation."""
unvalid_gaze_movement = GazeFeatures.UnvalidGazeMovement('test')
# Check UnvalidGazeMovement
self.assertEqual(len(unvalid_gaze_movement.positions), 0)
self.assertEqual(unvalid_gaze_movement.duration, -1)
self.assertEqual(unvalid_gaze_movement.amplitude, -1)
self.assertEqual(unvalid_gaze_movement.valid, False)
self.assertEqual(unvalid_gaze_movement.finished, False)
self.assertEqual(unvalid_gaze_movement.message, 'test')
class TestScanStepClass(unittest.TestCase):
"""Test ScanStep class."""
def test_new(self):
"""Test ScanStep creation."""
fixation = TestFixation(random_gaze_positions(10))
saccade = TestSaccade(random_gaze_positions(2))
scan_step = GazeFeatures.ScanStep(fixation, saccade)
# Check aoi scan step creation
self.assertEqual(scan_step.first_fixation, fixation)
self.assertEqual(scan_step.last_saccade, saccade)
self.assertGreater(scan_step.duration, 0)
def build_scan_path(size, frame_dimension: tuple[float, float] = (1, 1)):
"""Build scan path"""
scan_path = GazeFeatures.ScanPath()
for i in range(size):
fixation = TestFixation(random_gaze_positions(10, frame_dimension))
ts, _ = fixation.positions.first
scan_path.append_fixation(ts, fixation)
saccade = TestSaccade(random_gaze_positions(2, frame_dimension))
ts, _ = saccade.positions.first
scan_path.append_saccade(ts, saccade)
return scan_path
class TestScanPathClass(unittest.TestCase):
"""Test ScanPath class."""
def test_new(self):
"""Test ScanPath creation."""
# Check aoi scan step creation
scan_path = GazeFeatures.ScanPath()
self.assertEqual(len(scan_path), 0)
self.assertEqual(scan_path.duration, 0)
def test_append(self):
"""Test ScanPath append methods."""
scan_path = GazeFeatures.ScanPath()
# Append a saccade that should be ignored
saccade = TestSaccade(random_gaze_positions(2))
ts, _ = saccade.positions.first
new_step = scan_path.append_saccade(ts, saccade)
# Check that no scan step have been created yet
self.assertEqual(len(scan_path), 0)
self.assertEqual(scan_path.duration, 0)
self.assertEqual(new_step, None)
# Append first fixation
fixation_A = TestFixation(random_gaze_positions(10))
ts, _ = fixation_A.positions.first
new_step = scan_path.append_fixation(ts, fixation_A)
# Check that no scan step have been created yet
self.assertEqual(len(scan_path), 0)
self.assertEqual(scan_path.duration, 0)
self.assertEqual(new_step, None)
# Append consecutive saccade
saccade_A = TestSaccade(random_gaze_positions(2))
ts, _ = saccade_A.positions.first
new_step_A = scan_path.append_saccade(ts, saccade_A)
# Check that new scan step have been created
self.assertEqual(len(scan_path), 1)
self.assertEqual(scan_path.duration, new_step_A.duration)
self.assertEqual(new_step_A.first_fixation, fixation_A)
self.assertEqual(new_step_A.last_saccade, saccade_A)
self.assertEqual(new_step_A.duration, fixation_A.duration + saccade_A.duration)
# Append 2 consecutive fixations then a saccade
fixation_B1 = TestFixation(random_gaze_positions(10))
ts, _ = fixation_B1.positions.first
new_step = scan_path.append_fixation(ts, fixation_B1)
# Check that no scan step have been created yet
self.assertEqual(len(scan_path), 1)
self.assertEqual(scan_path.duration, new_step_A.duration)
self.assertEqual(new_step, None)
fixation_B2 = TestFixation(random_gaze_positions(10))
ts, _ = fixation_B2.positions.first
new_step = scan_path.append_fixation(ts, fixation_B2)
# Check that no scan step have been created yet
self.assertEqual(len(scan_path), 1)
self.assertEqual(scan_path.duration, new_step_A.duration)
self.assertEqual(new_step, None)
saccade_B = TestSaccade(random_gaze_positions(2))
ts, _ = saccade_B.positions.first
new_step_B = scan_path.append_saccade(ts, saccade_B)
# Check that new scan step have been created
self.assertEqual(len(scan_path), 2)
self.assertEqual(scan_path.duration, new_step_A.duration + new_step_B.duration)
self.assertEqual(new_step_B.first_fixation, fixation_B2)
self.assertEqual(new_step_B.last_saccade, saccade_B)
self.assertEqual(new_step_B.duration, fixation_B2.duration + saccade_B.duration)
class TestAOIScanStepClass(unittest.TestCase):
"""Test AOIScanStep class."""
def test_new(self):
"""Test AOIScanStep creation."""
movements = GazeFeatures.TimeStampedGazeMovements()
fixation = TestFixation(random_gaze_positions(10))
ts, _ = fixation.positions.first
movements[ts] = fixation
saccade = TestSaccade(random_gaze_positions(2))
ts, _ = saccade.positions.first
movements[ts] = saccade
aoi_scan_step = GazeFeatures.AOIScanStep(movements, 'Test')
# Check aoi scan step creation
self.assertEqual(len(aoi_scan_step.movements), 2)
self.assertEqual(aoi_scan_step.aoi, 'Test')
self.assertEqual(aoi_scan_step.first_fixation, fixation)
self.assertEqual(aoi_scan_step.last_saccade, saccade)
self.assertGreater(aoi_scan_step.duration, 0)
def test_error(self):
"""Test AOIScanStep creation error."""
movements = GazeFeatures.TimeStampedGazeMovements()
saccade = TestSaccade(random_gaze_positions(2))
ts, _ = saccade.positions.first
movements[ts] = saccade
fixation = TestFixation(random_gaze_positions(10))
ts, _ = fixation.positions.first
movements[ts] = fixation
# Check that aoi scan step creation fail
with self.assertRaises(GazeFeatures.AOIScanStepError):
aoi_scan_step = GazeFeatures.AOIScanStep(movements, 'Test')
def build_aoi_scan_path(expected_aois, aoi_path):
"""Build AOI scan path"""
aoi_scan_path = GazeFeatures.AOIScanPath(expected_aois)
# Append a hidden last step to allow last given step creation
aoi_path.append(aoi_path[-2])
for aoi in aoi_path:
fixation = TestFixation(random_gaze_positions(10))
ts, _ = fixation.positions.first
aoi_scan_path.append_fixation(ts, fixation, aoi)
saccade = TestSaccade(random_gaze_positions(2))
ts, _ = saccade.positions.first
aoi_scan_path.append_saccade(ts, saccade)
return aoi_scan_path
class TestAOIScanPathClass(unittest.TestCase):
"""Test AOIScanPath class."""
def test_new(self):
"""Test AOIScanPath creation."""
# Check aoi scan step creation
aoi_scan_path = GazeFeatures.AOIScanPath()
self.assertEqual(len(aoi_scan_path), 0)
def test_append(self):
"""Test AOIScanPath append methods."""
aoi_scan_path = GazeFeatures.AOIScanPath(['Foo', 'Bar'])
# Append fixation on A aoi
fixation_A = TestFixation(random_gaze_positions(10))
ts, _ = fixation_A.positions.first
new_step = aoi_scan_path.append_fixation(ts, fixation_A, 'Foo')
# Check that no aoi scan step have been created yet
self.assertEqual(len(aoi_scan_path), 0)
self.assertEqual(aoi_scan_path.duration, 0)
self.assertEqual(new_step, None)
# Append saccade
saccade_A = TestSaccade(random_gaze_positions(2))
ts, _ = saccade_A.positions.first
new_step = aoi_scan_path.append_saccade(ts, saccade_A)
# Check that no aoi scan step have been created yet
self.assertEqual(len(aoi_scan_path), 0)
self.assertEqual(aoi_scan_path.duration, 0)
self.assertEqual(new_step, None)
# Append fixation on B aoi
fixation_B = TestFixation(random_gaze_positions(10))
ts, _ = fixation_B.positions.first
new_step_A = aoi_scan_path.append_fixation(ts, fixation_B, 'Bar')
# Check a first aoi scan step have been created once a new fixation is appened
self.assertEqual(len(aoi_scan_path), 1)
self.assertEqual(aoi_scan_path.duration, new_step_A.duration)
self.assertEqual(len(new_step_A.movements), 2)
self.assertEqual(new_step_A.aoi, 'Foo')
self.assertEqual(new_step_A.letter, 'A')
first_ts, _ = fixation_A.positions.first
last_ts, _ = saccade_A.positions.last
self.assertEqual(new_step_A.duration, last_ts - first_ts)
# Check letter affectation
self.assertEqual(aoi_scan_path.get_letter_aoi('A'), 'Foo')
def test_append_error(self):
"""Test AOIScanPath append error."""
aoi_scan_path = GazeFeatures.AOIScanPath(['Foo', 'Bar'])
# Append fixation on A aoi
fixation = TestFixation(random_gaze_positions(10))
ts, _ = fixation.positions.first
new_step = aoi_scan_path.append_fixation(ts, fixation, 'Foo')
# Check that no aoi scan step have been created yet
self.assertEqual(len(aoi_scan_path), 0)
self.assertEqual(new_step, None)
# Append fixation on B aoi
fixation = TestFixation(random_gaze_positions(10))
ts, _ = fixation.positions.first
# Check that aoi scan step creation fail when fixation is appened after another fixation
with self.assertRaises(GazeFeatures.AOIScanStepError):
new_step = aoi_scan_path.append_fixation(ts, fixation, 'Bar')
# Check that unexpected aoi scan step creation fail
with self.assertRaises(GazeFeatures.AOIScanStepError):
new_step = aoi_scan_path.append_fixation(ts, fixation, 'Shu')
def test_letter_index_and_string_reprentation(self):
"""Test AOIScanPath letter index and string representation feature."""
aoi_scan_path = build_aoi_scan_path(['Foo', 'Bar', 'Shu'], ['Bar', 'Shu', 'Foo', 'Bar'])
# Check first step
self.assertEqual(aoi_scan_path[0].aoi, 'Bar')
self.assertEqual(aoi_scan_path[0].letter, 'A')
# Check second step
self.assertEqual(aoi_scan_path[1].aoi, 'Shu')
self.assertEqual(aoi_scan_path[1].letter, 'B')
# Check thrird step
self.assertEqual(aoi_scan_path[2].aoi, 'Foo')
self.assertEqual(aoi_scan_path[2].letter, 'C')
# Check fourth step
self.assertEqual(aoi_scan_path[3].aoi, 'Bar')
self.assertEqual(aoi_scan_path[3].letter, 'A')
# Check letter affectation
self.assertEqual(aoi_scan_path.get_letter_aoi('A'), 'Bar')
self.assertEqual(aoi_scan_path.get_letter_aoi('B'), 'Shu')
self.assertEqual(aoi_scan_path.get_letter_aoi('C'), 'Foo')
# Check string representation
self.assertEqual(str(aoi_scan_path), 'ABCA')
def test_transition_matrix(self):
"""Test AOIScanPath transition matrix feature."""
aoi_scan_path = build_aoi_scan_path(['Foo', 'Bar', 'Shu'], ['Bar', 'Shu', 'Foo'])
# Check transition matrix ([destination][departure])
self.assertEqual(aoi_scan_path.transition_matrix['Foo']['Foo'], 0)
self.assertEqual(aoi_scan_path.transition_matrix['Bar']['Bar'], 0)
self.assertEqual(aoi_scan_path.transition_matrix['Shu']['Shu'], 0)
self.assertEqual(aoi_scan_path.transition_matrix['Foo']['Bar'], 0)
self.assertEqual(aoi_scan_path.transition_matrix['Foo']['Shu'], 1)
self.assertEqual(aoi_scan_path.transition_matrix['Bar']['Foo'], 0)
self.assertEqual(aoi_scan_path.transition_matrix['Bar']['Shu'], 0)
self.assertEqual(aoi_scan_path.transition_matrix['Shu']['Foo'], 0)
self.assertEqual(aoi_scan_path.transition_matrix['Shu']['Bar'], 1)
def test_transition_matrix(self):
"""Test AOIScanPath fixations count feature."""
aoi_scan_path = build_aoi_scan_path(['Foo', 'Bar', 'Shu'], ['Bar', 'Shu', 'Foo'])
# Check fixations count
scan_fixations_count, aoi_fixations_count = aoi_scan_path.fixations_count()
self.assertEqual(scan_fixations_count, 3)
self.assertEqual(aoi_fixations_count['Foo'], 1)
self.assertEqual(aoi_fixations_count['Bar'], 1)
self.assertEqual(aoi_fixations_count['Shu'], 1)
if __name__ == '__main__':
unittest.main()
|