b@1554: #!/usr/bin/python b@1554: b@1554: import sys b@1554: import os b@1554: import csv b@1554: import matplotlib.pyplot as plt b@1554: import numpy as np b@1554: import scipy as sp b@1554: import scipy.stats b@1554: b@2069: # COMMAND LINE ARGUMENTS b@1554: b@2069: #TODO: Merge, implement this functionality b@2069: #TODO: Control by CLI arguments (plot types, save and/or show, ...) b@2069: b@2069: assert len(sys.argv)<4, "score_plot takes at most 2 command line arguments\n"+\ b@2069: "Use: python score_plot.py [ratings_folder_location]."+\ b@2069: "Type 'python score_plot.py -h' for more options" b@2069: b@2069: # initialise plot types (false by default) and options b@2069: enable_boxplot = False # show box plot b@2057: enable_confidence = False # show confidence interval b@1554: confidence = 0.90 # confidence value (for confidence interval plot) b@2057: enable_individual = False # show all individual ratings b@2069: show_individual = [] # show specific individuals (empty: show all individuals found) b@1554: show_legend = False # show names of individuals b@2069: b@2069: # DEFAULT: Looks in 'saves/ratings/' folder from 'scripts/' folder b@2069: rating_folder = "../saves/ratings/" b@2069: b@2069: # XML results files location b@2069: if len(sys.argv) == 1: # no extra arguments b@2069: enable_boxplot = True # show box plot b@2069: print "Use: python score_plot.py [rating folder] [plot_type] [-l/-legend]" b@2069: print "Type 'python score_plot.py -h' for help." b@2069: print "Using default path: " + rating_folder + " with boxplot." b@2069: else: b@2069: for arg in sys.argv: # go over all arguments b@2069: if arg == '-h': b@2069: # show help b@2069: #TODO: replace with contents of helpfile score_plot.info (or similar) b@2069: print "Use: python score_plot.py [rating_folder] [plot_type] [-l] [confidence]" b@2069: print " rating_folder:" b@2069: print " folder where output of 'score_parser' can be found, and" b@2069: print " where plots will be stored." b@2069: print " By default, '../saves/ratings/' is used." b@2069: print "" b@2069: print "PLOT TYPES" b@2069: print " Can be used in combination." b@2069: print " box | boxplot | -b" b@2069: print " Enables the boxplot" b@2069: print " conf | confidence | -c" b@2069: print " Enables the confidence interval plot" b@2069: print " ind | individual | -i" b@2069: print " Enables plot of individual ratings" b@2069: print "" b@2069: print "PLOT OPTIONS" b@2069: print " legend | -l" b@2069: print " For individual plot: show legend with individual file names" b@2069: print " " b@2069: assert False, ""# stop immediately after showing help #TODO cleaner way b@2069: b@2069: # PLOT TYPES b@2069: elif arg == 'box' or arg == 'boxplot' or arg == '-b': b@2069: enable_boxplot = True # show box plot b@2069: elif arg == 'conf' or arg == 'confidence' or arg == '-c': b@2069: enable_confidence = True # show confidence interval b@2069: #TODO add confidence value input b@2069: elif arg == 'ind' or arg == 'individual' or arg == '-i': b@2069: enable_individual = True # show all individual ratings b@2069: b@2069: # PLOT OPTIONS b@2069: elif arg == 'leg' or arg == 'legend' or arg == '-l': b@2069: if not enable_individual: b@2071: print "WARNING: The 'legend' option is only relevant to plots of "+\ b@2071: "individual ratings" b@2069: show_legend = True # show all individual ratings b@2071: elif arg.isnumeric(): b@2071: if not enable_confidence: b@2071: print "WARNING: The numeric confidence value is only relevant when "+\ b@2071: "confidence plot is enabled" b@2071: if float(arg)>0 and float(arg)<1: b@2071: confidence = float(arg) b@2071: b@2069: # FOLDER NAME b@2069: else: b@2069: # assume it's the folder name b@2069: rating_folder = arg b@2069: #TODO try it exists, otherwise show exception b@2069: b@2069: # at least one plot type should be selected: box plot by default b@2069: if not enable_boxplot and not enable_confidence and not enable_individual: b@2069: enable_boxplot = True b@2069: b@2069: # CONFIGURATION b@1554: b@1554: # Enter folder where rating CSV files are (generated with score_parser.py or same format). b@1554: rating_folder = '../saves/ratings/' # folder with rating csv files b@1554: b@1554: # Font settings b@1554: font = {'weight' : 'bold', b@1554: 'size' : 10} b@1554: plt.rc('font', **font) b@1554: b@1554: b@1554: # CODE b@1554: b@1554: # get every csv file in folder b@1554: for file in os.listdir(rating_folder): # You have to put this in folder where rating csv files are. b@1554: if file.endswith(".csv"): b@1554: page_name = file[:-4] # file name (without extension) is page ID b@1554: b@1554: # get header b@1554: with open(rating_folder+file, 'rb') as readfile: # read this csv file b@1554: filereader = csv.reader(readfile, delimiter=',') b@1554: headerrow = filereader.next() # use headerrow as X-axis b@1554: headerrow = headerrow[1:] b@1554: b@1554: # read ratings into matrix b@1554: # ratings = np.loadtxt(open(rating_folder+file,"rb"), b@1554: # delimiter=",", b@1554: # skiprows=1, b@1554: # usecols=range(1,len(headerrow)+1) b@1554: # ) b@1554: ratings = np.genfromtxt(readfile, b@1554: delimiter=",", b@1554: #skip_header = 1, b@1554: converters = {3: lambda s: float(s or 'Nan')}, b@1554: usecols=range(1,len(headerrow)+1) b@1554: ) b@1554: b@1554: # assert at least 2 subjects (move on to next file if violated) b@2057: if ratings.shape[0]<2: b@1554: print "WARNING: Just one subject for " + page_name + ". Moving on to next file." b@1554: break b@1554: b@1554: # BOXPLOT b@1554: if enable_boxplot: b@1554: plt.boxplot(ratings) b@1554: b@1554: # CONFIDENCE INTERVAL b@1554: if enable_confidence: b@1554: iterator = 0 b@1554: for column in ratings.T: # iterate over transposed matrix b@1554: # remove all 'Nan's from column b@1554: column = column[~np.isnan(column)] b@1554: b@1554: # get number of non-Nan ratings (= #subjects) b@1554: n = column.size b@1554: b@1554: # get mean b@1554: mean_rating = np.mean(column) b@1554: b@1554: # get errors b@1554: err = scipy.stats.sem(column)* sp.stats.t._ppf((1+confidence)/2., n-1) b@1554: b@1554: # draw plot b@1554: plt.errorbar(iterator+1, b@1554: mean_rating, b@1554: yerr=err, b@1554: marker="x", b@1554: color ="k", b@1554: markersize=12, b@1554: linestyle='None') b@1554: b@1554: iterator += 1 # increase counter b@1554: b@1554: b@1554: # INDIVIDUAL PLOT b@1554: if enable_individual or show_individual: b@1554: # marker list and color map to cycle through b@1554: markerlist = ["x", ".", "o", "*", "+", "v", ">", "<", "8", "s", "p"] b@1554: colormap = ['b', 'r', 'g', 'c', 'm', 'y', 'k'] b@1554: increment = 0 b@1554: linehandles = [] b@1554: legendnames = [] b@1554: with open(rating_folder+file, 'rb') as readfile: # read this csv file b@1554: filereader = csv.reader(readfile, delimiter=',') b@1554: headerrow = filereader.next() # use headerrow as X-axis b@1554: headerrow = headerrow[1:] b@1554: for row in filereader: b@1554: subject_id = row[0][:-4] # read from beginning of line b@1554: # assume plotting all individuals if no individual(s) specified b@1554: if not show_individual or subject_id in show_individual: b@1554: plothandle, = plt.plot(range(1,len(row)), # x-values b@1554: ratings[increment,:],#row[1:], # y-values: csv values except subject name b@1554: color=colormap[increment%len(colormap)], b@1554: marker=markerlist[increment%len(markerlist)], b@1554: markersize=10, b@1554: linestyle='None', b@1554: label=subject_id b@1554: ) b@1554: linehandles.append(plothandle) b@1554: legendnames.append(subject_id) b@1554: if show_legend: b@1554: plt.legend(linehandles, legendnames, b@1554: loc='upper right', b@1554: bbox_to_anchor=(1.1, 1), b@1554: borderaxespad=0., b@1554: numpoints=1 # remove extra marker b@1554: ) b@1554: increment += 1 # increase counter b@1554: b@1554: # TITLE, AXIS LABELS AND LIMITS b@1554: plt.title(page_name) b@1554: plt.xlabel('Fragment') b@1554: plt.xlim(0, len(headerrow)+1) # only show relevant region, leave space left & right) b@1554: plt.xticks(range(1, len(headerrow)+1), headerrow) # show fragment names b@1554: plt.ylabel('Rating') b@1554: plt.ylim(0,1) b@1554: b@1554: b@1554: b@1554: # SHOW PLOT b@1554: #plt.show() b@1554: #exit() b@1554: b@1554: # SAVE PLOT b@1554: # automatically b@1554: plot_type = ("-box" if enable_boxplot else "") + \ b@1554: ("-conf" if enable_confidence else "") + \ b@1554: ("-ind" if enable_individual else "") b@1554: plt.savefig(rating_folder+page_name+plot_type+".png") b@1554: plt.close()