annotate scripts/evaluation_stats.py @ 2066:a13e5cf40feb

Scripts: evaluation_stats shows number of total fragments
author Brecht De Man <b.deman@qmul.ac.uk>
date Tue, 14 Jul 2015 19:40:58 +0100
parents 416fc71e0374
children edc386c86c86
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@2057 5 import os
b@2057 6
b@2057 7 # XML results files location (modify as needed):
b@2066 8 folder_name = "xml_with_statements" # Looks in 'saves/' folder from 'scripts/' folder
b@2057 9
b@2057 10 # Turn number of seconds (int) to '[minutes] min [seconds] s' (string)
b@2057 11 def seconds2timestr(time_in_seconds):
b@2057 12 time_in_minutes = int(time_in_seconds/60)
b@2057 13 remaining_seconds = int(time_in_seconds%60)
b@2057 14 return str(time_in_minutes) + " min " + str(remaining_seconds) + " s"
b@2057 15
b@2057 16 # stats initialisation
b@2057 17 number_of_XML_files = 0
b@2057 18 number_of_pages = 0
b@2066 19 number_of_fragments = 0
b@2057 20 time_per_page_accum = 0
b@2057 21
b@2057 22 # arrays initialisation
b@2057 23 page_names = []
b@2057 24 page_count = []
b@2057 25 duration_page = [] # duration of experiment in function of page
b@2057 26 duration_subject = []
b@2057 27 duration_order = []
b@2057 28
b@2057 29 # get every XML file in folder
b@2057 30 files_list = os.listdir(folder_name)
b@2057 31 for file in files_list: # iterate over all files in files_list
b@2057 32 if file.endswith(".xml"): # check if XML file
b@2057 33 number_of_XML_files += 1
b@2057 34 tree = ET.parse(folder_name + '/' + file)
b@2057 35 root = tree.getroot()
b@2057 36
b@2057 37 print file # print file name (subject name)
b@2057 38
b@2057 39 # reset for new subject
b@2057 40 total_duration = 0
b@2057 41 page_number = 0
b@2057 42
b@2057 43 # get list of all page names
b@2057 44 for audioholder in root.findall("./audioholder"): # iterate over pages
b@2057 45 page_name = audioholder.get('id') # get page name
b@2057 46
b@2057 47 if page_name is None: # ignore 'empty' audio_holders
b@2057 48 print "WARNING: " + file + " contains empty audio holder. (evaluation_stats.py)"
b@2057 49 break # move on to next
b@2066 50
b@2057 51 # keep list of audioholder ids and count how many times each audioholder id was tested
b@2057 52 if page_name in page_names:
b@2057 53 page_index = page_names.index(page_name) # get index
b@2057 54 page_count[page_index] += 1
b@2066 55 else:
b@2057 56 page_names.append(page_name)
b@2057 57 page_count.append(1)
b@2057 58
b@2057 59 # 'testTime' keeps total duration: subtract time so far for duration of this audioholder
b@2057 60 duration = float(audioholder.find("./metric/metricresult[@id='testTime']").text) - total_duration
b@2057 61
b@2057 62 # total duration of test
b@2057 63 total_duration += duration
b@2057 64
b@2057 65 # print audioholder id and duration
b@2057 66 print " " + page_name + ": " + seconds2timestr(duration)
b@2057 67
b@2057 68 # keep track of duration in function of page index
b@2057 69 if len(duration_order)>page_number:
b@2057 70 duration_order[page_number].append(duration)
b@2057 71 else:
b@2057 72 duration_order.append([duration])
b@2066 73
b@2066 74 # number of audio elements
b@2066 75 audioelements = audioholder.findall("./audioelement") # get audioelements
b@2066 76 number_of_fragments += len(audioelements) # add length of this list to total
b@2057 77
b@2057 78 page_number += 1 # increase page count for this specific test
b@2057 79 number_of_pages += 1 # increase total number of pages
b@2057 80 time_per_page_accum += duration # total duration (for average time spent per page)
b@2066 81
b@2066 82 # print total duration of this test
b@2066 83 print " TOTAL: " + seconds2timestr(total_duration)
b@2066 84
b@2066 85
b@2057 86 # PRINT EVERYTHING
b@2057 87
b@2057 88 print "Number of XML files: " + str(number_of_XML_files)
b@2057 89 print "Number of pages: " + str(number_of_pages)
b@2066 90 print "Number of fragments: " + str(number_of_fragments)
b@2057 91 print "Average time per page: " + seconds2timestr(time_per_page_accum/number_of_pages)
b@2057 92 page_count_strings = list(str(x) for x in page_count)
b@2057 93 count_list = page_names + page_count_strings
b@2057 94 count_list[::2] = page_names
b@2057 95 count_list[1::2] = page_count_strings
b@2057 96 print "Pages tested: " + str(count_list)
b@2057 97
b@2057 98 # Average duration for first, second, ... page
b@2057 99 for page_number in range(len(duration_order)): #TODO make maximum page number automatic
b@2057 100 print "Average duration page " + str(page_number+1) + ": " +\
b@2066 101 seconds2timestr(sum(duration_order[page_number])/len(duration_order[page_number])) +\
b@2066 102 " ("+str(len(duration_order[page_number]))+" subjects)"
b@2066 103
b@2057 104
b@2057 105 #TODO
b@2057 106 # time per page in function of number of fragments (plot)
b@2057 107 # time per participant in function of number of pages
b@2057 108 # plot total time for each participant
b@2066 109 # plot total time
b@2057 110 # show 'count' per page (in order)
b@2057 111
b@2057 112 # clear up page_index <> page_count <> page_number confusion