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