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