From 3b68cf3e48bd3449b321d46be4e045478ecfaa9c Mon Sep 17 00:00:00 2001 From: Ishaan Desai Date: Mon, 30 Dec 2024 10:49:04 +0100 Subject: [PATCH] Set the default logger to a stdout stream, and add option to manually specify an output directory for file loggers --- micro_manager/adaptivity/adaptivity.py | 13 +++++++++--- micro_manager/config.py | 20 ++++++++++++++++++- micro_manager/micro_manager.py | 4 +--- micro_manager/tools/logging_wrapper.py | 12 ++++++++--- ...ger-config-global-adaptivity-parallel.json | 1 + 5 files changed, 40 insertions(+), 10 deletions(-) diff --git a/micro_manager/adaptivity/adaptivity.py b/micro_manager/adaptivity/adaptivity.py index 4feb63b..b61e008 100644 --- a/micro_manager/adaptivity/adaptivity.py +++ b/micro_manager/adaptivity/adaptivity.py @@ -37,9 +37,16 @@ def __init__(self, configurator, rank) -> None: configurator.get_adaptivity_similarity_measure() ) - self._metrics_logger = Logger( - "Adaptivity", "adaptivity-metrics.csv", rank, csv_logger=True - ) + output_dir = configurator.get_output_dir() + + if output_dir is not None: + self._metrics_logger = Logger( + __name__, output_dir + "adaptivity-metrics.csv", rank, csv_logger=True + ) + else: + self._metrics_logger = Logger( + __name__, "adaptivity-metrics.csv", rank=rank, csv_logger=True + ) self._metrics_logger.log_info_one_rank( "Time Window,Avg Active Sims,Avg Inactive Sims,Max Active,Max Inactive" diff --git a/micro_manager/config.py b/micro_manager/config.py index 4e2cc32..deb0729 100644 --- a/micro_manager/config.py +++ b/micro_manager/config.py @@ -58,7 +58,7 @@ def __init__(self, config_file_name): self._postprocessing_file_name = None self._initialize_once = False - self._output_micro_sim_time = False + self._output_dir = None def set_logger(self, logger): """ @@ -93,6 +93,13 @@ def _read_json(self, config_file_name): .replace(".py", "") ) + try: + self._output_dir = self._data["output_dir"] + except BaseException: + self._logger.log_info_one_rank( + "No output directory provided. Output (including logging) will be saved in the current working directory." + ) + try: self._write_data_names = self._data["coupling_params"]["write_data_names"] assert isinstance( @@ -642,3 +649,14 @@ def create_single_sim_object(self): True if initialization is done only once, False otherwise. """ return self._initialize_once + + def get_output_dir_name(self): + """ + Get the name of the output directory. + + Returns + ------- + output_dir : string + Name of the output folder. + """ + return self._output_dir diff --git a/micro_manager/micro_manager.py b/micro_manager/micro_manager.py index 9636b83..3b54f98 100644 --- a/micro_manager/micro_manager.py +++ b/micro_manager/micro_manager.py @@ -57,9 +57,7 @@ def __init__(self, config_file: str) -> None: """ super().__init__(config_file) - self._logger = Logger( - "MicroManagerCoupling", "micro-manager-coupling.log", self._rank - ) + self._logger = Logger(__name__, self._rank) self._config.set_logger(self._logger) self._config.read_json_micro_manager() diff --git a/micro_manager/tools/logging_wrapper.py b/micro_manager/tools/logging_wrapper.py index 8ef7c6b..b1671bc 100644 --- a/micro_manager/tools/logging_wrapper.py +++ b/micro_manager/tools/logging_wrapper.py @@ -10,7 +10,9 @@ class Logger: Provides a logging wrapper for the Micro Manager classes. """ - def __init__(self, name, log_file, rank=0, level=logging.INFO, csv_logger=False): + def __init__( + self, name, log_file=None, rank=0, level=logging.INFO, csv_logger=False + ): """ Set up a logger. @@ -19,7 +21,7 @@ def __init__(self, name, log_file, rank=0, level=logging.INFO, csv_logger=False) name : string Name of the logger. log_file : string - Name of the log file. + Name of the log file (default is "micro-manager.log"). rank : int, optional Rank of the logger (default is 0). level : int, optional @@ -30,7 +32,11 @@ def __init__(self, name, log_file, rank=0, level=logging.INFO, csv_logger=False) self._rank = rank - handler = logging.FileHandler(log_file) + if log_file is None: + handler = logging.StreamHandler() + else: + handler = logging.FileHandler(log_file) + handler.setLevel(level) if csv_logger: diff --git a/tests/integration/test_unit_cube/micro-manager-config-global-adaptivity-parallel.json b/tests/integration/test_unit_cube/micro-manager-config-global-adaptivity-parallel.json index bd2c738..cc2cd67 100644 --- a/tests/integration/test_unit_cube/micro-manager-config-global-adaptivity-parallel.json +++ b/tests/integration/test_unit_cube/micro-manager-config-global-adaptivity-parallel.json @@ -1,5 +1,6 @@ { "micro_file_name": "micro_dummy", + "output_dir": "adaptivity_output", "coupling_params": { "precice_config_file_name": "precice-config.xml", "macro_mesh_name": "macro-cube-mesh",