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