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