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