annotate scripts/evaluation_stats.py @ 1150:2674d80c66ff

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