annotate scripts/timeline_view.py @ 240:5fd3ba494551 Dev_main

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