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