annotate Perceptual Evaluation/webaudioevaluationtool/scripts/score_plot.py @ 0:55c282f01a30 tip

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