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