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