b@246: #!/usr/bin/python b@246: b@208: import xml.etree.ElementTree as ET b@264: import os # list files in directory b@264: import sys # command line arguments b@264: import matplotlib.pyplot as plt # plots b@264: b@264: # COMMAND LINE ARGUMENTS b@264: b@264: assert len(sys.argv)<3, "timeline_view takes at most 1 command line argument\n"+\ b@264: "Use: python timeline_view.py [timeline_folder_location]" b@264: b@264: # XML results files location b@264: if len(sys.argv) == 1: b@264: folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder b@264: print "Use: python timeline_view.py [timeline_folder_location]" b@264: print "Using default path: " + folder_name b@264: elif len(sys.argv) == 2: b@264: folder_name = sys.argv[1] # First command line argument is folder b@208: b@246: # CONFIGURATION 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@283: # x-axis shows time per audioholder, not total test time b@283: show_audioholder_time = True b@283: 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@283: time_offset = 0 # test starts at zero b@283: 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@283: start_time = float(event.find('testtime').get('start')) b@283: stop_time = float(event.find('testtime').get('stop')) b@208: # event lines: b@283: plt.plot([start_time-time_offset, start_time-time_offset], # x-values b@208: [0, N_audioelements+1], # y-values b@208: color='k' b@208: ) b@283: plt.plot([stop_time-time_offset, stop_time-time_offset], # x-values b@208: [0, N_audioelements+1], # y-values b@208: color='k' b@208: ) b@208: # plot time: b@283: plt.plot([start_time-time_offset, stop_time-time_offset], # 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@283: b@283: # subtract total audioholder length from subsequent audioholder event times b@283: audioholder_time = audioholder.find("./metric/metricresult/[@id='testTime']") b@283: if audioholder_time is not None and show_audioholder_time: b@283: time_offset = float(audioholder_time.text) 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()