-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMobilityEventsSummarizer.py
237 lines (192 loc) · 8.09 KB
/
MobilityEventsSummarizer.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
import tkinter as tk
from tkinter import filedialog
from statistics import median
import sys
################################################################CLASSES:
class parsedCollision():
def __init__( self, line ):
split_by_commas = line.split( "," )
## split_by_close_paren = line.split
class completedCourse():
def __init__( self, course_name ):
self.name = course_name
self.times_off_course = 0
self.total_time_off_course = 0
self.pulses = 0
self.total_time_off_course_after_pulse = 0
self.completion_time = -1
self.buttons_pressed = 0
self.total_collisions = 0
self.foot_collisions = 0
self.hand_collisions = 0
self.head_collisions = 0
self.time_off_course_after_pulse = 0
self.sec_off_course = 0
self.sec_pulsed = 0
self.did_pulse = False
self.config_string = ""
def parse_event_line( self, event_line ):
time = float( event_line.split( "," )[-1] )
if event_line.startswith( "Off course" ):
self.sec_off_course = time
self.did_pulse = False
elif event_line.startswith( "Pulse" ):
if self.sec_off_course == 0:
return
self.did_pulse = True
self.sec_pulsed = time
elif event_line.startswith( "Back" ):
if self.sec_off_course == 0:
return
self.times_off_course += 1
self.total_time_off_course += time - self.sec_off_course
time_since_pulse = time - self.sec_pulsed
if self.did_pulse:
self.pulses += 1
self.total_time_off_course_after_pulse += time - self.sec_pulsed
elif event_line.startswith( "collide" ):
collision = parsedCollision( event_line )
collider_name = event_line.split( "*" )[2]
if collider_name.endswith( "RH" ) or collider_name.endswith( "LH" ):
self.hand_collisions += 1
elif collider_name.endswith( "LF" ) or collider_name.endswith( "RF" ):
self.foot_collisions += 1
elif collider_name.endswith( "EYE" ):
self.head_collisions += 1
self.total_collisions += 1
elif event_line.startswith( "pressed" ):
self.buttons_pressed += 1
########################################################################
# Ask user for input filenames and then open them
root = tk.Tk()
root.withdraw()
print( "Now select events file" )
####################################################REAL:
events_filepath = filedialog.askopenfilename()
###################################################DEBUG:
##events_filepath = "recorded_events_8-10-2018_9am.csv"
#####################################################
if events_filepath == "":
sys.exit( "Bad filepaths" )
events_file = open( events_filepath, "r" )
print( "selected events file: %s" % events_filepath )
##### Parse events file:
course = None
completed_courses = list()
is_navigating = False
second_started = 0
config = ""
while True:
events_line = events_file.readline()
if events_line is "" or events_line is None:
break;
if events_line.startswith( "Loaded" ):
components = events_line.split( "," )
course_name = components[0].split( " " )[1]
course = completedCourse( course_name )
is_navigating = False
elif events_line.startswith( "{" ):
config = "\n{\n"
while True:
events_line = events_file.readline()
if events_line.startswith( "}" ):
break;
config += events_line;
config += "}\n"
elif events_line.startswith( "Subject navigating course: True" ):
is_navigating = True
second_started = float( events_line.split( "," )[1] )
elif not is_navigating:
continue;
elif events_line.startswith( "Subject navigating course: False" ):
second_finished = float( events_line.split( "," )[1] )
course.completion_time = second_finished - second_started
course.config_string = config
completed_courses.append( course )
is_navigating = False
else:
course.parse_event_line( events_line )
#####
output_filepath = events_filepath[:-4] + "_summary.txt"
output_file = open( output_filepath, "w" )
##### Summarize events:
num_completed_courses = len( completed_courses )
total_collisions = 0
total_hand_collisions = 0
total_head_collisions = 0
total_foot_collisions = 0
total_times_off_course = 0
total_buttons_pressed = 0
median_completion_time = 0
median_time_off_course = 0
completion_times = list()
off_course_times = list()
for i in range( 0, num_completed_courses ):
course = completed_courses[i]
completion_times.append(course.completion_time)
off_course_times.append(course.total_time_off_course)
total_collisions += course.total_collisions
total_hand_collisions += course.hand_collisions
total_head_collisions += course.head_collisions
total_foot_collisions += course.foot_collisions
total_times_off_course += course.times_off_course
total_buttons_pressed += course.buttons_pressed
median_completion_time = median(completion_times)
median_time_off_course = median(off_course_times)
output_file.write( "-------------------------------------------\n" )
output_file.write( "Summary\n" )
output_file.write( "-------------------------------------------\n\n" )
output_file.write( "Completed courses: " )
output_file.write( str( num_completed_courses ) )
output_file.write( "\n" )
output_file.write( "Median course time: " )
output_file.write( "{:.3f}".format( median_completion_time ) )
output_file.write( "\n" )
output_file.write( "Median time spent off course: " )
output_file.write( "{:.3f}".format( median_time_off_course ) )
output_file.write( "\n" )
output_file.write( "Total times off course: " )
output_file.write( str( total_times_off_course ) )
output_file.write( "\n" )
output_file.write( "Total buttons pressed: " )
output_file.write( str( total_buttons_pressed ) )
output_file.write( "\n" )
output_file.write( "Total foot collisions: " )
output_file.write( str( total_foot_collisions ) )
output_file.write( "\n" )
output_file.write( "Total hand collisions: " )
output_file.write( str( total_hand_collisions ) )
output_file.write( "\n" )
output_file.write( "Total head collisions: " )
output_file.write( str( total_head_collisions ) )
output_file.write( "\n\n\n-------------------------------------------\n" )
output_file.write( "Breakdown\n" )
output_file.write( "-------------------------------------------\n\n" )
for i in range( 0, num_completed_courses ):
course = completed_courses[i]
output_file.write( "Course name: " )
output_file.write( course.name )
output_file.write( course.config_string )
output_file.write( "\nTotal course time: " )
output_file.write( "{:.3f}".format( course.completion_time ) )
output_file.write( "\nTimes off course: " )
output_file.write( str( course.times_off_course ) )
output_file.write( "\nTotal time off course: " )
output_file.write( "{:.3f}".format( course.total_time_off_course ) )
output_file.write( "\nTimes pulsed: " )
output_file.write( str( course.pulses ) )
output_file.write( "\nTotal time off course after pulse: " )
output_file.write( "{:.3f}".format( course.total_time_off_course_after_pulse ) )
output_file.write( "\nButtons pressed: " )
output_file.write( str( course.buttons_pressed ) )
output_file.write( "\nFoot collisions: " )
output_file.write( str( course.foot_collisions ) )
output_file.write( "\nHand collisions: " )
output_file.write( str( course.hand_collisions ) )
output_file.write( "\nHead collisions: " )
output_file.write( str( course.head_collisions ) )
if i < num_completed_courses - 1:
output_file.write( "\n\n" )
events_file.close()
output_file.close()
input( "Success! Press Enter to close program" )