annotate scripts/timeline_view.py @ 284:f32e58635091

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