annotate scripts/timeline_view.py @ 2073:0a846d951bc1

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