annotate scripts/timeline_view.py @ 1066:a2a245542ae6

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