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