nicholas@872: import xml.etree.ElementTree as ET nicholas@872: import os nicholas@872: import matplotlib.pyplot as plt nicholas@872: nicholas@872: colormap = ['b', 'r', 'g', 'c', 'm', 'y', 'k'] # colormap for to cycle through nicholas@872: nicholas@872: timeline_folder = 'timelines/' # folder where to store timelines, e.g. 'timelines/' nicholas@872: 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 nicholas@872: for file in os.listdir("."): # You have to put this script in folder where output XML files are. nicholas@872: if file.endswith(".xml"): nicholas@872: tree = ET.parse(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 nicholas@872: audioelements = root.findall("*/[@id='"+page_name+"']/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 nicholas@872: listen_events = root.findall("*/[@id='" nicholas@872: + page_name nicholas@872: + "']/audioelement/[@id='" nicholas@872: + audio_id nicholas@872: + "']/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()