@@ -297,112 +297,122 @@ def close(
297297
298298 ### Create summary report
299299 self .logger .info ("> Creating summary report..." )
300- summary_report_dir = self .get_experiment_path () / "summary_report"
301- Path .mkdir (summary_report_dir , exist_ok = True )
302-
303- ### NOTE: xhtml2pdf fails if you provide relative image file paths, e.g ("../thumbnails/ring/1.png")
304- ### so provide absolute filepaths only. Note this means that the html file will be broken if viewed from anywhere other than the Pi.
305-
306- class_to_all_thumbnails_abs_path : Dict [str , List [str ]] = {
307- x : [
308- str (y .resolve ())
309- for y in list (class_to_thumbnails_path [x ].rglob ("*.png" ))
310- ]
311- for x in class_to_thumbnails_path .keys ()
312- }
313-
314- html_abs_path_temp_loc = (
315- summary_report_dir / f"{ self .time_str } _temp_summary.html"
316- )
317- pdf_save_loc = summary_report_dir / f"{ self .time_str } _summary.pdf"
318-
319- # Create per-image metadata plot
320- per_image_metadata_plot_save_loc = str (
321- summary_report_dir / f"{ self .time_str } _per_image_metadata_plot.jpg"
322- )
323-
324- counts_plot_loc = str (summary_report_dir / "counts.jpg" )
325- conf_plot_loc = str (summary_report_dir / "confs.jpg" )
326- objectness_plot_loc = str (summary_report_dir / "objectness.jpg" )
300+ try :
301+ summary_report_dir = self .get_experiment_path () / "summary_report"
302+ Path .mkdir (summary_report_dir , exist_ok = True )
303+
304+ ### NOTE: xhtml2pdf fails if you provide relative image file paths, e.g ("../thumbnails/ring/1.png")
305+ ### so provide absolute filepaths only. Note this means that the html file will be broken if viewed from anywhere other than the Pi.
306+
307+ class_to_all_thumbnails_abs_path : Dict [str , List [str ]] = {
308+ x : [
309+ str (y .resolve ())
310+ for y in list (class_to_thumbnails_path [x ].rglob ("*.png" ))
311+ ]
312+ for x in class_to_thumbnails_path .keys ()
313+ }
314+
315+ html_abs_path_temp_loc = (
316+ summary_report_dir / f"{ self .time_str } _temp_summary.html"
317+ )
318+ pdf_save_loc = summary_report_dir / f"{ self .time_str } _summary.pdf"
327319
328- # Only generate additional plots if DEBUG_REPORT environment variable is set to True
329- if DEBUG_REPORT :
330- with open (self .per_img_metadata_filename , "r" ) as per_img_metadata_file :
331- make_per_image_metadata_plots (
332- per_img_metadata_file , per_image_metadata_plot_save_loc
333- )
320+ # Create per-image metadata plot
321+ per_image_metadata_plot_save_loc = str (
322+ summary_report_dir / f"{ self .time_str } _per_image_metadata_plot.jpg"
323+ )
334324
335- try :
336- make_cell_count_plot ( pred_tensors , counts_plot_loc )
337- except Exception as e :
338- self . logger . error ( f"Failed to make cell count plot - { e } " )
339- try :
340- make_yogo_conf_plots ( pred_tensors , conf_plot_loc )
341- except Exception as e :
342- self . logger . error ( f"Failed to make yogo confidence plots - { e } " )
343- try :
344- make_yogo_objectness_plots ( pred_tensors , objectness_plot_loc )
345- except Exception as e :
346- self . logger . error ( f"Failed to make yogo objectness plots - { e } " )
347-
348- # Get cell counts
349- raw_cell_counts = np . asarray ( get_class_counts ( pred_tensors ) )
350- (
351- comp_parasitemia ,
352- comp_parasitemia_err ,
353- ) = self . compensator . get_res_from_counts ( raw_cell_counts , units_ul_out = True )
354- # Associate class with counts
355- class_name_to_cell_count = {
356- x . capitalize (): y for ( x , y ) in zip ( YOGO_CLASS_LIST , raw_cell_counts )
357- }
358- # 'parasites per ul' is # of rings / total rbcs * scaling factor (RBCS_PER_UL)
359-
360- # Create parasitemia plot
361- parasitemia_plot_loc = str ( self . get_parasitemia_vis_filename ())
362- try :
363- make_parasitemia_plot (
325+ counts_plot_loc = str ( summary_report_dir / "counts.jpg" )
326+ conf_plot_loc = str ( summary_report_dir / "confs.jpg" )
327+ objectness_plot_loc = str ( summary_report_dir / "objectness.jpg" )
328+
329+ # Only generate additional plots if DEBUG_REPORT environment variable is set to True
330+ if DEBUG_REPORT :
331+ with open (
332+ self . per_img_metadata_filename , "r"
333+ ) as per_img_metadata_file :
334+ make_per_image_metadata_plots (
335+ per_img_metadata_file , per_image_metadata_plot_save_loc
336+ )
337+
338+ try :
339+ make_cell_count_plot ( pred_tensors , counts_plot_loc )
340+ except Exception as e :
341+ self . logger . error ( f"Failed to make cell count plot - { e } " )
342+ try :
343+ make_yogo_conf_plots ( pred_tensors , conf_plot_loc )
344+ except Exception as e :
345+ self . logger . error ( f"Failed to make yogo confidence plots - { e } " )
346+ try :
347+ make_yogo_objectness_plots ( pred_tensors , objectness_plot_loc )
348+ except Exception as e :
349+ self . logger . error ( f"Failed to make yogo objectness plots - { e } " )
350+
351+ # Get cell counts
352+ raw_cell_counts = np . asarray ( get_class_counts ( pred_tensors ))
353+ (
364354 comp_parasitemia ,
365355 comp_parasitemia_err ,
356+ ) = self .compensator .get_res_from_counts (
357+ raw_cell_counts , units_ul_out = True
358+ )
359+ # Associate class with counts
360+ class_name_to_cell_count = {
361+ x .capitalize (): y
362+ for (x , y ) in zip (YOGO_CLASS_LIST , raw_cell_counts )
363+ }
364+ # 'parasites per ul' is # of rings / total rbcs * scaling factor (RBCS_PER_UL)
365+
366+ # Create parasitemia plot
367+ parasitemia_plot_loc = str (self .get_parasitemia_vis_filename ())
368+ try :
369+ make_parasitemia_plot (
370+ comp_parasitemia ,
371+ comp_parasitemia_err ,
372+ parasitemia_plot_loc ,
373+ )
374+ except Exception as e :
375+ self .logger .error (f"Failed to make parasitemia plot - { e } " )
376+
377+ # HTML w/ absolute path
378+ abs_css_file_path = str ((summary_report_dir / CSS_FILE_NAME ).resolve ())
379+ html_report_with_abs_path = make_html_report (
380+ self .compensator ,
381+ self .time_str ,
382+ self .experiment_level_metadata ,
383+ per_image_metadata_plot_save_loc ,
384+ raw_cell_counts ,
385+ class_to_all_thumbnails_abs_path ,
366386 parasitemia_plot_loc ,
387+ counts_plot_loc ,
388+ conf_plot_loc ,
389+ objectness_plot_loc ,
390+ css_path = abs_css_file_path ,
367391 )
368- except Exception as e :
369- self .logger .error (f"Failed to make parasitemia plot - { e } " )
370-
371- # HTML w/ absolute path
372- abs_css_file_path = str ((summary_report_dir / CSS_FILE_NAME ).resolve ())
373- html_report_with_abs_path = make_html_report (
374- self .compensator ,
375- self .time_str ,
376- self .experiment_level_metadata ,
377- per_image_metadata_plot_save_loc ,
378- raw_cell_counts ,
379- class_to_all_thumbnails_abs_path ,
380- parasitemia_plot_loc ,
381- counts_plot_loc ,
382- conf_plot_loc ,
383- objectness_plot_loc ,
384- css_path = abs_css_file_path ,
385- )
386392
387- # Copy the CSS file to the summary directory
388- shutil .copy (SUMMARY_REPORT_CSS_FILE , summary_report_dir )
393+ # Copy the CSS file to the summary directory
394+ shutil .copy (SUMMARY_REPORT_CSS_FILE , summary_report_dir )
389395
390- # Save the temporary HTML file w/ absolute path so we can properly generate the PDF
391- save_html_report (html_report_with_abs_path , html_abs_path_temp_loc )
392- create_pdf_from_html (html_abs_path_temp_loc , pdf_save_loc )
396+ # Save the temporary HTML file w/ absolute path so we can properly generate the PDF
397+ save_html_report (html_report_with_abs_path , html_abs_path_temp_loc )
398+ create_pdf_from_html (html_abs_path_temp_loc , pdf_save_loc )
393399
394- # Make a copy of the summary PDF to the Desktop
395- shutil .copy (pdf_save_loc , DESKTOP_SUMMARY_DIR )
400+ # Make a copy of the summary PDF to the Desktop
401+ shutil .copy (pdf_save_loc , DESKTOP_SUMMARY_DIR )
396402
397- # Remove intermediate files
398- remove (html_abs_path_temp_loc )
399- remove (summary_report_dir / CSS_FILE_NAME )
403+ # Remove intermediate files
404+ remove (html_abs_path_temp_loc )
405+ remove (summary_report_dir / CSS_FILE_NAME )
400406
401- if DEBUG_REPORT :
402- remove (counts_plot_loc )
403- remove (per_image_metadata_plot_save_loc )
404- remove (conf_plot_loc )
405- remove (objectness_plot_loc )
407+ if DEBUG_REPORT :
408+ remove (counts_plot_loc )
409+ remove (per_image_metadata_plot_save_loc )
410+ remove (conf_plot_loc )
411+ remove (objectness_plot_loc )
412+ except Exception as e :
413+ self .logger .error (
414+ f"Unexpected exception when saving summary report: { e } . Skipping report and continuing..."
415+ )
406416
407417 # Write to a separate csv with just cell counts for each class
408418 self .logger .info ("Writing cell counts to csv..." )
0 commit comments