annotate scripts/generate_report.py @ 1460:1b81ab727352

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