annotate scripts/generate_report.py @ 1505:a667f80c417e

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