annotate scripts/score_plot.py @ 1528:766bff1a8f73

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