-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
245 lines (190 loc) · 6.39 KB
/
main.py
File metadata and controls
245 lines (190 loc) · 6.39 KB
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
import csv
import json
import ajaxHandler
import os
# write required gazedata to .json file
def prepareGazeData(filename, data):
#create temp ordner
if not os.path.exists("temp"):
os.makedirs("temp")
#check whether the gazedata is already encoded, if yes do nothing
if not os.path.isfile("temp/"+filename+"_gazedata.json"):
datafile = open("temp/"+filename+'_gazedata.json', 'w')
datafile.write(data)
datafile.close()
# search for relevant files in "data" directory
def listFiles():
csvfiles = [] # .csv files
imagefiles = [] # .jpg or .png files
list = [] # contains files which are available as gazedata and image
# get current path
currentpath = os.path.dirname(os.path.abspath(__file__))
# iterate over data directory, gather .csv and image files
for file in os.listdir(currentpath + "/data"):
if file.endswith(".csv"):
csvfiles.append(file)
prefix = (file.partition(".")[0]).partition("_")[0]
if not prefix in list:
list.append(prefix)
if file.endswith(".jpg") or file.endswith(".png"):
imagefiles.append(file)
# init buckets for gazedata files
arr = [0] * len(list)
# get number of gaze data files per image
for file in csvfiles:
prefix = (file.partition(".")[0]).partition("_")[0]
if prefix in list:
i = list.index(prefix)
arr[i]+=1
# create .json file containing all possibly displayable filecombinations
content = "{\"selectiondata\":["
# find pairs
for csvfile in csvfiles:
for imagefile in imagefiles:
if ((csvfile.partition(".")[0]).partition("_")[0] == imagefile.partition(".")[0]):
f = csvfile.partition(".")[0]
# create gaze data files for matching pairs
prepareGazeData(f, extractCSVData("data/"+csvfile))
urlfile = open("temp/" + f + "_url.json", 'w')
urlfile.write(extractURLChange("data/"+csvfile))
urlfile.close()
for f in list:
idx = list.index(f)
content += "{\"name\":\"" + f + "\", \"type\":\"" + imagefiles[idx].partition(".")[2] + "\", \"count\":" + str(arr[idx]) + "},"
content = content[:-1]
content += "]}"
selectionfile = open("temp/selectiondata.json", 'w')
selectionfile.write(content)
selectionfile.close()
#print content
def extractURLChange(filename):
file = open(filename, 'rb')
reader = csv.reader(file)
list = "{\"timestamps\":["
rownum = 0
line = ""
first = True
leftClick = False
for row in reader:
if rownum == 0:
header = row
else:
colnum = 0
list += "{"
stamp = 0
for col in row:
if (header[colnum] == 'RecordingTimestamp'):
if first:
starttime = int(col)
first = False
stamp = int(col) - starttime
# log leftclick position
elif(header[colnum] == 'MouseEvent' and col == 'Left'):
leftClick = True
elif(header[colnum] == 'MouseEventX (MCSpx)' and col != '' and leftClick):
line += "\"lx\":" + col + ","
elif(header[colnum] == 'MouseEventY (MCSpx)' and col != '' and leftClick):
line += "\"ly\":" + col + ","
line += "\"lt\":" + str(stamp) + "},"
leftClick = False
#elif (header[colnum] == 'StudioEvent' and col == 'URLStart'):
#line += "\"lt\":" + str(stamp) + ","
#elif (header[colnum] == 'StudioEventData' and col != '' and line != ""):
#line += "\"data\": \"" + col + "\"},"
#print line
colnum += 1
list += line
if list[len(list)-1] == "{":
list = list[:-1]
line = ""
rownum += 1
# remove last comma
list = list[:-1]
if list[len(list)-1] == ":":
list += "{}}"
else:
list += "]}"
file.close()
#print list
return list
# get relevant content from .csv files
def extractCSVData(filename):
# create csv file reader
csvFile = open(filename, 'rb')
reader = csv.reader(csvFile)
# string to create json object
completeList = "{\"gazedata\":["
rownum = 0
line = ""
first = True
# set flag if GazeEventType is unclassified
eventFlag = False
# use only first occurrence of index
index = 0
lastIndex = 0
# set flag if fixation coordinates are not available
fixFlag = False
for row in reader:
# save header row
if rownum == 0:
header = row
else:
colnum = 0
completeList += "{"
for col in row:
# select relevant data and save it to separate list
# skip line if not suitable to viewed file
if (header[colnum] == 'MediaName' and col == ''):
completeList = completeList[:-1]
break
if (header[colnum] == 'GazeEventType' and col == 'Unclassified'):
eventFlag = True
completeList = completeList[:-1]
break
if (header[colnum] == "FixationPointX (MCSpx)" and col == '') or (header[colnum] == "FixationPointY (MCSpx)" and col == ''):
fixFlag = True
break
# fixation x coordinate
if header[colnum] == "FixationPointX (MCSpx)":
line += "\"fx\":" + col + ","
# fixation y coordinate
elif header[colnum] == "FixationPointY (MCSpx)":
line += "\"fy\":" + col + "},"
# gaze duration
elif header[colnum] == "GazeEventDuration":
line += "\"gd\":" + col + ","
# fixation index
elif header[colnum] == "FixationIndex":
line += "\"fi\":" + col + ","
index = col
# fixation time
elif header[colnum] == "RecordingTimestamp":
if first:
starttime = int(col)
time = int(col) - starttime + 1
line += "\"ft\":" + str(time) + ","
first = False
colnum += 1
if eventFlag:
line = ""
eventFlag = False
if index == lastIndex:
line = ""
if fixFlag:
line = ""
fixFlag = False
completeList += line
if completeList[len(completeList)-1] == "{":
completeList = completeList[:-1]
lastIndex = index
line = ""
rownum += 1
# remove last comma
completeList = completeList[:-1]
completeList += "]}"
csvFile.close()
return completeList
# get possible files
listFiles()
# start ajax server
ajaxHandler.start_server()