annotate scripts/timeline_view.py @ 1063:e67a76e9ba7a

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