annotate scripts/generate_report.py @ 1405:a9f1b91b6335

Minor fixes to test_create Specification before merge with core.js Specification
author Nicholas Jillings <nickjillings@users.noreply.github.com>
date Fri, 04 Dec 2015 18:51:58 +0000
parents 3b9f0bc523d6
children 17b550310734 235594325b84
rev   line source
nickjillings@1404 1 #!/usr/bin/python
nickjillings@1404 2 # -*- coding: utf-8 -*-
nickjillings@1404 3
nickjillings@1404 4 import xml.etree.ElementTree as ET
nickjillings@1404 5 import os # for getting files from directory
nickjillings@1404 6 import operator # for sorting data with multiple keys
nickjillings@1404 7 import sys # for accessing command line arguments
nickjillings@1404 8 import subprocess # for calling pdflatex
nickjillings@1404 9 import shlex # for calling pdflatex
nickjillings@1404 10 import matplotlib.pyplot as plt # plots
nickjillings@1404 11 import numpy as np # numbers
nickjillings@1404 12
nickjillings@1404 13 # Command line arguments
nickjillings@1404 14 assert len(sys.argv)<4, "evaluation_stats takes at most 2 command line argument\n"+\
nickjillings@1404 15 "Use: python generate_report.py [results_folder] [no_render | -nr]"
nickjillings@1404 16
nickjillings@1404 17 render_figures = True
nickjillings@1404 18
nickjillings@1404 19 # XML results files location
nickjillings@1404 20 if len(sys.argv) == 1:
nickjillings@1404 21 folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder
nickjillings@1404 22 print "Use: python generate_report.py [results_folder] [no_render | -nr]"
nickjillings@1404 23 print "Using default path: " + folder_name
nickjillings@1404 24 elif len(sys.argv) == 2:
nickjillings@1404 25 folder_name = sys.argv[1] # First command line argument is folder
nickjillings@1404 26 elif len(sys.argv) == 3:
nickjillings@1404 27 folder_name = sys.argv[1] # First command line argument is folder
nickjillings@1404 28 assert sys.argv[2] in ('no_render','-nr'), "Second argument not recognised. \n" +\
nickjillings@1404 29 "Use: python generate_report.py [results_folder] [no_render | -nr]"
nickjillings@1404 30 # Second command line argument is [no_render | -nr]
nickjillings@1404 31 render_figures = False
nickjillings@1404 32
nickjillings@1404 33 def isNaN(num):
nickjillings@1404 34 return num != num
nickjillings@1404 35
nickjillings@1404 36 # Turn number of seconds (int) to '[minutes] min [seconds] s' (string)
nickjillings@1404 37 def seconds2timestr(time_in_seconds):
nickjillings@1404 38 if time_in_seconds is not None and not isNaN(time_in_seconds):
nickjillings@1404 39 time_in_minutes = int(time_in_seconds/60)
nickjillings@1404 40 remaining_seconds = int(time_in_seconds%60)
nickjillings@1404 41 return str(time_in_minutes) + " min " + str(remaining_seconds) + " s"
nickjillings@1404 42 else:
nickjillings@1404 43 return 'N/A'
nickjillings@1404 44
nickjillings@1404 45 # stats initialisation
nickjillings@1404 46 number_of_XML_files = 0
nickjillings@1404 47 number_of_pages = 0
nickjillings@1404 48 number_of_fragments = 0
nickjillings@1404 49 total_empty_comments = 0
nickjillings@1404 50 total_not_played = 0
nickjillings@1404 51 total_not_moved = 0
nickjillings@1404 52 time_per_page_accum = 0
nickjillings@1404 53
nickjillings@1404 54 # arrays initialisation
nickjillings@1404 55 page_names = []
nickjillings@1404 56 real_page_names = [] # regardless of differing numbers of fragments
nickjillings@1404 57 subject_count = [] # subjects per audioholder name
nickjillings@1404 58 page_count = []
nickjillings@1404 59 duration_page = [] # duration of experiment in function of page content
nickjillings@1404 60 duration_order = [] # duration of experiment in function of page number
nickjillings@1404 61 fragments_per_page = [] # number of fragments for corresponding page
nickjillings@1404 62
nickjillings@1404 63 # survey stats
nickjillings@1404 64 gender = []
nickjillings@1404 65 age = []
nickjillings@1404 66
nickjillings@1404 67 # get username if available
nickjillings@1404 68 for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
nickjillings@1404 69 user = os.environ.get(name)
nickjillings@1404 70 if user:
nickjillings@1404 71 break
nickjillings@1404 72 else:
nickjillings@1404 73 user = ''
nickjillings@1404 74
nickjillings@1404 75
nickjillings@1404 76 # begin LaTeX document
nickjillings@1404 77 header = r'''\documentclass[11pt, oneside]{article}
nickjillings@1404 78 \usepackage{geometry}
nickjillings@1404 79 \geometry{a4paper}
nickjillings@1404 80 \usepackage[parfill]{parskip} % empty line instead of indent
nickjillings@1404 81 \usepackage{graphicx} % figures
nickjillings@1404 82 \usepackage{hyperref}
nickjillings@1404 83 \usepackage{tikz} % pie charts
nickjillings@1404 84 \title{Report}
nickjillings@1404 85 \author{'''+\
nickjillings@1404 86 user+\
nickjillings@1404 87 r'''}
nickjillings@1404 88 \graphicspath{{'''+\
nickjillings@1404 89 folder_name+\
nickjillings@1404 90 r'''/}}
nickjillings@1404 91 %\setcounter{section}{-1} % Summary section 0 so number of sections equals number of files
nickjillings@1404 92 \begin{document}
nickjillings@1404 93 \maketitle
nickjillings@1404 94 This is an automatically generated report using the `generate\_report.py' Python script
nickjillings@1404 95 included with the Web Audio Evaluation Tool \cite{WAET} distribution which can be found
nickjillings@1404 96 at \texttt{code.soundsoftware.ac.uk/projects/webaudioevaluationtool}.
nickjillings@1404 97 \tableofcontents
nickjillings@1404 98
nickjillings@1404 99 '''
nickjillings@1404 100
nickjillings@1404 101 footer = '\n\t\t'+r'''\begin{thebibliography}{9}
nickjillings@1404 102 \bibitem{WAET} % reference to accompanying publication
nickjillings@1404 103 Nicholas Jillings, Brecht De Man, David Moffat and Joshua D. Reiss,
nickjillings@1404 104 ``Web Audio Evaluation Tool: A browser-based listening test environment,''
nickjillings@1404 105 presented at the 12th Sound and Music Computing Conference, July 2015.
nickjillings@1404 106 \end{thebibliography}
nickjillings@1404 107 \end{document}'''
nickjillings@1404 108
nickjillings@1404 109 body = ''
nickjillings@1404 110
nickjillings@1404 111 # generate images for later use
nickjillings@1404 112 if render_figures:
nickjillings@1404 113 subprocess.call("python timeline_view_movement.py "+folder_name, shell=True)
nickjillings@1404 114 subprocess.call("python score_parser.py "+folder_name, shell=True)
nickjillings@1404 115 subprocess.call("python score_plot.py "+folder_name, shell=True)
nickjillings@1404 116
nickjillings@1404 117 # get every XML file in folder
nickjillings@1404 118 files_list = os.listdir(folder_name)
nickjillings@1404 119 for file in files_list: # iterate over all files in files_list
nickjillings@1404 120 if file.endswith(".xml"): # check if XML file
nickjillings@1404 121 number_of_XML_files += 1
nickjillings@1404 122 tree = ET.parse(folder_name + '/' + file)
nickjillings@1404 123 root = tree.getroot()
nickjillings@1404 124
nickjillings@1404 125 # PRINT name as section
nickjillings@1404 126 body+= '\n\section{'+file[:-4].capitalize()+'}\n' # make section header from name without extension
nickjillings@1404 127
nickjillings@1404 128 # reset for new subject
nickjillings@1404 129 total_duration = 0
nickjillings@1404 130 page_number = 0
nickjillings@1404 131
nickjillings@1404 132 individual_table = '\n' # table with stats for this individual test file
nickjillings@1404 133 timeline_plots = '' # plots of timeline (movements and plays)
nickjillings@1404 134
nickjillings@1404 135 # DEMO survey stats
nickjillings@1404 136 # get gender
nickjillings@1404 137 this_subjects_gender = root.find("./posttest/radio/[@id='gender']")
nickjillings@1404 138 if this_subjects_gender is not None:
nickjillings@1404 139 gender.append(this_subjects_gender.get("name"))
nickjillings@1404 140 else:
nickjillings@1404 141 gender.append('UNAVAILABLE')
nickjillings@1404 142 # get age
nickjillings@1404 143 this_subjects_age = root.find("./posttest/number/[@id='age']")
nickjillings@1404 144 if this_subjects_age is not None:
nickjillings@1404 145 age.append(this_subjects_age.text)
nickjillings@1404 146 #TODO add plot of age
nickjillings@1404 147
nickjillings@1404 148 # get list of all page names
nickjillings@1404 149 for audioholder in root.findall("./audioholder"): # iterate over pages
nickjillings@1404 150 page_name = audioholder.get('id') # get page name
nickjillings@1404 151
nickjillings@1404 152 if page_name is None: # ignore 'empty' audio_holders
nickjillings@1404 153 print "WARNING: " + file + " contains empty audio holder. (evaluation_stats.py)"
nickjillings@1404 154 break # move on to next
nickjillings@1404 155
nickjillings@1404 156 number_of_comments = 0 # for this page
nickjillings@1404 157 number_of_missing_comments = 0 # for this page
nickjillings@1404 158 not_played = [] # for this page
nickjillings@1404 159 not_moved = [] # for this page
nickjillings@1404 160
nickjillings@1404 161 if audioholder.find("./metric/metricresult[@id='testTime']") is not None: # check if time is included
nickjillings@1404 162 # 'testTime' keeps total duration: subtract time so far for duration of this audioholder
nickjillings@1404 163 duration = float(audioholder.find("./metric/metricresult[@id='testTime']").text) - total_duration
nickjillings@1404 164
nickjillings@1404 165 # total duration of test
nickjillings@1404 166 total_duration += duration
nickjillings@1404 167 else:
nickjillings@1404 168 duration = float('nan')
nickjillings@1404 169 total_duration = float('nan')
nickjillings@1404 170
nickjillings@1404 171 # number of audio elements
nickjillings@1404 172 audioelements = audioholder.findall("./audioelement") # get audioelements
nickjillings@1404 173 number_of_fragments += len(audioelements) # add length of this list to total
nickjillings@1404 174
nickjillings@1404 175 # number of comments (interesting if comments not mandatory)
nickjillings@1404 176 for audioelement in audioelements:
nickjillings@1404 177 response = audioelement.find("./comment/response")
nickjillings@1404 178 was_played = audioelement.find("./metric/metricresult/[@name='elementFlagListenedTo']")
nickjillings@1404 179 was_moved = audioelement.find("./metric/metricresult/[@name='elementFlagMoved']")
nickjillings@1404 180 if response.text is not None and len(response.text) > 1:
nickjillings@1404 181 number_of_comments += 1
nickjillings@1404 182 else:
nickjillings@1404 183 number_of_missing_comments += 1
nickjillings@1404 184 if was_played is not None and was_played.text == 'false':
nickjillings@1404 185 not_played.append(audioelement.get('id'))
nickjillings@1404 186 if was_moved is not None and was_moved.text == 'false':
nickjillings@1404 187 not_moved.append(audioelement.get('id'))
nickjillings@1404 188
nickjillings@1404 189 # update global counters
nickjillings@1404 190 total_empty_comments += number_of_missing_comments
nickjillings@1404 191 total_not_played += len(not_played)
nickjillings@1404 192 total_not_moved += len(not_moved)
nickjillings@1404 193
nickjillings@1404 194 # PRINT alerts when elements not played or markers not moved
nickjillings@1404 195 # number of audio elements not played
nickjillings@1404 196 if len(not_played) > 1:
nickjillings@1404 197 body += '\t\t\\emph{\\textbf{ATTENTION: '+str(len(not_played))+\
nickjillings@1404 198 ' fragments were not listened to in '+page_name+'! }}'+\
nickjillings@1404 199 ', '.join(not_played)+'\\\\ \n'
nickjillings@1404 200 if len(not_played) == 1:
nickjillings@1404 201 body += '\t\t\\emph{\\textbf{ATTENTION: one fragment was not listened to in '+page_name+'! }}'+\
nickjillings@1404 202 not_played[0]+'\\\\ \n'
nickjillings@1404 203
nickjillings@1404 204 # number of audio element markers not moved
nickjillings@1404 205 if len(not_moved) > 1:
nickjillings@1404 206 body += '\t\t\\emph{\\textbf{ATTENTION: '+str(len(not_moved))+\
nickjillings@1404 207 ' markers were not moved in '+page_name+'! }}'+\
nickjillings@1404 208 ', '.join(not_moved)+'\\\\ \n'
nickjillings@1404 209 if len(not_moved) == 1:
nickjillings@1404 210 body += '\t\t\\emph{\\textbf{ATTENTION: one marker was not moved in '+page_name+'! }}'+\
nickjillings@1404 211 not_moved[0]+'\\\\ \n'
nickjillings@1404 212
nickjillings@1404 213 # PRINT song-specific statistic
nickjillings@1404 214 individual_table += '\t\t'+page_name+'&'+\
nickjillings@1404 215 str(number_of_comments) + '/' +\
nickjillings@1404 216 str(number_of_comments+number_of_missing_comments)+'&'+\
nickjillings@1404 217 seconds2timestr(duration)+'\\\\\n'
nickjillings@1404 218
nickjillings@1404 219 # get timeline for this audioholder
nickjillings@1404 220 img_path = 'timelines_movement/'+file[:-4]+'-'+page_name+'.pdf'
nickjillings@1404 221
nickjillings@1404 222 # check if available
nickjillings@1404 223 if os.path.isfile(folder_name+'/'+img_path):
nickjillings@1404 224 # SHOW timeline image
nickjillings@1404 225 timeline_plots += '\\includegraphics[width=\\textwidth]{'+\
nickjillings@1404 226 folder_name+'/'+img_path+'}\n\t\t'
nickjillings@1404 227
nickjillings@1404 228 # keep track of duration in function of page index
nickjillings@1404 229 if len(duration_order)>page_number:
nickjillings@1404 230 duration_order[page_number].append(duration)
nickjillings@1404 231 else:
nickjillings@1404 232 duration_order.append([duration])
nickjillings@1404 233
nickjillings@1404 234 # keep list of audioholder ids and count how many times each audioholder id
nickjillings@1404 235 # was tested, how long it took, and how many fragments there were
nickjillings@1404 236 # (if number of fragments is different, store as different audioholder id)
nickjillings@1404 237 if page_name in page_names:
nickjillings@1404 238 page_index = page_names.index(page_name) # get index
nickjillings@1404 239 # check if number of audioelements the same
nickjillings@1404 240 if len(audioelements) == fragments_per_page[page_index]:
nickjillings@1404 241 page_count[page_index] += 1
nickjillings@1404 242 duration_page[page_index].append(duration)
nickjillings@1404 243 else: # make new entry
nickjillings@1404 244 alt_page_name = page_name+"("+str(len(audioelements))+")"
nickjillings@1404 245 if alt_page_name in page_names: # if already there
nickjillings@1404 246 alt_page_index = page_names.index(alt_page_name) # get index
nickjillings@1404 247 page_count[alt_page_index] += 1
nickjillings@1404 248 duration_page[alt_page_index].append(duration)
nickjillings@1404 249 else:
nickjillings@1404 250 page_names.append(alt_page_name)
nickjillings@1404 251 page_count.append(1)
nickjillings@1404 252 duration_page.append([duration])
nickjillings@1404 253 fragments_per_page.append(len(audioelements))
nickjillings@1404 254 else:
nickjillings@1404 255 page_names.append(page_name)
nickjillings@1404 256 page_count.append(1)
nickjillings@1404 257 duration_page.append([duration])
nickjillings@1404 258 fragments_per_page.append(len(audioelements))
nickjillings@1404 259
nickjillings@1404 260 # number of subjects per audioholder regardless of differing numbers of
nickjillings@1404 261 # fragments (for inclusion in box plots)
nickjillings@1404 262 if page_name in real_page_names:
nickjillings@1404 263 page_index = real_page_names.index(page_name) # get index
nickjillings@1404 264 subject_count[page_index] += 1
nickjillings@1404 265 else:
nickjillings@1404 266 real_page_names.append(page_name)
nickjillings@1404 267 subject_count.append(1)
nickjillings@1404 268
nickjillings@1404 269 # bookkeeping
nickjillings@1404 270 page_number += 1 # increase page count for this specific test
nickjillings@1404 271 number_of_pages += 1 # increase total number of pages
nickjillings@1404 272 time_per_page_accum += duration # total duration (for average time spent per page)
nickjillings@1404 273
nickjillings@1404 274 # PRINT table with statistics about this test
nickjillings@1404 275 body += '\t\t'+r'''\begin{tabular}{|p{3.5cm}|c|p{2.5cm}|}
nickjillings@1404 276 \hline
nickjillings@1404 277 \textbf{Song name} & \textbf{Comments} & \textbf{Duration} \\ \hline '''+\
nickjillings@1404 278 individual_table+'\t\t'+\
nickjillings@1404 279 r'''\hline
nickjillings@1404 280 \textbf{TOTAL} & & \textbf{'''+\
nickjillings@1404 281 seconds2timestr(total_duration)+\
nickjillings@1404 282 r'''}\\
nickjillings@1404 283 \hline
nickjillings@1404 284 \end{tabular}
nickjillings@1404 285
nickjillings@1404 286 '''
nickjillings@1404 287 # PRINT timeline plots
nickjillings@1404 288 body += timeline_plots
nickjillings@1404 289
nickjillings@1404 290 # join to footer
nickjillings@1404 291 footer = body + footer
nickjillings@1404 292
nickjillings@1404 293 # empty body again
nickjillings@1404 294 body = ''
nickjillings@1404 295
nickjillings@1404 296 # PRINT summary of everything (at start)
nickjillings@1404 297 # unnumbered so that number of sections equals number of files
nickjillings@1404 298 body += '\section*{Summary}\n\t\t\\addcontentsline{toc}{section}{Summary}\n'
nickjillings@1404 299
nickjillings@1404 300 # PRINT table with statistics
nickjillings@1404 301 body += '\t\t\\begin{tabular}{ll}\n\t\t\t'
nickjillings@1404 302 body += r'Number of XML files: &' + str(number_of_XML_files) + r'\\'+'\n\t\t\t'
nickjillings@1404 303 body += r'Number of pages: &' + str(number_of_pages) + r'\\'+'\n\t\t\t'
nickjillings@1404 304 body += r'Number of fragments: &' + str(number_of_fragments) + r'\\'+'\n\t\t\t'
nickjillings@1404 305 body += r'Number of empty comments: &' + str(total_empty_comments) +\
nickjillings@1404 306 " (" + str(round(100.0*total_empty_comments/number_of_fragments,2)) + r"\%)\\"+'\n\t\t\t'
nickjillings@1404 307 body += r'Number of unplayed fragments: &' + str(total_not_played) +\
nickjillings@1404 308 " (" + str(round(100.0*total_not_played/number_of_fragments,2)) + r"\%)\\"+'\n\t\t\t'
nickjillings@1404 309 body += r'Number of unmoved markers: &' + str(total_not_moved) +\
nickjillings@1404 310 " (" + str(round(100.0*total_not_moved/number_of_fragments,2)) + r"\%)\\"+'\n\t\t\t'
nickjillings@1404 311 body += r'Average time per page: &' + seconds2timestr(time_per_page_accum/number_of_pages) + r"\\"+'\n\t\t'
nickjillings@1404 312 body += '\\end{tabular} \\vspace{1.5cm} \\\\ \n'
nickjillings@1404 313
nickjillings@1404 314 # Average duration for first, second, ... page
nickjillings@1404 315 body += "\t\t\\vspace{.5cm} \n\n\t\tAverage duration per page (see also Figure \\ref{fig:avgtimeperpage}): \\\\ \n\t\t"
nickjillings@1404 316 body += r'''\begin{tabular}{lll}
nickjillings@1404 317 \textbf{Page} & \textbf{Duration} & \textbf{\# subjects}\\'''
nickjillings@1404 318 tpp_averages = [] # store average time per page
nickjillings@1404 319 for page_number in range(len(duration_order)):
nickjillings@1404 320 body += '\n\t\t\t'+str(page_number+1) + "&" +\
nickjillings@1404 321 seconds2timestr(sum(duration_order[page_number])/len(duration_order[page_number])) +\
nickjillings@1404 322 "&"+str(len(duration_order[page_number]))+r"\\"
nickjillings@1404 323 tpp_averages.append(sum(duration_order[page_number])/len(duration_order[page_number]))
nickjillings@1404 324
nickjillings@1404 325 body += '\n\t\t\\end{tabular} \\vspace{1.5cm} \\\\ \n\n\t\t'
nickjillings@1404 326
nickjillings@1404 327 # SHOW bar plot of average time per page
nickjillings@1404 328 plt.bar(range(1,len(duration_order)+1), np.array(tpp_averages)/60)
nickjillings@1404 329 plt.xlabel('Page order')
nickjillings@1404 330 plt.xlim(.8, len(duration_order)+1)
nickjillings@1404 331 plt.xticks(np.arange(1,len(duration_order)+1)+.4, range(1,len(duration_order)+1))
nickjillings@1404 332 plt.ylabel('Average time [minutes]')
nickjillings@1404 333 plt.savefig(folder_name+"/time_per_page.pdf", bbox_inches='tight')
nickjillings@1404 334 plt.close()
nickjillings@1404 335 #TODO add error bars
nickjillings@1404 336
nickjillings@1404 337
nickjillings@1404 338 # Sort pages by number of audioelements, then by duration
nickjillings@1404 339
nickjillings@1404 340 # average duration and number of subjects per page
nickjillings@1404 341 average_duration_page = []
nickjillings@1404 342 number_of_subjects_page = []
nickjillings@1404 343 for line in duration_page:
nickjillings@1404 344 number_of_subjects_page.append(len(line))
nickjillings@1404 345 average_duration_page.append(sum(line)/len(line))
nickjillings@1404 346
nickjillings@1404 347 # combine and sort in function of number of audioelements and duration
nickjillings@1404 348 combined_list = [page_names, average_duration_page, fragments_per_page, number_of_subjects_page]
nickjillings@1404 349 combined_list = sorted(zip(*combined_list), key=operator.itemgetter(1, 2)) # sort
nickjillings@1404 350
nickjillings@1404 351 # Show average duration for all songs
nickjillings@1404 352 body += r'''\vspace{.5cm}
nickjillings@1404 353 Average duration per audioholder (see also Figure \ref{fig:avgtimeperaudioholder}): \\
nickjillings@1404 354 \begin{tabular}{llll}
nickjillings@1404 355 \textbf{Audioholder} & \textbf{Duration} & \textbf{\# subjects} & \textbf{\# fragments} \\'''
nickjillings@1404 356 audioholder_names_ordered = []
nickjillings@1404 357 average_duration_audioholder_ordered = []
nickjillings@1404 358 number_of_subjects = []
nickjillings@1404 359 for page_index in range(len(page_names)):
nickjillings@1404 360 audioholder_names_ordered.append(combined_list[page_index][0])
nickjillings@1404 361 average_duration_audioholder_ordered.append(combined_list[page_index][1])
nickjillings@1404 362 number_of_subjects.append(combined_list[page_index][3])
nickjillings@1404 363 body += '\n\t\t\t'+combined_list[page_index][0] + "&" +\
nickjillings@1404 364 seconds2timestr(combined_list[page_index][1]) + "&" +\
nickjillings@1404 365 str(combined_list[page_index][3]) + "&" +\
nickjillings@1404 366 str(combined_list[page_index][2]) + r"\\"
nickjillings@1404 367 body += '\n\t\t\\end{tabular}\n'
nickjillings@1404 368
nickjillings@1404 369 # SHOW bar plot of average time per page
nickjillings@1404 370 plt.bar(range(1,len(audioholder_names_ordered)+1), np.array(average_duration_audioholder_ordered)/60)
nickjillings@1404 371 plt.xlabel('Audioholder')
nickjillings@1404 372 plt.xlim(.8, len(audioholder_names_ordered)+1)
nickjillings@1404 373 plt.xticks(np.arange(1,len(audioholder_names_ordered)+1)+.4, audioholder_names_ordered, rotation=90)
nickjillings@1404 374 plt.ylabel('Average time [minutes]')
nickjillings@1404 375 plt.savefig(folder_name+"/time_per_audioholder.pdf", bbox_inches='tight')
nickjillings@1404 376 plt.close()
nickjillings@1404 377
nickjillings@1404 378 # SHOW bar plot of average time per page
nickjillings@1404 379 plt.bar(range(1,len(audioholder_names_ordered)+1), number_of_subjects)
nickjillings@1404 380 plt.xlabel('Audioholder')
nickjillings@1404 381 plt.xlim(.8, len(audioholder_names_ordered)+1)
nickjillings@1404 382 plt.xticks(np.arange(1,len(audioholder_names_ordered)+1)+.4, audioholder_names_ordered, rotation=90)
nickjillings@1404 383 plt.ylabel('Number of subjects')
nickjillings@1404 384 ax = plt.gca()
nickjillings@1404 385 ylims = ax.get_ylim()
nickjillings@1404 386 yint = np.arange(int(np.floor(ylims[0])), int(np.ceil(ylims[1]))+1)
nickjillings@1404 387 plt.yticks(yint)
nickjillings@1404 388 plt.savefig(folder_name+"/subjects_per_audioholder.pdf", bbox_inches='tight')
nickjillings@1404 389 plt.close()
nickjillings@1404 390
nickjillings@1404 391 # SHOW both figures
nickjillings@1404 392 body += r'''
nickjillings@1404 393 \begin{figure}[htbp]
nickjillings@1404 394 \begin{center}
nickjillings@1404 395 \includegraphics[width=.65\textwidth]{'''+\
nickjillings@1404 396 folder_name+"/time_per_page.pdf"+\
nickjillings@1404 397 r'''}
nickjillings@1404 398 \caption{Average time spent per page.}
nickjillings@1404 399 \label{fig:avgtimeperpage}
nickjillings@1404 400 \end{center}
nickjillings@1404 401 \end{figure}
nickjillings@1404 402
nickjillings@1404 403 '''
nickjillings@1404 404 body += r'''\begin{figure}[htbp]
nickjillings@1404 405 \begin{center}
nickjillings@1404 406 \includegraphics[width=.65\textwidth]{'''+\
nickjillings@1404 407 folder_name+"/time_per_audioholder.pdf"+\
nickjillings@1404 408 r'''}
nickjillings@1404 409 \caption{Average time spent per audioholder.}
nickjillings@1404 410 \label{fig:avgtimeperaudioholder}
nickjillings@1404 411 \end{center}
nickjillings@1404 412 \end{figure}
nickjillings@1404 413
nickjillings@1404 414 '''
nickjillings@1404 415 body += r'''\begin{figure}[htbp]
nickjillings@1404 416 \begin{center}
nickjillings@1404 417 \includegraphics[width=.65\textwidth]{'''+\
nickjillings@1404 418 folder_name+"/subjects_per_audioholder.pdf"+\
nickjillings@1404 419 r'''}
nickjillings@1404 420 \caption{Number of subjects per audioholder.}
nickjillings@1404 421 \label{fig:subjectsperaudioholder}
nickjillings@1404 422 \end{center}
nickjillings@1404 423 \end{figure}
nickjillings@1404 424
nickjillings@1404 425 '''
nickjillings@1404 426 #TODO add error bars
nickjillings@1404 427 #TODO layout of figures
nickjillings@1404 428
nickjillings@1404 429 # SHOW boxplot per audioholder
nickjillings@1404 430 #TODO order in decreasing order of participants
nickjillings@1404 431 for audioholder_name in page_names: # get each name
nickjillings@1404 432 # plot boxplot if exists (not so for the 'alt' names)
nickjillings@1404 433 if os.path.isfile(folder_name+'/ratings/'+audioholder_name+'-ratings-box.pdf'):
nickjillings@1404 434 body += r'''\begin{figure}[htbp]
nickjillings@1404 435 \begin{center}
nickjillings@1404 436 \includegraphics[width=.65\textwidth]{'''+\
nickjillings@1404 437 folder_name+"/ratings/"+audioholder_name+'-ratings-box.pdf'+\
nickjillings@1404 438 r'''}
nickjillings@1404 439 \caption{Box plot of ratings for audioholder '''+\
nickjillings@1404 440 audioholder_name+' ('+str(subject_count[real_page_names.index(audioholder_name)])+\
nickjillings@1404 441 ''' participants).}
nickjillings@1404 442 \label{fig:boxplot'''+audioholder_name.replace(" ", "")+'''}
nickjillings@1404 443 \end{center}
nickjillings@1404 444 \end{figure}
nickjillings@1404 445
nickjillings@1404 446 '''
nickjillings@1404 447
nickjillings@1404 448 # DEMO pie chart of gender distribution among subjects
nickjillings@1404 449 genders = ['male', 'female', 'other', 'preferNotToSay', 'UNAVAILABLE']
nickjillings@1404 450 # TODO: get the above automatically
nickjillings@1404 451 gender_distribution = ''
nickjillings@1404 452 for item in genders:
nickjillings@1404 453 number = gender.count(item)
nickjillings@1404 454 if number>0:
nickjillings@1404 455 gender_distribution += str("{:.2f}".format((100.0*number)/len(gender)))+\
nickjillings@1404 456 '/'+item.capitalize()+' ('+str(number)+'),\n'
nickjillings@1404 457
nickjillings@1404 458 body += r'''
nickjillings@1404 459 % Pie chart of gender distribution
nickjillings@1404 460 \def\angle{0}
nickjillings@1404 461 \def\radius{3}
nickjillings@1404 462 \def\cyclelist{{"orange","blue","red","green"}}
nickjillings@1404 463 \newcount\cyclecount \cyclecount=-1
nickjillings@1404 464 \newcount\ind \ind=-1
nickjillings@1404 465 \begin{figure}[htbp]
nickjillings@1404 466 \begin{center}\begin{tikzpicture}[nodes = {font=\sffamily}]
nickjillings@1404 467 \foreach \percent/\name in {'''+\
nickjillings@1404 468 gender_distribution+\
nickjillings@1404 469 r'''} {\ifx\percent\empty\else % If \percent is empty, do nothing
nickjillings@1404 470 \global\advance\cyclecount by 1 % Advance cyclecount
nickjillings@1404 471 \global\advance\ind by 1 % Advance list index
nickjillings@1404 472 \ifnum6<\cyclecount % If cyclecount is larger than list
nickjillings@1404 473 \global\cyclecount=0 % reset cyclecount and
nickjillings@1404 474 \global\ind=0 % reset list index
nickjillings@1404 475 \fi
nickjillings@1404 476 \pgfmathparse{\cyclelist[\the\ind]} % Get color from cycle list
nickjillings@1404 477 \edef\color{\pgfmathresult} % and store as \color
nickjillings@1404 478 % Draw angle and set labels
nickjillings@1404 479 \draw[fill={\color!50},draw={\color}] (0,0) -- (\angle:\radius)
nickjillings@1404 480 arc (\angle:\angle+\percent*3.6:\radius) -- cycle;
nickjillings@1404 481 \node at (\angle+0.5*\percent*3.6:0.7*\radius) {\percent\,\%};
nickjillings@1404 482 \node[pin=\angle+0.5*\percent*3.6:\name]
nickjillings@1404 483 at (\angle+0.5*\percent*3.6:\radius) {};
nickjillings@1404 484 \pgfmathparse{\angle+\percent*3.6} % Advance angle
nickjillings@1404 485 \xdef\angle{\pgfmathresult} % and store in \angle
nickjillings@1404 486 \fi
nickjillings@1404 487 };
nickjillings@1404 488 \end{tikzpicture}
nickjillings@1404 489 \caption{Representation of gender across subjects}
nickjillings@1404 490 \label{default}
nickjillings@1404 491 \end{center}
nickjillings@1404 492 \end{figure}
nickjillings@1404 493
nickjillings@1404 494 '''
nickjillings@1404 495 # problem: some people entered twice?
nickjillings@1404 496
nickjillings@1404 497 #TODO
nickjillings@1404 498 # time per page in function of number of fragments (plot)
nickjillings@1404 499 # time per participant in function of number of pages
nickjillings@1404 500 # plot total time for each participant
nickjillings@1404 501 # show 'count' per page (in order)
nickjillings@1404 502
nickjillings@1404 503 # clear up page_index <> page_count <> page_number confusion
nickjillings@1404 504
nickjillings@1404 505
nickjillings@1404 506 texfile = header+body+footer # add bits together
nickjillings@1404 507
nickjillings@1404 508 # write TeX file
nickjillings@1404 509 with open(folder_name + '/' + 'Report.tex','w') as f:
nickjillings@1404 510 f.write(texfile)
nickjillings@1404 511 proc=subprocess.Popen(shlex.split('pdflatex -output-directory='+folder_name+' '+ folder_name + '/Report.tex'))
nickjillings@1404 512 proc.communicate()
nickjillings@1404 513 # run again
nickjillings@1404 514 proc=subprocess.Popen(shlex.split('pdflatex -output-directory='+folder_name+' '+ folder_name + '/Report.tex'))
nickjillings@1404 515 proc.communicate()
nickjillings@1404 516
nickjillings@1404 517 #TODO remove auxiliary LaTeX files
nickjillings@1404 518 try:
nickjillings@1404 519 os.remove(folder_name + '/' + 'Report.aux')
nickjillings@1404 520 os.remove(folder_name + '/' + 'Report.log')
nickjillings@1404 521 os.remove(folder_name + '/' + 'Report.out')
nickjillings@1404 522 os.remove(folder_name + '/' + 'Report.toc')
nickjillings@1404 523 except OSError:
nickjillings@1404 524 pass
nickjillings@1404 525