annotate scripts/timeline_view.py @ 1528:766bff1a8f73

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