annotate scripts/timeline_view.py @ 1150:2674d80c66ff

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