annotate scripts/timeline_view.py @ 1775:aab95577ba11

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