annotate scripts/timeline_view.py @ 872:ca4ae613f1dd

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