annotate scripts/timeline_view.py @ 287:4fb6448759a9

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