annotate scripts/evaluation_stats.py @ 1528:766bff1a8f73

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