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