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