annotate scripts/timeline_view_movement.py @ 1350:b6389ceaeaa5

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