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