annotate scripts/timeline_view_movement.py @ 789:3539d6c992e4

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