annotate scripts/evaluation_stats.py @ 1453:04e8a9c07c7e

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