annotate scripts/score_plot.py @ 2057:416fc71e0374

Scripts: evaluation_stats.py added to quickly show a report on tests done so far (to be expanded)
author Brecht De Man <b.deman@qmul.ac.uk>
date Wed, 01 Jul 2015 11:09:17 +0100
parents 276482e6215f
children 4345ba8a1b6e
rev   line source
b@1554 1 #!/usr/bin/python
b@1554 2
b@1554 3 import sys
b@1554 4 import os
b@1554 5 import csv
b@1554 6 import matplotlib.pyplot as plt
b@1554 7 import numpy as np
b@1554 8 import scipy as sp
b@1554 9 import scipy.stats
b@1554 10
b@1554 11 # CONFIGURATION
b@1554 12
b@1554 13 # Which type(s) of plot do you want?
b@2057 14 enable_boxplot = True # show box plot
b@2057 15 enable_confidence = False # show confidence interval
b@1554 16 confidence = 0.90 # confidence value (for confidence interval plot)
b@2057 17 enable_individual = False # show all individual ratings
b@1554 18 show_individual = [] # show specific individuals
b@1554 19 show_legend = False # show names of individuals
b@1554 20 #TODO: Merge, implement this functionality
b@1554 21 #TODO: Control by CLI arguments (plot types, save and/or show, ...)
b@1554 22
b@1554 23 # Enter folder where rating CSV files are (generated with score_parser.py or same format).
b@1554 24 rating_folder = '../saves/ratings/' # folder with rating csv files
b@1554 25
b@1554 26 # Font settings
b@1554 27 font = {'weight' : 'bold',
b@1554 28 'size' : 10}
b@1554 29 plt.rc('font', **font)
b@1554 30
b@1554 31
b@1554 32 # CODE
b@1554 33
b@1554 34 # get every csv file in folder
b@1554 35 for file in os.listdir(rating_folder): # You have to put this in folder where rating csv files are.
b@1554 36 if file.endswith(".csv"):
b@1554 37 page_name = file[:-4] # file name (without extension) is page ID
b@1554 38
b@1554 39 # get header
b@1554 40 with open(rating_folder+file, 'rb') as readfile: # read this csv file
b@1554 41 filereader = csv.reader(readfile, delimiter=',')
b@1554 42 headerrow = filereader.next() # use headerrow as X-axis
b@1554 43 headerrow = headerrow[1:]
b@1554 44
b@1554 45 # read ratings into matrix
b@1554 46 # ratings = np.loadtxt(open(rating_folder+file,"rb"),
b@1554 47 # delimiter=",",
b@1554 48 # skiprows=1,
b@1554 49 # usecols=range(1,len(headerrow)+1)
b@1554 50 # )
b@1554 51 ratings = np.genfromtxt(readfile,
b@1554 52 delimiter=",",
b@1554 53 #skip_header = 1,
b@1554 54 converters = {3: lambda s: float(s or 'Nan')},
b@1554 55 usecols=range(1,len(headerrow)+1)
b@1554 56 )
b@1554 57
b@1554 58 # assert at least 2 subjects (move on to next file if violated)
b@2057 59 if ratings.shape[0]<2:
b@1554 60 print "WARNING: Just one subject for " + page_name + ". Moving on to next file."
b@1554 61 break
b@1554 62
b@1554 63 # BOXPLOT
b@1554 64 if enable_boxplot:
b@1554 65 plt.boxplot(ratings)
b@1554 66
b@1554 67 # CONFIDENCE INTERVAL
b@1554 68 if enable_confidence:
b@1554 69 iterator = 0
b@1554 70 for column in ratings.T: # iterate over transposed matrix
b@1554 71 # remove all 'Nan's from column
b@1554 72 column = column[~np.isnan(column)]
b@1554 73
b@1554 74 # get number of non-Nan ratings (= #subjects)
b@1554 75 n = column.size
b@1554 76
b@1554 77 # get mean
b@1554 78 mean_rating = np.mean(column)
b@1554 79
b@1554 80 # get errors
b@1554 81 err = scipy.stats.sem(column)* sp.stats.t._ppf((1+confidence)/2., n-1)
b@1554 82
b@1554 83 # draw plot
b@1554 84 plt.errorbar(iterator+1,
b@1554 85 mean_rating,
b@1554 86 yerr=err,
b@1554 87 marker="x",
b@1554 88 color ="k",
b@1554 89 markersize=12,
b@1554 90 linestyle='None')
b@1554 91
b@1554 92 iterator += 1 # increase counter
b@1554 93
b@1554 94
b@1554 95 # INDIVIDUAL PLOT
b@1554 96 if enable_individual or show_individual:
b@1554 97 # marker list and color map to cycle through
b@1554 98 markerlist = ["x", ".", "o", "*", "+", "v", ">", "<", "8", "s", "p"]
b@1554 99 colormap = ['b', 'r', 'g', 'c', 'm', 'y', 'k']
b@1554 100 increment = 0
b@1554 101 linehandles = []
b@1554 102 legendnames = []
b@1554 103 with open(rating_folder+file, 'rb') as readfile: # read this csv file
b@1554 104 filereader = csv.reader(readfile, delimiter=',')
b@1554 105 headerrow = filereader.next() # use headerrow as X-axis
b@1554 106 headerrow = headerrow[1:]
b@1554 107 for row in filereader:
b@1554 108 subject_id = row[0][:-4] # read from beginning of line
b@1554 109 # assume plotting all individuals if no individual(s) specified
b@1554 110 if not show_individual or subject_id in show_individual:
b@1554 111 plothandle, = plt.plot(range(1,len(row)), # x-values
b@1554 112 ratings[increment,:],#row[1:], # y-values: csv values except subject name
b@1554 113 color=colormap[increment%len(colormap)],
b@1554 114 marker=markerlist[increment%len(markerlist)],
b@1554 115 markersize=10,
b@1554 116 linestyle='None',
b@1554 117 label=subject_id
b@1554 118 )
b@1554 119 linehandles.append(plothandle)
b@1554 120 legendnames.append(subject_id)
b@1554 121 if show_legend:
b@1554 122 plt.legend(linehandles, legendnames,
b@1554 123 loc='upper right',
b@1554 124 bbox_to_anchor=(1.1, 1),
b@1554 125 borderaxespad=0.,
b@1554 126 numpoints=1 # remove extra marker
b@1554 127 )
b@1554 128 increment += 1 # increase counter
b@1554 129
b@1554 130 # TITLE, AXIS LABELS AND LIMITS
b@1554 131 plt.title(page_name)
b@1554 132 plt.xlabel('Fragment')
b@1554 133 plt.xlim(0, len(headerrow)+1) # only show relevant region, leave space left & right)
b@1554 134 plt.xticks(range(1, len(headerrow)+1), headerrow) # show fragment names
b@1554 135 plt.ylabel('Rating')
b@1554 136 plt.ylim(0,1)
b@1554 137
b@1554 138
b@1554 139
b@1554 140 # SHOW PLOT
b@1554 141 #plt.show()
b@1554 142 #exit()
b@1554 143
b@1554 144 # SAVE PLOT
b@1554 145 # automatically
b@1554 146 plot_type = ("-box" if enable_boxplot else "") + \
b@1554 147 ("-conf" if enable_confidence else "") + \
b@1554 148 ("-ind" if enable_individual else "")
b@1554 149 plt.savefig(rating_folder+page_name+plot_type+".png")
b@1554 150 plt.close()