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