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