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