d@0: #!/usr/bin/python d@0: # -*- coding: utf-8 -*- d@0: d@0: import xml.etree.ElementTree as ET d@0: import os # for getting files from directory d@0: import operator # for sorting data with multiple keys d@0: d@0: # XML results files location (modify as needed): d@0: folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder d@0: d@0: # Turn number of seconds (int) to '[minutes] min [seconds] s' (string) d@0: def seconds2timestr(time_in_seconds): d@0: time_in_minutes = int(time_in_seconds/60) d@0: remaining_seconds = int(time_in_seconds%60) d@0: return str(time_in_minutes) + " min " + str(remaining_seconds) + " s" d@0: d@0: # stats initialisation d@0: number_of_XML_files = 0 d@0: number_of_pages = 0 d@0: number_of_fragments = 0 d@0: total_empty_comments = 0 d@0: time_per_page_accum = 0 d@0: d@0: # arrays initialisation d@0: page_names = [] d@0: page_count = [] d@0: duration_page = [] # duration of experiment in function of page content d@0: duration_order = [] # duration of experiment in function of page number d@0: fragments_per_page = [] # number of fragments for corresponding page d@0: d@0: # get every XML file in folder d@0: files_list = os.listdir(folder_name) d@0: for file in files_list: # iterate over all files in files_list d@0: if file.endswith(".xml"): # check if XML file d@0: number_of_XML_files += 1 d@0: tree = ET.parse(folder_name + '/' + file) d@0: root = tree.getroot() d@0: d@0: print file # print file name (subject name) d@0: d@0: # reset for new subject d@0: total_duration = 0 d@0: page_number = 0 d@0: d@0: # get list of all page names d@0: for audioholder in root.findall("./audioholder"): # iterate over pages d@0: page_name = audioholder.get('id') # get page name d@0: d@0: if page_name is None: # ignore 'empty' audio_holders d@0: print "WARNING: " + file + " contains empty audio holder. (evaluation_stats.py)" d@0: break # move on to next d@0: d@0: number_of_comments = 0 # for this page d@0: number_of_missing_comments = 0 # for this page d@0: d@0: # number of audio elements d@0: audioelements = audioholder.findall("./audioelement") # get audioelements d@0: number_of_fragments += len(audioelements) # add length of this list to total d@0: d@0: # number of comments (interesting if comments not mandatory) d@0: for audioelement in audioelements: d@0: response = audioelement.find("./comment/response") d@0: if response.text is not None and len(response.text) > 1: d@0: number_of_comments += 1 d@0: else: d@0: number_of_missing_comments += 1 d@0: d@0: total_empty_comments += number_of_missing_comments d@0: d@0: # 'testTime' keeps total duration: subtract time so far for duration of this audioholder d@0: duration = float(audioholder.find("./metric/metricresult[@id='testTime']").text) - total_duration d@0: d@0: # total duration of test d@0: total_duration += duration d@0: d@0: # print audioholder id and duration d@0: print " " + page_name + ": " + seconds2timestr(duration) + ", "\ d@0: + str(number_of_comments)+"/"\ d@0: +str(number_of_comments+number_of_missing_comments)+" comments" d@0: d@0: # keep track of duration in function of page index d@0: if len(duration_order)>page_number: d@0: duration_order[page_number].append(duration) d@0: else: d@0: duration_order.append([duration]) d@0: d@0: # keep list of audioholder ids and count how many times each audioholder id d@0: # was tested, how long it took, and how many fragments there were (if number of d@0: # fragments is different, store as different audioholder id) d@0: if page_name in page_names: d@0: page_index = page_names.index(page_name) # get index d@0: # check if number of audioelements the same d@0: if len(audioelements) == fragments_per_page[page_index]: d@0: page_count[page_index] += 1 d@0: duration_page[page_index].append(duration) d@0: else: # make new entry d@0: alt_page_name = page_name+"("+str(len(audioelements))+")" d@0: if alt_page_name in page_names: # if already there d@0: alt_page_index = page_names.index(alt_page_name) # get index d@0: page_count[alt_page_index] += 1 d@0: duration_page[alt_page_index].append(duration) d@0: else: d@0: page_names.append(alt_page_name) d@0: page_count.append(1) d@0: duration_page.append([duration]) d@0: fragments_per_page.append(len(audioelements)) d@0: else: d@0: page_names.append(page_name) d@0: page_count.append(1) d@0: duration_page.append([duration]) d@0: fragments_per_page.append(len(audioelements)) d@0: d@0: # bookkeeping d@0: page_number += 1 # increase page count for this specific test d@0: number_of_pages += 1 # increase total number of pages d@0: time_per_page_accum += duration # total duration (for average time spent per page) d@0: d@0: # print total duration of this test d@0: print " TOTAL: " + seconds2timestr(total_duration) d@0: d@0: d@0: # PRINT EVERYTHING d@0: d@0: print "Number of XML files: " + str(number_of_XML_files) d@0: print "Number of pages: " + str(number_of_pages) d@0: print "Number of fragments: " + str(number_of_fragments) d@0: print "Number of empty comments: " + str(total_empty_comments) d@0: print "Average time per page: " + seconds2timestr(time_per_page_accum/number_of_pages) d@0: page_count_strings = list(str(x) for x in page_count) d@0: count_list = page_names + page_count_strings d@0: count_list[::2] = page_names d@0: count_list[1::2] = page_count_strings d@0: print "Pages tested: " + str(count_list) d@0: d@0: # Average duration for first, second, ... page d@0: for page_number in range(len(duration_order)): d@0: print "Average duration page " + str(page_number+1) + ": " +\ d@0: seconds2timestr(sum(duration_order[page_number])/len(duration_order[page_number])) +\ d@0: " ("+str(len(duration_order[page_number]))+" subjects)" d@0: d@0: d@0: # Sort pages by number of audioelements, then by duration d@0: d@0: # average duration and number of subjects per page d@0: average_duration_page = [] d@0: number_of_subjects_page = [] d@0: for line in duration_page: d@0: number_of_subjects_page.append(len(line)) d@0: average_duration_page.append(sum(line)/len(line)) d@0: d@0: # combine and sort in function of number of audioelements and duration d@0: combined_list = [page_names, average_duration_page, fragments_per_page, number_of_subjects_page] d@0: combined_list = sorted(zip(*combined_list), key=operator.itemgetter(1, 2)) # sort d@0: d@0: # Show average duration for all songs d@0: for page_index in range(len(page_names)): d@0: print "Average duration audioholder " + combined_list[page_index][0] + ": " \ d@0: + seconds2timestr(combined_list[page_index][1]) \ d@0: + " (" + str(combined_list[page_index][3]) + " subjects, " \ d@0: + str(combined_list[page_index][2]) + " fragments)" d@0: d@0: d@0: #TODO d@0: # time per page in function of number of fragments (plot) d@0: # time per participant in function of number of pages d@0: # plot total time for each participant d@0: # plot total time d@0: # show 'count' per page (in order) d@0: d@0: # clear up page_index <> page_count <> page_number confusion