annotate scripts/timeline_view.py @ 1481:8be2d08fbe15

Started WAC paper
author Brecht De Man <b.deman@qmul.ac.uk>
date Tue, 22 Sep 2015 19:22:02 +0200
parents
children 1b6fa37d46a4 235594325b84
rev   line source
b@1481 1 #!/usr/bin/python
b@1481 2
b@1481 3 import xml.etree.ElementTree as ET
b@1481 4 import os # list files in directory
b@1481 5 import sys # command line arguments
b@1481 6 import matplotlib.pyplot as plt # plots
b@1481 7 import matplotlib.patches as patches # rectangles
b@1481 8
b@1481 9 # COMMAND LINE ARGUMENTS
b@1481 10
b@1481 11 assert len(sys.argv)<3, "timeline_view takes at most 1 command line argument\n"+\
b@1481 12 "Use: python timeline_view.py [XML_files_location]"
b@1481 13
b@1481 14 # XML results files location
b@1481 15 if len(sys.argv) == 1:
b@1481 16 folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder
b@1481 17 print "Use: python timeline_view.py [XML_files_location]"
b@1481 18 print "Using default path: " + folder_name
b@1481 19 elif len(sys.argv) == 2:
b@1481 20 folder_name = sys.argv[1] # First command line argument is folder
b@1481 21
b@1481 22 # check if folder_name exists
b@1481 23 if not os.path.exists(folder_name):
b@1481 24 #the file is not there
b@1481 25 print "Folder '"+folder_name+"' does not exist."
b@1481 26 sys.exit() # terminate script execution
b@1481 27 elif not os.access(os.path.dirname(folder_name), os.W_OK):
b@1481 28 #the file does exist but write privileges are not given
b@1481 29 print "No write privileges in folder '"+folder_name+"'."
b@1481 30
b@1481 31
b@1481 32 # CONFIGURATION
b@1481 33
b@1481 34 # Folder where to store timelines
b@1481 35 timeline_folder = folder_name + '/timelines/' # Stores in 'saves/timelines/'
b@1481 36
b@1481 37 # Font settings
b@1481 38 font = {'weight' : 'bold',
b@1481 39 'size' : 16}
b@1481 40 plt.rc('font', **font)
b@1481 41
b@1481 42 # Colormap for to cycle through
b@1481 43 colormap = ['b', 'r', 'g', 'c', 'm', 'y', 'k']
b@1481 44
b@1481 45 # if enabled, x-axis shows time per audioholder, not total test time
b@1481 46 show_audioholder_time = True
b@1481 47
b@1481 48 # bar height (<1 to avoid overlapping)
b@1481 49 bar_height = 0.6
b@1481 50
b@1481 51 # figure size
b@1481 52 fig_width = 25
b@1481 53 fig_height = 5
b@1481 54
b@1481 55
b@1481 56 # CODE
b@1481 57
b@1481 58 # create timeline_folder if not yet created
b@1481 59 if not os.path.exists(timeline_folder):
b@1481 60 os.makedirs(timeline_folder)
b@1481 61
b@1481 62 # get every XML file in folder
b@1481 63 for file in os.listdir(folder_name):
b@1481 64 if file.endswith(".xml"):
b@1481 65 tree = ET.parse(folder_name + '/' + file)
b@1481 66 root = tree.getroot()
b@1481 67 subject_id = file[:-4] # drop '.xml'
b@1481 68
b@1481 69 time_offset = 0 # test starts at zero
b@1481 70
b@1481 71 # ONE TIMELINE PER PAGE - make new plot per page
b@1481 72
b@1481 73 # get list of all page names
b@1481 74 for audioholder in root.findall("./audioholder"): # iterate over pages
b@1481 75 page_name = audioholder.get('id') # get page name
b@1481 76 plot_empty = True # check if any data is plotted
b@1481 77
b@1481 78 if page_name is None: # ignore 'empty' audio_holders
b@1481 79 break
b@1481 80
b@1481 81 # SORT AUDIO ELEMENTS ALPHABETICALLY
b@1481 82 audioelements = audioholder.findall("./audioelement")
b@1481 83
b@1481 84 data = []
b@1481 85 for elem in audioelements: # from http://effbot.org/zone/element-sort.htm
b@1481 86 key = elem.get("id")
b@1481 87 data.append((key, elem))
b@1481 88 data.sort()
b@1481 89
b@1481 90 N_audioelements = len(audioelements) # number of audio elements for this page
b@1481 91 increment = 0 # increased for every new audioelement
b@1481 92 audioelements_names = [] # store names of audioelements
b@1481 93
b@1481 94 # get axes handle
b@1481 95 fig = plt.figure(figsize=(fig_width, fig_height))
b@1481 96 ax = fig.add_subplot(111) #, aspect='equal'
b@1481 97
b@1481 98 # for page [page_name], print comments related to fragment [id]
b@1481 99 for tuple in data:
b@1481 100 audioelement = tuple[1]
b@1481 101 if audioelement is not None: # Check it exists
b@1481 102 audio_id = str(audioelement.get('id'))
b@1481 103 audioelements_names.append(audio_id)
b@1481 104
b@1481 105 # for this audioelement, loop over all listen events
b@1481 106 listen_events = audioelement.findall("./metric/metricresult/[@name='elementListenTracker']/event")
b@1481 107 for event in listen_events:
b@1481 108 # mark this plot as not empty
b@1481 109 plot_empty = False
b@1481 110
b@1481 111 # get testtime: start and stop
b@1481 112 start_time = float(event.find('testtime').get('start'))-time_offset
b@1481 113 stop_time = float(event.find('testtime').get('stop'))-time_offset
b@1481 114 # event lines:
b@1481 115 ax.plot([start_time, start_time], # x-values
b@1481 116 [0, N_audioelements+1], # y-values
b@1481 117 color='k'
b@1481 118 )
b@1481 119 ax.plot([stop_time, stop_time], # x-values
b@1481 120 [0, N_audioelements+1], # y-values
b@1481 121 color='k'
b@1481 122 )
b@1481 123 # plot time:
b@1481 124 ax.add_patch(
b@1481 125 patches.Rectangle(
b@1481 126 (start_time, N_audioelements-increment-bar_height/2), # (x, y)
b@1481 127 stop_time - start_time, # width
b@1481 128 bar_height, # height
b@1481 129 color=colormap[increment%len(colormap)] # colour
b@1481 130 )
b@1481 131 )
b@1481 132
b@1481 133 increment+=1 # to next audioelement
b@1481 134
b@1481 135 # subtract total audioholder length from subsequent audioholder event times
b@1481 136 audioholder_time = audioholder.find("./metric/metricresult/[@id='testTime']")
b@1481 137 if audioholder_time is not None and show_audioholder_time:
b@1481 138 time_offset = float(audioholder_time.text)
b@1481 139
b@1481 140 if not plot_empty:
b@1481 141 # set plot parameters
b@1481 142 plt.title('Timeline ' + file + ": "+page_name)
b@1481 143 plt.xlabel('Time [seconds]')
b@1481 144 plt.ylabel('Fragment')
b@1481 145 plt.ylim(0, N_audioelements+1)
b@1481 146
b@1481 147 #y-ticks: fragment IDs, top to bottom
b@1481 148 plt.yticks(range(N_audioelements, 0, -1), audioelements_names) # show fragment names
b@1481 149
b@1481 150
b@1481 151 #plt.show() # uncomment to show plot; comment when just saving
b@1481 152 #exit()
b@1481 153
b@1481 154 plt.savefig(timeline_folder+subject_id+"-"+page_name+".pdf", bbox_inches='tight')
b@1481 155 plt.close()
b@1481 156
b@1481 157 #TODO: if 'nonsensical' or unknown: dashed line until next event
b@1481 158 #TODO: Vertical lines for fragment looping point
b@1481 159