diff --git a/micro_manager/adaptivity/adaptivity.py b/micro_manager/adaptivity/adaptivity.py index bf0fa3c..4feb63b 100644 --- a/micro_manager/adaptivity/adaptivity.py +++ b/micro_manager/adaptivity/adaptivity.py @@ -5,12 +5,13 @@ from math import exp from typing import Callable from warnings import warn +from micro_manager.tools.logging_wrapper import Logger import numpy as np class AdaptivityCalculator: - def __init__(self, configurator) -> None: + def __init__(self, configurator, rank) -> None: """ Class constructor. @@ -18,7 +19,8 @@ def __init__(self, configurator) -> None: ---------- configurator : object of class Config Object which has getter functions to get parameters defined in the configuration file. - logger : Logger defined from the standard package logging + rank : int + Rank of the MPI communicator. """ self._refine_const = configurator.get_adaptivity_refining_const() self._coarse_const = configurator.get_adaptivity_coarsening_const() @@ -29,10 +31,20 @@ def __init__(self, configurator) -> None: self._coarse_tol = 0.0 self._ref_tol = 0.0 + self._rank = rank + self._similarity_measure = self._get_similarity_measure( configurator.get_adaptivity_similarity_measure() ) + self._metrics_logger = Logger( + "Adaptivity", "adaptivity-metrics.csv", rank, csv_logger=True + ) + + self._metrics_logger.log_info_one_rank( + "Time Window,Avg Active Sims,Avg Inactive Sims,Max Active,Max Inactive" + ) + def _get_similarity_dists( self, dt: float, similarity_dists: np.ndarray, data: dict ) -> np.ndarray: diff --git a/micro_manager/adaptivity/global_adaptivity.py b/micro_manager/adaptivity/global_adaptivity.py index bf7b700..47bff8b 100644 --- a/micro_manager/adaptivity/global_adaptivity.py +++ b/micro_manager/adaptivity/global_adaptivity.py @@ -42,7 +42,7 @@ def __init__( comm : MPI.COMM_WORLD Global communicator of MPI. """ - super().__init__(configurator) + super().__init__(configurator, rank) self._global_ids = global_ids self._comm = comm self._rank = rank @@ -190,13 +190,18 @@ def get_full_field_micro_output( return micro_sims_output - def log_metrics(self, logger, adaptivity_data: list, n: int) -> None: - """ """ + def log_metrics(self, adaptivity_data: list, n: int) -> None: + """ + Log metrics for global adaptivity. + + Parameters + ---------- + """ is_sim_active = adaptivity_data[1] global_active_sims = np.count_nonzero(is_sim_active) global_inactive_sims = np.count_nonzero(is_sim_active == False) - logger.log_info_one_rank( + self._metrics_logger.log_info_one_rank( "{},{},{},{},{}".format( n, np.mean(global_active_sims), diff --git a/micro_manager/adaptivity/local_adaptivity.py b/micro_manager/adaptivity/local_adaptivity.py index c89f229..42661dc 100644 --- a/micro_manager/adaptivity/local_adaptivity.py +++ b/micro_manager/adaptivity/local_adaptivity.py @@ -10,7 +10,7 @@ class LocalAdaptivityCalculator(AdaptivityCalculator): - def __init__(self, configurator, comm) -> None: + def __init__(self, configurator, rank, comm) -> None: """ Class constructor. @@ -21,7 +21,7 @@ def __init__(self, configurator, comm) -> None: comm : MPI.COMM_WORLD Global communicator of MPI. """ - super().__init__(configurator) + super().__init__(configurator, rank) self._comm = comm def compute_adaptivity( @@ -144,8 +144,13 @@ def get_full_field_micro_output( return micro_sims_output - def log_metrics(self, logger, adaptivity_list: list, n: int) -> None: - """ """ + def log_metrics(self, adaptivity_list: list, n: int) -> None: + """ + Log metrics for local adaptivity. + + Parameters + ---------- + """ is_sim_active = adaptivity_list[1] # MPI Gather is necessary as local adaptivity only stores local data @@ -155,7 +160,7 @@ def log_metrics(self, logger, adaptivity_list: list, n: int) -> None: local_inactive_sims = np.count_nonzero(is_sim_active == False) global_inactive_sims = self._comm.gather(local_inactive_sims) - logger.log_info_one_rank( + self._metrics_logger.log_info_one_rank( "{},{},{},{},{}".format( n, np.mean(global_active_sims), diff --git a/micro_manager/micro_manager.py b/micro_manager/micro_manager.py index 56b7023..06a4763 100644 --- a/micro_manager/micro_manager.py +++ b/micro_manager/micro_manager.py @@ -106,14 +106,6 @@ def __init__(self, config_file: str) -> None: self._is_adaptivity_on = self._config.turn_on_adaptivity() if self._is_adaptivity_on: - self._adaptivity_logger = Logger( - "Adaptivity", "adaptivity-metrics.csv", self._rank, csv_logger=True - ) - - self._adaptivity_logger.log_info_one_rank( - "Time Window,Avg Active Sims,Avg Inactive Sims,Max Active,Max Inactive" - ) - self._number_of_sims_for_adaptivity: int = 0 self._data_for_adaptivity: Dict[str, np.ndarray] = dict() @@ -311,9 +303,7 @@ def solve(self) -> None: and n % self._adaptivity_output_n == 0 and self._rank == 0 ): - self._adaptivity_controller.log_metrics( - self._adaptivity_logger, adaptivity_data, n - ) + self._adaptivity_controller.log_metrics(adaptivity_data, n) self._logger.log_info_one_rank("Time window {} converged.".format(n))