annotate scripts/timeline_view.py @ 1350:b6389ceaeaa5

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