comparison scripts/timeline_view.py @ 1457:c8a9825aaa05

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