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