From 035a5de0e879bb8450c5f3f8483f869b0b2ec0cb Mon Sep 17 00:00:00 2001 From: Friso Grace Date: Tue, 16 Jul 2024 13:35:44 +0200 Subject: [PATCH 1/2] Update logging, now easier to set --- ...ice_digital_twins.detection_geometries.rst | 6 ---- ..._digital_twins.illumination_geometries.rst | 10 +++---- .../simpa.core.device_digital_twins.rst | 5 ---- docs/source/simpa_examples.rst | 12 ++++---- simpa/io_handling/io_hdf5.py | 3 +- simpa/log/file_logger.py | 30 ++++++++++++++++--- simpa/utils/tags.py | 30 +++++++++++++++++++ .../optical_and_acoustic_simulation.py | 4 +++ 8 files changed, 72 insertions(+), 28 deletions(-) diff --git a/docs/source/simpa.core.device_digital_twins.detection_geometries.rst b/docs/source/simpa.core.device_digital_twins.detection_geometries.rst index 413d22fa..3eb9caa8 100644 --- a/docs/source/simpa.core.device_digital_twins.detection_geometries.rst +++ b/docs/source/simpa.core.device_digital_twins.detection_geometries.rst @@ -12,12 +12,6 @@ detection\_geometries :show-inheritance: -.. automodule:: simpa.core.device_digital_twins.detection_geometries.detection_geometry_base - :members: - :undoc-members: - :show-inheritance: - - .. automodule:: simpa.core.device_digital_twins.detection_geometries.linear_array :members: :undoc-members: diff --git a/docs/source/simpa.core.device_digital_twins.illumination_geometries.rst b/docs/source/simpa.core.device_digital_twins.illumination_geometries.rst index 0759ced0..15239223 100644 --- a/docs/source/simpa.core.device_digital_twins.illumination_geometries.rst +++ b/docs/source/simpa.core.device_digital_twins.illumination_geometries.rst @@ -18,31 +18,31 @@ illumination\_geometries :show-inheritance: -.. automodule:: simpa.core.device_digital_twins.illumination_geometries.illumination_geometry_base +.. automodule:: simpa.core.device_digital_twins.illumination_geometries.ithera_msot_acuity_illumination :members: :undoc-members: :show-inheritance: -.. automodule:: simpa.core.device_digital_twins.illumination_geometries.ithera_msot_acuity_illumination +.. automodule:: simpa.core.device_digital_twins.illumination_geometries.ithera_msot_invision_illumination :members: :undoc-members: :show-inheritance: -.. automodule:: simpa.core.device_digital_twins.illumination_geometries.ithera_msot_invision_illumination +.. automodule:: simpa.core.device_digital_twins.illumination_geometries.pencil_array_illumination :members: :undoc-members: :show-inheritance: -.. automodule:: simpa.core.device_digital_twins.illumination_geometries.pencil_array_illumination +.. automodule:: simpa.core.device_digital_twins.illumination_geometries.pencil_beam_illumination :members: :undoc-members: :show-inheritance: -.. automodule:: simpa.core.device_digital_twins.illumination_geometries.pencil_beam_illumination +.. automodule:: simpa.core.device_digital_twins.illumination_geometries.rectangle_illumination :members: :undoc-members: :show-inheritance: diff --git a/docs/source/simpa.core.device_digital_twins.rst b/docs/source/simpa.core.device_digital_twins.rst index a8aeb366..5b25dcc9 100644 --- a/docs/source/simpa.core.device_digital_twins.rst +++ b/docs/source/simpa.core.device_digital_twins.rst @@ -12,8 +12,3 @@ device\_digital\_twins simpa.core.device_digital_twins.detection_geometries simpa.core.device_digital_twins.illumination_geometries simpa.core.device_digital_twins.pa_devices - -.. automodule:: simpa.core.device_digital_twins.digital_device_twin_base - :members: - :undoc-members: - :show-inheritance: diff --git a/docs/source/simpa_examples.rst b/docs/source/simpa_examples.rst index 39eb6bb7..8b58264e 100644 --- a/docs/source/simpa_examples.rst +++ b/docs/source/simpa_examples.rst @@ -4,13 +4,13 @@ simpa\_examples .. toctree:: :maxdepth: 2 - perform_iterative_qPAI_reconstruction - linear_unmixing - msot_invision_simulation + create_a_custom_digital_device_twin create_custom_tissues - perform_image_reconstruction - minimal_optical_simulation_uniform_cube + linear_unmixing minimal_optical_simulation + minimal_optical_simulation_uniform_cube + perform_iterative_qPAI_reconstruction optical_and_acoustic_simulation - create_a_custom_digital_device_twin segmentation_loader + msot_invision_simulation + perform_image_reconstruction diff --git a/simpa/io_handling/io_hdf5.py b/simpa/io_handling/io_hdf5.py index 2464827b..9f23c3c2 100644 --- a/simpa/io_handling/io_hdf5.py +++ b/simpa/io_handling/io_hdf5.py @@ -9,8 +9,6 @@ from simpa.log import Logger from simpa.utils.serializer import SerializableSIMPAClass -logger = Logger() - def save_hdf5(save_item, file_path: str, file_dictionary_path: str = "/", file_compression: str = None): """ @@ -32,6 +30,7 @@ def data_grabber(file, path, data_dictionary, compression: str = None): :param data_dictionary: Dictionary to save. :param compression: possible file compression for the corresponding dataset. Values are: gzip, lzf and szip. """ + logger = Logger() for key, item in data_dictionary.items(): key = str(key) diff --git a/simpa/log/file_logger.py b/simpa/log/file_logger.py index 5911bdbc..af2120c7 100644 --- a/simpa/log/file_logger.py +++ b/simpa/log/file_logger.py @@ -6,6 +6,7 @@ from pathlib import Path import sys from simpa.utils.serializer import SerializableSIMPAClass +from simpa.utils import Tags class Logger(SerializableSIMPAClass): @@ -28,22 +29,43 @@ class Logger(SerializableSIMPAClass): _simpa_default_logging_path = str(Path.home())+"/simpa.log" _logger = None - def __new__(cls, path=None, force_new_instance=False, startup_verbose=False): + def __new__(cls, path=None, force_new_instance: bool = False, startup_verbose: bool = False, + logging_level: str = Tags.LOGGER_DEBUG): # This pattern can be used to realise a singleton implementation in Python + """ + Here, we create an instance of the Logger class and set the logging level. + :param path: Where to write the log file. + :param force_new_instance: Whether to create a new instance of the Logger class or not. + :param startup_verbose: Whether to add a verbose for starting up the logger. + :param logging_level: the level of the logging module to use. + """ if cls._instance is None or force_new_instance: cls._instance = super(Logger, cls).__new__(cls) if path is None: path = cls._simpa_default_logging_path + if logging_level == Tags.LOGGER_DEBUG: + _logging_level = logging.DEBUG + elif logging_level == Tags.LOGGER_INFO: + _logging_level = logging.INFO + elif logging_level == Tags.LOGGER_WARNING: + _logging_level = logging.WARNING + elif logging_level == Tags.LOGGER_ERROR: + _logging_level = logging.ERROR + elif logging_level == Tags.LOGGER_CRITICAL: + _logging_level = logging.CRITICAL + else: + raise ValueError('Invalid logging level') + cls._logger = logging.getLogger("SIMPA Logger") - cls._logger.setLevel(logging.DEBUG) + cls._logger.setLevel(_logging_level) console_handler = logging.StreamHandler(stream=sys.stdout) file_handler = logging.FileHandler(path, mode="w") - console_handler.setLevel(logging.DEBUG) - file_handler.setLevel(logging.DEBUG) + console_handler.setLevel(_logging_level) + file_handler.setLevel(_logging_level) console_handler.setFormatter(cls._simpa_logging_formatter) file_handler.setFormatter(cls._simpa_logging_formatter) diff --git a/simpa/utils/tags.py b/simpa/utils/tags.py index 84bd1e61..6134ea0d 100644 --- a/simpa/utils/tags.py +++ b/simpa/utils/tags.py @@ -1490,3 +1490,33 @@ class Tags: """ Identifier for the environment varibale that defines the path the the matlab executable. """ + + LOGGER_DEBUG = "debug" + """ + Identifies the logging level to be set to logging.DEBUG. + Usage: log.file_logger + """ + + LOGGER_INFO = "info" + """ + Identifies the logging level to be set to logging.INFO. + Usage: log.file_logger + """ + + LOGGER_WARNING = "warning" + """ + Identifies the logging level to be set to logging.WARNING. + Usage: log.file_logger + """ + + LOGGER_ERROR = "error" + """ + Identifies the logging level to be set to logging.ERROR. + Usage: log.file_logger + """ + + LOGGER_CRITICAL = "critical" + """ + Identifies the logging level to be set to logging.CRITICAL. + Usage: log.file_logger + """ diff --git a/simpa_examples/optical_and_acoustic_simulation.py b/simpa_examples/optical_and_acoustic_simulation.py index 68bb856c..fec115b6 100644 --- a/simpa_examples/optical_and_acoustic_simulation.py +++ b/simpa_examples/optical_and_acoustic_simulation.py @@ -10,6 +10,10 @@ import os os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE" +# TODO: set the level at which you wish to receive a log for +logger = sp.log.Logger(logging_level=Tags.LOGGER_WARNING) + + VOLUME_TRANSDUCER_DIM_IN_MM = 75 VOLUME_PLANAR_DIM_IN_MM = 20 VOLUME_HEIGHT_IN_MM = 25 From 41cb7077fb2b9f8a1199a32028f3eaf723d0e990 Mon Sep 17 00:00:00 2001 From: Friso Grace Date: Fri, 9 Aug 2024 11:28:59 +0200 Subject: [PATCH 2/2] logger in simulate method --- simpa/core/simulation.py | 8 +++++--- simpa_examples/optical_and_acoustic_simulation.py | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/simpa/core/simulation.py b/simpa/core/simulation.py index 52c90245..577b3b98 100644 --- a/simpa/core/simulation.py +++ b/simpa/core/simulation.py @@ -16,7 +16,8 @@ import time -def simulate(simulation_pipeline: list, settings: Settings, digital_device_twin: DigitalDeviceTwinBase): +def simulate(simulation_pipeline: list, settings: Settings, digital_device_twin: DigitalDeviceTwinBase, + logging_level: str = Tags.LOGGER_DEBUG): """ This method constitutes the staring point for the simulation pipeline of the SIMPA toolkit. @@ -25,12 +26,13 @@ def simulate(simulation_pipeline: list, settings: Settings, digital_device_twin: :param settings: settings dictionary containing the simulation instructions :param digital_device_twin: a digital device twin of an imaging device as specified by the DigitalDeviceTwinBase class. + :param logging_level: The logging level of the simulation pipeline. :raises TypeError: if one of the given parameters is not of the correct type :raises AssertionError: if the digital device twin is not able to simulate the settings specification :return: list with the save paths of the simulated data within the HDF5 file. """ start_time = time.time() - logger = Logger() + logger = Logger(logging_level=logging_level, force_new_instance=True) if not isinstance(settings, Settings): logger.critical("The second argument was not a settings instance!") raise TypeError("Use a Settings instance from simpa.utils.settings_generator as simulation input.") @@ -56,7 +58,7 @@ def simulate(simulation_pipeline: list, settings: Settings, digital_device_twin: simpa_output_path = path + settings[Tags.VOLUME_NAME] settings[Tags.SIMPA_OUTPUT_PATH] = simpa_output_path + ".hdf5" - + simpa_output[Tags.SIMPA_VERSION] = __version__ simpa_output[Tags.SETTINGS] = settings simpa_output[Tags.DIGITAL_DEVICE] = digital_device_twin diff --git a/simpa_examples/optical_and_acoustic_simulation.py b/simpa_examples/optical_and_acoustic_simulation.py index f036b13e..c92567f4 100644 --- a/simpa_examples/optical_and_acoustic_simulation.py +++ b/simpa_examples/optical_and_acoustic_simulation.py @@ -200,7 +200,7 @@ def create_example_tissue(): sp.FieldOfViewCropping(settings) ] - sp.simulate(SIMULATION_PIPELINE, settings, device) + sp.simulate(SIMULATION_PIPELINE, settings, device, logging_level=Tags.LOGGER_ERROR) if Tags.WAVELENGTH in settings: WAVELENGTH = settings[Tags.WAVELENGTH]