From a2845e41962f5e624cd84629b77b92bb80e686d9 Mon Sep 17 00:00:00 2001 From: Ishaan Desai Date: Mon, 2 Dec 2024 17:45:39 +0100 Subject: [PATCH] Add CSV logging --- micro_manager/micro_manager.py | 20 ++++++++++++-------- micro_manager/tools/logging_wrapper.py | 20 ++++++++++++++------ 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/micro_manager/micro_manager.py b/micro_manager/micro_manager.py index 1d4ae50..8457615 100644 --- a/micro_manager/micro_manager.py +++ b/micro_manager/micro_manager.py @@ -88,11 +88,11 @@ def __init__(self, config_file: str) -> None: if self._is_adaptivity_on: self._adaptivity_logger = Logger( - "Adaptivity", "micro-manager-adaptivity.log", self._rank + "Adaptivity", "adaptivity_metrics.csv", self._rank, csv_logger=True ) self._adaptivity_logger.log_info_one_rank( - "Time,Avg Active Sims,Avg Inactive Sims" + "Time,Avg Active Sims,Avg Inactive Sims,Max Active,Max Inactive" ) self._number_of_sims_for_adaptivity = 0 @@ -316,12 +316,15 @@ def solve(self) -> None: ) self._adaptivity_logger.log_info_one_rank( - "{},{},{}".format( + "{},{},{},{},{}".format( t, np.mean(global_active_sims), np.mean(global_inactive_sims), + np.max(global_active_sims), + np.max(global_inactive_sims), ) ) + self._logger.log_info_one_rank("Time window {} converged.".format(n)) self._participant.finalize() @@ -365,9 +368,6 @@ def initialize(self) -> None: assert self._mesh_vertex_coords.size != 0, "Macro mesh has no vertices." self._local_number_of_sims, _ = self._mesh_vertex_coords.shape - self._logger.log_info_any_rank( - "Number of local micro simulations = {}".format(self._local_number_of_sims) - ) if self._local_number_of_sims == 0: if self._is_parallel: @@ -389,6 +389,10 @@ def initialize(self) -> None: # Get global number of micro simulations self._global_number_of_sims = np.sum(nms_all_ranks) + self._logger.log_info_one_rank( + "Number of micro simulations: {}".format(self._global_number_of_sims) + ) + if self._is_adaptivity_on: for name, is_data_vector in self._adaptivity_data_names.items(): if is_data_vector: @@ -480,7 +484,7 @@ def initialize(self) -> None: "The initialize() method of the Micro simulation has an incorrect number of arguments." ) except TypeError: - self._logger.log_one_rank( + self._logger.log_info_one_rank( "The signature of initialize() method of the micro simulation cannot be determined. Trying to determine the signature by calling the method." ) # Try to get the signature of the initialize() method, if it is not written in Python @@ -488,7 +492,7 @@ def initialize(self) -> None: self._micro_sims[0].initialize() is_initial_data_required = False except TypeError: - self._logger.log_one_rank( + self._logger.log_info_one_rank( "The initialize() method of the micro simulation has arguments. Attempting to call it again with initial data." ) try: # Try to call the initialize() method with initial data diff --git a/micro_manager/tools/logging_wrapper.py b/micro_manager/tools/logging_wrapper.py index 72c44aa..8ef7c6b 100644 --- a/micro_manager/tools/logging_wrapper.py +++ b/micro_manager/tools/logging_wrapper.py @@ -10,7 +10,7 @@ class Logger: Provides a logging wrapper for the Micro Manager classes. """ - def __init__(self, name, log_file, rank=0, level=logging.INFO): + def __init__(self, name, log_file, rank=0, level=logging.INFO, csv_logger=False): """ Set up a logger. @@ -20,8 +20,12 @@ def __init__(self, name, log_file, rank=0, level=logging.INFO): Name of the logger. log_file : string Name of the log file. + rank : int, optional + Rank of the logger (default is 0). level : int, optional Logging level (default is logging.INFO). + csv_logger : bool, optional + If True, the logger will log in CSV format (default is False). """ self._rank = rank @@ -29,11 +33,15 @@ def __init__(self, name, log_file, rank=0, level=logging.INFO): handler = logging.FileHandler(log_file) handler.setLevel(level) - formatter = logging.Formatter( - "[" - + str(self._rank) - + "] %(asctime)s - %(name)s - %(levelname)s - %(message)s" - ) + if csv_logger: + formatter = logging.Formatter("%(message)s") + else: + formatter = logging.Formatter( + "[" + + str(self._rank) + + "] %(asctime)s - %(name)s - %(levelname)s - %(message)s" + ) + handler.setFormatter(formatter) self._logger = logging.getLogger(name)