Skip to content

Commit

Permalink
Add CSV logging
Browse files Browse the repository at this point in the history
  • Loading branch information
IshaanDesai committed Dec 2, 2024
1 parent 60d8607 commit a2845e4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
20 changes: 12 additions & 8 deletions micro_manager/micro_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -480,15 +484,15 @@ 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
try: # Try to call the initialize() method without initial data
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
Expand Down
20 changes: 14 additions & 6 deletions micro_manager/tools/logging_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -20,20 +20,28 @@ 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

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)
Expand Down

0 comments on commit a2845e4

Please sign in to comment.