annotate scripts/timeline_view.py @ 789:3539d6c992e4

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