b@2074
|
1 #!/usr/bin/python
|
b@2074
|
2
|
b@2074
|
3 import xml.etree.ElementTree as ET
|
b@2074
|
4 import os # list files in directory
|
b@2074
|
5 import sys # command line arguments
|
b@2074
|
6 import matplotlib.pyplot as plt # plots
|
b@2074
|
7 import matplotlib.patches as patches # rectangles
|
b@2074
|
8
|
b@2074
|
9
|
b@2074
|
10 # COMMAND LINE ARGUMENTS
|
b@2074
|
11
|
b@2074
|
12 assert len(sys.argv)<3, "timeline_view_movement takes at most 1 command line argument\n"+\
|
b@2074
|
13 "Use: python timeline_view_movement.py [XML_files_location]"
|
b@2074
|
14
|
b@2074
|
15 # XML results files location
|
b@2074
|
16 if len(sys.argv) == 1:
|
b@2074
|
17 folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder
|
b@2074
|
18 print "Use: python timeline_view_movement.py [XML_files_location]"
|
b@2074
|
19 print "Using default path: " + folder_name
|
b@2074
|
20 elif len(sys.argv) == 2:
|
b@2074
|
21 folder_name = sys.argv[1] # First command line argument is folder
|
b@2074
|
22
|
b@2074
|
23 # check if folder_name exists
|
b@2074
|
24 if not os.path.exists(folder_name):
|
b@2074
|
25 #the file is not there
|
b@2074
|
26 print "Folder '"+folder_name+"' does not exist."
|
b@2074
|
27 sys.exit() # terminate script execution
|
b@2074
|
28 elif not os.access(os.path.dirname(folder_name), os.W_OK):
|
b@2074
|
29 #the file does exist but write privileges are not given
|
b@2074
|
30 print "No write privileges in folder '"+folder_name+"'."
|
b@2074
|
31
|
b@2074
|
32
|
b@2074
|
33 # CONFIGURATION
|
b@2074
|
34
|
b@2074
|
35 # Folder where to store timelines
|
b@2074
|
36 timeline_folder = folder_name + '/timelines_movement/' # Stores in 'saves/timelines_movement/' by default
|
b@2074
|
37
|
b@2074
|
38 # Font settings
|
b@2074
|
39 font = {'weight' : 'bold',
|
b@2074
|
40 'size' : 16}
|
b@2074
|
41 plt.rc('font', **font)
|
b@2074
|
42
|
b@2074
|
43 # Colormap for to cycle through
|
b@2074
|
44 colormap = ['b', 'g', 'c', 'm', 'y', 'k']
|
b@2074
|
45
|
b@2074
|
46 # figure size
|
b@2074
|
47 fig_width = 25
|
b@2074
|
48 fig_height = 10
|
b@2074
|
49
|
b@2074
|
50
|
b@2074
|
51 # CODE
|
b@2074
|
52
|
b@2074
|
53 # create timeline_folder if not yet created
|
b@2074
|
54 if not os.path.exists(timeline_folder):
|
b@2074
|
55 os.makedirs(timeline_folder)
|
b@2074
|
56
|
b@2074
|
57 # get every XML file in folder
|
b@2074
|
58 for file in os.listdir(folder_name):
|
b@2074
|
59 if file.endswith(".xml"):
|
b@2074
|
60 tree = ET.parse(folder_name + '/' + file)
|
b@2074
|
61 root = tree.getroot()
|
b@2074
|
62 subject_id = file[:-4] # drop '.xml'
|
b@2074
|
63
|
b@2074
|
64 previous_audioholder_time = 0 # time spent before current audioholder
|
b@2074
|
65 time_offset = 0 # test starts at zero
|
b@2074
|
66
|
b@2074
|
67 # ONE TIMELINE PER PAGE - make new plot per page
|
b@2074
|
68
|
b@2074
|
69 # get list of all page names
|
b@2074
|
70 for audioholder in root.findall("./audioholder"): # iterate over pages
|
b@2074
|
71 page_name = audioholder.get('id') # get page name
|
b@2074
|
72
|
b@2074
|
73 if page_name is None: # ignore 'empty' audio_holders
|
b@2074
|
74 print "Skipping empty audioholder name from "+subject_id+"."
|
b@2074
|
75 break
|
b@2074
|
76
|
b@2074
|
77 # subtract total audioholder length from subsequent audioholder event times
|
b@2074
|
78 audioholder_time_temp = audioholder.find("./metric/metricresult/[@id='testTime']")
|
b@2074
|
79 if audioholder_time_temp is not None:
|
b@2074
|
80 audioholder_time = float(audioholder_time_temp.text)
|
b@2074
|
81 else:
|
b@2074
|
82 print "Skipping audioholder without total time specified from "+subject_id+"."
|
b@2074
|
83 break
|
b@2074
|
84
|
b@2074
|
85 # get audioelements
|
b@2074
|
86 audioelements = audioholder.findall("./audioelement")
|
b@2074
|
87
|
b@2074
|
88 # sort alphabetically
|
b@2074
|
89 data = []
|
b@2074
|
90 for elem in audioelements: # from http://effbot.org/zone/element-sort.htm
|
b@2074
|
91 key = elem.get("id")
|
b@2074
|
92 data.append((key, elem))
|
b@2074
|
93 data.sort()
|
b@2074
|
94
|
b@2074
|
95 N_audioelements = len(audioelements) # number of audio elements for this page
|
b@2074
|
96 increment = 0 # increased for every new audioelement
|
b@2074
|
97
|
b@2074
|
98 # get axes handle
|
b@2074
|
99 fig = plt.figure(figsize=(fig_width, fig_height))
|
b@2074
|
100 ax = fig.add_subplot(111)
|
b@2074
|
101
|
b@2074
|
102 # for page [page_name], print comments related to fragment [id]
|
b@2074
|
103 #for tuple in data:
|
b@2074
|
104 # audioelement = tuple[1]
|
b@2074
|
105 for tuple in data:
|
b@2074
|
106 audioelement = tuple[1]
|
b@2074
|
107 if audioelement is not None: # Check it exists
|
b@2074
|
108 audio_id = str(audioelement.get('id'))
|
b@2074
|
109
|
b@2074
|
110 # break if no initial position or move events registered
|
b@2074
|
111 initial_position_temp = audioelement.find("./metric/metricresult/[@name='elementInitialPosition']")
|
b@2074
|
112 if initial_position_temp is None:
|
b@2074
|
113 print "Skipping "+page_name+" from "+subject_id+": does not have initial positions specified."
|
b@2074
|
114 break
|
b@2074
|
115
|
b@2075
|
116 # get move events, initial and eventual position
|
b@2074
|
117 initial_position = float(initial_position_temp.text)
|
b@2074
|
118 move_events = audioelement.findall("./metric/metricresult/[@name='elementTrackerFull']/timepos")
|
b@2074
|
119 final_position = float(audioelement.find("./value").text)
|
b@2074
|
120
|
b@2075
|
121 # get listen events
|
b@2075
|
122 start_times_global = []
|
b@2075
|
123 stop_times_global = []
|
b@2075
|
124 listen_events = audioelement.findall("./metric/metricresult/[@name='elementListenTracker']/event")
|
b@2075
|
125 for event in listen_events:
|
b@2075
|
126 # get testtime: start and stop
|
b@2075
|
127 start_times_global.append(float(event.find('testtime').get('start'))-time_offset)
|
b@2075
|
128 stop_times_global.append(float(event.find('testtime').get('stop'))-time_offset)
|
b@2075
|
129
|
b@2074
|
130 # display fragment name at start
|
b@2074
|
131 plt.text(0,initial_position+0.02,audio_id,color=colormap[increment%len(colormap)]) #,rotation=45
|
b@2074
|
132
|
b@2074
|
133 # previous position and time
|
b@2074
|
134 previous_position = initial_position
|
b@2074
|
135 previous_time = 0
|
b@2074
|
136
|
b@2075
|
137 # assume not playing at start
|
b@2075
|
138 currently_playing = False # keep track of whether fragment is playing during move event
|
b@2075
|
139
|
b@2074
|
140 # draw all segments except final one
|
b@2074
|
141 for event in move_events:
|
b@2075
|
142 # get time and final position of move event
|
b@2074
|
143 new_time = float(event.find("./time").text)-time_offset
|
b@2074
|
144 new_position = float(event.find("./position").text)
|
b@2075
|
145
|
b@2075
|
146 # get play/stop events since last move until current move event
|
b@2075
|
147 stop_times = []
|
b@2075
|
148 start_times = []
|
b@2075
|
149 # is there a play and/or stop event between previous_time and new_time?
|
b@2075
|
150 for time in start_times_global:
|
b@2075
|
151 if time>previous_time and time<new_time:
|
b@2075
|
152 start_times.append(time)
|
b@2075
|
153 for time in stop_times_global:
|
b@2075
|
154 if time>previous_time and time<new_time:
|
b@2075
|
155 stop_times.append(time)
|
b@2075
|
156 # if no play/stop events between move events, find out whether playing
|
b@2075
|
157
|
b@2075
|
158 segment_start = previous_time # first segment starts at previous move event
|
b@2075
|
159
|
b@2075
|
160 # draw segments (horizontal line)
|
b@2075
|
161 while len(start_times)+len(stop_times)>0: # while still play/stop events left
|
b@2075
|
162 if len(stop_times)<1: # upcoming event is 'play'
|
b@2075
|
163 # draw non-playing segment from segment_start to 'play'
|
b@2075
|
164 currently_playing = False
|
b@2075
|
165 segment_stop = start_times.pop(0) # remove and return first item
|
b@2075
|
166 elif len(start_times)<1: # upcoming event is 'stop'
|
b@2075
|
167 # draw playing segment (red) from segment_start to 'stop'
|
b@2075
|
168 currently_playing = True
|
b@2075
|
169 segment_stop = stop_times.pop(0) # remove and return first item
|
b@2075
|
170 elif start_times[0]<stop_times[0]: # upcoming event is 'play'
|
b@2075
|
171 # draw non-playing segment from segment_start to 'play'
|
b@2075
|
172 currently_playing = False
|
b@2075
|
173 segment_stop = start_times.pop(0) # remove and return first item
|
b@2075
|
174 else: # stop_times[0]<start_times[0]: upcoming event is 'stop'
|
b@2075
|
175 # draw playing segment (red) from segment_start to 'stop'
|
b@2075
|
176 currently_playing = True
|
b@2075
|
177 segment_stop = stop_times.pop(0) # remove and return first item
|
b@2075
|
178
|
b@2075
|
179 # draw segment
|
b@2075
|
180 plt.plot([segment_start, segment_stop], # x-values
|
b@2075
|
181 [previous_position, previous_position], # y-values
|
b@2075
|
182 color='r' if currently_playing else colormap[increment%len(colormap)],
|
b@2075
|
183 linewidth=3
|
b@2075
|
184 )
|
b@2075
|
185 segment_start = segment_stop # move on to next segment
|
b@2075
|
186 currently_playing = not currently_playing # toggle to draw final segment correctly
|
b@2075
|
187
|
b@2075
|
188 # draw final segment (horizontal line) from last 'segment_start' to current move event time
|
b@2075
|
189 plt.plot([segment_start, new_time], # x-values
|
b@2074
|
190 [previous_position, previous_position], # y-values
|
b@2075
|
191 # color depends on playing during move event or not:
|
b@2075
|
192 color='r' if currently_playing else colormap[increment%len(colormap)],
|
b@2074
|
193 linewidth=3
|
b@2074
|
194 )
|
b@2075
|
195
|
b@2074
|
196 # vertical line from previous to current position
|
b@2075
|
197 #TODO red if currently playing, orig color if not
|
b@2074
|
198 plt.plot([new_time, new_time], # x-values
|
b@2074
|
199 [previous_position, new_position], # y-values
|
b@2075
|
200 # color depends on playing during move event or not:
|
b@2075
|
201 color='r' if currently_playing else colormap[increment%len(colormap)],
|
b@2074
|
202 linewidth=3
|
b@2074
|
203 )
|
b@2074
|
204
|
b@2074
|
205 # update previous_position value
|
b@2074
|
206 previous_position = new_position
|
b@2074
|
207 previous_time = new_time
|
b@2074
|
208
|
b@2074
|
209 # draw final segment
|
b@2074
|
210 # horizontal line from previous time to end of audioholder
|
b@2074
|
211 plt.plot([previous_time, audioholder_time-time_offset], # x-values
|
b@2074
|
212 [previous_position, previous_position], # y-values
|
b@2074
|
213 color=colormap[increment%len(colormap)],
|
b@2074
|
214 linewidth=3
|
b@2074
|
215 )
|
b@2074
|
216
|
b@2074
|
217 # display fragment name at end
|
b@2074
|
218 plt.text(audioholder_time-time_offset,previous_position,\
|
b@2074
|
219 audio_id,color=colormap[increment%len(colormap)]) #,rotation=45
|
b@2074
|
220
|
b@2074
|
221 increment+=1 # to next audioelement
|
b@2074
|
222
|
b@2074
|
223 last_audioholder_duration = audioholder_time-time_offset
|
b@2074
|
224 time_offset = audioholder_time
|
b@2074
|
225
|
b@2074
|
226
|
b@2074
|
227 # set plot parameters
|
b@2074
|
228 plt.title('Timeline ' + file + ": "+page_name)
|
b@2074
|
229 plt.xlabel('Time [seconds]')
|
b@2074
|
230 plt.xlim(0, last_audioholder_duration)
|
b@2074
|
231 plt.ylabel('Rating') # default
|
b@2074
|
232 plt.ylim(0, 1) # rating between 0 and 1
|
b@2074
|
233
|
b@2074
|
234 #y-ticks: labels on rating axis
|
b@2074
|
235 label_positions = []
|
b@2074
|
236 label_text = []
|
b@2074
|
237 scale_tags = root.findall("./BrowserEvalProjectDocument/audioHolder/interface/scale")
|
b@2074
|
238 scale_title = root.find("./BrowserEvalProjectDocument/audioHolder/interface/title")
|
b@2074
|
239 for tag in scale_tags:
|
b@2074
|
240 label_positions.append(float(tag.get('position'))/100) # on a scale from 0 to 100
|
b@2074
|
241 label_text.append(tag.text)
|
b@2075
|
242 if len(label_positions) > 0: # if any labels available
|
b@2074
|
243 plt.yticks(label_positions, label_text) # show rating axis labels
|
b@2074
|
244 # set label Y-axis
|
b@2074
|
245 if scale_title is not None:
|
b@2074
|
246 plt.ylabel(scale_title.text)
|
b@2074
|
247
|
b@2074
|
248 #plt.show() # uncomment to show plot; comment when just saving
|
b@2074
|
249 #exit()
|
b@2074
|
250
|
b@2074
|
251 plt.savefig(timeline_folder+subject_id+"-"+page_name+".pdf", bbox_inches='tight')
|
b@2074
|
252 plt.close()
|
b@2074
|
253 |