annotate scripts/timeline_view_movement.py @ 1068:8eb0c24ea50a

Scripts: timeline view of marker movements
author Brecht De Man <BrechtDeMan@users.noreply.github.com>
date Tue, 11 Aug 2015 20:49:34 +0200
parents
children 4fb6448759a9
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@1068 116 # for this audioelement, loop over all move events
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@1068 121 # display fragment name at start
BrechtDeMan@1068 122 plt.text(0,initial_position+0.02,audio_id,color=colormap[increment%len(colormap)]) #,rotation=45
BrechtDeMan@1068 123
BrechtDeMan@1068 124 # previous position and time
BrechtDeMan@1068 125 previous_position = initial_position
BrechtDeMan@1068 126 previous_time = 0
BrechtDeMan@1068 127
BrechtDeMan@1068 128 # draw all segments except final one
BrechtDeMan@1068 129 for event in move_events:
BrechtDeMan@1068 130 new_time = float(event.find("./time").text)-time_offset
BrechtDeMan@1068 131 new_position = float(event.find("./position").text)
BrechtDeMan@1068 132 # horizontal line from previous to current time
BrechtDeMan@1068 133 plt.plot([previous_time, new_time], # x-values
BrechtDeMan@1068 134 [previous_position, previous_position], # y-values
BrechtDeMan@1068 135 color=colormap[increment%len(colormap)],
BrechtDeMan@1068 136 linewidth=3
BrechtDeMan@1068 137 )
BrechtDeMan@1068 138 # vertical line from previous to current position
BrechtDeMan@1068 139 plt.plot([new_time, new_time], # x-values
BrechtDeMan@1068 140 [previous_position, new_position], # y-values
BrechtDeMan@1068 141 color=colormap[increment%len(colormap)],
BrechtDeMan@1068 142 linewidth=3
BrechtDeMan@1068 143 )
BrechtDeMan@1068 144
BrechtDeMan@1068 145 # update previous_position value
BrechtDeMan@1068 146 previous_position = new_position
BrechtDeMan@1068 147 previous_time = new_time
BrechtDeMan@1068 148
BrechtDeMan@1068 149 # draw final segment
BrechtDeMan@1068 150 # horizontal line from previous time to end of audioholder
BrechtDeMan@1068 151 plt.plot([previous_time, audioholder_time-time_offset], # x-values
BrechtDeMan@1068 152 [previous_position, previous_position], # y-values
BrechtDeMan@1068 153 color=colormap[increment%len(colormap)],
BrechtDeMan@1068 154 linewidth=3
BrechtDeMan@1068 155 )
BrechtDeMan@1068 156
BrechtDeMan@1068 157 # display fragment name at end
BrechtDeMan@1068 158 plt.text(audioholder_time-time_offset,previous_position,\
BrechtDeMan@1068 159 audio_id,color=colormap[increment%len(colormap)]) #,rotation=45
BrechtDeMan@1068 160
BrechtDeMan@1068 161 # for this audioelement, loop over all listen events
BrechtDeMan@1068 162 # listen_events = audioelement.findall("./metric/metricresult/[@name='elementListenTracker']/event")
BrechtDeMan@1068 163 # for event in listen_events:
BrechtDeMan@1068 164 # # get testtime: start and stop
BrechtDeMan@1068 165 # start_time = float(event.find('testtime').get('start'))
BrechtDeMan@1068 166 # stop_time = float(event.find('testtime').get('stop'))
BrechtDeMan@1068 167
BrechtDeMan@1068 168
BrechtDeMan@1068 169 increment+=1 # to next audioelement
BrechtDeMan@1068 170
BrechtDeMan@1068 171 last_audioholder_duration = audioholder_time-time_offset
BrechtDeMan@1068 172 time_offset = audioholder_time
BrechtDeMan@1068 173
BrechtDeMan@1068 174
BrechtDeMan@1068 175 # set plot parameters
BrechtDeMan@1068 176 plt.title('Timeline ' + file + ": "+page_name)
BrechtDeMan@1068 177 plt.xlabel('Time [seconds]')
BrechtDeMan@1068 178 plt.xlim(0, last_audioholder_duration)
BrechtDeMan@1068 179 plt.ylabel('Rating') # default
BrechtDeMan@1068 180 plt.ylim(0, 1) # rating between 0 and 1
BrechtDeMan@1068 181
BrechtDeMan@1068 182 #y-ticks: labels on rating axis
BrechtDeMan@1068 183 label_positions = []
BrechtDeMan@1068 184 label_text = []
BrechtDeMan@1068 185 scale_tags = root.findall("./BrowserEvalProjectDocument/audioHolder/interface/scale")
BrechtDeMan@1068 186 scale_title = root.find("./BrowserEvalProjectDocument/audioHolder/interface/title")
BrechtDeMan@1068 187 for tag in scale_tags:
BrechtDeMan@1068 188 label_positions.append(float(tag.get('position'))/100) # on a scale from 0 to 100
BrechtDeMan@1068 189 label_text.append(tag.text)
BrechtDeMan@1068 190 if len(label_positions) > 0:
BrechtDeMan@1068 191 plt.yticks(label_positions, label_text) # show rating axis labels
BrechtDeMan@1068 192 # set label Y-axis
BrechtDeMan@1068 193 if scale_title is not None:
BrechtDeMan@1068 194 plt.ylabel(scale_title.text)
BrechtDeMan@1068 195
BrechtDeMan@1068 196 #plt.show() # uncomment to show plot; comment when just saving
BrechtDeMan@1068 197 #exit()
BrechtDeMan@1068 198
BrechtDeMan@1068 199 plt.savefig(timeline_folder+subject_id+"-"+page_name+".pdf", bbox_inches='tight')
BrechtDeMan@1068 200 plt.close()
BrechtDeMan@1068 201