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@1453: # CONFIGURATION nickjillings@1453: nickjillings@1453: # Which type(s) of plot do you want? nickjillings@1453: enable_boxplot = True # 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@1453: show_individual = [] # show specific individuals nickjillings@1453: show_legend = False # show names of individuals nickjillings@1453: #TODO: Merge, implement this functionality nickjillings@1453: #TODO: Control by CLI arguments (plot types, save and/or show, ...) nickjillings@1453: nickjillings@1453: # Enter folder where rating CSV files are (generated with score_parser.py or same format). nickjillings@1453: rating_folder = '../saves/ratings/' # folder with rating csv files 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@1453: plt.xticks(range(1, len(headerrow)+1), headerrow) # 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@1453: plt.savefig(rating_folder+page_name+plot_type+".png") nickjillings@1453: plt.close()