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