annotate scripts/timeline_view.py @ 1453:04e8a9c07c7e

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