Skip to content

Commit

Permalink
save_state method, safer logging
Browse files Browse the repository at this point in the history
Call save_state to dump the current attributes of the XFaster object to disk.
Useful for debugging.
  • Loading branch information
arahlin committed Nov 14, 2023
1 parent da94c2a commit c385943
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions xfaster/xfaster_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ def root_notice(msg, *args, **kwargs):
handler.setFormatter(fmt)

# configure logger
self.logger = logging.getLogger("xfaster")
self.logger = logging.getLogger("xfaster.{}".format(id(self)))
# replace any existing handlers before adding one
if self.logger.hasHandlers():
for h in list(self.logger.handlers):
Expand Down Expand Up @@ -488,7 +488,7 @@ def __del__(self):
"""

# cleanup logging handlers
for handler in self.logger.handlers[::-1]:
for handler in list(self.logger.handlers[::-1]):
try:
handler.acquire()
handler.flush()
Expand All @@ -497,6 +497,7 @@ def __del__(self):
pass
finally:
handler.release()
self.logger.removeHandler(handler)

def _get_data_files(
self,
Expand Down Expand Up @@ -1658,6 +1659,31 @@ def save_data(self, name, from_attrs=[], file_attrs=[], **data):
data["output_file"] = output_file
return data

def save_state(self, tag):
"""
Save current object state to disk.
Arguments
---------
tag : str
Set the name for the output file to ``state_<tag>``. Otherwise the
standard file options are applied to produce the output filename.
See ``get_filename`` for details.
"""
data = vars(self)
data["data_version"] = self.data_version
data.pop("logger")

file_opts = {}
for opt in ["map_tag", "iter_index", "data_opts", "bp_opts", "extra_tag"]:
if opt in data:
file_opts[opt] = data[opt]

name = "state_{}".format(tag)
output_file = self.get_filename(name, ext=".npz", **file_opts)

pt.save(output_file, **data)

def save_config(self, cfg):
"""
Save a configuration file for the current run on disk.
Expand Down

0 comments on commit c385943

Please sign in to comment.