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