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