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