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