From 10f434b776e1aff0c34efbe19d78d80d1edbae16 Mon Sep 17 00:00:00 2001 From: Ishaan Desai Date: Thu, 7 Nov 2024 14:45:40 +0100 Subject: [PATCH] Fix tests and add CHANGELOG entry --- CHANGELOG.md | 1 + micro_manager/micro_manager.py | 4 ++-- micro_manager/tools/logging_wrapper.py | 6 ++++-- tests/unit/test_adaptivity_parallel.py | 6 +++--- tests/unit/test_adaptivity_serial.py | 13 +++++-------- tests/unit/test_micro_manager.py | 2 +- tests/unit/test_snapshot_computation.py | 2 +- 7 files changed, 17 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dd8133c5..4c3e31be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## latest +- Improve logging by wrapping Python logger in a class https://github.com/precice/micro-manager/pull/133 - Add information about adaptivity tuning parameters https://github.com/precice/micro-manager/pull/131 - Put computation of counting active steps inside the adaptivity variant `if` condition https://github.com/precice/micro-manager/pull/130 diff --git a/micro_manager/micro_manager.py b/micro_manager/micro_manager.py index b595992c..8d212838 100644 --- a/micro_manager/micro_manager.py +++ b/micro_manager/micro_manager.py @@ -665,7 +665,7 @@ def _solve_micro_simulations(self, micro_sims_input: list, dt: float) -> list: self._mesh_vertex_coords[count], micro_sims_input[count] ) ) - self._logger.error(error_message) + self._logger.log_error_any_rank(error_message) self._has_sim_crashed[count] = True # If interpolate is off, terminate after crash @@ -782,7 +782,7 @@ def _solve_micro_simulations_with_adaptivity( self._mesh_vertex_coords[active_id] ) ) - self._logger.error(error_message) + self._logger.log_error_any_rank(error_message) self._has_sim_crashed[active_id] = True # If interpolate is off, terminate after crash diff --git a/micro_manager/tools/logging_wrapper.py b/micro_manager/tools/logging_wrapper.py index a38c32e1..72c44aa7 100644 --- a/micro_manager/tools/logging_wrapper.py +++ b/micro_manager/tools/logging_wrapper.py @@ -30,7 +30,9 @@ def __init__(self, name, log_file, rank=0, level=logging.INFO): handler.setLevel(level) formatter = logging.Formatter( - "%(asctime)s - %(name)s - %(levelname)s - %(message)s" + "[" + + str(self._rank) + + "] %(asctime)s - %(name)s - %(levelname)s - %(message)s" ) handler.setFormatter(formatter) @@ -81,4 +83,4 @@ def log_error_any_rank(self, message): message : string Message to log. """ - self._logger.error("[" + str(self._rank) + "] " + message) + self._logger.error(message) diff --git a/tests/unit/test_adaptivity_parallel.py b/tests/unit/test_adaptivity_parallel.py index b07ee923..7d5c3156 100644 --- a/tests/unit/test_adaptivity_parallel.py +++ b/tests/unit/test_adaptivity_parallel.py @@ -31,7 +31,7 @@ def test_update_inactive_sims_global_adaptivity(self): configurator = MagicMock() configurator.get_adaptivity_similarity_measure = MagicMock(return_value="L1") adaptivity_controller = GlobalAdaptivityCalculator( - configurator, MagicMock(), 5, global_ids, rank=self._rank, comm=self._comm + configurator, 5, global_ids, rank=self._rank, comm=self._comm ) # Force the activation of sim #0 and #4 @@ -105,7 +105,7 @@ def test_update_all_active_sims_global_adaptivity(self): configurator.get_adaptivity_coarsening_const = MagicMock(return_value=0.2) configurator.get_adaptivity_similarity_measure = MagicMock(return_value="L2rel") adaptivity_controller = GlobalAdaptivityCalculator( - configurator, MagicMock(), 5, global_ids, rank=self._rank, comm=self._comm + configurator, 5, global_ids, rank=self._rank, comm=self._comm ) adaptivity_controller._adaptivity_data_names = { @@ -173,7 +173,7 @@ def test_communicate_micro_output(self): configurator = MagicMock() configurator.get_adaptivity_similarity_measure = MagicMock(return_value="L1") adaptivity_controller = GlobalAdaptivityCalculator( - configurator, MagicMock(), 5, global_ids, rank=self._rank, comm=self._comm + configurator, 5, global_ids, rank=self._rank, comm=self._comm ) adaptivity_controller.communicate_micro_output( diff --git a/tests/unit/test_adaptivity_serial.py b/tests/unit/test_adaptivity_serial.py index 13155351..72349f39 100644 --- a/tests/unit/test_adaptivity_serial.py +++ b/tests/unit/test_adaptivity_serial.py @@ -68,7 +68,7 @@ def test_get_similarity_dists(self): """ configurator = MagicMock() configurator.get_adaptivity_similarity_measure = MagicMock(return_value="L1") - adaptivity_controller = AdaptivityCalculator(configurator, logger=MagicMock()) + adaptivity_controller = AdaptivityCalculator(configurator) adaptivity_controller._hist_param = 0.5 adaptivity_controller._adaptivity_data_names = [ "micro-scalar-data", @@ -102,7 +102,7 @@ def test_update_active_sims(self): """ configurator = MagicMock() configurator.get_adaptivity_similarity_measure = MagicMock(return_value="L1") - adaptivity_controller = AdaptivityCalculator(configurator, logger=MagicMock()) + adaptivity_controller = AdaptivityCalculator(configurator) adaptivity_controller._refine_const = self._refine_const adaptivity_controller._coarse_const = self._coarse_const adaptivity_controller._adaptivity_data_names = [ @@ -127,8 +127,7 @@ def test_adaptivity_norms(self): """ Test functionality for calculating similarity criteria between pairs of simulations using different norms in class AdaptivityCalculator. """ - logger = MagicMock() - calc = AdaptivityCalculator(Config(logger, "micro-manager-config.json"), logger) + calc = AdaptivityCalculator(Config("micro-manager-config.json")) fake_data = np.array([[1], [2], [3]]) self.assertTrue( @@ -209,7 +208,7 @@ def test_associate_active_to_inactive(self): """ configurator = MagicMock() configurator.get_adaptivity_similarity_measure = MagicMock(return_value="L1") - adaptivity_controller = AdaptivityCalculator(configurator, logger=MagicMock()) + adaptivity_controller = AdaptivityCalculator(configurator) adaptivity_controller._refine_const = self._refine_const adaptivity_controller._coarse_const = self._coarse_const adaptivity_controller._adaptivity_data_names = [ @@ -236,9 +235,7 @@ def test_update_inactive_sims_local_adaptivity(self): """ configurator = MagicMock() configurator.get_adaptivity_similarity_measure = MagicMock(return_value="L1") - adaptivity_controller = LocalAdaptivityCalculator( - configurator, logger=MagicMock() - ) + adaptivity_controller = LocalAdaptivityCalculator(configurator) adaptivity_controller._refine_const = self._refine_const adaptivity_controller._coarse_const = self._coarse_const adaptivity_controller._adaptivity_data_names = [ diff --git a/tests/unit/test_micro_manager.py b/tests/unit/test_micro_manager.py index b43850cd..b733069f 100644 --- a/tests/unit/test_micro_manager.py +++ b/tests/unit/test_micro_manager.py @@ -119,7 +119,7 @@ def test_config(self): """ Test if the functions in the Config class work. """ - config = micro_manager.Config(MagicMock(), "micro-manager-config.json") + config = micro_manager.Config("micro-manager-config.json") config.read_json_micro_manager() self.assertEqual(config._config_file_name.split("/")[-1], "dummy-config.xml") self.assertEqual(config._micro_file_name, "test_micro_manager") diff --git a/tests/unit/test_snapshot_computation.py b/tests/unit/test_snapshot_computation.py index 68bde45e..c62bef24 100644 --- a/tests/unit/test_snapshot_computation.py +++ b/tests/unit/test_snapshot_computation.py @@ -136,7 +136,7 @@ def test_config(self): """ Test if the functions in the SnapshotConfig class work. """ - config = Config(MagicMock(), "snapshot-config.json") + config = Config("snapshot-config.json") config.read_json_snapshot() self.assertEqual(