annotate scripts/timeline_view.py @ 884:1dd209550560

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