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