b@1481: #!/usr/bin/python b@1481: b@1481: import xml.etree.ElementTree as ET b@1481: import os # list files in directory b@1481: import sys # command line arguments b@1481: import matplotlib.pyplot as plt # plots b@1481: import matplotlib.patches as patches # rectangles b@1481: b@1481: b@1481: # COMMAND LINE ARGUMENTS b@1481: b@1481: assert len(sys.argv)<3, "timeline_view_movement takes at most 1 command line argument\n"+\ b@1481: "Use: python timeline_view_movement.py [XML_files_location]" b@1481: b@1481: # XML results files location b@1481: if len(sys.argv) == 1: b@1481: folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder b@1481: print "Use: python timeline_view_movement.py [XML_files_location]" b@1481: print "Using default path: " + folder_name b@1481: elif len(sys.argv) == 2: b@1481: folder_name = sys.argv[1] # First command line argument is folder b@1481: b@1481: # check if folder_name exists b@1481: if not os.path.exists(folder_name): b@1481: #the file is not there b@1481: print "Folder '"+folder_name+"' does not exist." b@1481: sys.exit() # terminate script execution b@1481: elif not os.access(os.path.dirname(folder_name), os.W_OK): b@1481: #the file does exist but write privileges are not given b@1481: print "No write privileges in folder '"+folder_name+"'." b@1481: b@1481: b@1481: # CONFIGURATION b@1481: b@1481: # Folder where to store timelines b@1481: timeline_folder = folder_name + '/timelines_movement/' # Stores in 'saves/timelines_movement/' by default b@1481: b@1481: # Font settings b@1481: font = {'weight' : 'bold', b@1481: 'size' : 16} b@1481: plt.rc('font', **font) b@1481: b@1481: # Colormap for to cycle through b@1481: colormap = ['b', 'g', 'c', 'm', 'y', 'k'] b@1481: b@1481: # figure size b@1481: fig_width = 25 b@1481: fig_height = 10 b@1481: b@1481: b@1481: # CODE b@1481: b@1481: # create timeline_folder if not yet created b@1481: if not os.path.exists(timeline_folder): b@1481: os.makedirs(timeline_folder) b@1481: b@1481: # get every XML file in folder b@1481: for file in os.listdir(folder_name): b@1481: if file.endswith(".xml"): b@1481: tree = ET.parse(folder_name + '/' + file) b@1481: root = tree.getroot() b@1481: subject_id = file[:-4] # drop '.xml' b@1481: b@1481: previous_audioholder_time = 0 # time spent before current audioholder b@1481: time_offset = 0 # test starts at zero b@1481: b@1481: # ONE TIMELINE PER PAGE - make new plot per page b@1481: b@1481: # get list of all page names b@1481: for audioholder in root.findall("./audioholder"): # iterate over pages b@1481: page_name = audioholder.get('id') # get page name b@1481: plot_empty = True # check if any data is plotted b@1481: b@1481: if page_name is None: # ignore 'empty' audio_holders b@1481: print "Skipping empty audioholder name from "+subject_id+"." b@1481: break b@1481: b@1481: # subtract total audioholder length from subsequent audioholder event times b@1481: audioholder_time_temp = audioholder.find("./metric/metricresult/[@id='testTime']") b@1481: if audioholder_time_temp is not None: b@1481: audioholder_time = float(audioholder_time_temp.text) b@1481: else: b@1481: print "Skipping audioholder without total time specified from "+subject_id+"." b@1481: break b@1481: b@1481: # get audioelements b@1481: audioelements = audioholder.findall("./audioelement") b@1481: b@1481: # sort alphabetically b@1481: data = [] b@1481: for elem in audioelements: # from http://effbot.org/zone/element-sort.htm b@1481: key = elem.get("id") b@1481: data.append((key, elem)) b@1481: data.sort() b@1481: b@1481: N_audioelements = len(audioelements) # number of audio elements for this page b@1481: increment = 0 # increased for every new audioelement b@1481: b@1481: # get axes handle b@1481: fig = plt.figure(figsize=(fig_width, fig_height)) b@1481: ax = fig.add_subplot(111) b@1481: b@1481: # for page [page_name], print comments related to fragment [id] b@1481: #for tuple in data: b@1481: # audioelement = tuple[1] b@1481: for tuple in data: b@1481: audioelement = tuple[1] b@1481: if audioelement is not None: # Check it exists b@1481: audio_id = str(audioelement.get('id')) b@1481: b@1481: # break if no initial position or move events registered b@1481: initial_position_temp = audioelement.find("./metric/metricresult/[@name='elementInitialPosition']") b@1481: if initial_position_temp is None: b@1481: print "Skipping "+page_name+" from "+subject_id+": does not have initial positions specified." b@1481: break b@1481: b@1481: # get move events, initial and eventual position b@1481: initial_position = float(initial_position_temp.text) b@1481: move_events = audioelement.findall("./metric/metricresult/[@name='elementTrackerFull']/timepos") b@1481: final_position = float(audioelement.find("./value").text) b@1481: b@1481: # get listen events b@1481: start_times_global = [] b@1481: stop_times_global = [] b@1481: listen_events = audioelement.findall("./metric/metricresult/[@name='elementListenTracker']/event") b@1481: for event in listen_events: b@1481: # get testtime: start and stop b@1481: start_times_global.append(float(event.find('testtime').get('start'))-time_offset) b@1481: stop_times_global.append(float(event.find('testtime').get('stop'))-time_offset) b@1481: b@1481: # display fragment name at start b@1481: plt.text(0,initial_position+0.02,audio_id,color=colormap[increment%len(colormap)]) #,rotation=45 b@1481: b@1481: # previous position and time b@1481: previous_position = initial_position b@1481: previous_time = 0 b@1481: b@1481: # assume not playing at start b@1481: currently_playing = False # keep track of whether fragment is playing during move event b@1481: b@1481: # draw all segments except final one b@1481: for event in move_events: b@1481: # mark this plot as not empty b@1481: plot_empty = False b@1481: b@1481: # get time and final position of move event b@1481: new_time = float(event.find("./time").text)-time_offset b@1481: new_position = float(event.find("./position").text) b@1481: b@1481: # get play/stop events since last move until current move event b@1481: stop_times = [] b@1481: start_times = [] b@1481: # is there a play and/or stop event between previous_time and new_time? b@1481: for time in start_times_global: b@1481: if time>previous_time and timeprevious_time and time0: # while still play/stop events left b@1481: if len(stop_times)<1: # upcoming event is 'play' b@1481: # draw non-playing segment from segment_start to 'play' b@1481: currently_playing = False b@1481: segment_stop = start_times.pop(0) # remove and return first item b@1481: elif len(start_times)<1: # upcoming event is 'stop' b@1481: # draw playing segment (red) from segment_start to 'stop' b@1481: currently_playing = True b@1481: segment_stop = stop_times.pop(0) # remove and return first item b@1481: elif start_times[0]previous_time and timeprevious_time and time0: # while still play/stop events left b@1481: # mark this plot as not empty b@1481: plot_empty = False b@1481: if len(stop_times)<1: # upcoming event is 'play' b@1481: # draw non-playing segment from segment_start to 'play' b@1481: currently_playing = False b@1481: segment_stop = start_times.pop(0) # remove and return first item b@1481: elif len(start_times)<1: # upcoming event is 'stop' b@1481: # draw playing segment (red) from segment_start to 'stop' b@1481: currently_playing = True b@1481: segment_stop = stop_times.pop(0) # remove and return first item b@1481: elif start_times[0] 0: # if any labels available b@1481: plt.yticks(label_positions, label_text) # show rating axis labels b@1481: # set label Y-axis b@1481: if scale_title is not None: b@1481: plt.ylabel(scale_title.text) b@1481: b@1481: #plt.show() # uncomment to show plot; comment when just saving b@1481: #exit() b@1481: b@1481: plt.savefig(timeline_folder+subject_id+"-"+page_name+".pdf", bbox_inches='tight') b@1481: plt.close() b@1481: