-
Notifications
You must be signed in to change notification settings - Fork 4
/
eyesmetriccalculator.py
118 lines (86 loc) · 3.76 KB
/
eyesmetriccalculator.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
from .metrics import *
import numpy as np
class EyesMetricCalculator():
def __init__(self, data_fixations,data_gazes, screen_dimension):
"""Init Method
Parameters
----------
data_fixations: DataFrame or numpyarray
having 3 columns containing fixations coordinates x,y and their duration as columns
data_gazes: DataFrame or numpyarray
similar structure to data_fixations but for gaze data
screen_dimensions : tuple or list of size 2
tuple containing the screen dimensions (width , length)
"""
if not isinstance(screen_dimension, tuple) and not isinstance(screen_dimension, list):
raise Exception('screen_dimension is not a tuple or list')
else:
if len(screen_dimension) > 2 or len(screen_dimension) < 2:
raise Exception('tuple size > 2')
self.screen_dimensions = screen_dimension
if self.screen_dimensions[0] < 0 or self.screen_dimensions[1] < 0:
raise Exception('Screen dimension cannot be negative')
self.fixation_array = np.array(data_fixations)
self.gaze_array = np.array(data_gazes)
def spatialDensity(self, cellx=20, celly=20):
"""Calculates the spatialDensity
Parameters
----------
cellx : float-int
width of the cell
celly : float-int
length of the cell
Returns
-------
spatialDensity metric object
"""
if not isinstance(cellx, (int, float)):
raise Exception('cellx is not an int or float ')
if not isinstance(celly, (int, float)):
raise Exception('celly is not an int or float ')
if cellx > self.screen_dimensions[0]:
raise Exception('cellx greate than screen width')
if celly > self.screen_dimensions[0]:
raise Exception('celly greate than screen width')
if celly < 0 or cellx < 0:
raise Exception('cell inputs should be positive')
self._check_dimensions(cellx, celly) # raises assertion error
# compute spatial Density
return SpatialDensity(self.fixation_array[:, [0, 1]], cellx, celly, self.screen_dimensions)
def convexHull(self, func='area'):
"""Calculates the ConvexHull based on Scipy Spatial ConvexHull
Returns
-------
ConvexHullArea metric object
"""
# taking only the x and y columns from the fixation array
return ConvexHull(self.fixation_array[:, [0, 1]], func)
def NNI(self):
"""Calculates the NNI metric
Returns
-------
NNI metric
"""
return NNI(self.fixation_array[:, [0, 1]], self.screen_dimensions)
def GEntropy(self,aoi_dict,entropy='transition'):
"""Calculates the transition or stationary entropy for the given gaze data
Parameters
----------
aoi_dict : dict {'aoi_poly1': PolyAOI(..)}
dictionary containing AOI names as keys
and AOI object as values
entropy : str
specifying which entropy metric to calculate, takes values
(transition,stationary)
Returns
-------
GazeEntropy object
"""
return GazeEntropy(self.screen_dimensions,aoi_dict,self.gaze_array,entropy)
def _check_dimensions(self, cellx, celly):
# check width
assert(self.screen_dimensions[0] % cellx
== 0), "Please change cellx to fit the screen width"
# check width
assert(self.screen_dimensions[1] % celly
== 0), "Please change celly to fit the screen length"