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