annotate scripts/evaluation_stats.py @ 789:3539d6c992e4

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