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