comparison python/evaluation_stats.py @ 2264:556b79c72eee

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