annotate scripts/timeline_view.py @ 1460:1b81ab727352

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