comparison Perceptual Evaluation/webaudioevaluationtool/scripts/evaluation_stats.py @ 0:55c282f01a30 tip

Adding files to Repo. Initial Commit
author Dave <d.j.moffat@qmul.ac.uk>
date Fri, 16 Oct 2015 18:04:00 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:55c282f01a30
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
8 # XML results files location (modify as needed):
9 folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder
10
11 # Turn number of seconds (int) to '[minutes] min [seconds] s' (string)
12 def seconds2timestr(time_in_seconds):
13 time_in_minutes = int(time_in_seconds/60)
14 remaining_seconds = int(time_in_seconds%60)
15 return str(time_in_minutes) + " min " + str(remaining_seconds) + " s"
16
17 # stats initialisation
18 number_of_XML_files = 0
19 number_of_pages = 0
20 number_of_fragments = 0
21 total_empty_comments = 0
22 time_per_page_accum = 0
23
24 # arrays initialisation
25 page_names = []
26 page_count = []
27 duration_page = [] # duration of experiment in function of page content
28 duration_order = [] # duration of experiment in function of page number
29 fragments_per_page = [] # number of fragments for corresponding page
30
31 # get every XML file in folder
32 files_list = os.listdir(folder_name)
33 for file in files_list: # iterate over all files in files_list
34 if file.endswith(".xml"): # check if XML file
35 number_of_XML_files += 1
36 tree = ET.parse(folder_name + '/' + file)
37 root = tree.getroot()
38
39 print file # print file name (subject name)
40
41 # reset for new subject
42 total_duration = 0
43 page_number = 0
44
45 # get list of all page names
46 for audioholder in root.findall("./audioholder"): # iterate over pages
47 page_name = audioholder.get('id') # get page name
48
49 if page_name is None: # ignore 'empty' audio_holders
50 print "WARNING: " + file + " contains empty audio holder. (evaluation_stats.py)"
51 break # move on to next
52
53 number_of_comments = 0 # for this page
54 number_of_missing_comments = 0 # for this page
55
56 # number of audio elements
57 audioelements = audioholder.findall("./audioelement") # get audioelements
58 number_of_fragments += len(audioelements) # add length of this list to total
59
60 # number of comments (interesting if comments not mandatory)
61 for audioelement in audioelements:
62 response = audioelement.find("./comment/response")
63 if response.text is not None and len(response.text) > 1:
64 number_of_comments += 1
65 else:
66 number_of_missing_comments += 1
67
68 total_empty_comments += number_of_missing_comments
69
70 # 'testTime' keeps total duration: subtract time so far for duration of this audioholder
71 duration = float(audioholder.find("./metric/metricresult[@id='testTime']").text) - total_duration
72
73 # total duration of test
74 total_duration += duration
75
76 # print audioholder id and duration
77 print " " + page_name + ": " + seconds2timestr(duration) + ", "\
78 + str(number_of_comments)+"/"\
79 +str(number_of_comments+number_of_missing_comments)+" comments"
80
81 # keep track of duration in function of page index
82 if len(duration_order)>page_number:
83 duration_order[page_number].append(duration)
84 else:
85 duration_order.append([duration])
86
87 # keep list of audioholder ids and count how many times each audioholder id
88 # was tested, how long it took, and how many fragments there were (if number of
89 # fragments is different, store as different audioholder id)
90 if page_name in page_names:
91 page_index = page_names.index(page_name) # get index
92 # check if number of audioelements the same
93 if len(audioelements) == fragments_per_page[page_index]:
94 page_count[page_index] += 1
95 duration_page[page_index].append(duration)
96 else: # make new entry
97 alt_page_name = page_name+"("+str(len(audioelements))+")"
98 if alt_page_name in page_names: # if already there
99 alt_page_index = page_names.index(alt_page_name) # get index
100 page_count[alt_page_index] += 1
101 duration_page[alt_page_index].append(duration)
102 else:
103 page_names.append(alt_page_name)
104 page_count.append(1)
105 duration_page.append([duration])
106 fragments_per_page.append(len(audioelements))
107 else:
108 page_names.append(page_name)
109 page_count.append(1)
110 duration_page.append([duration])
111 fragments_per_page.append(len(audioelements))
112
113 # bookkeeping
114 page_number += 1 # increase page count for this specific test
115 number_of_pages += 1 # increase total number of pages
116 time_per_page_accum += duration # total duration (for average time spent per page)
117
118 # print total duration of this test
119 print " TOTAL: " + seconds2timestr(total_duration)
120
121
122 # PRINT EVERYTHING
123
124 print "Number of XML files: " + str(number_of_XML_files)
125 print "Number of pages: " + str(number_of_pages)
126 print "Number of fragments: " + str(number_of_fragments)
127 print "Number of empty comments: " + str(total_empty_comments)
128 print "Average time per page: " + seconds2timestr(time_per_page_accum/number_of_pages)
129 page_count_strings = list(str(x) for x in page_count)
130 count_list = page_names + page_count_strings
131 count_list[::2] = page_names
132 count_list[1::2] = page_count_strings
133 print "Pages tested: " + str(count_list)
134
135 # Average duration for first, second, ... page
136 for page_number in range(len(duration_order)):
137 print "Average duration page " + str(page_number+1) + ": " +\
138 seconds2timestr(sum(duration_order[page_number])/len(duration_order[page_number])) +\
139 " ("+str(len(duration_order[page_number]))+" subjects)"
140
141
142 # Sort pages by number of audioelements, then by duration
143
144 # average duration and number of subjects per page
145 average_duration_page = []
146 number_of_subjects_page = []
147 for line in duration_page:
148 number_of_subjects_page.append(len(line))
149 average_duration_page.append(sum(line)/len(line))
150
151 # combine and sort in function of number of audioelements and duration
152 combined_list = [page_names, average_duration_page, fragments_per_page, number_of_subjects_page]
153 combined_list = sorted(zip(*combined_list), key=operator.itemgetter(1, 2)) # sort
154
155 # Show average duration for all songs
156 for page_index in range(len(page_names)):
157 print "Average duration audioholder " + combined_list[page_index][0] + ": " \
158 + seconds2timestr(combined_list[page_index][1]) \
159 + " (" + str(combined_list[page_index][3]) + " subjects, " \
160 + str(combined_list[page_index][2]) + " fragments)"
161
162
163 #TODO
164 # time per page in function of number of fragments (plot)
165 # time per participant in function of number of pages
166 # plot total time for each participant
167 # plot total time
168 # show 'count' per page (in order)
169
170 # clear up page_index <> page_count <> page_number confusion