Mercurial > hg > webaudioevaluationtool
changeset 2272:585bc3002e9d
score_plot.py compatible with Python 2.7 and 3.x: print ... --> print(...); next() instead of .next(); read text from CSV instead of bytes when iterating using next()
Merge branch 'master' of https://github.com/BrechtDeMan/WebAudioEvaluationTool
author | Brecht De Man <b.deman@qmul.ac.uk> |
---|---|
date | Wed, 20 Apr 2016 19:03:07 +0200 |
parents | 418e419ddfd1 (diff) e0b0dde71ab1 (current diff) |
children | ad2e070c92e5 |
files | README.md python/score_plot.py |
diffstat | 3 files changed, 50 insertions(+), 62 deletions(-) [+] |
line wrap: on
line diff
--- a/README.md Wed Apr 20 18:43:32 2016 +0200 +++ b/README.md Wed Apr 20 19:03:07 2016 +0200 @@ -16,14 +16,6 @@ Please refer to ['docs/Instructions/Instructions.pdf'](https://github.com/BrechtDeMan/WebAudioEvaluationTool/raw/master/docs/Instructions/Instructions.pdf). -## Dependencies - -Runs out of the box on any web server with PHP (tested on PHP 5.1<=), no third party software needed. - -Alternatively, a local server (no web server or internet connection needed!) and optional Python analysis scripts run on Python 2.7 or 3.x. -Plots rendered using [matplotlib](http://matplotlib.org), [NumPy](http://matplotlib.org) and [SciPy](http://scipy.org). - - ## Academic use ### Citing @@ -45,7 +37,6 @@ Please refer to LICENSE.txt ([GNU General Public License](http://www.gnu.org/licenses/gpl-3.0.en.html)). - ## Other use Other licensing schemes are available - please contact the authors regarding non-academic use.
--- a/python/comment_parser.py Wed Apr 20 18:43:32 2016 +0200 +++ b/python/comment_parser.py Wed Apr 20 19:03:07 2016 +0200 @@ -14,19 +14,19 @@ # XML results files location if len(sys.argv) == 1: folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder - print "Use: python comment_parser.py [XML_files_location]" - print "Using default path: " + folder_name + print("Use: python comment_parser.py [XML_files_location]") + print("Using default path: " + folder_name) elif len(sys.argv) == 2: folder_name = sys.argv[1] # First command line argument is folder # check if folder_name exists if not os.path.exists(folder_name): #the file is not there - print "Folder '"+folder_name+"' does not exist." + print("Folder '"+folder_name+"' does not exist.") sys.exit() # terminate script execution elif not os.access(os.path.dirname(folder_name), os.W_OK): #the file does exist but write privileges are not given - print "No write privileges in folder '"+folder_name+"'." + print("No write privileges in folder '"+folder_name+"'.") # CODE @@ -45,11 +45,11 @@ page_name = audioholder.get('ref') # get page name if page_name is None: # ignore 'empty' audio_holders - print "WARNING: " + file + " contains empty page. (comment_parser.py)" + print("WARNING: " + file + " contains empty page. (comment_parser.py)") break if audioholder.get("state") != "complete": - print "WARNING: " + file + "test page " + page_name + " is not complete, skipping." + print("WARNING: " + file + "test page " + page_name + " is not complete, skipping.") else: # create folder [page_name] if not yet created if not os.path.exists(folder_name + "/" + page_name): @@ -79,9 +79,9 @@ commentstr = '' # anonymous comments: - #writer.writerow([commentstr.encode("utf-8")]) + #writer.writerow([commentstr]) # .encode("utf-8") # comments with (file) name: - writer.writerow([file[:-4]] + [commentstr.encode("utf-8")]) + writer.writerow([file[:-4]] + [commentstr]) #TODO Replace 'new line' in comment with something else?
--- a/python/score_plot.py Wed Apr 20 18:43:32 2016 +0200 +++ b/python/score_plot.py Wed Apr 20 19:03:07 2016 +0200 @@ -31,34 +31,34 @@ # XML results files location if len(sys.argv) == 1: # no extra arguments enable_boxplot = True # show box plot - print "Use: python score_plot.py [rating folder] [plot_type] [-l/-legend]" - print "Type 'python score_plot.py -h' for help." - print "Using default path: " + rating_folder + " with boxplot." + print("Use: python score_plot.py [rating folder] [plot_type] [-l/-legend]") + print("Type 'python score_plot.py -h' for help.") + print("Using default path: " + rating_folder + " with boxplot.") else: for arg in sys.argv: # go over all arguments if arg == '-h': # show help #TODO: replace with contents of helpfile score_plot.info (or similar) - print "Use: python score_plot.py [rating_folder] [plot_type] [-l] [confidence]" - print " rating_folder:" - print " folder where output of 'score_parser' can be found, and" - print " where plots will be stored." - print " By default, '../saves/ratings/' is used." - print "" - print "PLOT TYPES" - print " Can be used in combination." - print " box | boxplot | -b" - print " Enables the boxplot" - print " conf | confidence | -c" - print " Enables the confidence interval plot" - print " ind | individual | -i" - print " Enables plot of individual ratings" - print "" - print "PLOT OPTIONS" - print " leg | legend | -l" - print " For individual plot: show legend with individual file names" - print " numeric value between 0 and 1, e.g. 0.95" - print " For confidence interval plot: confidence value" + print("Use: python score_plot.py [rating_folder] [plot_type] [-l] [confidence]") + print(" rating_folder:") + print(" folder where output of 'score_parser' can be found, and") + print(" where plots will be stored.") + print(" By default, '../saves/ratings/' is used.") + print("") + print("PLOT TYPES") + print(" Can be used in combination.") + print(" box | boxplot | -b") + print(" Enables the boxplot" ) + print(" conf | confidence | -c") + print(" Enables the confidence interval plot" ) + print(" ind | individual | -i") + print(" Enables plot of individual ratings" ) + print("") + print("PLOT OPTIONS") + print(" leg | legend | -l") + print(" For individual plot: show legend with individual file names") + print(" numeric value between 0 and 1, e.g. 0.95") + print(" For confidence interval plot: confidence value") assert False, ""# stop immediately after showing help #TODO cleaner way # PLOT TYPES @@ -73,17 +73,17 @@ # PLOT OPTIONS elif arg == 'leg' or arg == 'legend' or arg == '-l': if not enable_individual: - print "WARNING: The 'legend' option is only relevant to plots of "+\ - "individual ratings" + print("WARNING: The 'legend' option is only relevant to plots of "+\ + "individual ratings") show_legend = True # show all individual ratings elif arg.isdigit(): if not enable_confidence: - print "WARNING: The numeric confidence value is only relevant when "+\ - "confidence plot is enabled" + print("WARNING: The numeric confidence value is only relevant when "+\ + "confidence plot is enabled") if float(arg)>0 and float(arg)<1: confidence = float(arg) else: - print "WARNING: The confidence value needs to be between 0 and 1" + print("WARNING: The confidence value needs to be between 0 and 1") # FOLDER NAME else: @@ -97,11 +97,11 @@ # check if folder_name exists if not os.path.exists(rating_folder): #the file is not there - print "Folder '"+rating_folder+"' does not exist." + print("Folder '"+rating_folder+"' does not exist.") sys.exit() # terminate script execution elif not os.access(os.path.dirname(rating_folder), os.W_OK): #the file does exist but write rating_folder are not given - print "No write privileges in folder '"+rating_folder+"'." + print("No write privileges in folder '"+rating_folder+"'.") # CONFIGURATION @@ -119,28 +119,25 @@ if file.endswith(".csv"): page_name = file[:-4] # file name (without extension) is page ID - # get header + # get header (as text) + with open(rating_folder+file, 'rt') as readfile: # read this csv file + filereader = csv.reader(readfile, delimiter=',') + headerrow = next(filereader) # use headerrow as X-axis + headerrow = headerrow[1:] + + # read ratings into matrix (as bytes) with open(rating_folder+file, 'rb') as readfile: # read this csv file filereader = csv.reader(readfile, delimiter=',') - headerrow = filereader.next() # use headerrow as X-axis - headerrow = headerrow[1:] - - # read ratings into matrix -# ratings = np.loadtxt(open(rating_folder+file,"rb"), -# delimiter=",", -# skiprows=1, -# usecols=range(1,len(headerrow)+1) -# ) ratings = np.genfromtxt(readfile, delimiter=",", - #skip_header = 1, + skip_header = 1, converters = {3: lambda s: float(s or 'Nan')}, - usecols=range(1,len(headerrow)+1) + usecols=list(range(1,len(headerrow)+1)) ) # assert at least 2 subjects (move on to next file if violated) if ratings.shape[0]<2: - print "WARNING: Just one subject for " + page_name + ". Moving on to next file." + print("WARNING: Just one subject for " + page_name + ". Moving on to next file.") break # BOXPLOT @@ -183,9 +180,9 @@ increment = 0 linehandles = [] legendnames = [] - with open(rating_folder+file, 'rb') as readfile: # read this csv file + with open(rating_folder+file, 'r') as readfile: # read this csv file filereader = csv.reader(readfile, delimiter=',') - headerrow = filereader.next() # use headerrow as X-axis + headerrow = next(filereader) # use headerrow as X-axis headerrow = headerrow[1:] for row in filereader: subject_id = row[0][:-4] # read from beginning of line