annotate scripts/evaluation_stats.py @ 1944:ac2380d3f0cb

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