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