annotate scripts/timeline_view.py @ 283:a1c1f032ff0a

Scripts: show which and how many markers not clicked or moved; option to plot timelines against 'audioholder time' (default) or 'total test time' (previously the only possibility)
author Brecht De Man <b.deman@qmul.ac.uk>
date Mon, 10 Aug 2015 18:45:45 +0200
parents 4345ba8a1b6e
children f32e58635091
rev   line source
b@246 1 #!/usr/bin/python
b@246 2
b@208 3 import xml.etree.ElementTree as ET
b@264 4 import os # list files in directory
b@264 5 import sys # command line arguments
b@264 6 import matplotlib.pyplot as plt # plots
b@264 7
b@264 8 # COMMAND LINE ARGUMENTS
b@264 9
b@264 10 assert len(sys.argv)<3, "timeline_view takes at most 1 command line argument\n"+\
b@264 11 "Use: python timeline_view.py [timeline_folder_location]"
b@264 12
b@264 13 # XML results files location
b@264 14 if len(sys.argv) == 1:
b@264 15 folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder
b@264 16 print "Use: python timeline_view.py [timeline_folder_location]"
b@264 17 print "Using default path: " + folder_name
b@264 18 elif len(sys.argv) == 2:
b@264 19 folder_name = sys.argv[1] # First command line argument is folder
b@208 20
b@246 21 # CONFIGURATION
b@208 22
b@246 23 # Folder where to store timelines
b@246 24 timeline_folder = folder_name + '/timelines/' # Stores in 'saves/timelines/'
b@246 25
b@246 26 # Font settings
b@246 27 font = {'weight' : 'bold',
b@246 28 'size' : 16}
b@246 29 plt.rc('font', **font)
b@246 30
b@246 31 # Colormap for to cycle through
b@246 32 colormap = ['b', 'r', 'g', 'c', 'm', 'y', 'k']
b@246 33
b@283 34 # x-axis shows time per audioholder, not total test time
b@283 35 show_audioholder_time = True
b@283 36
b@246 37
b@246 38 # CODE
b@208 39
b@208 40 # create timeline_folder if not yet created
b@208 41 if not os.path.exists(timeline_folder):
b@208 42 os.makedirs(timeline_folder)
b@208 43
b@208 44 # get every XML file in folder
b@247 45 for file in os.listdir(folder_name):
b@208 46 if file.endswith(".xml"):
b@246 47 tree = ET.parse(folder_name + '/' + file)
b@208 48 root = tree.getroot()
b@208 49 subject_id = file[:-4] # drop '.xml'
b@208 50
b@283 51 time_offset = 0 # test starts at zero
b@283 52
b@208 53 # ONE TIMELINE PER PAGE - make new plot per page
b@208 54
b@208 55 # get list of all page names
b@208 56 for audioholder in root.findall("./audioholder"): # iterate over pages
b@208 57 page_name = audioholder.get('id') # get page name
b@208 58
b@208 59 if page_name is None: # ignore 'empty' audio_holders
b@208 60 break
b@208 61
b@208 62 # SORT AUDIO ELEMENTS ALPHABETICALLY
b@246 63 audioelements = audioholder.findall("./audioelement")
b@208 64
b@208 65 data = []
b@208 66 for elem in audioelements: # from http://effbot.org/zone/element-sort.htm
b@208 67 key = elem.get("id")
b@208 68 data.append((key, elem))
b@208 69 data.sort()
b@208 70
b@208 71 N_audioelements = len(audioelements) # number of audio elements for this page
b@208 72 increment = 0 # increased for every new audioelement
b@208 73 audioelements_names = [] # store names of audioelements
b@208 74
b@208 75 # for page [page_name], print comments related to fragment [id]
b@208 76 for tuple in data:
b@208 77 audioelement = tuple[1]
b@208 78 if audioelement is not None: # Check it exists
b@208 79 audio_id = str(audioelement.get('id'))
b@208 80 audioelements_names.append(audio_id)
b@208 81
b@208 82 # for this audioelement, loop over all listen events
b@246 83 listen_events = audioelement.findall("./metric/metricresult/[@name='elementListenTracker']/event")
b@208 84 for event in listen_events:
b@208 85 # get testtime: start and stop
b@283 86 start_time = float(event.find('testtime').get('start'))
b@283 87 stop_time = float(event.find('testtime').get('stop'))
b@208 88 # event lines:
b@283 89 plt.plot([start_time-time_offset, start_time-time_offset], # x-values
b@208 90 [0, N_audioelements+1], # y-values
b@208 91 color='k'
b@208 92 )
b@283 93 plt.plot([stop_time-time_offset, stop_time-time_offset], # x-values
b@208 94 [0, N_audioelements+1], # y-values
b@208 95 color='k'
b@208 96 )
b@208 97 # plot time:
b@283 98 plt.plot([start_time-time_offset, stop_time-time_offset], # x-values
b@208 99 [N_audioelements-increment, N_audioelements-increment], # y-values
b@208 100 color=colormap[increment%len(colormap)],
b@208 101 linewidth=6
b@208 102 )
b@208 103
b@208 104 increment+=1
b@283 105
b@283 106 # subtract total audioholder length from subsequent audioholder event times
b@283 107 audioholder_time = audioholder.find("./metric/metricresult/[@id='testTime']")
b@283 108 if audioholder_time is not None and show_audioholder_time:
b@283 109 time_offset = float(audioholder_time.text)
b@208 110
b@208 111 #TODO: if 'nonsensical' or unknown: dashed line until next event
b@208 112 #TODO: Vertical lines for fragment looping point
b@208 113
b@208 114 plt.title('Timeline ' + file) #TODO add song too
b@208 115 plt.xlabel('Time [seconds]')
b@208 116 plt.ylabel('Fragment')
b@208 117 plt.ylim(0, N_audioelements+1)
b@208 118
b@208 119 #y-ticks: fragment IDs, top to bottom
b@208 120 plt.yticks(range(N_audioelements, 0, -1), audioelements_names) # show fragment names
b@208 121
b@208 122
b@208 123 #plt.show() # uncomment to show plot; comment when just saving
b@208 124 #exit()
b@208 125
b@208 126 plt.savefig(timeline_folder+subject_id+"-"+page_name+".png")
b@208 127 plt.close()