annotate scripts/timeline_view_movement.py @ 310:1b168b627cb9 WAC2016

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