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