comparison scripts/generate_report.py @ 780:17b550310734

Scripts: generate_report.py accepts folder names with spaces - uses package 'grffile'. Minor correction to Instructions (manual emergency saves).
author Brecht De Man <BrechtDeMan@users.noreply.github.com>
date Tue, 08 Dec 2015 17:17:39 +0100
parents e66434e0f573
children 18dc725bb5c2
comparison
equal deleted inserted replaced
779:01a882ec2b72 780:17b550310734
16 16
17 render_figures = True 17 render_figures = True
18 18
19 # XML results files location 19 # XML results files location
20 if len(sys.argv) == 1: 20 if len(sys.argv) == 1:
21 folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder 21 folder_name = "../saves/" # Looks in 'saves/' folder from 'scripts/' folder
22 print "Use: python generate_report.py [results_folder] [no_render | -nr]" 22 print "Use: python generate_report.py [results_folder] [no_render | -nr]"
23 print "Using default path: " + folder_name 23 print "Using default path: " + folder_name
24 elif len(sys.argv) == 2: 24 elif len(sys.argv) == 2:
25 folder_name = sys.argv[1] # First command line argument is folder 25 folder_name = sys.argv[1] # First command line argument is folder
26 elif len(sys.argv) == 3: 26 elif len(sys.argv) == 3:
77 header = r'''\documentclass[11pt, oneside]{article} 77 header = r'''\documentclass[11pt, oneside]{article}
78 \usepackage{geometry} 78 \usepackage{geometry}
79 \geometry{a4paper} 79 \geometry{a4paper}
80 \usepackage[parfill]{parskip} % empty line instead of indent 80 \usepackage[parfill]{parskip} % empty line instead of indent
81 \usepackage{graphicx} % figures 81 \usepackage{graphicx} % figures
82 \usepackage[space]{grffile} % include figures with spaces in paths
82 \usepackage{hyperref} 83 \usepackage{hyperref}
83 \usepackage{tikz} % pie charts 84 \usepackage{tikz} % pie charts
84 \title{Report} 85 \title{Report}
85 \author{'''+\ 86 \author{'''+\
86 user+\ 87 user+\
87 r'''} 88 r'''}
88 \graphicspath{{'''+\ 89 \graphicspath{{'''+\
89 folder_name+\ 90 folder_name+\
90 r'''/}} 91 r'''}}
91 %\setcounter{section}{-1} % Summary section 0 so number of sections equals number of files 92 %\setcounter{section}{-1} % Summary section 0 so number of sections equals number of files
92 \begin{document} 93 \begin{document}
93 \maketitle 94 \maketitle
94 This is an automatically generated report using the `generate\_report.py' Python script 95 This is an automatically generated report using the `generate\_report.py' Python script
95 included with the Web Audio Evaluation Tool \cite{WAET} distribution which can be found 96 included with the Web Audio Evaluation Tool \cite{WAET} distribution which can be found
106 \end{thebibliography} 107 \end{thebibliography}
107 \end{document}''' 108 \end{document}'''
108 109
109 body = '' 110 body = ''
110 111
112 # make sure folder_name ends in '/'
113 folder_name = os.path.join(folder_name, '')
114
111 # generate images for later use 115 # generate images for later use
112 if render_figures: 116 if render_figures:
113 subprocess.call("python timeline_view_movement.py "+folder_name, shell=True) 117 subprocess.call("python timeline_view_movement.py '"+folder_name+"'", shell=True)
114 subprocess.call("python score_parser.py "+folder_name, shell=True) 118 subprocess.call("python score_parser.py '"+folder_name+"'", shell=True)
115 subprocess.call("python score_plot.py "+folder_name, shell=True) 119 subprocess.call("python score_plot.py '"+folder_name+"'", shell=True)
116 120
117 # get every XML file in folder 121 # get every XML file in folder
118 files_list = os.listdir(folder_name) 122 files_list = os.listdir(folder_name)
119 for file in files_list: # iterate over all files in files_list 123 for file in files_list: # iterate over all files in files_list
120 if file.endswith(".xml"): # check if XML file 124 if file.endswith(".xml"): # check if XML file
121 number_of_XML_files += 1 125 number_of_XML_files += 1
122 tree = ET.parse(folder_name + '/' + file) 126 tree = ET.parse(folder_name + file)
123 root = tree.getroot() 127 root = tree.getroot()
124 128
125 # PRINT name as section 129 # PRINT name as section
126 body+= '\n\section{'+file[:-4].capitalize()+'}\n' # make section header from name without extension 130 body+= '\n\section{'+file[:-4].capitalize()+'}\n' # make section header from name without extension
127 131
218 222
219 # get timeline for this audioholder 223 # get timeline for this audioholder
220 img_path = 'timelines_movement/'+file[:-4]+'-'+page_name+'.pdf' 224 img_path = 'timelines_movement/'+file[:-4]+'-'+page_name+'.pdf'
221 225
222 # check if available 226 # check if available
223 if os.path.isfile(folder_name+'/'+img_path): 227 if os.path.isfile(folder_name+img_path):
224 # SHOW timeline image 228 # SHOW timeline image
225 timeline_plots += '\\includegraphics[width=\\textwidth]{'+\ 229 timeline_plots += '\\includegraphics[width=\\textwidth]{'+\
226 folder_name+'/'+img_path+'}\n\t\t' 230 folder_name+img_path+'}\n\t\t'
227 231
228 # keep track of duration in function of page index 232 # keep track of duration in function of page index
229 if len(duration_order)>page_number: 233 if len(duration_order)>page_number:
230 duration_order[page_number].append(duration) 234 duration_order[page_number].append(duration)
231 else: 235 else:
328 plt.bar(range(1,len(duration_order)+1), np.array(tpp_averages)/60) 332 plt.bar(range(1,len(duration_order)+1), np.array(tpp_averages)/60)
329 plt.xlabel('Page order') 333 plt.xlabel('Page order')
330 plt.xlim(.8, len(duration_order)+1) 334 plt.xlim(.8, len(duration_order)+1)
331 plt.xticks(np.arange(1,len(duration_order)+1)+.4, range(1,len(duration_order)+1)) 335 plt.xticks(np.arange(1,len(duration_order)+1)+.4, range(1,len(duration_order)+1))
332 plt.ylabel('Average time [minutes]') 336 plt.ylabel('Average time [minutes]')
333 plt.savefig(folder_name+"/time_per_page.pdf", bbox_inches='tight') 337 plt.savefig(folder_name+"time_per_page.pdf", bbox_inches='tight')
334 plt.close() 338 plt.close()
335 #TODO add error bars 339 #TODO add error bars
336 340
337 341
338 # Sort pages by number of audioelements, then by duration 342 # Sort pages by number of audioelements, then by duration
370 plt.bar(range(1,len(audioholder_names_ordered)+1), np.array(average_duration_audioholder_ordered)/60) 374 plt.bar(range(1,len(audioholder_names_ordered)+1), np.array(average_duration_audioholder_ordered)/60)
371 plt.xlabel('Audioholder') 375 plt.xlabel('Audioholder')
372 plt.xlim(.8, len(audioholder_names_ordered)+1) 376 plt.xlim(.8, len(audioholder_names_ordered)+1)
373 plt.xticks(np.arange(1,len(audioholder_names_ordered)+1)+.4, audioholder_names_ordered, rotation=90) 377 plt.xticks(np.arange(1,len(audioholder_names_ordered)+1)+.4, audioholder_names_ordered, rotation=90)
374 plt.ylabel('Average time [minutes]') 378 plt.ylabel('Average time [minutes]')
375 plt.savefig(folder_name+"/time_per_audioholder.pdf", bbox_inches='tight') 379 plt.savefig(folder_name+"time_per_audioholder.pdf", bbox_inches='tight')
376 plt.close() 380 plt.close()
377 381
378 # SHOW bar plot of average time per page 382 # SHOW bar plot of average time per page
379 plt.bar(range(1,len(audioholder_names_ordered)+1), number_of_subjects) 383 plt.bar(range(1,len(audioholder_names_ordered)+1), number_of_subjects)
380 plt.xlabel('Audioholder') 384 plt.xlabel('Audioholder')
383 plt.ylabel('Number of subjects') 387 plt.ylabel('Number of subjects')
384 ax = plt.gca() 388 ax = plt.gca()
385 ylims = ax.get_ylim() 389 ylims = ax.get_ylim()
386 yint = np.arange(int(np.floor(ylims[0])), int(np.ceil(ylims[1]))+1) 390 yint = np.arange(int(np.floor(ylims[0])), int(np.ceil(ylims[1]))+1)
387 plt.yticks(yint) 391 plt.yticks(yint)
388 plt.savefig(folder_name+"/subjects_per_audioholder.pdf", bbox_inches='tight') 392 plt.savefig(folder_name+"subjects_per_audioholder.pdf", bbox_inches='tight')
389 plt.close() 393 plt.close()
390 394
391 # SHOW both figures 395 # SHOW both figures
392 body += r''' 396 body += r'''
393 \begin{figure}[htbp] 397 \begin{figure}[htbp]
394 \begin{center} 398 \begin{center}
395 \includegraphics[width=.65\textwidth]{'''+\ 399 \includegraphics[width=.65\textwidth]{'''+\
396 folder_name+"/time_per_page.pdf"+\ 400 folder_name+'time_per_page.pdf'+\
397 r'''} 401 r'''}
398 \caption{Average time spent per page.} 402 \caption{Average time spent per page.}
399 \label{fig:avgtimeperpage} 403 \label{fig:avgtimeperpage}
400 \end{center} 404 \end{center}
401 \end{figure} 405 \end{figure}
402 406
403 ''' 407 '''
404 body += r'''\begin{figure}[htbp] 408 body += r'''\begin{figure}[htbp]
405 \begin{center} 409 \begin{center}
406 \includegraphics[width=.65\textwidth]{'''+\ 410 \includegraphics[width=.65\textwidth]{'''+\
407 folder_name+"/time_per_audioholder.pdf"+\ 411 folder_name+'time_per_audioholder.pdf'+\
408 r'''} 412 r'''}
409 \caption{Average time spent per audioholder.} 413 \caption{Average time spent per audioholder.}
410 \label{fig:avgtimeperaudioholder} 414 \label{fig:avgtimeperaudioholder}
411 \end{center} 415 \end{center}
412 \end{figure} 416 \end{figure}
413 417
414 ''' 418 '''
415 body += r'''\begin{figure}[htbp] 419 body += r'''\begin{figure}[htbp]
416 \begin{center} 420 \begin{center}
417 \includegraphics[width=.65\textwidth]{'''+\ 421 \includegraphics[width=.65\textwidth]{'''+\
418 folder_name+"/subjects_per_audioholder.pdf"+\ 422 folder_name+'subjects_per_audioholder.pdf'+\
419 r'''} 423 r'''}
420 \caption{Number of subjects per audioholder.} 424 \caption{Number of subjects per audioholder.}
421 \label{fig:subjectsperaudioholder} 425 \label{fig:subjectsperaudioholder}
422 \end{center} 426 \end{center}
423 \end{figure} 427 \end{figure}
428 432
429 # SHOW boxplot per audioholder 433 # SHOW boxplot per audioholder
430 #TODO order in decreasing order of participants 434 #TODO order in decreasing order of participants
431 for audioholder_name in page_names: # get each name 435 for audioholder_name in page_names: # get each name
432 # plot boxplot if exists (not so for the 'alt' names) 436 # plot boxplot if exists (not so for the 'alt' names)
433 if os.path.isfile(folder_name+'/ratings/'+audioholder_name+'-ratings-box.pdf'): 437 if os.path.isfile(folder_name+'ratings/'+audioholder_name+'-ratings-box.pdf'):
434 body += r'''\begin{figure}[htbp] 438 body += r'''\begin{figure}[htbp]
435 \begin{center} 439 \begin{center}
436 \includegraphics[width=.65\textwidth]{'''+\ 440 \includegraphics[width=.65\textwidth]{'''+\
437 folder_name+"/ratings/"+audioholder_name+'-ratings-box.pdf'+\ 441 folder_name+"ratings/"+audioholder_name+'-ratings-box.pdf'+\
438 r'''} 442 r'''}
439 \caption{Box plot of ratings for audioholder '''+\ 443 \caption{Box plot of ratings for audioholder '''+\
440 audioholder_name+' ('+str(subject_count[real_page_names.index(audioholder_name)])+\ 444 audioholder_name+' ('+str(subject_count[real_page_names.index(audioholder_name)])+\
441 ''' participants).} 445 ''' participants).}
442 \label{fig:boxplot'''+audioholder_name.replace(" ", "")+'''} 446 \label{fig:boxplot'''+audioholder_name.replace(" ", "")+'''}
503 # clear up page_index <> page_count <> page_number confusion 507 # clear up page_index <> page_count <> page_number confusion
504 508
505 509
506 texfile = header+body+footer # add bits together 510 texfile = header+body+footer # add bits together
507 511
512 print 'pdflatex -output-directory="'+folder_name+'"" "'+ folder_name + 'Report.tex"' # DEBUG
513
508 # write TeX file 514 # write TeX file
509 with open(folder_name + '/' + 'Report.tex','w') as f: 515 with open(folder_name + 'Report.tex','w') as f:
510 f.write(texfile) 516 f.write(texfile)
511 proc=subprocess.Popen(shlex.split('pdflatex -output-directory='+folder_name+' '+ folder_name + '/Report.tex')) 517 proc=subprocess.Popen(shlex.split('pdflatex -output-directory="'+folder_name+'" "'+ folder_name + 'Report.tex"'))
512 proc.communicate() 518 proc.communicate()
513 # run again 519 # run again
514 proc=subprocess.Popen(shlex.split('pdflatex -output-directory='+folder_name+' '+ folder_name + '/Report.tex')) 520 proc=subprocess.Popen(shlex.split('pdflatex -output-directory="'+folder_name+'" "'+ folder_name + 'Report.tex"'))
515 proc.communicate() 521 proc.communicate()
516 522
517 #TODO remove auxiliary LaTeX files 523 #TODO remove auxiliary LaTeX files
518 try: 524 try:
519 os.remove(folder_name + '/' + 'Report.aux') 525 os.remove(folder_name + 'Report.aux')
520 os.remove(folder_name + '/' + 'Report.log') 526 os.remove(folder_name + 'Report.log')
521 os.remove(folder_name + '/' + 'Report.out') 527 os.remove(folder_name + 'Report.out')
522 os.remove(folder_name + '/' + 'Report.toc') 528 os.remove(folder_name + 'Report.toc')
523 except OSError: 529 except OSError:
524 pass 530 pass
525 531