/
LogParser.py
243 lines (220 loc) · 8.26 KB
/
LogParser.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
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
import viz
import vizact
import pickle
import fnmatch
import os
import datetime
def sgetGameLoadingTime (set):
global timeOffset
if set:
timeOffset = viz.tick()
else:
return timeOffset
##############################
## STORE AND PARSE LOG DATA ##
##############################
def storeLogData (FSM, players, cond, gr):
output = [] #user interaction with machinery [(time, user, name, input, machine, actions), ...]
output2 = [] #machine state changes [(time, machine, before, trigger, after, actions), ...]
output3 = [] #user inventory changes [(time, user, tool, pickOrDrop), (), ...]
output4 = [] #user proximity to machinery [(time, user, macine, enterOrLeave), (), ...]
output5 = {} #{user1: [(time1, posx1, posz1), ...], user2: [...]}
for m in FSM.keys():
#parse user log from state machines
log = FSM[m].log
for p,data in log.iteritems():
for e in data: #e[0]-> input, e[1]-> actions, e[2] -> time stamp
output.append((e[2], p, getPName(players, p), e[0], m, e[1]))
#parse machine state changes
slog = FSM[m].sLog
for t,data in slog.iteritems():
output2.append((t, m, data[0], data[1], data[2], data[3]))
#consolidate inventory data from all players
for p in players.values():
output3 += p['player']._iLog
#consolidate proximity data from all players
for p in players.values():
output4 += p['player']._proxLog
#dictionary with position data per player
for p, player in players.iteritems():
output5[p] = player['player']._pLog
#sort data according to time stamp
output.sort()
output2.sort()
saveLogFile('user', output, cond, gr)
saveLogFile('state',output2, cond, gr)
saveLogFile('inventory', output3, cond, gr)
saveLogFile('proximity', output4, cond, gr)
saveLogFile('position', output5, cond, gr)
def saveLogFile(kind, content, condi, group):
#stamp = str(datetime.date.today().month)+'.'+str(datetime.date.today().day)
folder = 'Trial'+chr(66+condi*condi)+str(group) #(0)1P->B, (1)3P->C, (2)F->F
d = os.path.dirname(viz.res.getPublishedPath()+'study/'+folder+'/')
if not os.path.exists(d):
os.makedirs(d)
file = open(viz.res.getPublishedPath()+'study/'+folder+'/log_'+kind, 'w')
pickle.dump(content, file)
file.close()
saveLastTrial(chr(66+condi*condi)+str(group))
def saveLastTrial(trial): #store the code of the latest trial
file = open(viz.res.getPublishedPath()+'study/last_trial', 'w')
pickle.dump(trial, file)
file.close()
def getLatestTrial():
try:
file = open(viz.res.getPublishedPath()+'study/last_trial', 'r')
trial = pickle.load(file)
file.close()
return trial
except IOError:
return None
def loadLogFile(kind, trial=getLatestTrial()):
folder = 'Trial'+trial
try:
file = open(viz.res.getPublishedPath()+'study/'+folder+'/log_'+kind, 'r')
log = pickle.load(file)
file.close()
return log
except IOError:
print "FILE NOT FOUND!"
def printPlayerLog(parser, trial=getLatestTrial()):
import time
data = loadLogFile('user', trial)
# Output log in readable format: i[0]->time, i[1]->player, i[2]->name, i[3]->machine, i[4]->input, i[5]->actions
if parser == 'time':
data.sort(key=lambda tup: tup[0])
elif parser == 'machine':
data.sort(key=lambda tup: tup[4])
elif parser == 'player':
data.sort(key=lambda tup: tup[1])
for i in data:
if parser == 'time':
print "Time Stamp: %s | P%s (%s) performed %s on machine %s and caused actions: %s" %(i[0], i[1], i[2], i[3], i[4], str(i[5]))
elif parser == 'machine':
print "Machine: %s | At %s player P%s (%s) performed %s and caused actions: %s" %(i[4], i[0], i[1], i[2], i[3], str(i[5]))
elif parser == 'player':
print "Player%s: %s | At %s performed %s on machine %s and caused actions: %s" %(i[1], i[2], i[0], i[3], i[4], str(i[5]))
def printMachineLog(mach, input='', trial=getLatestTrial()):
data = loadLogFile('state', trial)
for i in data:
if i[1] == mach:
if input == '':
print "Time Stamp: %s | Input: %s | State: %s -> %s | Actions: %s" %(i[0], i[3], i[2], i[4], i[5])
elif input == i[3]:
print "Time Stamp: %s | State: %s -> %s | Actions: %s" %(i[0], i[2], i[4], i[5])
def getPName (playerList, p):
return playerList[p]['player']._name.getMessage()
def retrieveScoreProgress(trial=getLatestTrial()):
data = loadLogFile('state', trial)
#convert tuples to list and make time a string to allow string search
scoreEntries = [d for d in data if fnmatch.filter(d[5], 'score*')!=[]]
print scoreEntries
score = 0
for s in scoreEntries:
sc = fnmatch.filter(s[5], 'score*')[0].rpartition('[')[2][:-1]
score += int(sc)
print "Time: %s | Action: %s | Score: %s" % (s[0], s[5][0], score)
def setupPathView ():
import vizcam
# viz.MainWindow.ortho(-25,25,-15,20,-1,1)
viz.MainView.setEuler(0,90,0)
flmap=viz.add('models/floor_map.IVE')
flmap.texture(viz.addTexture('textures/map_view.png'),'',0)
cam = vizcam.PivotNavigate(distance=50)
cam.centerNode(flmap)
def playerAnimationPath (player, trial=getLatestTrial()):
data = loadLogFile('position', trial)
if data == None or not data.has_key(player):
return
data = data[player]
#Add the ball as avatar to animate
avatar = viz.addChild('beachball.osgb', scale=[1.5,1.5,1.5])
colors = [[197, 106, 183], [83, 171, 224], [255, 189, 0]]
avatar.color([float(c)/255 for c in colors[player-1]])
#Create the animation path
path = viz.addAnimationPath()
#Initialize an array of control points and add them to the path
positions = [[p[1],0,p[2]] for p in data]
for x,pos in enumerate(positions):
path.addControlPoint(x+1,pos=pos)
#Set the initial loop mode to circular
# path.setLoopMode(viz.LOOP)
#Automatically rotate the path to face trajectory
path.setAutoRotate(viz.ON)
path.setRotateMode(viz.BEZIER)
#Link the ball to the path
viz.link(path, avatar)
#Play the animation path
path.play()
#############################################
### GETTING DATA FOR STATISTICAL ANALYSIS ###
def getMachineInteractions (act, trial=getLatestTrial()):
data = loadLogFile('user', trial)
# Output log in readable format: i[0]->time, i[1]->player, i[2]->name, i[3]->machine, i[4]->input, i[5]->actions
data.sort(key=lambda tup: tup[0])
actions = {}
for a in data:
if (len(a[5]) > 0) == act: #True for actions, False for errors
# print a #print the exact action/error
actions.setdefault(a[1], 0)
actions[a[1]] += 1
if act and 'enable' in a[5][0]: #don't count the 'enable' actions
actions[a[1]] -= 1
return actions
def getMachineProximity (trial=getLatestTrial()):
data = loadLogFile('proximity', trial)
# Output log in readable format: i[0]->time, i[1]->player, i[2]->machine, i[3]->enter/exit)
data.sort(key=lambda tup: tup[1]) #sort by player
stations = {}
for p in data:
if p[3] > 0: #count only when entering a machine area
stations.setdefault(p[1], 0)
stations[p[1]] += 1
return stations
def getDuration (trial=getLatestTrial()):
data = loadLogFile('state', trial)
return data[-1:][0]
def getScore (trial=getLatestTrial()):
total = 0
# get points based on machine state changes
timeStamp = []
data = loadLogFile('state', trial)
for d in data:
actions = d[5]
for a in actions:
if ('score' in a) and not (d[0] in timeStamp):
timeStamp.append(d[0])
total += int(a.partition('[')[2].partition(']')[0])
print d #a.partition('[')[2].partition(']')[0]
#subtract the last pitcher's points, but check if both pitchers were clicked in less than 8 secs
return total*160 - 15*80
def getFirstAction (trial=getLatestTrial()):
data = loadLogFile('state', trial)
for d in data:
newState = d[4]
if newState == 'BOILER/LOADED':
boilerTime = d[0]
elif newState == 'LAVALR/BELT-ON':
beltTime = d[0]
return str(boilerTime)+'\t'+str(beltTime)
def parseTrials (func, arg=None, desc='', filter=''):
trials = os.listdir(viz.res.getPublishedPath()+'study/')
trials = [t for t in trials if t[:5+len(filter)]=='Trial'+filter]
for t in trials:
if arg != None:
data = func(arg, trial=t[5:])
else:
data = func(trial=t[5:])
# print all the values by subject (if dictionary)
if type(data) == type(dict()):
for k,n in data.iteritems():
print desc, t[5:]+str(k)+':\t', n
elif type(data) in [type(int()), type(str())]:
print desc, t[5:]+':\t', data
else: #...or by group (if list or tuple)
print desc, t[5:]+':\t', data[0]
if __name__ == '__main__':
viz.go()
vizact.onkeydown('p', setupPathView)
for p in [1,2,3]:
vizact.onkeydown(str(p), playerAnimationPath, p)