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