annotate scripts/timeline_view.py @ 247:c3f29c2b9b12

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