comparison scripts/timeline_view.py @ 2073:0a846d951bc1

Scripts: timeline with rectangles (accurate start and end, customisable width); timeline and score plots to PDF by default.
author Brecht De Man <b.deman@qmul.ac.uk>
date Tue, 11 Aug 2015 11:06:55 +0200
parents 49b03ad3dcf9
children 8eb0c24ea50a
comparison
equal deleted inserted replaced
2072:49b03ad3dcf9 2073:0a846d951bc1
2 2
3 import xml.etree.ElementTree as ET 3 import xml.etree.ElementTree as ET
4 import os # list files in directory 4 import os # list files in directory
5 import sys # command line arguments 5 import sys # command line arguments
6 import matplotlib.pyplot as plt # plots 6 import matplotlib.pyplot as plt # plots
7 import matplotlib.patches as patches # rectangles
7 8
8 # COMMAND LINE ARGUMENTS 9 # COMMAND LINE ARGUMENTS
9 10
10 assert len(sys.argv)<3, "timeline_view takes at most 1 command line argument\n"+\ 11 assert len(sys.argv)<3, "timeline_view takes at most 1 command line argument\n"+\
11 "Use: python timeline_view.py [timeline_folder_location]" 12 "Use: python timeline_view.py [timeline_folder_location]"
15 folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder 16 folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder
16 print "Use: python timeline_view.py [timeline_folder_location]" 17 print "Use: python timeline_view.py [timeline_folder_location]"
17 print "Using default path: " + folder_name 18 print "Using default path: " + folder_name
18 elif len(sys.argv) == 2: 19 elif len(sys.argv) == 2:
19 folder_name = sys.argv[1] # First command line argument is folder 20 folder_name = sys.argv[1] # First command line argument is folder
20 21
21 # check if folder_name exists 22 # check if folder_name exists
22 if not os.path.exists(folder_name): 23 if not os.path.exists(folder_name):
23 #the file is not there 24 #the file is not there
24 print "Folder '"+folder_name+"' does not exist." 25 print "Folder '"+folder_name+"' does not exist."
25 sys.exit() # terminate script execution 26 sys.exit() # terminate script execution
39 plt.rc('font', **font) 40 plt.rc('font', **font)
40 41
41 # Colormap for to cycle through 42 # Colormap for to cycle through
42 colormap = ['b', 'r', 'g', 'c', 'm', 'y', 'k'] 43 colormap = ['b', 'r', 'g', 'c', 'm', 'y', 'k']
43 44
44 # x-axis shows time per audioholder, not total test time 45 # if enabled, x-axis shows time per audioholder, not total test time
45 show_audioholder_time = True 46 show_audioholder_time = True
47
48 # bar height (<1 to avoid overlapping)
49 bar_height = 0.6
50
51 # figure size
52 fig_width = 25
53 fig_height = 5
46 54
47 55
48 # CODE 56 # CODE
49 57
50 # create timeline_folder if not yet created 58 # create timeline_folder if not yet created
80 88
81 N_audioelements = len(audioelements) # number of audio elements for this page 89 N_audioelements = len(audioelements) # number of audio elements for this page
82 increment = 0 # increased for every new audioelement 90 increment = 0 # increased for every new audioelement
83 audioelements_names = [] # store names of audioelements 91 audioelements_names = [] # store names of audioelements
84 92
93 # set plot parameters
94 plt.title('Timeline ' + file + ": "+page_name)
95 plt.xlabel('Time [seconds]')
96 plt.ylabel('Fragment')
97 plt.ylim(0, N_audioelements+1)
98
99 # get axes handle
100 fig = plt.figure(figsize=(fig_width, fig_height))
101 ax = fig.add_subplot(111) #, aspect='equal'
102
85 # for page [page_name], print comments related to fragment [id] 103 # for page [page_name], print comments related to fragment [id]
86 for tuple in data: 104 for tuple in data:
87 audioelement = tuple[1] 105 audioelement = tuple[1]
88 if audioelement is not None: # Check it exists 106 if audioelement is not None: # Check it exists
89 audio_id = str(audioelement.get('id')) 107 audio_id = str(audioelement.get('id'))
94 for event in listen_events: 112 for event in listen_events:
95 # get testtime: start and stop 113 # get testtime: start and stop
96 start_time = float(event.find('testtime').get('start')) 114 start_time = float(event.find('testtime').get('start'))
97 stop_time = float(event.find('testtime').get('stop')) 115 stop_time = float(event.find('testtime').get('stop'))
98 # event lines: 116 # event lines:
99 plt.plot([start_time-time_offset, start_time-time_offset], # x-values 117 ax.plot([start_time-time_offset, start_time-time_offset], # x-values
100 [0, N_audioelements+1], # y-values 118 [0, N_audioelements+1], # y-values
101 color='k' 119 color='k'
102 ) 120 )
103 plt.plot([stop_time-time_offset, stop_time-time_offset], # x-values 121 ax.plot([stop_time-time_offset, stop_time-time_offset], # x-values
104 [0, N_audioelements+1], # y-values 122 [0, N_audioelements+1], # y-values
105 color='k' 123 color='k'
106 ) 124 )
107 # plot time: 125 # plot time:
108 plt.plot([start_time-time_offset, stop_time-time_offset], # x-values 126 ax.add_patch(
109 [N_audioelements-increment, N_audioelements-increment], # y-values 127 patches.Rectangle(
110 color=colormap[increment%len(colormap)], 128 (start_time-time_offset, N_audioelements-increment-bar_height/2), # (x, y)
111 linewidth=6 129 stop_time - start_time, # width
130 bar_height, # height
131 color=colormap[increment%len(colormap)] # colour
112 ) 132 )
133 )
113 134
114 increment+=1 135 increment+=1 # to next audioelement
115 136
116 # subtract total audioholder length from subsequent audioholder event times 137 # subtract total audioholder length from subsequent audioholder event times
117 audioholder_time = audioholder.find("./metric/metricresult/[@id='testTime']") 138 audioholder_time = audioholder.find("./metric/metricresult/[@id='testTime']")
118 if audioholder_time is not None and show_audioholder_time: 139 if audioholder_time is not None and show_audioholder_time:
119 time_offset = float(audioholder_time.text) 140 time_offset = float(audioholder_time.text)
120 141
121 #TODO: if 'nonsensical' or unknown: dashed line until next event 142 #TODO: if 'nonsensical' or unknown: dashed line until next event
122 #TODO: Vertical lines for fragment looping point 143 #TODO: Vertical lines for fragment looping point
123 144
124 plt.title('Timeline ' + file) #TODO add song too
125 plt.xlabel('Time [seconds]')
126 plt.ylabel('Fragment')
127 plt.ylim(0, N_audioelements+1)
128
129 #y-ticks: fragment IDs, top to bottom 145 #y-ticks: fragment IDs, top to bottom
130 plt.yticks(range(N_audioelements, 0, -1), audioelements_names) # show fragment names 146 plt.yticks(range(N_audioelements, 0, -1), audioelements_names) # show fragment names
131 147
132 148
133 #plt.show() # uncomment to show plot; comment when just saving 149 #plt.show() # uncomment to show plot; comment when just saving
134 #exit() 150 #exit()
135 151
136 plt.savefig(timeline_folder+subject_id+"-"+page_name+".png") 152 plt.savefig(timeline_folder+subject_id+"-"+page_name+".pdf", bbox_inches='tight')
137 plt.close() 153 plt.close()