nickjillings@1528: #!/usr/bin/python nickjillings@1528: nickjillings@1528: import sys nickjillings@1528: import os nickjillings@1528: import csv nickjillings@1528: import matplotlib.pyplot as plt nickjillings@1528: import numpy as np nickjillings@1528: import scipy as sp nickjillings@1528: import scipy.stats nickjillings@1528: nickjillings@1528: # CONFIGURATION nickjillings@1528: nickjillings@1528: # Which type(s) of plot do you want? nickjillings@1528: enable_boxplot = True # show box plot nickjillings@1528: enable_confidence = False # show confidence interval nickjillings@1528: confidence = 0.90 # confidence value (for confidence interval plot) nickjillings@1528: enable_individual = False # show all individual ratings nickjillings@1528: show_individual = [] # show specific individuals nickjillings@1528: show_legend = False # show names of individuals nickjillings@1528: #TODO: Merge, implement this functionality nickjillings@1528: #TODO: Control by CLI arguments (plot types, save and/or show, ...) nickjillings@1528: nickjillings@1528: # Enter folder where rating CSV files are (generated with score_parser.py or same format). nickjillings@1528: rating_folder = '../saves/ratings/' # folder with rating csv files nickjillings@1528: nickjillings@1528: # Font settings nickjillings@1528: font = {'weight' : 'bold', nickjillings@1528: 'size' : 10} nickjillings@1528: plt.rc('font', **font) nickjillings@1528: nickjillings@1528: nickjillings@1528: # CODE nickjillings@1528: nickjillings@1528: # get every csv file in folder nickjillings@1528: for file in os.listdir(rating_folder): # You have to put this in folder where rating csv files are. nickjillings@1528: if file.endswith(".csv"): nickjillings@1528: page_name = file[:-4] # file name (without extension) is page ID nickjillings@1528: nickjillings@1528: # get header nickjillings@1528: with open(rating_folder+file, 'rb') as readfile: # read this csv file nickjillings@1528: filereader = csv.reader(readfile, delimiter=',') nickjillings@1528: headerrow = filereader.next() # use headerrow as X-axis nickjillings@1528: headerrow = headerrow[1:] nickjillings@1528: nickjillings@1528: # read ratings into matrix nickjillings@1528: # ratings = np.loadtxt(open(rating_folder+file,"rb"), nickjillings@1528: # delimiter=",", nickjillings@1528: # skiprows=1, nickjillings@1528: # usecols=range(1,len(headerrow)+1) nickjillings@1528: # ) nickjillings@1528: ratings = np.genfromtxt(readfile, nickjillings@1528: delimiter=",", nickjillings@1528: #skip_header = 1, nickjillings@1528: converters = {3: lambda s: float(s or 'Nan')}, nickjillings@1528: usecols=range(1,len(headerrow)+1) nickjillings@1528: ) nickjillings@1528: nickjillings@1528: # assert at least 2 subjects (move on to next file if violated) nickjillings@1528: if ratings.shape[0]<2: nickjillings@1528: print "WARNING: Just one subject for " + page_name + ". Moving on to next file." nickjillings@1528: break nickjillings@1528: nickjillings@1528: # BOXPLOT nickjillings@1528: if enable_boxplot: nickjillings@1528: plt.boxplot(ratings) nickjillings@1528: nickjillings@1528: # CONFIDENCE INTERVAL nickjillings@1528: if enable_confidence: nickjillings@1528: iterator = 0 nickjillings@1528: for column in ratings.T: # iterate over transposed matrix nickjillings@1528: # remove all 'Nan's from column nickjillings@1528: column = column[~np.isnan(column)] nickjillings@1528: nickjillings@1528: # get number of non-Nan ratings (= #subjects) nickjillings@1528: n = column.size nickjillings@1528: nickjillings@1528: # get mean nickjillings@1528: mean_rating = np.mean(column) nickjillings@1528: nickjillings@1528: # get errors nickjillings@1528: err = scipy.stats.sem(column)* sp.stats.t._ppf((1+confidence)/2., n-1) nickjillings@1528: nickjillings@1528: # draw plot nickjillings@1528: plt.errorbar(iterator+1, nickjillings@1528: mean_rating, nickjillings@1528: yerr=err, nickjillings@1528: marker="x", nickjillings@1528: color ="k", nickjillings@1528: markersize=12, nickjillings@1528: linestyle='None') nickjillings@1528: nickjillings@1528: iterator += 1 # increase counter nickjillings@1528: nickjillings@1528: nickjillings@1528: # INDIVIDUAL PLOT nickjillings@1528: if enable_individual or show_individual: nickjillings@1528: # marker list and color map to cycle through nickjillings@1528: markerlist = ["x", ".", "o", "*", "+", "v", ">", "<", "8", "s", "p"] nickjillings@1528: colormap = ['b', 'r', 'g', 'c', 'm', 'y', 'k'] nickjillings@1528: increment = 0 nickjillings@1528: linehandles = [] nickjillings@1528: legendnames = [] nickjillings@1528: with open(rating_folder+file, 'rb') as readfile: # read this csv file nickjillings@1528: filereader = csv.reader(readfile, delimiter=',') nickjillings@1528: headerrow = filereader.next() # use headerrow as X-axis nickjillings@1528: headerrow = headerrow[1:] nickjillings@1528: for row in filereader: nickjillings@1528: subject_id = row[0][:-4] # read from beginning of line nickjillings@1528: # assume plotting all individuals if no individual(s) specified nickjillings@1528: if not show_individual or subject_id in show_individual: nickjillings@1528: plothandle, = plt.plot(range(1,len(row)), # x-values nickjillings@1528: ratings[increment,:],#row[1:], # y-values: csv values except subject name nickjillings@1528: color=colormap[increment%len(colormap)], nickjillings@1528: marker=markerlist[increment%len(markerlist)], nickjillings@1528: markersize=10, nickjillings@1528: linestyle='None', nickjillings@1528: label=subject_id nickjillings@1528: ) nickjillings@1528: linehandles.append(plothandle) nickjillings@1528: legendnames.append(subject_id) nickjillings@1528: if show_legend: nickjillings@1528: plt.legend(linehandles, legendnames, nickjillings@1528: loc='upper right', nickjillings@1528: bbox_to_anchor=(1.1, 1), nickjillings@1528: borderaxespad=0., nickjillings@1528: numpoints=1 # remove extra marker nickjillings@1528: ) nickjillings@1528: increment += 1 # increase counter nickjillings@1528: nickjillings@1528: # TITLE, AXIS LABELS AND LIMITS nickjillings@1528: plt.title(page_name) nickjillings@1528: plt.xlabel('Fragment') nickjillings@1528: plt.xlim(0, len(headerrow)+1) # only show relevant region, leave space left & right) nickjillings@1528: plt.xticks(range(1, len(headerrow)+1), headerrow) # show fragment names nickjillings@1528: plt.ylabel('Rating') nickjillings@1528: plt.ylim(0,1) nickjillings@1528: nickjillings@1528: nickjillings@1528: nickjillings@1528: # SHOW PLOT nickjillings@1528: #plt.show() nickjillings@1528: #exit() nickjillings@1528: nickjillings@1528: # SAVE PLOT nickjillings@1528: # automatically nickjillings@1528: plot_type = ("-box" if enable_boxplot else "") + \ nickjillings@1528: ("-conf" if enable_confidence else "") + \ nickjillings@1528: ("-ind" if enable_individual else "") nickjillings@1528: plt.savefig(rating_folder+page_name+plot_type+".png") nickjillings@1528: plt.close()