annotate scripts/score_boxplot.py @ 923:7a7a72880996

Merge from the default branch
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Tue, 02 Jun 2015 11:24:23 +0100
parents 533d51508e93
children 4a0bfa7bef24
rev   line source
n@921 1 import sys
n@921 2 import os
n@921 3 import csv
n@921 4 import matplotlib.pyplot as plt
n@921 5 import numpy as np
n@921 6
n@921 7 rating_folder = 'ratings/' # folder with rating csv files
n@921 8 show_individual = 'frank'
n@921 9
n@921 10 # get every csv file in folder
n@921 11 for file in os.listdir(rating_folder): # You have to put this in folder where rating csv files are.
n@921 12 if file.endswith(".csv"):
n@921 13 page_name = file[:-4] # file name (without extension) is page ID
n@921 14
n@921 15 # get header
n@921 16 with open(rating_folder+file, 'r') as readfile: # read this csv file
n@921 17 filereader = csv.reader(readfile, delimiter=',')
n@921 18 headerrow = filereader.next() # use headerrow as X-axis
n@921 19 headerrow = headerrow[1:]
n@921 20
n@921 21 # read ratings into matrix
n@921 22 ratings = np.loadtxt(open(rating_folder+file,"rb"),
n@921 23 delimiter=",",
n@921 24 skiprows=1,
n@921 25 usecols=range(1,len(headerrow)+1)
n@921 26 )
n@921 27
n@921 28 # draw boxplot
n@921 29 plt.boxplot(ratings)
n@921 30
n@921 31 # add rating of individual(s)
n@921 32 with open(rating_folder+file, 'r') as readfile: # read this csv file
n@921 33 filereader = csv.reader(readfile, delimiter=',')
n@921 34 headerrow = filereader.next() # use headerrow as X-axis
n@921 35 headerrow = headerrow[1:]
n@921 36 markerlist = ["x", ".", "o", "*", "+", "v", ">", "<", "8", "s", "p"]
n@921 37 increment = 0
n@921 38 linehandles = []
n@921 39 legendnames = []
n@921 40 for row in filereader:
n@921 41 subject_id = row[0][:-4]
n@921 42 if subject_id in show_individual:
n@921 43 plothandle, = plt.plot(range(1,len(row)), # x-values
n@921 44 row[1:], # y-values: csv values except subject name
n@921 45 color='k',
n@921 46 marker=markerlist[increment%len(markerlist)],
n@921 47 markersize=10,
n@921 48 linestyle='None',
n@921 49 label=subject_id
n@921 50 )
n@921 51 increment += 1 # increase counter
n@921 52 linehandles.append(plothandle)
n@921 53 legendnames.append(subject_id)
n@921 54 plt.legend(linehandles, legendnames,
n@921 55 loc='upper right',
n@921 56 bbox_to_anchor=(1.1, 1), borderaxespad=0.)
n@921 57
n@921 58
n@921 59 plt.xlabel('Fragment')
n@921 60 plt.title('Box plot '+page_name)
n@921 61 plt.xlim(0, len(headerrow)+1) # only show relevant region, leave space left & right)
n@921 62 plt.xticks(range(1, len(headerrow)+1), headerrow) # show fragment names
n@921 63
n@921 64 plt.ylabel('Rating')
n@921 65 plt.ylim(0,1)
n@921 66
n@921 67 plt.show()
n@921 68 #exit()
n@921 69
n@921 70 #TODO Save output automatically
n@921 71