BrechtDeMan@884: #!/usr/bin/python BrechtDeMan@884: nicholas@872: import xml.etree.ElementTree as ET BrechtDeMan@1063: import os # list files in directory BrechtDeMan@1063: import sys # command line arguments BrechtDeMan@1063: import matplotlib.pyplot as plt # plots BrechtDeMan@1063: BrechtDeMan@1063: # COMMAND LINE ARGUMENTS BrechtDeMan@1063: BrechtDeMan@1063: assert len(sys.argv)<3, "timeline_view takes at most 1 command line argument\n"+\ BrechtDeMan@1063: "Use: python timeline_view.py [timeline_folder_location]" BrechtDeMan@1063: BrechtDeMan@1063: # XML results files location BrechtDeMan@1063: if len(sys.argv) == 1: BrechtDeMan@1063: folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder BrechtDeMan@1063: print "Use: python timeline_view.py [timeline_folder_location]" BrechtDeMan@1063: print "Using default path: " + folder_name BrechtDeMan@1063: elif len(sys.argv) == 2: BrechtDeMan@1063: folder_name = sys.argv[1] # First command line argument is folder nicholas@872: BrechtDeMan@884: # CONFIGURATION nicholas@872: BrechtDeMan@884: # Folder where to store timelines BrechtDeMan@884: timeline_folder = folder_name + '/timelines/' # Stores in 'saves/timelines/' BrechtDeMan@884: BrechtDeMan@884: # Font settings BrechtDeMan@884: font = {'weight' : 'bold', BrechtDeMan@884: 'size' : 16} BrechtDeMan@884: plt.rc('font', **font) BrechtDeMan@884: BrechtDeMan@884: # Colormap for to cycle through BrechtDeMan@884: colormap = ['b', 'r', 'g', 'c', 'm', 'y', 'k'] BrechtDeMan@884: BrechtDeMan@1065: # x-axis shows time per audioholder, not total test time BrechtDeMan@1065: show_audioholder_time = True BrechtDeMan@1065: BrechtDeMan@884: BrechtDeMan@884: # CODE nicholas@872: nicholas@872: # create timeline_folder if not yet created nicholas@872: if not os.path.exists(timeline_folder): nicholas@872: os.makedirs(timeline_folder) nicholas@872: nicholas@872: # get every XML file in folder BrechtDeMan@885: for file in os.listdir(folder_name): nicholas@872: if file.endswith(".xml"): BrechtDeMan@884: tree = ET.parse(folder_name + '/' + file) nicholas@872: root = tree.getroot() nicholas@872: subject_id = file[:-4] # drop '.xml' nicholas@872: BrechtDeMan@1065: time_offset = 0 # test starts at zero BrechtDeMan@1065: nicholas@872: # ONE TIMELINE PER PAGE - make new plot per page nicholas@872: nicholas@872: # get list of all page names nicholas@872: for audioholder in root.findall("./audioholder"): # iterate over pages nicholas@872: page_name = audioholder.get('id') # get page name nicholas@872: nicholas@872: if page_name is None: # ignore 'empty' audio_holders nicholas@872: break nicholas@872: nicholas@872: # SORT AUDIO ELEMENTS ALPHABETICALLY BrechtDeMan@884: audioelements = audioholder.findall("./audioelement") nicholas@872: nicholas@872: data = [] nicholas@872: for elem in audioelements: # from http://effbot.org/zone/element-sort.htm nicholas@872: key = elem.get("id") nicholas@872: data.append((key, elem)) nicholas@872: data.sort() nicholas@872: nicholas@872: N_audioelements = len(audioelements) # number of audio elements for this page nicholas@872: increment = 0 # increased for every new audioelement nicholas@872: audioelements_names = [] # store names of audioelements nicholas@872: nicholas@872: # for page [page_name], print comments related to fragment [id] nicholas@872: for tuple in data: nicholas@872: audioelement = tuple[1] nicholas@872: if audioelement is not None: # Check it exists nicholas@872: audio_id = str(audioelement.get('id')) nicholas@872: audioelements_names.append(audio_id) nicholas@872: nicholas@872: # for this audioelement, loop over all listen events BrechtDeMan@884: listen_events = audioelement.findall("./metric/metricresult/[@name='elementListenTracker']/event") nicholas@872: for event in listen_events: nicholas@872: # get testtime: start and stop BrechtDeMan@1065: start_time = float(event.find('testtime').get('start')) BrechtDeMan@1065: stop_time = float(event.find('testtime').get('stop')) nicholas@872: # event lines: BrechtDeMan@1065: plt.plot([start_time-time_offset, start_time-time_offset], # x-values nicholas@872: [0, N_audioelements+1], # y-values nicholas@872: color='k' nicholas@872: ) BrechtDeMan@1065: plt.plot([stop_time-time_offset, stop_time-time_offset], # x-values nicholas@872: [0, N_audioelements+1], # y-values nicholas@872: color='k' nicholas@872: ) nicholas@872: # plot time: BrechtDeMan@1065: plt.plot([start_time-time_offset, stop_time-time_offset], # x-values nicholas@872: [N_audioelements-increment, N_audioelements-increment], # y-values nicholas@872: color=colormap[increment%len(colormap)], nicholas@872: linewidth=6 nicholas@872: ) nicholas@872: nicholas@872: increment+=1 BrechtDeMan@1065: BrechtDeMan@1065: # subtract total audioholder length from subsequent audioholder event times BrechtDeMan@1065: audioholder_time = audioholder.find("./metric/metricresult/[@id='testTime']") BrechtDeMan@1065: if audioholder_time is not None and show_audioholder_time: BrechtDeMan@1065: time_offset = float(audioholder_time.text) nicholas@872: nicholas@872: #TODO: if 'nonsensical' or unknown: dashed line until next event nicholas@872: #TODO: Vertical lines for fragment looping point nicholas@872: nicholas@872: plt.title('Timeline ' + file) #TODO add song too nicholas@872: plt.xlabel('Time [seconds]') nicholas@872: plt.ylabel('Fragment') nicholas@872: plt.ylim(0, N_audioelements+1) nicholas@872: nicholas@872: #y-ticks: fragment IDs, top to bottom nicholas@872: plt.yticks(range(N_audioelements, 0, -1), audioelements_names) # show fragment names nicholas@872: nicholas@872: nicholas@872: #plt.show() # uncomment to show plot; comment when just saving nicholas@872: #exit() nicholas@872: nicholas@872: plt.savefig(timeline_folder+subject_id+"-"+page_name+".png") nicholas@872: plt.close()