From 43e20365a9dd19625d5871ab0f15c300b995e4f3 Mon Sep 17 00:00:00 2001 From: Thomas Fish Date: Mon, 21 Oct 2024 18:36:14 +0100 Subject: [PATCH] Add input and output data file paths to log header --- src/sim_recon/recon.py | 64 ++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/src/sim_recon/recon.py b/src/sim_recon/recon.py index 3bd088a..eef6ac7 100644 --- a/src/sim_recon/recon.py +++ b/src/sim_recon/recon.py @@ -211,7 +211,7 @@ def _reconstructions_to_output( stitch_channels: bool = True, overwrite: bool = True, file_type: OutputFileTypes = "dv", -) -> None: +) -> tuple[Path, ...]: input_dv = read_mrc_bound_array(sim_data_path).mrc input_resolution = image_resolution_from_mrc(input_dv, warn_not_square=False) if file_type == "dv": @@ -257,27 +257,25 @@ def _reconstructions_to_output( if pi.status == ProcessingStatus.COMPLETE ) if not output_wavelengths_path_tuples: - logger.warning( - "No reconstructions were created from %s", - sim_data_path, + raise ReconstructionError( + f"No reconstructions completed from '{sim_data_path}'", ) - else: - zoom_fact = float(zoom_factors[0][0]) - zzoom = zoom_factors[0][1] - write_output( - output_image_path, - tuple( - generate_channels_from_tiffs(*output_wavelengths_path_tuples) - ), - resolution=ImageResolution( - input_resolution.x / zoom_fact, - input_resolution.y / zoom_fact, - input_resolution.z / zzoom, - )) - return + zoom_fact = float(zoom_factors[0][0]) + zzoom = zoom_factors[0][1] + write_output( + output_image_path, + tuple(generate_channels_from_tiffs(*output_wavelengths_path_tuples)), + resolution=ImageResolution( + input_resolution.x / zoom_fact, + input_resolution.y / zoom_fact, + input_resolution.z / zzoom, + ), + ) + return (output_image_path,) except InvalidValueError as e: logger.warning("Unable to stitch files due to error: %s", e) + output_paths: list[Path] = [] for ( wavelength, processing_info, @@ -296,15 +294,23 @@ def _reconstructions_to_output( zzoom = processing_info.kwargs["zzoom"] write_output( output_image_path, - tuple(generate_channels_from_tiffs( - (processing_info.wavelengths, processing_info.output_path) - )), + tuple( + generate_channels_from_tiffs( + (processing_info.wavelengths, processing_info.output_path) + ) + ), resolution=ImageResolution( input_resolution.x / zoom_fact, input_resolution.y / zoom_fact, input_resolution.z / zzoom, ), ) + output_paths.append(output_image_path) + if not output_paths: + raise ReconstructionError( + f"No reconstructions completed from '{sim_data_path}'", + ) + return tuple(output_paths) def _reconstruction_process_callback( @@ -382,6 +388,7 @@ def run_reconstructions( for sim_data_path in progress_wrapper( sim_data_paths, desc="SIM data files", unit="file" ): + output_paths: tuple[Path, ...] | None = None try: sim_data_path = Path(sim_data_path) file_output_directory = ( @@ -452,13 +459,17 @@ def run_reconstructions( f"Failed to reconstruct channels: {', '.join(str(i) for i in incomplete_channels)}" ) - _reconstructions_to_output( + output_paths = _reconstructions_to_output( sim_data_path, file_output_directory=file_output_directory, processing_info_dict=processing_info_dict, stitch_channels=stitch_channels, overwrite=overwrite, - file_type=output_file_type + file_type=output_file_type, + ) + logger.info( + "Reconstructed data saved to: %s", + ", ".join(str(_) for _ in output_paths), ) finally: proc_log_files: list[Path] = [] @@ -484,10 +495,15 @@ def run_reconstructions( output_directory=file_output_directory, ensure_unique=True, ) + log_header = f"Reconstruction log for:\n{sim_data_path}" + if output_paths is None: + log_header += "\nNo output was created" + else: + log_header += f"\nOutput:\n{'\n'.join(str(_) for _ in output_paths)}" combine_text_files( log_path, *proc_log_files, - header="Reconstruction log", + header=log_header, ) logger.info( "Reconstruction log file created at '%s'", log_path