annotate scripts/timeline_view.py @ 2071:84411745a981

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