comparison scripts/timeline_view.py @ 1542:db0823f2210d

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