annotate scripts/timeline_view.py @ 264:4345ba8a1b6e

Scripts: choose input/output folder from command line; score_plot also choose options from command line (WIP)
author Brecht De Man <b.deman@qmul.ac.uk>
date Mon, 20 Jul 2015 12:47:16 +0100
parents c3f29c2b9b12
children a1c1f032ff0a
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@246 34
b@246 35 # CODE
b@208 36
b@208 37 # create timeline_folder if not yet created
b@208 38 if not os.path.exists(timeline_folder):
b@208 39 os.makedirs(timeline_folder)
b@208 40
b@208 41 # get every XML file in folder
b@247 42 for file in os.listdir(folder_name):
b@208 43 if file.endswith(".xml"):
b@246 44 tree = ET.parse(folder_name + '/' + file)
b@208 45 root = tree.getroot()
b@208 46 subject_id = file[:-4] # drop '.xml'
b@208 47
b@208 48 # ONE TIMELINE PER PAGE - make new plot per page
b@208 49
b@208 50 # get list of all page names
b@208 51 for audioholder in root.findall("./audioholder"): # iterate over pages
b@208 52 page_name = audioholder.get('id') # get page name
b@208 53
b@208 54 if page_name is None: # ignore 'empty' audio_holders
b@208 55 break
b@208 56
b@208 57 # SORT AUDIO ELEMENTS ALPHABETICALLY
b@246 58 audioelements = audioholder.findall("./audioelement")
b@208 59
b@208 60 data = []
b@208 61 for elem in audioelements: # from http://effbot.org/zone/element-sort.htm
b@208 62 key = elem.get("id")
b@208 63 data.append((key, elem))
b@208 64 data.sort()
b@208 65
b@208 66 N_audioelements = len(audioelements) # number of audio elements for this page
b@208 67 increment = 0 # increased for every new audioelement
b@208 68 audioelements_names = [] # store names of audioelements
b@208 69
b@208 70 # for page [page_name], print comments related to fragment [id]
b@208 71 for tuple in data:
b@208 72 audioelement = tuple[1]
b@208 73 if audioelement is not None: # Check it exists
b@208 74 audio_id = str(audioelement.get('id'))
b@208 75 audioelements_names.append(audio_id)
b@208 76
b@208 77 # for this audioelement, loop over all listen events
b@246 78 listen_events = audioelement.findall("./metric/metricresult/[@name='elementListenTracker']/event")
b@208 79 for event in listen_events:
b@208 80 # get testtime: start and stop
b@208 81 start_time = event.find('testtime').get('start')
b@208 82 stop_time = event.find('testtime').get('stop')
b@208 83 # event lines:
b@208 84 plt.plot([start_time, start_time], # x-values
b@208 85 [0, N_audioelements+1], # y-values
b@208 86 color='k'
b@208 87 )
b@208 88 plt.plot([stop_time, stop_time], # x-values
b@208 89 [0, N_audioelements+1], # y-values
b@208 90 color='k'
b@208 91 )
b@208 92 # plot time:
b@208 93 plt.plot([start_time, stop_time], # x-values
b@208 94 [N_audioelements-increment, N_audioelements-increment], # y-values
b@208 95 color=colormap[increment%len(colormap)],
b@208 96 linewidth=6
b@208 97 )
b@208 98
b@208 99 increment+=1
b@208 100
b@208 101 #TODO: if 'nonsensical' or unknown: dashed line until next event
b@208 102 #TODO: Vertical lines for fragment looping point
b@208 103
b@208 104 plt.title('Timeline ' + file) #TODO add song too
b@208 105 plt.xlabel('Time [seconds]')
b@208 106 plt.ylabel('Fragment')
b@208 107 plt.ylim(0, N_audioelements+1)
b@208 108
b@208 109 #y-ticks: fragment IDs, top to bottom
b@208 110 plt.yticks(range(N_audioelements, 0, -1), audioelements_names) # show fragment names
b@208 111
b@208 112
b@208 113 #plt.show() # uncomment to show plot; comment when just saving
b@208 114 #exit()
b@208 115
b@208 116 plt.savefig(timeline_folder+subject_id+"-"+page_name+".png")
b@208 117 plt.close()