annotate scripts/timeline_view.py @ 2202:61c8e13f1e2e

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