BrechtDeMan@884: #!/usr/bin/python BrechtDeMan@884: nicholas@872: import xml.etree.ElementTree as ET nicholas@872: import os nicholas@872: import matplotlib.pyplot as plt nicholas@872: BrechtDeMan@884: # CONFIGURATION nicholas@872: BrechtDeMan@884: # XML results files location (modify as needed): BrechtDeMan@884: folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder nicholas@872: BrechtDeMan@884: # Folder where to store timelines BrechtDeMan@884: timeline_folder = folder_name + '/timelines/' # Stores in 'saves/timelines/' BrechtDeMan@884: BrechtDeMan@884: # Font settings BrechtDeMan@884: font = {'weight' : 'bold', BrechtDeMan@884: 'size' : 16} BrechtDeMan@884: plt.rc('font', **font) BrechtDeMan@884: BrechtDeMan@884: # Colormap for to cycle through BrechtDeMan@884: colormap = ['b', 'r', 'g', 'c', 'm', 'y', 'k'] BrechtDeMan@884: BrechtDeMan@884: BrechtDeMan@884: # CODE nicholas@872: nicholas@872: # create timeline_folder if not yet created nicholas@872: if not os.path.exists(timeline_folder): nicholas@872: os.makedirs(timeline_folder) nicholas@872: nicholas@872: # get every XML file in folder BrechtDeMan@884: for file in os.listdir(folder_name): # You have to put this script in folder where output XML files are. nicholas@872: if file.endswith(".xml"): BrechtDeMan@884: tree = ET.parse(folder_name + '/' + file) nicholas@872: root = tree.getroot() nicholas@872: subject_id = file[:-4] # drop '.xml' nicholas@872: nicholas@872: # ONE TIMELINE PER PAGE - make new plot per page nicholas@872: nicholas@872: # get list of all page names nicholas@872: for audioholder in root.findall("./audioholder"): # iterate over pages nicholas@872: page_name = audioholder.get('id') # get page name nicholas@872: nicholas@872: if page_name is None: # ignore 'empty' audio_holders nicholas@872: break nicholas@872: nicholas@872: # SORT AUDIO ELEMENTS ALPHABETICALLY BrechtDeMan@884: audioelements = audioholder.findall("./audioelement") nicholas@872: nicholas@872: data = [] nicholas@872: for elem in audioelements: # from http://effbot.org/zone/element-sort.htm nicholas@872: key = elem.get("id") nicholas@872: data.append((key, elem)) nicholas@872: data.sort() nicholas@872: nicholas@872: N_audioelements = len(audioelements) # number of audio elements for this page nicholas@872: increment = 0 # increased for every new audioelement nicholas@872: audioelements_names = [] # store names of audioelements nicholas@872: nicholas@872: # for page [page_name], print comments related to fragment [id] nicholas@872: for tuple in data: nicholas@872: audioelement = tuple[1] nicholas@872: if audioelement is not None: # Check it exists nicholas@872: audio_id = str(audioelement.get('id')) nicholas@872: audioelements_names.append(audio_id) nicholas@872: nicholas@872: # for this audioelement, loop over all listen events BrechtDeMan@884: listen_events = audioelement.findall("./metric/metricresult/[@name='elementListenTracker']/event") nicholas@872: for event in listen_events: nicholas@872: # get testtime: start and stop nicholas@872: start_time = event.find('testtime').get('start') nicholas@872: stop_time = event.find('testtime').get('stop') nicholas@872: # event lines: nicholas@872: plt.plot([start_time, start_time], # x-values nicholas@872: [0, N_audioelements+1], # y-values nicholas@872: color='k' nicholas@872: ) nicholas@872: plt.plot([stop_time, stop_time], # x-values nicholas@872: [0, N_audioelements+1], # y-values nicholas@872: color='k' nicholas@872: ) nicholas@872: # plot time: nicholas@872: plt.plot([start_time, stop_time], # x-values nicholas@872: [N_audioelements-increment, N_audioelements-increment], # y-values nicholas@872: color=colormap[increment%len(colormap)], nicholas@872: linewidth=6 nicholas@872: ) nicholas@872: nicholas@872: increment+=1 nicholas@872: nicholas@872: #TODO: if 'nonsensical' or unknown: dashed line until next event nicholas@872: #TODO: Vertical lines for fragment looping point nicholas@872: nicholas@872: plt.title('Timeline ' + file) #TODO add song too nicholas@872: plt.xlabel('Time [seconds]') nicholas@872: plt.ylabel('Fragment') nicholas@872: plt.ylim(0, N_audioelements+1) nicholas@872: nicholas@872: #y-ticks: fragment IDs, top to bottom nicholas@872: plt.yticks(range(N_audioelements, 0, -1), audioelements_names) # show fragment names nicholas@872: nicholas@872: nicholas@872: #plt.show() # uncomment to show plot; comment when just saving nicholas@872: #exit() nicholas@872: nicholas@872: plt.savefig(timeline_folder+subject_id+"-"+page_name+".png") nicholas@872: plt.close()