annotate scripts/evaluation_stats.py @ 1350:b6389ceaeaa5

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