annotate scripts/evaluation_stats.py @ 264:4345ba8a1b6e

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