-
Notifications
You must be signed in to change notification settings - Fork 0
/
mouseHandler.py
195 lines (180 loc) · 7.72 KB
/
mouseHandler.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
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import numpy as np
import os
import seaborn as sns
import config as conf
# from keras import backend as K
def grayscale_to_rgb(img):
return np.stack((img,) * 3, axis=-1)
class mouseHandler:
max_x = 1279.0
max_y = 1023.0
def __init__(self, matcherId, M=None):
self.matcherId = matcherId
if not M:
self.mouseData = {}
self.newMouseData = []
self.dir = conf.dir
self.processMouseData()
self.list2dict()
else:
self.mouseData = {}
self.newMouseData = M
self.list2dict()
def processMouseData(self):
last_line = None
try:
# with open(str(self.dir + 'ExperimentData/' + self.matcherId + '/Excel - CIDX/2.rms')) as f:
with open(str(self.dir + 'ExperimentData/' + self.matcherId + '/Excel - CIDX/exam.rms')) as f:
for line in f.readlines():
line.replace('{', '').replace('}', '').split()
action, value = line.replace('{', '').replace('}', '').split()[:2]
if last_line:
delay = last_line.replace('}', '').split()[1]
if not '.' in delay:
delay = 0.0
else:
delay = ''
# if action not in self.mouseData:
# self.mouseData[action] = list()
if action == 'Move':
p1, p2 = value.replace('(', '').replace(')', '').split(',')[:2]
# self.mouseData[action] += [tuple((tuple((float(p1), float(p2))), delay)), ]
self.newMouseData += [tuple((action, tuple((float(p1), float(p2))), delay)), ]
elif 'Mouse' in action:
add = line.replace('{', '').replace('}', '').split()[1:3]
if 'down' in add:
p1, p2 = add[1].replace('(', '').replace(')', '').split(',')[:2]
# self.mouseData[action] += [tuple((tuple((float(p1), float(p2))), delay)), ]
self.newMouseData += [tuple((action, tuple((float(p1), float(p2))), delay)), ]
elif 'Delay' in action:
# self.mouseData[action] += [value, ]
if isinstance(delay, str):
delay = 0.0
self.newMouseData += [tuple((action, None, delay)), ]
else:
# self.mouseData[action] += [value, ]
self.newMouseData += [tuple((action, value, delay)), ]
last_line = line
except:
print('couldnt find data for' + self.matcherId)
def list2dict(self):
for a in self.newMouseData:
action = a[0]
if action not in self.mouseData:
self.mouseData[action] = list()
self.mouseData[action] += [tuple(a[1:]), ]
def exportMouseData(self, method, save_heatmaps=False):
# USE THE LIST!!
x = []
y = []
weights = []
if method not in self.mouseData:
return np.zeros((37, 45, 3))
for k in self.mouseData[method]:
try:
d = float(k[1])
except:
continue
i, j = k[0]
x += [float(i), ]
y += [float(j), ]
weights += [d, ]
if len(x) == 0 or len(y) == 0:
return
xedges = list(range(0, int(mouseHandler.max_x) + 100, 30))
yedges = list(range(0, int(mouseHandler.max_y) + 100, 30))
# heatmap, _, _ = np.histogram2d(x, y, bins=(xedges, yedges), weights=weights)
heatmap, _, _ = np.histogram2d(x, y, bins=(xedges, yedges))
heatmap = heatmap.T
if save_heatmaps:
plt.clf()
map_img = plt.imread(self.dir + 'screen.jpg')
hmax = sns.heatmap(heatmap,
cmap='Reds',
alpha=0.5, # whole heatmap is translucent
zorder=2,
cbar=False
)
hmax.imshow(map_img,
aspect=hmax.get_aspect(),
extent=hmax.get_xlim() + hmax.get_ylim(),
zorder=1) # put the map under the heatmap
plt.axis('off')
if not os.path.exists('./figs/' + method):
os.makedirs('./figs/' + method)
plt.savefig('./figs/' + method + '/' + self.matcherId + '.jpg', bbox_inches='tight', format='jpg', dpi=300)
return grayscale_to_rgb(heatmap)
def split2ns(self, matchers):
M_list = self.newMouseData
M_dict = self.mouseData
sub_matchers_size = len(matchers)
bucket_size = int(len(self.newMouseData) / sub_matchers_size)
submouses = {}
last = 0
for i, m in enumerate(matchers):
submouse = M_list[last: (i + 1) * bucket_size]
last = (i + 1) * bucket_size
submouses[m] = mouseHandler(m, submouse)
return submouses
def extract_mouse_features(self):
total_length = float(len(self.newMouseData))
total_actions = float(len(self.mouseData.keys()))
min_x, min_y, max_x, max_y, sum_x, count_pos, sum_y, \
total_time, total_dist, max_speed = [0.0, ] * 10
i = 0
while i < len(self.newMouseData):
currElapsedTime = 0.0
if self.newMouseData[i][0] == 'Delay':
currElapsedTime += float(self.newMouseData[i][2])
i += 1
if i >= len(self.newMouseData): break
while not isinstance(self.newMouseData[i][1], tuple):
currElapsedTime += float(self.newMouseData[i][2])
i += 1
if i >= len(self.newMouseData): break
currElapsedTime += float(self.newMouseData[i][2])
j = i + 1
if j >= len(self.newMouseData): break
if self.newMouseData[j][0] == 'Delay':
j += 1
if j >= len(self.newMouseData): break
while not isinstance(self.newMouseData[j][1], tuple):
currElapsedTime += float(self.newMouseData[j][2])
j += 1
if j >= len(self.newMouseData): break
if j >= len(self.newMouseData): break
# print(self.newMouseData[i], self.newMouseData[j])
currElapsedTime += float(self.newMouseData[j][2])
currDist = dist(self.newMouseData[i][1], self.newMouseData[j][1])
currElapsedTime = currElapsedTime * 60
total_time += currElapsedTime
total_dist += dist(self.newMouseData[i][1], self.newMouseData[j][1])
if currElapsedTime > 0.0:
if currDist / currElapsedTime > max_speed:
max_speed = currDist / currElapsedTime
x_i, y_i = self.newMouseData[i][1]
sum_x += x_i
sum_y += y_i
count_pos += 1
if x_i < min_x:
min_x = x_i
if x_i > max_x:
max_x = x_i
if y_i < min_y:
min_y = y_i
if y_i > max_y:
max_y = y_i
i = j
avg_speed = 0.0
if total_time > 0.0:
avg_speed = total_dist / total_time
avg_x = 0.0
avg_x = sum_x / count_pos
avg_y = sum_y / count_pos
return total_length, total_actions, total_time, total_dist, \
max_speed, min_x, min_y, max_x, max_y, avg_speed, avg_x, avg_y
def dist(a, b):
return np.linalg.norm(np.array(a) - np.array(b))