annotate scripts/timeline_view.py @ 1555:45b469cfcd51

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