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