BrechtDeMan@1068: #!/usr/bin/python BrechtDeMan@1068: BrechtDeMan@1068: import xml.etree.ElementTree as ET BrechtDeMan@1068: import os # list files in directory BrechtDeMan@1068: import sys # command line arguments BrechtDeMan@1068: import matplotlib.pyplot as plt # plots BrechtDeMan@1068: import matplotlib.patches as patches # rectangles BrechtDeMan@1068: BrechtDeMan@1068: BrechtDeMan@1068: # COMMAND LINE ARGUMENTS BrechtDeMan@1068: BrechtDeMan@1068: assert len(sys.argv)<3, "timeline_view_movement takes at most 1 command line argument\n"+\ BrechtDeMan@1068: "Use: python timeline_view_movement.py [XML_files_location]" BrechtDeMan@1068: BrechtDeMan@1068: # XML results files location BrechtDeMan@1068: if len(sys.argv) == 1: BrechtDeMan@1068: folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder BrechtDeMan@1068: print "Use: python timeline_view_movement.py [XML_files_location]" BrechtDeMan@1068: print "Using default path: " + folder_name BrechtDeMan@1068: elif len(sys.argv) == 2: BrechtDeMan@1068: folder_name = sys.argv[1] # First command line argument is folder BrechtDeMan@1068: BrechtDeMan@1068: # check if folder_name exists BrechtDeMan@1068: if not os.path.exists(folder_name): BrechtDeMan@1068: #the file is not there BrechtDeMan@1068: print "Folder '"+folder_name+"' does not exist." BrechtDeMan@1068: sys.exit() # terminate script execution BrechtDeMan@1068: elif not os.access(os.path.dirname(folder_name), os.W_OK): BrechtDeMan@1068: #the file does exist but write privileges are not given BrechtDeMan@1068: print "No write privileges in folder '"+folder_name+"'." BrechtDeMan@1068: BrechtDeMan@1068: BrechtDeMan@1068: # CONFIGURATION BrechtDeMan@1068: BrechtDeMan@1068: # Folder where to store timelines BrechtDeMan@1068: timeline_folder = folder_name + '/timelines_movement/' # Stores in 'saves/timelines_movement/' by default BrechtDeMan@1068: BrechtDeMan@1068: # Font settings BrechtDeMan@1068: font = {'weight' : 'bold', BrechtDeMan@1068: 'size' : 16} BrechtDeMan@1068: plt.rc('font', **font) BrechtDeMan@1068: BrechtDeMan@1068: # Colormap for to cycle through BrechtDeMan@1068: colormap = ['b', 'g', 'c', 'm', 'y', 'k'] BrechtDeMan@1068: BrechtDeMan@1068: # figure size BrechtDeMan@1068: fig_width = 25 BrechtDeMan@1068: fig_height = 10 BrechtDeMan@1068: BrechtDeMan@1068: BrechtDeMan@1068: # CODE BrechtDeMan@1068: BrechtDeMan@1068: # create timeline_folder if not yet created BrechtDeMan@1068: if not os.path.exists(timeline_folder): BrechtDeMan@1068: os.makedirs(timeline_folder) BrechtDeMan@1068: BrechtDeMan@1068: # get every XML file in folder BrechtDeMan@1068: for file in os.listdir(folder_name): BrechtDeMan@1068: if file.endswith(".xml"): BrechtDeMan@1068: tree = ET.parse(folder_name + '/' + file) BrechtDeMan@1068: root = tree.getroot() BrechtDeMan@1068: subject_id = file[:-4] # drop '.xml' BrechtDeMan@1068: BrechtDeMan@1068: previous_audioholder_time = 0 # time spent before current audioholder BrechtDeMan@1068: time_offset = 0 # test starts at zero BrechtDeMan@1068: BrechtDeMan@1068: # ONE TIMELINE PER PAGE - make new plot per page BrechtDeMan@1068: BrechtDeMan@1068: # get list of all page names BrechtDeMan@1068: for audioholder in root.findall("./audioholder"): # iterate over pages BrechtDeMan@1068: page_name = audioholder.get('id') # get page name BrechtDeMan@1068: BrechtDeMan@1068: if page_name is None: # ignore 'empty' audio_holders BrechtDeMan@1068: print "Skipping empty audioholder name from "+subject_id+"." BrechtDeMan@1068: break BrechtDeMan@1068: BrechtDeMan@1068: # subtract total audioholder length from subsequent audioholder event times BrechtDeMan@1068: audioholder_time_temp = audioholder.find("./metric/metricresult/[@id='testTime']") BrechtDeMan@1068: if audioholder_time_temp is not None: BrechtDeMan@1068: audioholder_time = float(audioholder_time_temp.text) BrechtDeMan@1068: else: BrechtDeMan@1068: print "Skipping audioholder without total time specified from "+subject_id+"." BrechtDeMan@1068: break BrechtDeMan@1068: BrechtDeMan@1068: # get audioelements BrechtDeMan@1068: audioelements = audioholder.findall("./audioelement") BrechtDeMan@1068: BrechtDeMan@1068: # sort alphabetically BrechtDeMan@1068: data = [] BrechtDeMan@1068: for elem in audioelements: # from http://effbot.org/zone/element-sort.htm BrechtDeMan@1068: key = elem.get("id") BrechtDeMan@1068: data.append((key, elem)) BrechtDeMan@1068: data.sort() BrechtDeMan@1068: BrechtDeMan@1068: N_audioelements = len(audioelements) # number of audio elements for this page BrechtDeMan@1068: increment = 0 # increased for every new audioelement BrechtDeMan@1068: BrechtDeMan@1068: # get axes handle BrechtDeMan@1068: fig = plt.figure(figsize=(fig_width, fig_height)) BrechtDeMan@1068: ax = fig.add_subplot(111) BrechtDeMan@1068: BrechtDeMan@1068: # for page [page_name], print comments related to fragment [id] BrechtDeMan@1068: #for tuple in data: BrechtDeMan@1068: # audioelement = tuple[1] BrechtDeMan@1068: for tuple in data: BrechtDeMan@1068: audioelement = tuple[1] BrechtDeMan@1068: if audioelement is not None: # Check it exists BrechtDeMan@1068: audio_id = str(audioelement.get('id')) BrechtDeMan@1068: BrechtDeMan@1068: # break if no initial position or move events registered BrechtDeMan@1068: initial_position_temp = audioelement.find("./metric/metricresult/[@name='elementInitialPosition']") BrechtDeMan@1068: if initial_position_temp is None: BrechtDeMan@1068: print "Skipping "+page_name+" from "+subject_id+": does not have initial positions specified." BrechtDeMan@1068: break BrechtDeMan@1068: BrechtDeMan@1068: # for this audioelement, loop over all move events BrechtDeMan@1068: initial_position = float(initial_position_temp.text) BrechtDeMan@1068: move_events = audioelement.findall("./metric/metricresult/[@name='elementTrackerFull']/timepos") BrechtDeMan@1068: final_position = float(audioelement.find("./value").text) BrechtDeMan@1068: BrechtDeMan@1068: # display fragment name at start BrechtDeMan@1068: plt.text(0,initial_position+0.02,audio_id,color=colormap[increment%len(colormap)]) #,rotation=45 BrechtDeMan@1068: BrechtDeMan@1068: # previous position and time BrechtDeMan@1068: previous_position = initial_position BrechtDeMan@1068: previous_time = 0 BrechtDeMan@1068: BrechtDeMan@1068: # draw all segments except final one BrechtDeMan@1068: for event in move_events: BrechtDeMan@1068: new_time = float(event.find("./time").text)-time_offset BrechtDeMan@1068: new_position = float(event.find("./position").text) BrechtDeMan@1068: # horizontal line from previous to current time BrechtDeMan@1068: plt.plot([previous_time, new_time], # x-values BrechtDeMan@1068: [previous_position, previous_position], # y-values BrechtDeMan@1068: color=colormap[increment%len(colormap)], BrechtDeMan@1068: linewidth=3 BrechtDeMan@1068: ) BrechtDeMan@1068: # vertical line from previous to current position BrechtDeMan@1068: plt.plot([new_time, new_time], # x-values BrechtDeMan@1068: [previous_position, new_position], # y-values BrechtDeMan@1068: color=colormap[increment%len(colormap)], BrechtDeMan@1068: linewidth=3 BrechtDeMan@1068: ) BrechtDeMan@1068: BrechtDeMan@1068: # update previous_position value BrechtDeMan@1068: previous_position = new_position BrechtDeMan@1068: previous_time = new_time BrechtDeMan@1068: BrechtDeMan@1068: # draw final segment BrechtDeMan@1068: # horizontal line from previous time to end of audioholder BrechtDeMan@1068: plt.plot([previous_time, audioholder_time-time_offset], # x-values BrechtDeMan@1068: [previous_position, previous_position], # y-values BrechtDeMan@1068: color=colormap[increment%len(colormap)], BrechtDeMan@1068: linewidth=3 BrechtDeMan@1068: ) BrechtDeMan@1068: BrechtDeMan@1068: # display fragment name at end BrechtDeMan@1068: plt.text(audioholder_time-time_offset,previous_position,\ BrechtDeMan@1068: audio_id,color=colormap[increment%len(colormap)]) #,rotation=45 BrechtDeMan@1068: BrechtDeMan@1068: # for this audioelement, loop over all listen events BrechtDeMan@1068: # listen_events = audioelement.findall("./metric/metricresult/[@name='elementListenTracker']/event") BrechtDeMan@1068: # for event in listen_events: BrechtDeMan@1068: # # get testtime: start and stop BrechtDeMan@1068: # start_time = float(event.find('testtime').get('start')) BrechtDeMan@1068: # stop_time = float(event.find('testtime').get('stop')) BrechtDeMan@1068: BrechtDeMan@1068: BrechtDeMan@1068: increment+=1 # to next audioelement BrechtDeMan@1068: BrechtDeMan@1068: last_audioholder_duration = audioholder_time-time_offset BrechtDeMan@1068: time_offset = audioholder_time BrechtDeMan@1068: BrechtDeMan@1068: BrechtDeMan@1068: # set plot parameters BrechtDeMan@1068: plt.title('Timeline ' + file + ": "+page_name) BrechtDeMan@1068: plt.xlabel('Time [seconds]') BrechtDeMan@1068: plt.xlim(0, last_audioholder_duration) BrechtDeMan@1068: plt.ylabel('Rating') # default BrechtDeMan@1068: plt.ylim(0, 1) # rating between 0 and 1 BrechtDeMan@1068: BrechtDeMan@1068: #y-ticks: labels on rating axis BrechtDeMan@1068: label_positions = [] BrechtDeMan@1068: label_text = [] BrechtDeMan@1068: scale_tags = root.findall("./BrowserEvalProjectDocument/audioHolder/interface/scale") BrechtDeMan@1068: scale_title = root.find("./BrowserEvalProjectDocument/audioHolder/interface/title") BrechtDeMan@1068: for tag in scale_tags: BrechtDeMan@1068: label_positions.append(float(tag.get('position'))/100) # on a scale from 0 to 100 BrechtDeMan@1068: label_text.append(tag.text) BrechtDeMan@1068: if len(label_positions) > 0: BrechtDeMan@1068: plt.yticks(label_positions, label_text) # show rating axis labels BrechtDeMan@1068: # set label Y-axis BrechtDeMan@1068: if scale_title is not None: BrechtDeMan@1068: plt.ylabel(scale_title.text) BrechtDeMan@1068: BrechtDeMan@1068: #plt.show() # uncomment to show plot; comment when just saving BrechtDeMan@1068: #exit() BrechtDeMan@1068: BrechtDeMan@1068: plt.savefig(timeline_folder+subject_id+"-"+page_name+".pdf", bbox_inches='tight') BrechtDeMan@1068: plt.close() BrechtDeMan@1068: