annotate scripts/generate_report.py @ 1365:c4156df1dcb5

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