2023-11-07 10:54:22 +01:00
|
|
|
"""
|
|
|
|
Partial copy of sensai.util.logging
|
|
|
|
"""
|
|
|
|
# ruff: noqa
|
|
|
|
import atexit
|
|
|
|
import logging as lg
|
|
|
|
import sys
|
|
|
|
from collections.abc import Callable
|
|
|
|
from datetime import datetime
|
|
|
|
from io import StringIO
|
|
|
|
from logging import *
|
2023-12-05 12:04:18 +01:00
|
|
|
from typing import Any, TypeVar, cast
|
2023-11-07 10:54:22 +01:00
|
|
|
|
2023-12-05 12:04:18 +01:00
|
|
|
log = getLogger(__name__) # type: ignore
|
2023-11-07 10:54:22 +01:00
|
|
|
|
|
|
|
LOG_DEFAULT_FORMAT = "%(levelname)-5s %(asctime)-15s %(name)s:%(funcName)s - %(message)s"
|
|
|
|
|
|
|
|
# Holds the log format that is configured by the user (using function `configure`), such
|
|
|
|
# that it can be reused in other places
|
|
|
|
_logFormat = LOG_DEFAULT_FORMAT
|
|
|
|
|
|
|
|
|
Feature/dataclasses (#996)
This PR adds strict typing to the output of `update` and `learn` in all
policies. This will likely be the last large refactoring PR before the
next release (0.6.0, not 1.0.0), so it requires some attention. Several
difficulties were encountered on the path to that goal:
1. The policy hierarchy is actually "broken" in the sense that the keys
of dicts that were output by `learn` did not follow the same enhancement
(inheritance) pattern as the policies. This is a real problem and should
be addressed in the near future. Generally, several aspects of the
policy design and hierarchy might deserve a dedicated discussion.
2. Each policy needs to be generic in the stats return type, because one
might want to extend it at some point and then also extend the stats.
Even within the source code base this pattern is necessary in many
places.
3. The interaction between learn and update is a bit quirky, we
currently handle it by having update modify special field inside
TrainingStats, whereas all other fields are handled by learn.
4. The IQM module is a policy wrapper and required a
TrainingStatsWrapper. The latter relies on a bunch of black magic.
They were addressed by:
1. Live with the broken hierarchy, which is now made visible by bounds
in generics. We use type: ignore where appropriate.
2. Make all policies generic with bounds following the policy
inheritance hierarchy (which is incorrect, see above). We experimented a
bit with nested TrainingStats classes, but that seemed to add more
complexity and be harder to understand. Unfortunately, mypy thinks that
the code below is wrong, wherefore we have to add `type: ignore` to the
return of each `learn`
```python
T = TypeVar("T", bound=int)
def f() -> T:
return 3
```
3. See above
4. Write representative tests for the `TrainingStatsWrapper`. Still, the
black magic might cause nasty surprises down the line (I am not proud of
it)...
Closes #933
---------
Co-authored-by: Maximilian Huettenrauch <m.huettenrauch@appliedai.de>
Co-authored-by: Michael Panchenko <m.panchenko@appliedai.de>
2023-12-30 11:09:03 +01:00
|
|
|
def set_numerical_fields_to_precision(data: dict[str, Any], precision: int = 3) -> dict[str, Any]:
|
|
|
|
"""Returns a copy of the given dictionary with all numerical values rounded to the given precision.
|
|
|
|
|
|
|
|
Note: does not recurse into nested dictionaries.
|
|
|
|
|
|
|
|
:param data: a dictionary
|
|
|
|
:param precision: the precision to be used
|
|
|
|
"""
|
|
|
|
result = {}
|
|
|
|
for k, v in data.items():
|
|
|
|
if isinstance(v, float):
|
|
|
|
v = round(v, precision)
|
|
|
|
result[k] = v
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2023-12-05 12:04:18 +01:00
|
|
|
def remove_log_handlers() -> None:
|
2023-11-07 10:54:22 +01:00
|
|
|
"""Removes all current log handlers."""
|
|
|
|
logger = getLogger()
|
|
|
|
while logger.hasHandlers():
|
|
|
|
logger.removeHandler(logger.handlers[0])
|
|
|
|
|
|
|
|
|
2023-12-05 12:04:18 +01:00
|
|
|
def remove_log_handler(handler: Handler) -> None:
|
2023-11-07 10:54:22 +01:00
|
|
|
getLogger().removeHandler(handler)
|
|
|
|
|
|
|
|
|
2023-12-05 12:04:18 +01:00
|
|
|
def is_log_handler_active(handler: Handler) -> bool:
|
2023-11-07 10:54:22 +01:00
|
|
|
"""Checks whether the given handler is active.
|
|
|
|
|
|
|
|
:param handler: a log handler
|
|
|
|
:return: True if the handler is active, False otherwise
|
|
|
|
"""
|
|
|
|
return handler in getLogger().handlers
|
|
|
|
|
|
|
|
|
|
|
|
# noinspection PyShadowingBuiltins
|
2023-12-05 12:04:18 +01:00
|
|
|
def configure(format: str = LOG_DEFAULT_FORMAT, level: int = lg.DEBUG) -> None:
|
2023-11-07 10:54:22 +01:00
|
|
|
"""Configures logging to stdout with the given format and log level,
|
|
|
|
also configuring the default log levels of some overly verbose libraries as well as some pandas output options.
|
|
|
|
|
|
|
|
:param format: the log format
|
|
|
|
:param level: the minimum log level
|
|
|
|
"""
|
|
|
|
global _logFormat
|
|
|
|
_logFormat = format
|
|
|
|
remove_log_handlers()
|
|
|
|
basicConfig(level=level, format=format, stream=sys.stdout)
|
|
|
|
# set log levels of third-party libraries
|
|
|
|
getLogger("numba").setLevel(INFO)
|
|
|
|
|
|
|
|
|
2023-12-05 12:04:18 +01:00
|
|
|
T = TypeVar("T")
|
|
|
|
|
|
|
|
|
2023-11-07 10:54:22 +01:00
|
|
|
# noinspection PyShadowingBuiltins
|
2023-12-05 12:04:18 +01:00
|
|
|
def run_main(
|
|
|
|
main_fn: Callable[[], T], format: str = LOG_DEFAULT_FORMAT, level: int = lg.DEBUG
|
|
|
|
) -> T | None:
|
2023-11-07 10:54:22 +01:00
|
|
|
"""Configures logging with the given parameters, ensuring that any exceptions that occur during
|
|
|
|
the execution of the given function are logged.
|
|
|
|
Logs two additional messages, one before the execution of the function, and one upon its completion.
|
|
|
|
|
|
|
|
:param main_fn: the function to be executed
|
|
|
|
:param format: the log message format
|
|
|
|
:param level: the minimum log level
|
|
|
|
:return: the result of `main_fn`
|
|
|
|
"""
|
|
|
|
configure(format=format, level=level)
|
2023-12-05 12:04:18 +01:00
|
|
|
log.info("Starting") # type: ignore
|
2023-11-07 10:54:22 +01:00
|
|
|
try:
|
|
|
|
result = main_fn()
|
2023-12-05 12:04:18 +01:00
|
|
|
log.info("Done") # type: ignore
|
2023-11-07 10:54:22 +01:00
|
|
|
return result
|
|
|
|
except Exception as e:
|
2023-12-05 12:04:18 +01:00
|
|
|
log.error("Exception during script execution", exc_info=e) # type: ignore
|
|
|
|
return None
|
2023-11-07 10:54:22 +01:00
|
|
|
|
|
|
|
|
2023-12-05 12:04:18 +01:00
|
|
|
def run_cli(
|
|
|
|
main_fn: Callable[[], T], format: str = LOG_DEFAULT_FORMAT, level: int = lg.DEBUG
|
|
|
|
) -> T | None:
|
2023-11-07 10:54:22 +01:00
|
|
|
"""
|
|
|
|
Configures logging with the given parameters and runs the given main function as a
|
|
|
|
CLI using `jsonargparse` (which is configured to also parse attribute docstrings, such
|
|
|
|
that dataclasses can be used as function arguments).
|
|
|
|
Using this function requires that `jsonargparse` and `docstring_parser` be available.
|
|
|
|
Like `run_main`, two additional log messages will be logged (at the beginning and end
|
|
|
|
of the execution), and it is ensured that all exceptions will be logged.
|
|
|
|
|
|
|
|
:param main_fn: the function to be executed
|
|
|
|
:param format: the log message format
|
|
|
|
:param level: the minimum log level
|
|
|
|
:return: the result of `main_fn`
|
|
|
|
"""
|
|
|
|
from jsonargparse import set_docstring_parse_options, CLI
|
|
|
|
|
|
|
|
set_docstring_parse_options(attribute_docstrings=True)
|
|
|
|
return run_main(lambda: CLI(main_fn), format=format, level=level)
|
|
|
|
|
|
|
|
|
|
|
|
def datetime_tag() -> str:
|
|
|
|
""":return: a string tag for use in log file names which contains the current date and time (compact but readable)"""
|
|
|
|
return datetime.now().strftime("%Y%m%d-%H%M%S")
|
|
|
|
|
|
|
|
|
|
|
|
_fileLoggerPaths: list[str] = []
|
|
|
|
_isAtExitReportFileLoggerRegistered = False
|
|
|
|
_memoryLogStream: StringIO | None = None
|
|
|
|
|
|
|
|
|
2023-12-05 12:04:18 +01:00
|
|
|
def _at_exit_report_file_logger() -> None:
|
2023-11-07 10:54:22 +01:00
|
|
|
for path in _fileLoggerPaths:
|
|
|
|
print(f"A log file was saved to {path}")
|
|
|
|
|
|
|
|
|
2023-12-05 12:04:18 +01:00
|
|
|
def add_file_logger(path: str, register_atexit: bool = True) -> FileHandler:
|
2023-11-07 10:54:22 +01:00
|
|
|
global _isAtExitReportFileLoggerRegistered
|
2023-12-05 12:04:18 +01:00
|
|
|
log.info(f"Logging to {path} ...") # type: ignore
|
2023-11-07 10:54:22 +01:00
|
|
|
handler = FileHandler(path)
|
|
|
|
handler.setFormatter(Formatter(_logFormat))
|
|
|
|
Logger.root.addHandler(handler)
|
|
|
|
_fileLoggerPaths.append(path)
|
|
|
|
if not _isAtExitReportFileLoggerRegistered and register_atexit:
|
|
|
|
atexit.register(_at_exit_report_file_logger)
|
|
|
|
_isAtExitReportFileLoggerRegistered = True
|
|
|
|
return handler
|
|
|
|
|
|
|
|
|
|
|
|
def add_memory_logger() -> None:
|
|
|
|
"""Enables in-memory logging (if it is not already enabled), i.e. all log statements are written to a memory buffer and can later be
|
|
|
|
read via function `get_memory_log()`.
|
|
|
|
"""
|
|
|
|
global _memoryLogStream
|
|
|
|
if _memoryLogStream is not None:
|
|
|
|
return
|
|
|
|
_memoryLogStream = StringIO()
|
|
|
|
handler = StreamHandler(_memoryLogStream)
|
|
|
|
handler.setFormatter(Formatter(_logFormat))
|
|
|
|
Logger.root.addHandler(handler)
|
|
|
|
|
|
|
|
|
2023-12-05 12:04:18 +01:00
|
|
|
def get_memory_log() -> Any:
|
2023-11-07 10:54:22 +01:00
|
|
|
""":return: the in-memory log (provided that `add_memory_logger` was called beforehand)"""
|
2023-12-05 12:04:18 +01:00
|
|
|
assert _memoryLogStream is not None, "This should not have happened and might be a bug."
|
2023-11-07 10:54:22 +01:00
|
|
|
return _memoryLogStream.getvalue()
|
|
|
|
|
|
|
|
|
|
|
|
class FileLoggerContext:
|
2023-12-05 12:04:18 +01:00
|
|
|
def __init__(self, path: str, enabled: bool = True):
|
2023-11-07 10:54:22 +01:00
|
|
|
self.enabled = enabled
|
|
|
|
self.path = path
|
2023-12-05 12:04:18 +01:00
|
|
|
self._log_handler: Handler | None = None
|
2023-11-07 10:54:22 +01:00
|
|
|
|
2023-12-05 12:04:18 +01:00
|
|
|
def __enter__(self) -> None:
|
2023-11-07 10:54:22 +01:00
|
|
|
if self.enabled:
|
|
|
|
self._log_handler = add_file_logger(self.path, register_atexit=False)
|
|
|
|
|
2023-12-05 12:04:18 +01:00
|
|
|
def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> None:
|
2023-11-07 10:54:22 +01:00
|
|
|
if self._log_handler is not None:
|
|
|
|
remove_log_handler(self._log_handler)
|