Mercurial > hg > webaudioevaluationtool
comparison scripts/score_plot.py @ 1316:279930a008ca
All interfaces support comment boxes. Comment box identification matches presented tag (for instance, AB will be Comment on fragment A, rather than 1). Tighter buffer loading protocol, audioObjects register with the buffer rather than checking for buffer existence (which can be buggy depending on the buffer state). Buffers now have a state to ensure exact location in loading chain (downloading, decoding, LUFS, ready).
author | Nicholas Jillings <nickjillings@users.noreply.github.com> |
---|---|
date | Fri, 29 Jan 2016 11:11:57 +0000 |
parents | |
children | 43801b3d6131 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 1316:279930a008ca |
---|---|
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 # COMMAND LINE ARGUMENTS | |
12 | |
13 #TODO: Merge, implement this functionality | |
14 #TODO: Control by CLI arguments (plot types, save and/or show, ...) | |
15 | |
16 assert len(sys.argv)<4, "score_plot takes at most 2 command line arguments\n"+\ | |
17 "Use: python score_plot.py [ratings_folder_location]."+\ | |
18 "Type 'python score_plot.py -h' for more options" | |
19 | |
20 # initialise plot types (false by default) and options | |
21 enable_boxplot = False # show box plot | |
22 enable_confidence = False # show confidence interval | |
23 confidence = 0.90 # confidence value (for confidence interval plot) | |
24 enable_individual = False # show all individual ratings | |
25 show_individual = [] # show specific individuals (empty: show all individuals found) | |
26 show_legend = False # show names of individuals | |
27 | |
28 # DEFAULT: Looks in 'saves/ratings/' folder from 'scripts/' folder | |
29 rating_folder = "../saves/ratings/" | |
30 | |
31 # XML results files location | |
32 if len(sys.argv) == 1: # no extra arguments | |
33 enable_boxplot = True # show box plot | |
34 print "Use: python score_plot.py [rating folder] [plot_type] [-l/-legend]" | |
35 print "Type 'python score_plot.py -h' for help." | |
36 print "Using default path: " + rating_folder + " with boxplot." | |
37 else: | |
38 for arg in sys.argv: # go over all arguments | |
39 if arg == '-h': | |
40 # show help | |
41 #TODO: replace with contents of helpfile score_plot.info (or similar) | |
42 print "Use: python score_plot.py [rating_folder] [plot_type] [-l] [confidence]" | |
43 print " rating_folder:" | |
44 print " folder where output of 'score_parser' can be found, and" | |
45 print " where plots will be stored." | |
46 print " By default, '../saves/ratings/' is used." | |
47 print "" | |
48 print "PLOT TYPES" | |
49 print " Can be used in combination." | |
50 print " box | boxplot | -b" | |
51 print " Enables the boxplot" | |
52 print " conf | confidence | -c" | |
53 print " Enables the confidence interval plot" | |
54 print " ind | individual | -i" | |
55 print " Enables plot of individual ratings" | |
56 print "" | |
57 print "PLOT OPTIONS" | |
58 print " leg | legend | -l" | |
59 print " For individual plot: show legend with individual file names" | |
60 print " numeric value between 0 and 1, e.g. 0.95" | |
61 print " For confidence interval plot: confidence value" | |
62 assert False, ""# stop immediately after showing help #TODO cleaner way | |
63 | |
64 # PLOT TYPES | |
65 elif arg == 'box' or arg == 'boxplot' or arg == '-b': | |
66 enable_boxplot = True # show box plot | |
67 elif arg == 'conf' or arg == 'confidence' or arg == '-c': | |
68 enable_confidence = True # show confidence interval | |
69 #TODO add confidence value input | |
70 elif arg == 'ind' or arg == 'individual' or arg == '-i': | |
71 enable_individual = True # show all individual ratings | |
72 | |
73 # PLOT OPTIONS | |
74 elif arg == 'leg' or arg == 'legend' or arg == '-l': | |
75 if not enable_individual: | |
76 print "WARNING: The 'legend' option is only relevant to plots of "+\ | |
77 "individual ratings" | |
78 show_legend = True # show all individual ratings | |
79 elif arg.isdigit(): | |
80 if not enable_confidence: | |
81 print "WARNING: The numeric confidence value is only relevant when "+\ | |
82 "confidence plot is enabled" | |
83 if float(arg)>0 and float(arg)<1: | |
84 confidence = float(arg) | |
85 else: | |
86 print "WARNING: The confidence value needs to be between 0 and 1" | |
87 | |
88 # FOLDER NAME | |
89 else: | |
90 # assume it's the folder name | |
91 rating_folder = arg | |
92 | |
93 # at least one plot type should be selected: box plot by default | |
94 if not enable_boxplot and not enable_confidence and not enable_individual: | |
95 enable_boxplot = True | |
96 | |
97 # check if folder_name exists | |
98 if not os.path.exists(rating_folder): | |
99 #the file is not there | |
100 print "Folder '"+rating_folder+"' does not exist." | |
101 sys.exit() # terminate script execution | |
102 elif not os.access(os.path.dirname(rating_folder), os.W_OK): | |
103 #the file does exist but write rating_folder are not given | |
104 print "No write privileges in folder '"+rating_folder+"'." | |
105 | |
106 | |
107 # CONFIGURATION | |
108 | |
109 # Font settings | |
110 font = {'weight' : 'bold', | |
111 'size' : 10} | |
112 plt.rc('font', **font) | |
113 | |
114 | |
115 # CODE | |
116 | |
117 # get every csv file in folder | |
118 for file in os.listdir(rating_folder): | |
119 if file.endswith(".csv"): | |
120 page_name = file[:-4] # file name (without extension) is page ID | |
121 | |
122 # get header | |
123 with open(rating_folder+file, 'rb') as readfile: # read this csv file | |
124 filereader = csv.reader(readfile, delimiter=',') | |
125 headerrow = filereader.next() # use headerrow as X-axis | |
126 headerrow = headerrow[1:] | |
127 | |
128 # read ratings into matrix | |
129 # ratings = np.loadtxt(open(rating_folder+file,"rb"), | |
130 # delimiter=",", | |
131 # skiprows=1, | |
132 # usecols=range(1,len(headerrow)+1) | |
133 # ) | |
134 ratings = np.genfromtxt(readfile, | |
135 delimiter=",", | |
136 #skip_header = 1, | |
137 converters = {3: lambda s: float(s or 'Nan')}, | |
138 usecols=range(1,len(headerrow)+1) | |
139 ) | |
140 | |
141 # assert at least 2 subjects (move on to next file if violated) | |
142 if ratings.shape[0]<2: | |
143 print "WARNING: Just one subject for " + page_name + ". Moving on to next file." | |
144 break | |
145 | |
146 # BOXPLOT | |
147 if enable_boxplot: | |
148 plt.boxplot(ratings) | |
149 | |
150 # CONFIDENCE INTERVAL | |
151 if enable_confidence: | |
152 iterator = 0 | |
153 for column in ratings.T: # iterate over transposed matrix | |
154 # remove all 'Nan's from column | |
155 column = column[~np.isnan(column)] | |
156 | |
157 # get number of non-Nan ratings (= #subjects) | |
158 n = column.size | |
159 | |
160 # get mean | |
161 mean_rating = np.mean(column) | |
162 | |
163 # get errors | |
164 err = scipy.stats.sem(column)* sp.stats.t._ppf((1+confidence)/2., n-1) | |
165 | |
166 # draw plot | |
167 plt.errorbar(iterator+1, | |
168 mean_rating, | |
169 yerr=err, | |
170 marker="x", | |
171 color ="k", | |
172 markersize=12, | |
173 linestyle='None') | |
174 | |
175 iterator += 1 # increase counter | |
176 | |
177 | |
178 # INDIVIDUAL PLOT | |
179 if enable_individual or show_individual: | |
180 # marker list and color map to cycle through | |
181 markerlist = ["x", ".", "o", "*", "+", "v", ">", "<", "8", "s", "p"] | |
182 colormap = ['b', 'r', 'g', 'c', 'm', 'y', 'k'] | |
183 increment = 0 | |
184 linehandles = [] | |
185 legendnames = [] | |
186 with open(rating_folder+file, 'rb') as readfile: # read this csv file | |
187 filereader = csv.reader(readfile, delimiter=',') | |
188 headerrow = filereader.next() # use headerrow as X-axis | |
189 headerrow = headerrow[1:] | |
190 for row in filereader: | |
191 subject_id = row[0][:-4] # read from beginning of line | |
192 # assume plotting all individuals if no individual(s) specified | |
193 if not show_individual or subject_id in show_individual: | |
194 plothandle, = plt.plot(range(1,len(row)), # x-values | |
195 ratings[increment,:],#row[1:], # y-values: csv values except subject name | |
196 color=colormap[increment%len(colormap)], | |
197 marker=markerlist[increment%len(markerlist)], | |
198 markersize=10, | |
199 linestyle='None', | |
200 label=subject_id | |
201 ) | |
202 linehandles.append(plothandle) | |
203 legendnames.append(subject_id) | |
204 if show_legend: | |
205 plt.legend(linehandles, legendnames, | |
206 loc='upper right', | |
207 bbox_to_anchor=(1.1, 1), | |
208 borderaxespad=0., | |
209 numpoints=1 # remove extra marker | |
210 ) | |
211 increment += 1 # increase counter | |
212 | |
213 # TITLE, AXIS LABELS AND LIMITS | |
214 plt.title(page_name) | |
215 plt.xlabel('Fragment') | |
216 plt.xlim(0, len(headerrow)+1) # only show relevant region, leave space left & right) | |
217 plt.xticks(range(1, len(headerrow)+1), headerrow, rotation=90) # show fragment names | |
218 plt.ylabel('Rating') | |
219 plt.ylim(0,1) | |
220 | |
221 | |
222 | |
223 # SHOW PLOT | |
224 #plt.show() | |
225 #exit() | |
226 | |
227 # SAVE PLOT | |
228 # automatically | |
229 plot_type = ("-box" if enable_boxplot else "") + \ | |
230 ("-conf" if enable_confidence else "") + \ | |
231 ("-ind" if enable_individual else "") | |
232 plt.savefig(rating_folder+page_name+plot_type+".pdf", bbox_inches='tight') | |
233 plt.close() |