annotate scripts/timeline_view_movement.py @ 1069:4dd1cb18b77c

Scripts: timeline_view_movement with 'playing' areas highlighted
author Brecht De Man <BrechtDeMan@users.noreply.github.com>
date Wed, 12 Aug 2015 10:31:10 +0200
parents 8eb0c24ea50a
children a00d0c8d5c74
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@1068 72
BrechtDeMan@1068 73 if page_name is None: # ignore 'empty' audio_holders
BrechtDeMan@1068 74 print "Skipping empty audioholder name from "+subject_id+"."
BrechtDeMan@1068 75 break
BrechtDeMan@1068 76
BrechtDeMan@1068 77 # subtract total audioholder length from subsequent audioholder event times
BrechtDeMan@1068 78 audioholder_time_temp = audioholder.find("./metric/metricresult/[@id='testTime']")
BrechtDeMan@1068 79 if audioholder_time_temp is not None:
BrechtDeMan@1068 80 audioholder_time = float(audioholder_time_temp.text)
BrechtDeMan@1068 81 else:
BrechtDeMan@1068 82 print "Skipping audioholder without total time specified from "+subject_id+"."
BrechtDeMan@1068 83 break
BrechtDeMan@1068 84
BrechtDeMan@1068 85 # get audioelements
BrechtDeMan@1068 86 audioelements = audioholder.findall("./audioelement")
BrechtDeMan@1068 87
BrechtDeMan@1068 88 # sort alphabetically
BrechtDeMan@1068 89 data = []
BrechtDeMan@1068 90 for elem in audioelements: # from http://effbot.org/zone/element-sort.htm
BrechtDeMan@1068 91 key = elem.get("id")
BrechtDeMan@1068 92 data.append((key, elem))
BrechtDeMan@1068 93 data.sort()
BrechtDeMan@1068 94
BrechtDeMan@1068 95 N_audioelements = len(audioelements) # number of audio elements for this page
BrechtDeMan@1068 96 increment = 0 # increased for every new audioelement
BrechtDeMan@1068 97
BrechtDeMan@1068 98 # get axes handle
BrechtDeMan@1068 99 fig = plt.figure(figsize=(fig_width, fig_height))
BrechtDeMan@1068 100 ax = fig.add_subplot(111)
BrechtDeMan@1068 101
BrechtDeMan@1068 102 # for page [page_name], print comments related to fragment [id]
BrechtDeMan@1068 103 #for tuple in data:
BrechtDeMan@1068 104 # audioelement = tuple[1]
BrechtDeMan@1068 105 for tuple in data:
BrechtDeMan@1068 106 audioelement = tuple[1]
BrechtDeMan@1068 107 if audioelement is not None: # Check it exists
BrechtDeMan@1068 108 audio_id = str(audioelement.get('id'))
BrechtDeMan@1068 109
BrechtDeMan@1068 110 # break if no initial position or move events registered
BrechtDeMan@1068 111 initial_position_temp = audioelement.find("./metric/metricresult/[@name='elementInitialPosition']")
BrechtDeMan@1068 112 if initial_position_temp is None:
BrechtDeMan@1068 113 print "Skipping "+page_name+" from "+subject_id+": does not have initial positions specified."
BrechtDeMan@1068 114 break
BrechtDeMan@1068 115
BrechtDeMan@1069 116 # get move events, initial and eventual position
BrechtDeMan@1068 117 initial_position = float(initial_position_temp.text)
BrechtDeMan@1068 118 move_events = audioelement.findall("./metric/metricresult/[@name='elementTrackerFull']/timepos")
BrechtDeMan@1068 119 final_position = float(audioelement.find("./value").text)
BrechtDeMan@1068 120
BrechtDeMan@1069 121 # get listen events
BrechtDeMan@1069 122 start_times_global = []
BrechtDeMan@1069 123 stop_times_global = []
BrechtDeMan@1069 124 listen_events = audioelement.findall("./metric/metricresult/[@name='elementListenTracker']/event")
BrechtDeMan@1069 125 for event in listen_events:
BrechtDeMan@1069 126 # get testtime: start and stop
BrechtDeMan@1069 127 start_times_global.append(float(event.find('testtime').get('start'))-time_offset)
BrechtDeMan@1069 128 stop_times_global.append(float(event.find('testtime').get('stop'))-time_offset)
BrechtDeMan@1069 129
BrechtDeMan@1068 130 # display fragment name at start
BrechtDeMan@1068 131 plt.text(0,initial_position+0.02,audio_id,color=colormap[increment%len(colormap)]) #,rotation=45
BrechtDeMan@1068 132
BrechtDeMan@1068 133 # previous position and time
BrechtDeMan@1068 134 previous_position = initial_position
BrechtDeMan@1068 135 previous_time = 0
BrechtDeMan@1068 136
BrechtDeMan@1069 137 # assume not playing at start
BrechtDeMan@1069 138 currently_playing = False # keep track of whether fragment is playing during move event
BrechtDeMan@1069 139
BrechtDeMan@1068 140 # draw all segments except final one
BrechtDeMan@1068 141 for event in move_events:
BrechtDeMan@1069 142 # get time and final position of move event
BrechtDeMan@1068 143 new_time = float(event.find("./time").text)-time_offset
BrechtDeMan@1068 144 new_position = float(event.find("./position").text)
BrechtDeMan@1069 145
BrechtDeMan@1069 146 # get play/stop events since last move until current move event
BrechtDeMan@1069 147 stop_times = []
BrechtDeMan@1069 148 start_times = []
BrechtDeMan@1069 149 # is there a play and/or stop event between previous_time and new_time?
BrechtDeMan@1069 150 for time in start_times_global:
BrechtDeMan@1069 151 if time>previous_time and time<new_time:
BrechtDeMan@1069 152 start_times.append(time)
BrechtDeMan@1069 153 for time in stop_times_global:
BrechtDeMan@1069 154 if time>previous_time and time<new_time:
BrechtDeMan@1069 155 stop_times.append(time)
BrechtDeMan@1069 156 # if no play/stop events between move events, find out whether playing
BrechtDeMan@1069 157
BrechtDeMan@1069 158 segment_start = previous_time # first segment starts at previous move event
BrechtDeMan@1069 159
BrechtDeMan@1069 160 # draw segments (horizontal line)
BrechtDeMan@1069 161 while len(start_times)+len(stop_times)>0: # while still play/stop events left
BrechtDeMan@1069 162 if len(stop_times)<1: # upcoming event is 'play'
BrechtDeMan@1069 163 # draw non-playing segment from segment_start to 'play'
BrechtDeMan@1069 164 currently_playing = False
BrechtDeMan@1069 165 segment_stop = start_times.pop(0) # remove and return first item
BrechtDeMan@1069 166 elif len(start_times)<1: # upcoming event is 'stop'
BrechtDeMan@1069 167 # draw playing segment (red) from segment_start to 'stop'
BrechtDeMan@1069 168 currently_playing = True
BrechtDeMan@1069 169 segment_stop = stop_times.pop(0) # remove and return first item
BrechtDeMan@1069 170 elif start_times[0]<stop_times[0]: # upcoming event is 'play'
BrechtDeMan@1069 171 # draw non-playing segment from segment_start to 'play'
BrechtDeMan@1069 172 currently_playing = False
BrechtDeMan@1069 173 segment_stop = start_times.pop(0) # remove and return first item
BrechtDeMan@1069 174 else: # stop_times[0]<start_times[0]: upcoming event is 'stop'
BrechtDeMan@1069 175 # draw playing segment (red) from segment_start to 'stop'
BrechtDeMan@1069 176 currently_playing = True
BrechtDeMan@1069 177 segment_stop = stop_times.pop(0) # remove and return first item
BrechtDeMan@1069 178
BrechtDeMan@1069 179 # draw segment
BrechtDeMan@1069 180 plt.plot([segment_start, segment_stop], # x-values
BrechtDeMan@1069 181 [previous_position, previous_position], # y-values
BrechtDeMan@1069 182 color='r' if currently_playing else colormap[increment%len(colormap)],
BrechtDeMan@1069 183 linewidth=3
BrechtDeMan@1069 184 )
BrechtDeMan@1069 185 segment_start = segment_stop # move on to next segment
BrechtDeMan@1069 186 currently_playing = not currently_playing # toggle to draw final segment correctly
BrechtDeMan@1069 187
BrechtDeMan@1069 188 # draw final segment (horizontal line) from last 'segment_start' to current move event time
BrechtDeMan@1069 189 plt.plot([segment_start, new_time], # x-values
BrechtDeMan@1068 190 [previous_position, previous_position], # y-values
BrechtDeMan@1069 191 # color depends on playing during move event or not:
BrechtDeMan@1069 192 color='r' if currently_playing else colormap[increment%len(colormap)],
BrechtDeMan@1068 193 linewidth=3
BrechtDeMan@1068 194 )
BrechtDeMan@1069 195
BrechtDeMan@1068 196 # vertical line from previous to current position
BrechtDeMan@1069 197 #TODO red if currently playing, orig color if not
BrechtDeMan@1068 198 plt.plot([new_time, new_time], # x-values
BrechtDeMan@1068 199 [previous_position, new_position], # y-values
BrechtDeMan@1069 200 # color depends on playing during move event or not:
BrechtDeMan@1069 201 color='r' if currently_playing else colormap[increment%len(colormap)],
BrechtDeMan@1068 202 linewidth=3
BrechtDeMan@1068 203 )
BrechtDeMan@1068 204
BrechtDeMan@1068 205 # update previous_position value
BrechtDeMan@1068 206 previous_position = new_position
BrechtDeMan@1068 207 previous_time = new_time
BrechtDeMan@1068 208
BrechtDeMan@1068 209 # draw final segment
BrechtDeMan@1068 210 # horizontal line from previous time to end of audioholder
BrechtDeMan@1068 211 plt.plot([previous_time, audioholder_time-time_offset], # x-values
BrechtDeMan@1068 212 [previous_position, previous_position], # y-values
BrechtDeMan@1068 213 color=colormap[increment%len(colormap)],
BrechtDeMan@1068 214 linewidth=3
BrechtDeMan@1068 215 )
BrechtDeMan@1068 216
BrechtDeMan@1068 217 # display fragment name at end
BrechtDeMan@1068 218 plt.text(audioholder_time-time_offset,previous_position,\
BrechtDeMan@1068 219 audio_id,color=colormap[increment%len(colormap)]) #,rotation=45
BrechtDeMan@1068 220
BrechtDeMan@1068 221 increment+=1 # to next audioelement
BrechtDeMan@1068 222
BrechtDeMan@1068 223 last_audioholder_duration = audioholder_time-time_offset
BrechtDeMan@1068 224 time_offset = audioholder_time
BrechtDeMan@1068 225
BrechtDeMan@1068 226
BrechtDeMan@1068 227 # set plot parameters
BrechtDeMan@1068 228 plt.title('Timeline ' + file + ": "+page_name)
BrechtDeMan@1068 229 plt.xlabel('Time [seconds]')
BrechtDeMan@1068 230 plt.xlim(0, last_audioholder_duration)
BrechtDeMan@1068 231 plt.ylabel('Rating') # default
BrechtDeMan@1068 232 plt.ylim(0, 1) # rating between 0 and 1
BrechtDeMan@1068 233
BrechtDeMan@1068 234 #y-ticks: labels on rating axis
BrechtDeMan@1068 235 label_positions = []
BrechtDeMan@1068 236 label_text = []
BrechtDeMan@1068 237 scale_tags = root.findall("./BrowserEvalProjectDocument/audioHolder/interface/scale")
BrechtDeMan@1068 238 scale_title = root.find("./BrowserEvalProjectDocument/audioHolder/interface/title")
BrechtDeMan@1068 239 for tag in scale_tags:
BrechtDeMan@1068 240 label_positions.append(float(tag.get('position'))/100) # on a scale from 0 to 100
BrechtDeMan@1068 241 label_text.append(tag.text)
BrechtDeMan@1069 242 if len(label_positions) > 0: # if any labels available
BrechtDeMan@1068 243 plt.yticks(label_positions, label_text) # show rating axis labels
BrechtDeMan@1068 244 # set label Y-axis
BrechtDeMan@1068 245 if scale_title is not None:
BrechtDeMan@1068 246 plt.ylabel(scale_title.text)
BrechtDeMan@1068 247
BrechtDeMan@1068 248 #plt.show() # uncomment to show plot; comment when just saving
BrechtDeMan@1068 249 #exit()
BrechtDeMan@1068 250
BrechtDeMan@1068 251 plt.savefig(timeline_folder+subject_id+"-"+page_name+".pdf", bbox_inches='tight')
BrechtDeMan@1068 252 plt.close()
BrechtDeMan@1068 253