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