annotate scripts/evaluation_stats.py @ 2248:9c7f09d3364d

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