BrechtDeMan@884: #!/usr/bin/python BrechtDeMan@884: nicholas@872: import xml.etree.ElementTree as ET BrechtDeMan@1063: import os # list files in directory BrechtDeMan@1063: import sys # command line arguments BrechtDeMan@1063: import matplotlib.pyplot as plt # plots BrechtDeMan@1067: import matplotlib.patches as patches # rectangles BrechtDeMan@1063: BrechtDeMan@1063: # COMMAND LINE ARGUMENTS BrechtDeMan@1063: BrechtDeMan@1063: assert len(sys.argv)<3, "timeline_view takes at most 1 command line argument\n"+\ BrechtDeMan@1063: "Use: python timeline_view.py [timeline_folder_location]" BrechtDeMan@1063: BrechtDeMan@1063: # XML results files location BrechtDeMan@1063: if len(sys.argv) == 1: BrechtDeMan@1063: folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder BrechtDeMan@1063: print "Use: python timeline_view.py [timeline_folder_location]" BrechtDeMan@1063: print "Using default path: " + folder_name BrechtDeMan@1063: elif len(sys.argv) == 2: BrechtDeMan@1063: folder_name = sys.argv[1] # First command line argument is folder BrechtDeMan@1067: BrechtDeMan@1066: # check if folder_name exists BrechtDeMan@1066: if not os.path.exists(folder_name): BrechtDeMan@1066: #the file is not there BrechtDeMan@1066: print "Folder '"+folder_name+"' does not exist." BrechtDeMan@1066: sys.exit() # terminate script execution BrechtDeMan@1066: elif not os.access(os.path.dirname(folder_name), os.W_OK): BrechtDeMan@1066: #the file does exist but write privileges are not given BrechtDeMan@1066: print "No write privileges in folder '"+folder_name+"'." BrechtDeMan@1066: nicholas@872: BrechtDeMan@884: # CONFIGURATION nicholas@872: BrechtDeMan@884: # Folder where to store timelines BrechtDeMan@884: timeline_folder = folder_name + '/timelines/' # Stores in 'saves/timelines/' BrechtDeMan@884: BrechtDeMan@884: # Font settings BrechtDeMan@884: font = {'weight' : 'bold', BrechtDeMan@884: 'size' : 16} BrechtDeMan@884: plt.rc('font', **font) BrechtDeMan@884: BrechtDeMan@884: # Colormap for to cycle through BrechtDeMan@884: colormap = ['b', 'r', 'g', 'c', 'm', 'y', 'k'] BrechtDeMan@884: BrechtDeMan@1067: # if enabled, x-axis shows time per audioholder, not total test time BrechtDeMan@1065: show_audioholder_time = True BrechtDeMan@1065: BrechtDeMan@1067: # bar height (<1 to avoid overlapping) BrechtDeMan@1067: bar_height = 0.6 BrechtDeMan@1067: BrechtDeMan@1067: # figure size BrechtDeMan@1067: fig_width = 25 BrechtDeMan@1067: fig_height = 5 BrechtDeMan@1067: BrechtDeMan@884: BrechtDeMan@884: # CODE nicholas@872: nicholas@872: # create timeline_folder if not yet created nicholas@872: if not os.path.exists(timeline_folder): nicholas@872: os.makedirs(timeline_folder) nicholas@872: nicholas@872: # get every XML file in folder BrechtDeMan@885: for file in os.listdir(folder_name): nicholas@872: if file.endswith(".xml"): BrechtDeMan@884: tree = ET.parse(folder_name + '/' + file) nicholas@872: root = tree.getroot() nicholas@872: subject_id = file[:-4] # drop '.xml' nicholas@872: BrechtDeMan@1065: time_offset = 0 # test starts at zero BrechtDeMan@1065: nicholas@872: # ONE TIMELINE PER PAGE - make new plot per page nicholas@872: nicholas@872: # get list of all page names nicholas@872: for audioholder in root.findall("./audioholder"): # iterate over pages nicholas@872: page_name = audioholder.get('id') # get page name nicholas@872: nicholas@872: if page_name is None: # ignore 'empty' audio_holders nicholas@872: break nicholas@872: nicholas@872: # SORT AUDIO ELEMENTS ALPHABETICALLY BrechtDeMan@884: audioelements = audioholder.findall("./audioelement") nicholas@872: nicholas@872: data = [] nicholas@872: for elem in audioelements: # from http://effbot.org/zone/element-sort.htm nicholas@872: key = elem.get("id") nicholas@872: data.append((key, elem)) nicholas@872: data.sort() nicholas@872: nicholas@872: N_audioelements = len(audioelements) # number of audio elements for this page nicholas@872: increment = 0 # increased for every new audioelement nicholas@872: audioelements_names = [] # store names of audioelements nicholas@872: BrechtDeMan@1067: # set plot parameters BrechtDeMan@1067: plt.title('Timeline ' + file + ": "+page_name) BrechtDeMan@1067: plt.xlabel('Time [seconds]') BrechtDeMan@1067: plt.ylabel('Fragment') BrechtDeMan@1067: plt.ylim(0, N_audioelements+1) BrechtDeMan@1067: BrechtDeMan@1067: # get axes handle BrechtDeMan@1067: fig = plt.figure(figsize=(fig_width, fig_height)) BrechtDeMan@1067: ax = fig.add_subplot(111) #, aspect='equal' BrechtDeMan@1067: nicholas@872: # for page [page_name], print comments related to fragment [id] nicholas@872: for tuple in data: nicholas@872: audioelement = tuple[1] nicholas@872: if audioelement is not None: # Check it exists nicholas@872: audio_id = str(audioelement.get('id')) nicholas@872: audioelements_names.append(audio_id) nicholas@872: nicholas@872: # for this audioelement, loop over all listen events BrechtDeMan@884: listen_events = audioelement.findall("./metric/metricresult/[@name='elementListenTracker']/event") nicholas@872: for event in listen_events: nicholas@872: # get testtime: start and stop BrechtDeMan@1065: start_time = float(event.find('testtime').get('start')) BrechtDeMan@1065: stop_time = float(event.find('testtime').get('stop')) nicholas@872: # event lines: BrechtDeMan@1067: ax.plot([start_time-time_offset, start_time-time_offset], # x-values nicholas@872: [0, N_audioelements+1], # y-values nicholas@872: color='k' nicholas@872: ) BrechtDeMan@1067: ax.plot([stop_time-time_offset, stop_time-time_offset], # x-values nicholas@872: [0, N_audioelements+1], # y-values nicholas@872: color='k' nicholas@872: ) nicholas@872: # plot time: BrechtDeMan@1067: ax.add_patch( BrechtDeMan@1067: patches.Rectangle( BrechtDeMan@1067: (start_time-time_offset, N_audioelements-increment-bar_height/2), # (x, y) BrechtDeMan@1067: stop_time - start_time, # width BrechtDeMan@1067: bar_height, # height BrechtDeMan@1067: color=colormap[increment%len(colormap)] # colour nicholas@872: ) BrechtDeMan@1067: ) nicholas@872: BrechtDeMan@1067: increment+=1 # to next audioelement BrechtDeMan@1065: BrechtDeMan@1065: # subtract total audioholder length from subsequent audioholder event times BrechtDeMan@1065: audioholder_time = audioholder.find("./metric/metricresult/[@id='testTime']") BrechtDeMan@1065: if audioholder_time is not None and show_audioholder_time: BrechtDeMan@1065: time_offset = float(audioholder_time.text) nicholas@872: nicholas@872: #TODO: if 'nonsensical' or unknown: dashed line until next event nicholas@872: #TODO: Vertical lines for fragment looping point nicholas@872: nicholas@872: #y-ticks: fragment IDs, top to bottom nicholas@872: plt.yticks(range(N_audioelements, 0, -1), audioelements_names) # show fragment names nicholas@872: nicholas@872: nicholas@872: #plt.show() # uncomment to show plot; comment when just saving nicholas@872: #exit() nicholas@872: BrechtDeMan@1067: plt.savefig(timeline_folder+subject_id+"-"+page_name+".pdf", bbox_inches='tight') nicholas@872: plt.close()