Mercurial > hg > webaudioevaluationtool
comparison scripts/generate_report.py @ 289:8a8e768bcbff
Scripts: Generate PDF report on set of result XML files (using LaTeX)
author | Brecht De Man <b.deman@qmul.ac.uk> |
---|---|
date | Tue, 18 Aug 2015 23:56:05 +0200 |
parents | |
children | 2c7dc66c102e |
comparison
equal
deleted
inserted
replaced
288:a00d0c8d5c74 | 289:8a8e768bcbff |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 import xml.etree.ElementTree as ET | |
5 import os # for getting files from directory | |
6 import operator # for sorting data with multiple keys | |
7 import sys # for accessing command line arguments | |
8 import subprocess # for calling pdflatex | |
9 import shlex # for calling pdflatex | |
10 import matplotlib.pyplot as plt # plots | |
11 import numpy as np # numbers | |
12 | |
13 # Command line arguments | |
14 assert len(sys.argv)<3, "evaluation_stats takes at most 1 command line argument\n"+\ | |
15 "Use: python evaluation_stats.py [results_folder]" | |
16 | |
17 # XML results files location | |
18 if len(sys.argv) == 1: | |
19 folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder | |
20 print "Use: python evaluation_stats.py [results_folder]" | |
21 print "Using default path: " + folder_name | |
22 elif len(sys.argv) == 2: | |
23 folder_name = sys.argv[1] # First command line argument is folder | |
24 | |
25 # Turn number of seconds (int) to '[minutes] min [seconds] s' (string) | |
26 def seconds2timestr(time_in_seconds): | |
27 time_in_minutes = int(time_in_seconds/60) | |
28 remaining_seconds = int(time_in_seconds%60) | |
29 return str(time_in_minutes) + " min " + str(remaining_seconds) + " s" | |
30 | |
31 # stats initialisation | |
32 number_of_XML_files = 0 | |
33 number_of_pages = 0 | |
34 number_of_fragments = 0 | |
35 total_empty_comments = 0 | |
36 total_not_played = 0 | |
37 total_not_moved = 0 | |
38 time_per_page_accum = 0 | |
39 | |
40 # arrays initialisation | |
41 page_names = [] | |
42 page_count = [] | |
43 duration_page = [] # duration of experiment in function of page content | |
44 duration_order = [] # duration of experiment in function of page number | |
45 fragments_per_page = [] # number of fragments for corresponding page | |
46 | |
47 # get username if available | |
48 for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'): | |
49 user = os.environ.get(name) | |
50 if user: | |
51 break | |
52 else: | |
53 user = '' | |
54 | |
55 | |
56 # begin LaTeX document | |
57 header = r'''\documentclass[11pt, oneside]{article} | |
58 \usepackage{geometry} | |
59 \geometry{letterpaper} | |
60 \usepackage[parfill]{parskip} | |
61 \usepackage{graphicx} | |
62 \title{Report} | |
63 \author{'''+\ | |
64 user+\ | |
65 r'''} | |
66 \graphicspath{{'''+\ | |
67 folder_name+\ | |
68 r'''/}} | |
69 \begin{document} | |
70 \maketitle | |
71 \tableofcontents | |
72 ''' | |
73 | |
74 footer = '\end{document}' | |
75 | |
76 body = '' | |
77 | |
78 # generate images for later use | |
79 subprocess.call("timeline_view_movement.py", shell=True) | |
80 | |
81 # get every XML file in folder | |
82 files_list = os.listdir(folder_name) | |
83 for file in files_list: # iterate over all files in files_list | |
84 if file.endswith(".xml"): # check if XML file | |
85 number_of_XML_files += 1 | |
86 tree = ET.parse(folder_name + '/' + file) | |
87 root = tree.getroot() | |
88 | |
89 # PRINT name as section | |
90 body+= '\section{'+file[:-4].capitalize()+'}\n' # make section header from name without extension | |
91 | |
92 # reset for new subject | |
93 total_duration = 0 | |
94 page_number = 0 | |
95 | |
96 individual_table = '' # table with stats for this individual test file | |
97 | |
98 # get list of all page names | |
99 for audioholder in root.findall("./audioholder"): # iterate over pages | |
100 page_name = audioholder.get('id') # get page name | |
101 | |
102 if page_name is None: # ignore 'empty' audio_holders | |
103 print "WARNING: " + file + " contains empty audio holder. (evaluation_stats.py)" | |
104 break # move on to next | |
105 | |
106 number_of_comments = 0 # for this page | |
107 number_of_missing_comments = 0 # for this page | |
108 not_played = 0 # for this page | |
109 not_moved = 0 # for this page | |
110 | |
111 # 'testTime' keeps total duration: subtract time so far for duration of this audioholder | |
112 duration = float(audioholder.find("./metric/metricresult[@id='testTime']").text) - total_duration | |
113 | |
114 # total duration of test | |
115 total_duration += duration | |
116 | |
117 # number of audio elements | |
118 audioelements = audioholder.findall("./audioelement") # get audioelements | |
119 number_of_fragments += len(audioelements) # add length of this list to total | |
120 | |
121 # number of comments (interesting if comments not mandatory) | |
122 for audioelement in audioelements: | |
123 response = audioelement.find("./comment/response") | |
124 was_played = audioelement.find("./metric/metricresult/[@name='elementFlagListenedTo']") | |
125 was_moved = audioelement.find("./metric/metricresult/[@name='elementFlagMoved']") | |
126 if response.text is not None and len(response.text) > 1: | |
127 number_of_comments += 1 | |
128 else: | |
129 number_of_missing_comments += 1 | |
130 if was_played is not None and was_played.text == 'false': | |
131 not_played += 1 | |
132 if was_moved is not None and was_moved.text == 'false': | |
133 not_moved += 1 | |
134 | |
135 # update global counters | |
136 total_empty_comments += number_of_missing_comments | |
137 total_not_played += not_played | |
138 total_not_moved += not_moved | |
139 | |
140 # PRINT alerts when elements not played or markers not moved | |
141 # number of audio elements not played | |
142 if not_played > 1: | |
143 body += '\\emph{\\textbf{ATTENTION: '+str(not_played)+' fragments were not listened to in '+page_name+'!}} \\\\ \n' | |
144 if not_played == 1: | |
145 body += '\\emph{\\textbf{ATTENTION: one fragment was not listened to in '+page_name+'!}} \\\\ \n ' | |
146 | |
147 # number of audio element markers not moved | |
148 if not_moved > 1: | |
149 body += '\\emph{\\textbf{ATTENTION: '+str(not_moved)+' markers were not moved in '+page_name+'!}} \\\\ \n' | |
150 if not_moved == 1: | |
151 body += '\\emph{\\textbf{ATTENTION: one marker was not moved in '+page_name+'!}} \\\\ \n' | |
152 | |
153 #TODO which one not moved/listened to? | |
154 | |
155 # PRINT song-specific statistic | |
156 individual_table += page_name+'&'+\ | |
157 str(number_of_comments) + '/' +\ | |
158 str(number_of_comments+number_of_missing_comments)+'&'+\ | |
159 seconds2timestr(duration)+'\\\\' | |
160 | |
161 # get timeline for this audioholder | |
162 img_path = 'timelines_movement/'+file[:-4]+'-'+page_name+'.pdf' | |
163 | |
164 # check if available | |
165 if os.path.isfile(folder_name+'/'+img_path): | |
166 # SHOW timeline image | |
167 body += r'''\begin{figure}[htbp] | |
168 \begin{center} | |
169 \includegraphics[width=\textwidth]{'''+\ | |
170 folder_name+'/'+img_path+\ | |
171 r'''} | |
172 \caption{Timeline of '''+\ | |
173 page_name+' by '+ file[:-4].capitalize() +\ | |
174 r'''.} | |
175 \end{center} | |
176 \end{figure} | |
177 ''' | |
178 | |
179 # keep track of duration in function of page index | |
180 if len(duration_order)>page_number: | |
181 duration_order[page_number].append(duration) | |
182 else: | |
183 duration_order.append([duration]) | |
184 | |
185 # keep list of audioholder ids and count how many times each audioholder id | |
186 # was tested, how long it took, and how many fragments there were (if number of | |
187 # fragments is different, store as different audioholder id) | |
188 if page_name in page_names: | |
189 page_index = page_names.index(page_name) # get index | |
190 # check if number of audioelements the same | |
191 if len(audioelements) == fragments_per_page[page_index]: | |
192 page_count[page_index] += 1 | |
193 duration_page[page_index].append(duration) | |
194 else: # make new entry | |
195 alt_page_name = page_name+"("+str(len(audioelements))+")" | |
196 if alt_page_name in page_names: # if already there | |
197 alt_page_index = page_names.index(alt_page_name) # get index | |
198 page_count[alt_page_index] += 1 | |
199 duration_page[alt_page_index].append(duration) | |
200 else: | |
201 page_names.append(alt_page_name) | |
202 page_count.append(1) | |
203 duration_page.append([duration]) | |
204 fragments_per_page.append(len(audioelements)) | |
205 else: | |
206 page_names.append(page_name) | |
207 page_count.append(1) | |
208 duration_page.append([duration]) | |
209 fragments_per_page.append(len(audioelements)) | |
210 | |
211 # bookkeeping | |
212 page_number += 1 # increase page count for this specific test | |
213 number_of_pages += 1 # increase total number of pages | |
214 time_per_page_accum += duration # total duration (for average time spent per page) | |
215 | |
216 # PRINT table with statistics about this test | |
217 body += r'''\begin{tabular}{|p{3.5cm}|c|p{2.5cm}|} | |
218 \hline | |
219 \textbf{Song name} & \textbf{Comments} & \textbf{Duration} \\ \hline '''+\ | |
220 individual_table+\ | |
221 r'''\hline | |
222 \textbf{TOTAL} & & \textbf{'''+\ | |
223 seconds2timestr(total_duration)+\ | |
224 r'''}\\ | |
225 \hline | |
226 \end{tabular}''' | |
227 | |
228 # join to footer | |
229 footer = body + footer | |
230 | |
231 # empty body again | |
232 body = '' | |
233 | |
234 # PRINT summary of everything (at start) | |
235 body += '\section{Summary}\n' | |
236 | |
237 # PRINT table with statistics | |
238 body += '\\begin{tabular}{ll}' | |
239 body += r'Number of XML files: &' + str(number_of_XML_files) + r'\\' | |
240 body += r'Number of pages: &' + str(number_of_pages) + r'\\' | |
241 body += r'Number of fragments: &' + str(number_of_fragments) + r'\\' | |
242 body += r'Number of empty comments: &' + str(total_empty_comments) +\ | |
243 " (" + str(round(100.0*total_empty_comments/number_of_fragments,2)) + r"\%)\\" | |
244 body += r'Number of unplayed fragments: &' + str(total_not_played) +\ | |
245 " (" + str(round(100.0*total_not_played/number_of_fragments,2)) + r"\%)\\" | |
246 body += r'Number of unmoved markers: &' + str(total_not_moved) +\ | |
247 " (" + str(round(100.0*total_not_moved/number_of_fragments,2)) + r"\%)\\" | |
248 body += r'Average time per page: &' + seconds2timestr(time_per_page_accum/number_of_pages) + r"\\" | |
249 | |
250 | |
251 # Pages and number of times tested | |
252 page_count_strings = list(str(x) for x in page_count) | |
253 count_list = page_names + page_count_strings | |
254 count_list[::2] = page_names | |
255 count_list[1::2] = page_count_strings | |
256 #body += r'Pages tested: &' + str(count_list) + r"\\" | |
257 | |
258 body += '\\end{tabular} \\vspace{1.5cm} \\\\ \n' | |
259 | |
260 # Average duration for first, second, ... page | |
261 body += " \\vspace{.5cm} Average duration per page (see also Figure \\ref{fig:avgtimeperpage}): \\\\ \n" | |
262 body += r'''\begin{tabular}{lll} | |
263 \textbf{Page} & \textbf{Duration} & \textbf{\# subjects}\\ | |
264 ''' | |
265 tpp_averages = [] # store average time per page | |
266 for page_number in range(len(duration_order)): | |
267 body += str(page_number+1) + "&" +\ | |
268 seconds2timestr(sum(duration_order[page_number])/len(duration_order[page_number])) +\ | |
269 "&"+str(len(duration_order[page_number]))+r"\\" | |
270 tpp_averages.append(sum(duration_order[page_number])/len(duration_order[page_number])) | |
271 | |
272 body += '\\end{tabular} \\vspace{1.5cm} \\\\ \n' | |
273 | |
274 # SHOW bar plot of average time per page | |
275 plt.bar(range(1,len(duration_order)+1), tpp_averages) | |
276 plt.xlabel('Page') | |
277 plt.xlim(.8, len(duration_order)+1) | |
278 plt.xticks(np.arange(1,len(duration_order)+1)+.4, range(1,len(duration_order)+1)) | |
279 plt.ylabel('Time [seconds]') | |
280 plt.savefig(folder_name+"/time_per_page.pdf", bbox_inches='tight') | |
281 plt.close() | |
282 body += r'''\begin{figure}[htbp] | |
283 \begin{center} | |
284 \includegraphics[width=\textwidth]{'''+\ | |
285 folder_name+"/time_per_page.pdf"+\ | |
286 r'''} | |
287 \caption{Average time spent per audioholder page.} | |
288 \label{fig:avgtimeperpage} | |
289 \end{center} | |
290 \end{figure} | |
291 ''' | |
292 #TODO add error bars | |
293 | |
294 | |
295 # Sort pages by number of audioelements, then by duration | |
296 | |
297 # average duration and number of subjects per page | |
298 average_duration_page = [] | |
299 number_of_subjects_page = [] | |
300 for line in duration_page: | |
301 number_of_subjects_page.append(len(line)) | |
302 average_duration_page.append(sum(line)/len(line)) | |
303 | |
304 # combine and sort in function of number of audioelements and duration | |
305 combined_list = [page_names, average_duration_page, fragments_per_page, number_of_subjects_page] | |
306 combined_list = sorted(zip(*combined_list), key=operator.itemgetter(1, 2)) # sort | |
307 | |
308 # Show average duration for all songs | |
309 body += r'''\vspace{.5cm} Average duration per audioholder: \\ | |
310 \begin{tabular}{llll} | |
311 \textbf{Audioholder} & \textbf{Duration} & \textbf{\# subjects} & \textbf{\# fragments} \\ | |
312 ''' | |
313 for page_index in range(len(page_names)): | |
314 body += combined_list[page_index][0] + "&" +\ | |
315 seconds2timestr(combined_list[page_index][1]) + "&" +\ | |
316 str(combined_list[page_index][3]) + "&" +\ | |
317 str(combined_list[page_index][2]) + r"\\" | |
318 body += '\\end{tabular}\n' | |
319 | |
320 #TODO | |
321 # time per page in function of number of fragments (plot) | |
322 # time per participant in function of number of pages | |
323 # plot total time for each participant | |
324 # plot total time | |
325 # show 'count' per page (in order) | |
326 | |
327 # clear up page_index <> page_count <> page_number confusion | |
328 | |
329 | |
330 texfile = header+body+footer | |
331 | |
332 # write TeX file | |
333 with open(folder_name + '/' + 'test.tex','w') as f: | |
334 f.write(texfile) | |
335 proc=subprocess.Popen(shlex.split('pdflatex -output-directory='+folder_name+' '+ folder_name + '/test.tex')) | |
336 proc.communicate() | |
337 # run again | |
338 proc=subprocess.Popen(shlex.split('pdflatex -output-directory='+folder_name+' '+ folder_name + '/test.tex')) | |
339 proc.communicate() | |
340 | |
341 #TODO remove auxiliary LaTeX files |