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