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