annotate scripts/timeline_view.py @ 1384:7cd2a8dcdc51

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