annotate scripts/evaluation_stats.py @ 263:ef2d63555702

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