annotate scripts/timeline_view_movement.py @ 1070:f1201566b54b

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