annotate scripts/score_plot.py @ 1513:f14d55481b60

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