comparison scripts/timeline_view.py @ 1453:04e8a9c07c7e

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