annotate scripts/timeline_view_movement.py @ 1150:2674d80c66ff

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