annotate scripts/timeline_view.py @ 2072:49b03ad3dcf9

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