-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
138 lines (105 loc) · 3.95 KB
/
generate.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
# Eye Tracking Data to Visualization using Bokeh
from __future__ import print_function
import sys
#data_folder = 'data/'
#data_output_file = 'data_out.html'
data = {}
def main():
if len(sys.argv) != 3:
print('Usage: python generate.py <DATA_FOLDER> <OUTPUT_HTML>')
return
data_folder = sys.argv[1]
data_output_file = sys.argv[2]
parse_data(data_folder)
visualize_data(data_output_file)
def comparator(x, y):
if 'Line' in x:
if 'Line' in y:
return cmp(x[5:], y[5:])
if 'Step' in y:
return -1
if 'Directions' in y:
return -1
if 'Step' in x:
if 'Line' in y:
return 1
if 'Step' in y:
return cmp(x[5:], y[5:])
if 'Directions' in y:
return -1
if 'Directions' in x:
if 'Line' in y:
return 1
if 'Step' in y:
return 1
if 'Directions' in y:
return 0
def parse_data(data_folder):
from os import listdir
data_ls = listdir(data_folder)
for data_file in data_ls:
with open(data_folder + data_file, 'r') as f:
file_data_list = []
time_sum = 0
for line in f:
split_line = line.split(', ')
# line_type, time, start_time, end_time, desc
line_triple = (split_line[0], int(split_line[1]), \
time_sum, time_sum + int(split_line[1]), split_line[2][:-1])
file_data_list.append(line_triple)
time_sum += line_triple[1]
#file_data_list.sort(cmp=comparator, key=lambda x: x[0])
data[data_file] = file_data_list
with open('outfile.txt', 'w') as f:
for data_item in data.iteritems():
f.write(str(data_item) + '\n')
def visualize_data(data_output_file):
from bokeh.plotting import figure, output_file, save, vplot
from bokeh.models import ColumnDataSource, FactorRange
plots = []
# output to static HTML file
output_file(data_output_file, title='Eye Tracking Data', mode='cdn')
for file_name, file_data in data.iteritems():
# prepare the data
line_types = [line_data[0] for line_data in file_data]
#line_types_top = [line_data[0] + '2.0' for line_data in file_data]
#line_types_bot = [line_data[0] + '1.0' for line_data in file_data]
times = [line_data[1] for line_data in file_data]
start_times = [line_data[2] for line_data in file_data]
end_times = [line_data[3] for line_data in file_data]
descs = [line_data[4] for line_data in file_data]
#x0 = [0 for type_ in line_types]
'''source = ColumnDataSource({
'time': times,
'start': start_times,
'end': end_times,
'line_types_top': line_types_top,
'line_types_bot': line_types_bot,
})'''
source = ColumnDataSource({
'x': [(t1 + t2)/2 for t1, t2 in zip(start_times, end_times)],
'y': line_types,
'width': times,
'height': [0.7 for _ in line_types],
'fill_color': ['red' if 'Incorrect' in desc \
else 'green' for desc in descs]
})
# create a new plot
plot = figure(
tools='pan,reset,save',
title=file_name, y_range=FactorRange(factors=line_types[::-1]),
x_range=[0, sum(times)],
x_axis_label='Time (ms)', y_axis_label='Line Type'
)
plot.rect(x='x', y='y', width='width', height='height', \
fill_color='fill_color', source=source)
#plot.quad(left='start', right='end', \
# top='line_types_top', bottom='line_types_bot', source=source)
# add some renderers
#plot.segment(x0, line_types, times, line_types, \
# line_width=2, line_color="green")
plots.append(plot)
# show the results
save(vplot(*plots))
if __name__ == '__main__':
main()