annotate scripts/timeline_view_movement.py @ 1505:a667f80c417e

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