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