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