annotate scripts/score_individual.py @ 206:59eea6eed91b

Comment and rating scripts: robust against audioholders without id or audioelements; fixes issue where plots were not cleared before adding new content; showing legend optional; robust against non-UTF8 characters in legend names
author Brecht De Man <b.deman@qmul.ac.uk>
date Mon, 15 Jun 2015 13:09:46 +0100
parents 566fc4fa2bea
children
rev   line source
b@148 1 import sys
b@148 2 import os
b@148 3 import csv
b@148 4 import matplotlib.pyplot as plt
b@148 5
b@148 6 rating_folder = 'ratings/' # folder with rating csv files
b@148 7
b@148 8 colormap = ['b', 'r', 'g', 'c', 'm', 'y', 'k'] # colormap for to cycle through
b@148 9 markerlist = ["x", ".", "o", "*", "+", "v", ">", "<", "8", "s", "p"]
b@148 10
b@206 11 show_legend = False
b@206 12
b@148 13 # get every csv file in folder
b@192 14 for file in os.listdir(rating_folder):
b@148 15 if file.endswith(".csv"):
b@206 16
b@148 17 page_name = file[:-4] # file name (without extension) is page ID
b@148 18
b@148 19 with open(rating_folder+file, 'r') as readfile: # read this csv file
b@148 20 filereader = csv.reader(readfile, delimiter=',')
b@148 21 headerrow = filereader.next() # use headerrow as X-axis
b@148 22 headerrow = headerrow[1:]
b@148 23
b@148 24
b@148 25 increment = 0
b@148 26 linehandles = []
b@148 27 legendnames = []
b@148 28 for row in filereader:
b@148 29 subject_id = row[0][:-4]
b@148 30 plothandle, = plt.plot(range(1,len(row)), # x-values
b@148 31 row[1:], # y-values: csv values except subject name
b@148 32 color=colormap[increment%len(colormap)],
b@148 33 marker=markerlist[increment%len(markerlist)],
b@148 34 markersize=10,
b@148 35 linestyle='None',
b@148 36 label=subject_id
b@148 37 )
b@148 38 increment += 1 # increase counter
b@148 39 linehandles.append(plothandle)
b@206 40 legendnames.append(subject_id.decode("utf-8")) # avoid decoding problems
b@148 41
b@148 42
b@148 43 plt.xlabel('Fragment')
b@148 44 plt.title('Individual ratings '+page_name)
b@148 45 plt.xlim(0, len(headerrow)+1) # only show relevant region, leave space left & right)
b@148 46 plt.xticks(range(1, len(headerrow)+1), headerrow) # show fragment names
b@148 47
b@148 48 plt.ylabel('Rating')
b@148 49 plt.ylim(0,1)
b@148 50
b@206 51 if show_legend:
b@206 52 plt.legend(linehandles, legendnames,
b@206 53 loc='upper right',
b@206 54 bbox_to_anchor=(1.1, 1),
b@206 55 borderaxespad=0.,
b@206 56 numpoints=1 # remove extra marker
b@206 57 )
b@148 58
b@148 59 #TODO Put legend outside of box
b@148 60
b@192 61 #plt.show() # show plot
b@148 62 #exit()
b@206 63
b@192 64 plt.savefig(rating_folder+page_name+"-ind.png")
b@206 65 plt.close()