annotate scripts/evaluation_stats.py @ 2068:e943dc4d3d6e

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