Mercurial > hg > webaudioevaluationtool
comparison scripts/timeline_view_movement.py @ 2074:8252be2ba747
Scripts: timeline view of marker movements
author | Brecht De Man <b.deman@qmul.ac.uk> |
---|---|
date | Tue, 11 Aug 2015 20:49:34 +0200 |
parents | |
children | 4fb6448759a9 |
comparison
equal
deleted
inserted
replaced
2073:0a846d951bc1 | 2074:8252be2ba747 |
---|---|
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 | |
10 # COMMAND LINE ARGUMENTS | |
11 | |
12 assert len(sys.argv)<3, "timeline_view_movement takes at most 1 command line argument\n"+\ | |
13 "Use: python timeline_view_movement.py [XML_files_location]" | |
14 | |
15 # XML results files location | |
16 if len(sys.argv) == 1: | |
17 folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder | |
18 print "Use: python timeline_view_movement.py [XML_files_location]" | |
19 print "Using default path: " + folder_name | |
20 elif len(sys.argv) == 2: | |
21 folder_name = sys.argv[1] # First command line argument is folder | |
22 | |
23 # check if folder_name exists | |
24 if not os.path.exists(folder_name): | |
25 #the file is not there | |
26 print "Folder '"+folder_name+"' does not exist." | |
27 sys.exit() # terminate script execution | |
28 elif not os.access(os.path.dirname(folder_name), os.W_OK): | |
29 #the file does exist but write privileges are not given | |
30 print "No write privileges in folder '"+folder_name+"'." | |
31 | |
32 | |
33 # CONFIGURATION | |
34 | |
35 # Folder where to store timelines | |
36 timeline_folder = folder_name + '/timelines_movement/' # Stores in 'saves/timelines_movement/' by default | |
37 | |
38 # Font settings | |
39 font = {'weight' : 'bold', | |
40 'size' : 16} | |
41 plt.rc('font', **font) | |
42 | |
43 # Colormap for to cycle through | |
44 colormap = ['b', 'g', 'c', 'm', 'y', 'k'] | |
45 | |
46 # figure size | |
47 fig_width = 25 | |
48 fig_height = 10 | |
49 | |
50 | |
51 # CODE | |
52 | |
53 # create timeline_folder if not yet created | |
54 if not os.path.exists(timeline_folder): | |
55 os.makedirs(timeline_folder) | |
56 | |
57 # get every XML file in folder | |
58 for file in os.listdir(folder_name): | |
59 if file.endswith(".xml"): | |
60 tree = ET.parse(folder_name + '/' + file) | |
61 root = tree.getroot() | |
62 subject_id = file[:-4] # drop '.xml' | |
63 | |
64 previous_audioholder_time = 0 # time spent before current audioholder | |
65 time_offset = 0 # test starts at zero | |
66 | |
67 # ONE TIMELINE PER PAGE - make new plot per page | |
68 | |
69 # get list of all page names | |
70 for audioholder in root.findall("./audioholder"): # iterate over pages | |
71 page_name = audioholder.get('id') # get page name | |
72 | |
73 if page_name is None: # ignore 'empty' audio_holders | |
74 print "Skipping empty audioholder name from "+subject_id+"." | |
75 break | |
76 | |
77 # subtract total audioholder length from subsequent audioholder event times | |
78 audioholder_time_temp = audioholder.find("./metric/metricresult/[@id='testTime']") | |
79 if audioholder_time_temp is not None: | |
80 audioholder_time = float(audioholder_time_temp.text) | |
81 else: | |
82 print "Skipping audioholder without total time specified from "+subject_id+"." | |
83 break | |
84 | |
85 # get audioelements | |
86 audioelements = audioholder.findall("./audioelement") | |
87 | |
88 # sort alphabetically | |
89 data = [] | |
90 for elem in audioelements: # from http://effbot.org/zone/element-sort.htm | |
91 key = elem.get("id") | |
92 data.append((key, elem)) | |
93 data.sort() | |
94 | |
95 N_audioelements = len(audioelements) # number of audio elements for this page | |
96 increment = 0 # increased for every new audioelement | |
97 | |
98 # get axes handle | |
99 fig = plt.figure(figsize=(fig_width, fig_height)) | |
100 ax = fig.add_subplot(111) | |
101 | |
102 # for page [page_name], print comments related to fragment [id] | |
103 #for tuple in data: | |
104 # audioelement = tuple[1] | |
105 for tuple in data: | |
106 audioelement = tuple[1] | |
107 if audioelement is not None: # Check it exists | |
108 audio_id = str(audioelement.get('id')) | |
109 | |
110 # break if no initial position or move events registered | |
111 initial_position_temp = audioelement.find("./metric/metricresult/[@name='elementInitialPosition']") | |
112 if initial_position_temp is None: | |
113 print "Skipping "+page_name+" from "+subject_id+": does not have initial positions specified." | |
114 break | |
115 | |
116 # for this audioelement, loop over all move events | |
117 initial_position = float(initial_position_temp.text) | |
118 move_events = audioelement.findall("./metric/metricresult/[@name='elementTrackerFull']/timepos") | |
119 final_position = float(audioelement.find("./value").text) | |
120 | |
121 # display fragment name at start | |
122 plt.text(0,initial_position+0.02,audio_id,color=colormap[increment%len(colormap)]) #,rotation=45 | |
123 | |
124 # previous position and time | |
125 previous_position = initial_position | |
126 previous_time = 0 | |
127 | |
128 # draw all segments except final one | |
129 for event in move_events: | |
130 new_time = float(event.find("./time").text)-time_offset | |
131 new_position = float(event.find("./position").text) | |
132 # horizontal line from previous to current time | |
133 plt.plot([previous_time, new_time], # x-values | |
134 [previous_position, previous_position], # y-values | |
135 color=colormap[increment%len(colormap)], | |
136 linewidth=3 | |
137 ) | |
138 # vertical line from previous to current position | |
139 plt.plot([new_time, new_time], # x-values | |
140 [previous_position, new_position], # y-values | |
141 color=colormap[increment%len(colormap)], | |
142 linewidth=3 | |
143 ) | |
144 | |
145 # update previous_position value | |
146 previous_position = new_position | |
147 previous_time = new_time | |
148 | |
149 # draw final segment | |
150 # horizontal line from previous time to end of audioholder | |
151 plt.plot([previous_time, audioholder_time-time_offset], # x-values | |
152 [previous_position, previous_position], # y-values | |
153 color=colormap[increment%len(colormap)], | |
154 linewidth=3 | |
155 ) | |
156 | |
157 # display fragment name at end | |
158 plt.text(audioholder_time-time_offset,previous_position,\ | |
159 audio_id,color=colormap[increment%len(colormap)]) #,rotation=45 | |
160 | |
161 # for this audioelement, loop over all listen events | |
162 # listen_events = audioelement.findall("./metric/metricresult/[@name='elementListenTracker']/event") | |
163 # for event in listen_events: | |
164 # # get testtime: start and stop | |
165 # start_time = float(event.find('testtime').get('start')) | |
166 # stop_time = float(event.find('testtime').get('stop')) | |
167 | |
168 | |
169 increment+=1 # to next audioelement | |
170 | |
171 last_audioholder_duration = audioholder_time-time_offset | |
172 time_offset = audioholder_time | |
173 | |
174 | |
175 # set plot parameters | |
176 plt.title('Timeline ' + file + ": "+page_name) | |
177 plt.xlabel('Time [seconds]') | |
178 plt.xlim(0, last_audioholder_duration) | |
179 plt.ylabel('Rating') # default | |
180 plt.ylim(0, 1) # rating between 0 and 1 | |
181 | |
182 #y-ticks: labels on rating axis | |
183 label_positions = [] | |
184 label_text = [] | |
185 scale_tags = root.findall("./BrowserEvalProjectDocument/audioHolder/interface/scale") | |
186 scale_title = root.find("./BrowserEvalProjectDocument/audioHolder/interface/title") | |
187 for tag in scale_tags: | |
188 label_positions.append(float(tag.get('position'))/100) # on a scale from 0 to 100 | |
189 label_text.append(tag.text) | |
190 if len(label_positions) > 0: | |
191 plt.yticks(label_positions, label_text) # show rating axis labels | |
192 # set label Y-axis | |
193 if scale_title is not None: | |
194 plt.ylabel(scale_title.text) | |
195 | |
196 #plt.show() # uncomment to show plot; comment when just saving | |
197 #exit() | |
198 | |
199 plt.savefig(timeline_folder+subject_id+"-"+page_name+".pdf", bbox_inches='tight') | |
200 plt.close() | |
201 |