b@251
|
1 #!/usr/bin/python
|
b@251
|
2 # -*- coding: utf-8 -*-
|
b@251
|
3
|
b@251
|
4 import xml.etree.ElementTree as ET
|
b@263
|
5 import os # for getting files from directory
|
b@263
|
6 import operator # for sorting data with multiple keys
|
b@264
|
7 import sys # for accessing command line arguments
|
b@251
|
8
|
b@264
|
9 # Command line arguments
|
b@264
|
10 assert len(sys.argv)<3, "evaluation_stats takes at most 1 command line argument\n"+\
|
b@264
|
11 "Use: python evaluation_stats.py [results_folder]"
|
b@264
|
12
|
b@264
|
13 # XML results files location
|
b@264
|
14 if len(sys.argv) == 1:
|
b@264
|
15 folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder
|
b@264
|
16 print "Use: python evaluation_stats.py [results_folder]"
|
b@264
|
17 print "Using default path: " + folder_name
|
b@264
|
18 elif len(sys.argv) == 2:
|
b@264
|
19 folder_name = sys.argv[1] # First command line argument is folder
|
b@251
|
20
|
b@251
|
21 # Turn number of seconds (int) to '[minutes] min [seconds] s' (string)
|
b@251
|
22 def seconds2timestr(time_in_seconds):
|
b@251
|
23 time_in_minutes = int(time_in_seconds/60)
|
b@251
|
24 remaining_seconds = int(time_in_seconds%60)
|
b@251
|
25 return str(time_in_minutes) + " min " + str(remaining_seconds) + " s"
|
b@251
|
26
|
b@251
|
27 # stats initialisation
|
b@263
|
28 number_of_XML_files = 0
|
b@263
|
29 number_of_pages = 0
|
b@263
|
30 number_of_fragments = 0
|
b@263
|
31 total_empty_comments = 0
|
b@283
|
32 total_not_played = 0
|
b@283
|
33 total_not_moved = 0
|
b@263
|
34 time_per_page_accum = 0
|
b@251
|
35
|
b@251
|
36 # arrays initialisation
|
b@263
|
37 page_names = []
|
b@263
|
38 page_count = []
|
b@263
|
39 duration_page = [] # duration of experiment in function of page content
|
b@263
|
40 duration_order = [] # duration of experiment in function of page number
|
b@263
|
41 fragments_per_page = [] # number of fragments for corresponding page
|
b@251
|
42
|
b@251
|
43 # get every XML file in folder
|
b@251
|
44 files_list = os.listdir(folder_name)
|
b@251
|
45 for file in files_list: # iterate over all files in files_list
|
b@251
|
46 if file.endswith(".xml"): # check if XML file
|
b@251
|
47 number_of_XML_files += 1
|
b@251
|
48 tree = ET.parse(folder_name + '/' + file)
|
b@251
|
49 root = tree.getroot()
|
b@251
|
50
|
b@251
|
51 print file # print file name (subject name)
|
b@251
|
52
|
b@251
|
53 # reset for new subject
|
b@251
|
54 total_duration = 0
|
b@251
|
55 page_number = 0
|
b@251
|
56
|
b@251
|
57 # get list of all page names
|
giuliomoro@549
|
58 for audioholder in root.findall("./page"): # iterate over pages
|
b@251
|
59 page_name = audioholder.get('id') # get page name
|
b@251
|
60
|
b@251
|
61 if page_name is None: # ignore 'empty' audio_holders
|
b@251
|
62 print "WARNING: " + file + " contains empty audio holder. (evaluation_stats.py)"
|
b@251
|
63 break # move on to next
|
b@261
|
64
|
b@263
|
65 number_of_comments = 0 # for this page
|
b@263
|
66 number_of_missing_comments = 0 # for this page
|
b@283
|
67 not_played = 0 # for this page
|
b@283
|
68 not_moved = 0 # for this page
|
b@283
|
69
|
b@283
|
70 # 'testTime' keeps total duration: subtract time so far for duration of this audioholder
|
b@283
|
71 duration = float(audioholder.find("./metric/metricresult[@id='testTime']").text) - total_duration
|
b@283
|
72
|
b@283
|
73 # total duration of test
|
b@283
|
74 total_duration += duration
|
b@263
|
75
|
b@263
|
76 # number of audio elements
|
b@263
|
77 audioelements = audioholder.findall("./audioelement") # get audioelements
|
b@263
|
78 number_of_fragments += len(audioelements) # add length of this list to total
|
b@263
|
79
|
b@263
|
80 # number of comments (interesting if comments not mandatory)
|
b@263
|
81 for audioelement in audioelements:
|
b@263
|
82 response = audioelement.find("./comment/response")
|
b@283
|
83 was_played = audioelement.find("./metric/metricresult/[@name='elementFlagListenedTo']")
|
b@283
|
84 was_moved = audioelement.find("./metric/metricresult/[@name='elementFlagMoved']")
|
b@263
|
85 if response.text is not None and len(response.text) > 1:
|
b@263
|
86 number_of_comments += 1
|
b@263
|
87 else:
|
b@263
|
88 number_of_missing_comments += 1
|
b@283
|
89 if was_played is not None and was_played.text == 'false':
|
b@283
|
90 not_played += 1
|
b@283
|
91 if was_moved is not None and was_moved.text == 'false':
|
b@283
|
92 not_moved += 1
|
b@283
|
93
|
b@283
|
94 # update global counters
|
b@263
|
95 total_empty_comments += number_of_missing_comments
|
b@283
|
96 total_not_played += not_played
|
b@283
|
97 total_not_moved += not_moved
|
b@251
|
98
|
b@251
|
99 # print audioholder id and duration
|
b@263
|
100 print " " + page_name + ": " + seconds2timestr(duration) + ", "\
|
b@263
|
101 + str(number_of_comments)+"/"\
|
b@263
|
102 +str(number_of_comments+number_of_missing_comments)+" comments"
|
b@251
|
103
|
b@283
|
104 # number of audio elements not played
|
b@283
|
105 if not_played > 1:
|
b@283
|
106 print 'ATTENTION: '+str(not_played)+' fragments were not listened to!'
|
b@283
|
107 if not_played == 1:
|
b@283
|
108 print 'ATTENTION: one fragment was not listened to!'
|
b@283
|
109
|
b@283
|
110 # number of audio element markers not moved
|
b@283
|
111 if not_moved > 1:
|
b@283
|
112 print 'ATTENTION: '+str(not_moved)+' markers were not moved!'
|
b@283
|
113 if not_moved == 1:
|
b@283
|
114 print 'ATTENTION: one marker was not moved!'
|
b@283
|
115
|
b@251
|
116 # keep track of duration in function of page index
|
b@251
|
117 if len(duration_order)>page_number:
|
b@251
|
118 duration_order[page_number].append(duration)
|
b@251
|
119 else:
|
b@251
|
120 duration_order.append([duration])
|
b@263
|
121
|
b@263
|
122 # keep list of audioholder ids and count how many times each audioholder id
|
b@263
|
123 # was tested, how long it took, and how many fragments there were (if number of
|
b@263
|
124 # fragments is different, store as different audioholder id)
|
b@263
|
125 if page_name in page_names:
|
b@263
|
126 page_index = page_names.index(page_name) # get index
|
b@263
|
127 # check if number of audioelements the same
|
b@263
|
128 if len(audioelements) == fragments_per_page[page_index]:
|
b@263
|
129 page_count[page_index] += 1
|
b@263
|
130 duration_page[page_index].append(duration)
|
b@263
|
131 else: # make new entry
|
b@263
|
132 alt_page_name = page_name+"("+str(len(audioelements))+")"
|
b@263
|
133 if alt_page_name in page_names: # if already there
|
b@263
|
134 alt_page_index = page_names.index(alt_page_name) # get index
|
b@263
|
135 page_count[alt_page_index] += 1
|
b@263
|
136 duration_page[alt_page_index].append(duration)
|
b@263
|
137 else:
|
b@263
|
138 page_names.append(alt_page_name)
|
b@263
|
139 page_count.append(1)
|
b@263
|
140 duration_page.append([duration])
|
b@263
|
141 fragments_per_page.append(len(audioelements))
|
b@263
|
142 else:
|
b@263
|
143 page_names.append(page_name)
|
b@263
|
144 page_count.append(1)
|
b@263
|
145 duration_page.append([duration])
|
b@263
|
146 fragments_per_page.append(len(audioelements))
|
b@263
|
147
|
b@263
|
148 # bookkeeping
|
b@251
|
149 page_number += 1 # increase page count for this specific test
|
b@251
|
150 number_of_pages += 1 # increase total number of pages
|
b@251
|
151 time_per_page_accum += duration # total duration (for average time spent per page)
|
b@261
|
152
|
b@262
|
153 # print total duration of this test
|
b@262
|
154 print " TOTAL: " + seconds2timestr(total_duration)
|
b@261
|
155
|
b@261
|
156
|
b@251
|
157 # PRINT EVERYTHING
|
b@251
|
158
|
b@251
|
159 print "Number of XML files: " + str(number_of_XML_files)
|
b@251
|
160 print "Number of pages: " + str(number_of_pages)
|
b@261
|
161 print "Number of fragments: " + str(number_of_fragments)
|
b@283
|
162 print "Number of empty comments: " + str(total_empty_comments) +\
|
b@283
|
163 " (" + str(round(100.0*total_empty_comments/number_of_fragments,2)) + "%)"
|
b@283
|
164 print "Number of unplayed fragments: " + str(total_not_played) +\
|
b@283
|
165 " (" + str(round(100.0*total_not_played/number_of_fragments,2)) + "%)"
|
b@283
|
166 print "Number of unmoved markers: " + str(total_not_moved) +\
|
b@283
|
167 " (" + str(round(100.0*total_not_moved/number_of_fragments,2)) + "%)"
|
b@251
|
168 print "Average time per page: " + seconds2timestr(time_per_page_accum/number_of_pages)
|
b@264
|
169
|
b@264
|
170 # Pages and number of times tested
|
b@251
|
171 page_count_strings = list(str(x) for x in page_count)
|
b@251
|
172 count_list = page_names + page_count_strings
|
b@251
|
173 count_list[::2] = page_names
|
b@251
|
174 count_list[1::2] = page_count_strings
|
b@251
|
175 print "Pages tested: " + str(count_list)
|
b@251
|
176
|
b@251
|
177 # Average duration for first, second, ... page
|
b@283
|
178 print "Average duration per page:"
|
b@263
|
179 for page_number in range(len(duration_order)):
|
b@283
|
180 print " page " + str(page_number+1) + ": " +\
|
b@261
|
181 seconds2timestr(sum(duration_order[page_number])/len(duration_order[page_number])) +\
|
b@261
|
182 " ("+str(len(duration_order[page_number]))+" subjects)"
|
b@261
|
183
|
b@251
|
184
|
b@263
|
185 # Sort pages by number of audioelements, then by duration
|
b@263
|
186
|
b@263
|
187 # average duration and number of subjects per page
|
b@263
|
188 average_duration_page = []
|
b@263
|
189 number_of_subjects_page = []
|
b@263
|
190 for line in duration_page:
|
b@263
|
191 number_of_subjects_page.append(len(line))
|
b@263
|
192 average_duration_page.append(sum(line)/len(line))
|
b@263
|
193
|
b@263
|
194 # combine and sort in function of number of audioelements and duration
|
b@263
|
195 combined_list = [page_names, average_duration_page, fragments_per_page, number_of_subjects_page]
|
b@263
|
196 combined_list = sorted(zip(*combined_list), key=operator.itemgetter(1, 2)) # sort
|
b@263
|
197
|
b@263
|
198 # Show average duration for all songs
|
b@283
|
199 print "Average duration per audioholder:"
|
b@263
|
200 for page_index in range(len(page_names)):
|
b@283
|
201 print " "+combined_list[page_index][0] + ": " \
|
b@263
|
202 + seconds2timestr(combined_list[page_index][1]) \
|
b@263
|
203 + " (" + str(combined_list[page_index][3]) + " subjects, " \
|
b@263
|
204 + str(combined_list[page_index][2]) + " fragments)"
|
b@263
|
205
|
b@263
|
206
|
b@251
|
207 #TODO
|
b@251
|
208 # time per page in function of number of fragments (plot)
|
b@251
|
209 # time per participant in function of number of pages
|
b@251
|
210 # plot total time for each participant
|
b@261
|
211 # plot total time
|
b@251
|
212 # show 'count' per page (in order)
|
b@251
|
213
|
b@251
|
214 # clear up page_index <> page_count <> page_number confusion
|
b@283
|
215
|
b@283
|
216 # LaTeX -> PDF print out
|