Mercurial > hg > webaudioevaluationtool
comparison scripts/timeline_view.py @ 1478:82f43919f385
Paper: Added interface screenshot and box plot example
author | Brecht De Man <b.deman@qmul.ac.uk> |
---|---|
date | Thu, 15 Oct 2015 20:10:00 +0100 |
parents | |
children | 1b6fa37d46a4 235594325b84 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 1478:82f43919f385 |
---|---|
1 #!/usr/bin/python | |
2 | |
3 import xml.etree.ElementTree as ET | |
4 import os # list files in directory | |
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 | |
31 | |
32 # CONFIGURATION | |
33 | |
34 # Folder where to store timelines | |
35 timeline_folder = folder_name + '/timelines/' # Stores in 'saves/timelines/' | |
36 | |
37 # Font settings | |
38 font = {'weight' : 'bold', | |
39 'size' : 16} | |
40 plt.rc('font', **font) | |
41 | |
42 # Colormap for to cycle through | |
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 | |
54 | |
55 | |
56 # CODE | |
57 | |
58 # create timeline_folder if not yet created | |
59 if not os.path.exists(timeline_folder): | |
60 os.makedirs(timeline_folder) | |
61 | |
62 # get every XML file in folder | |
63 for file in os.listdir(folder_name): | |
64 if file.endswith(".xml"): | |
65 tree = ET.parse(folder_name + '/' + file) | |
66 root = tree.getroot() | |
67 subject_id = file[:-4] # drop '.xml' | |
68 | |
69 time_offset = 0 # test starts at zero | |
70 | |
71 # ONE TIMELINE PER PAGE - make new plot per page | |
72 | |
73 # get list of all page names | |
74 for audioholder in root.findall("./audioholder"): # iterate over pages | |
75 page_name = audioholder.get('id') # get page name | |
76 plot_empty = True # check if any data is plotted | |
77 | |
78 if page_name is None: # ignore 'empty' audio_holders | |
79 break | |
80 | |
81 # SORT AUDIO ELEMENTS ALPHABETICALLY | |
82 audioelements = audioholder.findall("./audioelement") | |
83 | |
84 data = [] | |
85 for elem in audioelements: # from http://effbot.org/zone/element-sort.htm | |
86 key = elem.get("id") | |
87 data.append((key, elem)) | |
88 data.sort() | |
89 | |
90 N_audioelements = len(audioelements) # number of audio elements for this page | |
91 increment = 0 # increased for every new audioelement | |
92 audioelements_names = [] # store names of audioelements | |
93 | |
94 # get axes handle | |
95 fig = plt.figure(figsize=(fig_width, fig_height)) | |
96 ax = fig.add_subplot(111) #, aspect='equal' | |
97 | |
98 # for page [page_name], print comments related to fragment [id] | |
99 for tuple in data: | |
100 audioelement = tuple[1] | |
101 if audioelement is not None: # Check it exists | |
102 audio_id = str(audioelement.get('id')) | |
103 audioelements_names.append(audio_id) | |
104 | |
105 # for this audioelement, loop over all listen events | |
106 listen_events = audioelement.findall("./metric/metricresult/[@name='elementListenTracker']/event") | |
107 for event in listen_events: | |
108 # mark this plot as not empty | |
109 plot_empty = False | |
110 | |
111 # get testtime: start and stop | |
112 start_time = float(event.find('testtime').get('start'))-time_offset | |
113 stop_time = float(event.find('testtime').get('stop'))-time_offset | |
114 # event lines: | |
115 ax.plot([start_time, start_time], # x-values | |
116 [0, N_audioelements+1], # y-values | |
117 color='k' | |
118 ) | |
119 ax.plot([stop_time, stop_time], # x-values | |
120 [0, N_audioelements+1], # y-values | |
121 color='k' | |
122 ) | |
123 # plot time: | |
124 ax.add_patch( | |
125 patches.Rectangle( | |
126 (start_time, N_audioelements-increment-bar_height/2), # (x, y) | |
127 stop_time - start_time, # width | |
128 bar_height, # height | |
129 color=colormap[increment%len(colormap)] # colour | |
130 ) | |
131 ) | |
132 | |
133 increment+=1 # to next audioelement | |
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 | |
157 #TODO: if 'nonsensical' or unknown: dashed line until next event | |
158 #TODO: Vertical lines for fragment looping point | |
159 |