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