annotate scripts/timeline_view.py @ 1513:f14d55481b60

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