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