From 5cea6527510dc09ecfe8336aebaeda6c8a1fd38e Mon Sep 17 00:00:00 2001 From: Nic Ma Date: Thu, 24 Mar 2022 11:53:47 +0800 Subject: [PATCH 1/4] [DLMED] add logging config Signed-off-by: Nic Ma --- monai/bundle/scripts.py | 17 +++++++++++++++-- tests/test_integration_bundle_run.py | 3 ++- tests/testing_data/logging.conf | 27 +++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 tests/testing_data/logging.conf diff --git a/monai/bundle/scripts.py b/monai/bundle/scripts.py index 5bbde5fd62..1e342adff0 100644 --- a/monai/bundle/scripts.py +++ b/monai/bundle/scripts.py @@ -13,6 +13,7 @@ import pprint import re from typing import Dict, Optional, Sequence, Tuple, Union +from logging.config import fileConfig import torch @@ -106,6 +107,7 @@ def run( runner_id: Optional[str] = None, meta_file: Optional[Union[str, Sequence[str]]] = None, config_file: Optional[Union[str, Sequence[str]]] = None, + logging_file: Optional[str] = None, args_file: Optional[str] = None, **override, ): @@ -137,17 +139,28 @@ def run( meta_file: filepath of the metadata file, if it is a list of file paths, the content of them will be merged. config_file: filepath of the config file, if `None`, must be provided in `args_file`. if it is a list of file paths, the content of them will be merged. + logging_file: config file for `logging` module in the program, default to `None`. args_file: a JSON or YAML file to provide default values for `runner_id`, `meta_file`, - `config_file`, and override pairs. so that the command line inputs can be simplified. + `config_file`, `logging`, and override pairs. so that the command line inputs can be simplified. override: id-value pairs to override or add the corresponding config content. e.g. ``--net#input_chns 42``. """ - _args = _update_args(args=args_file, runner_id=runner_id, meta_file=meta_file, config_file=config_file, **override) + _args = _update_args( + args=args_file, + runner_id=runner_id, + meta_file=meta_file, + config_file=config_file, + logging_file=logging_file, + **override, + ) if "config_file" not in _args: raise ValueError(f"`config_file` is required for 'monai.bundle run'.\n{run.__doc__}") _log_input_summary(tag="run", args=_args) + log_conf = _args.pop("logging_file", None) + if log_conf is not None: + fileConfig(log_conf) parser = ConfigParser() parser.read_config(f=_args.pop("config_file")) diff --git a/tests/test_integration_bundle_run.py b/tests/test_integration_bundle_run.py index e6d4dfd89f..8ea1f44157 100644 --- a/tests/test_integration_bundle_run.py +++ b/tests/test_integration_bundle_run.py @@ -61,7 +61,8 @@ def test_shape(self, config_file, expected_shape): nib.save(nib.Nifti1Image(test_image, np.eye(4)), filename) # generate default args in a JSON file - def_args = {"config_file": "will be replaced by `config_file` arg"} + logging_conf = os.path.join(os.path.dirname(__file__), "testing_data", "logging.conf") + def_args = {"config_file": "will be replaced by `config_file` arg", "logging_file": logging_conf} def_args_file = os.path.join(tempdir, "def_args.json") ConfigParser.export_config_file(config=def_args, filepath=def_args_file) diff --git a/tests/testing_data/logging.conf b/tests/testing_data/logging.conf new file mode 100644 index 0000000000..2e3404e0ab --- /dev/null +++ b/tests/testing_data/logging.conf @@ -0,0 +1,27 @@ +[loggers] +keys=root,ignite.engine.SupervisedEvaluator + +[handlers] +keys=consoleHandler + +[formatters] +keys=fullFormatter + +[logger_root] +level=INFO +handlers=consoleHandler + +[logger_ignite.engine.SupervisedEvaluator] +level=INFO +handlers=consoleHandler +qualname=ignite.engine.SupervisedEvaluator +propagate=0 + +[handler_consoleHandler] +class=StreamHandler +level=INFO +formatter=fullFormatter +args=(sys.stdout,) + +[formatter_fullFormatter] +format=%(asctime)s - %(name)s - %(levelname)s - %(message)s From bb36fa4c1af12daf8a574f485fc988ba2bf735bb Mon Sep 17 00:00:00 2001 From: monai-bot Date: Thu, 24 Mar 2022 08:41:25 +0000 Subject: [PATCH 2/4] [MONAI] python code formatting Signed-off-by: monai-bot --- monai/bundle/scripts.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/monai/bundle/scripts.py b/monai/bundle/scripts.py index 36abc149b7..d1c4778401 100644 --- a/monai/bundle/scripts.py +++ b/monai/bundle/scripts.py @@ -13,8 +13,8 @@ import json import pprint import re -from typing import Dict, Optional, Sequence, Tuple, Union from logging.config import fileConfig +from typing import Dict, Optional, Sequence, Tuple, Union import torch from torch.cuda import is_available @@ -172,7 +172,7 @@ def run( raise ValueError(f"`config_file` is required for 'monai.bundle run'.\n{run.__doc__}") _log_input_summary(tag="run", args=_args) config_file_, meta_file_, runner_id_, logging_file_ = _pop_args( - _args, "config_file", meta_file=None, runner_id="", logging_file=None + _args, "config_file", meta_file=None, runner_id="", logging_file=None ) if logging_file_ is not None: fileConfig(logging_file_) From d45fc3d4d8baf26b5072d811fe95062f943808b3 Mon Sep 17 00:00:00 2001 From: Nic Ma Date: Thu, 24 Mar 2022 19:00:10 +0800 Subject: [PATCH 3/4] [DLMED] adjust config Signed-off-by: Nic Ma --- monai/bundle/scripts.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/monai/bundle/scripts.py b/monai/bundle/scripts.py index d1c4778401..3b4fe1d16e 100644 --- a/monai/bundle/scripts.py +++ b/monai/bundle/scripts.py @@ -175,7 +175,8 @@ def run( _args, "config_file", meta_file=None, runner_id="", logging_file=None ) if logging_file_ is not None: - fileConfig(logging_file_) + logger.info(f"set logging properties based on config: {logging_file_}.") + fileConfig(logging_file_, disable_existing_loggers=False) parser = ConfigParser() parser.read_config(f=config_file_) From 96860230b463ecfc1978970099b11f4c85a20180 Mon Sep 17 00:00:00 2001 From: Nic Ma Date: Thu, 24 Mar 2022 19:10:31 +0800 Subject: [PATCH 4/4] [DLMED] add doc-string Signed-off-by: Nic Ma --- monai/bundle/scripts.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/monai/bundle/scripts.py b/monai/bundle/scripts.py index 3b4fe1d16e..894fbd6c25 100644 --- a/monai/bundle/scripts.py +++ b/monai/bundle/scripts.py @@ -152,7 +152,8 @@ def run( meta_file: filepath of the metadata file, if it is a list of file paths, the content of them will be merged. config_file: filepath of the config file, if `None`, must be provided in `args_file`. if it is a list of file paths, the content of them will be merged. - logging_file: config file for `logging` module in the program, default to `None`. + logging_file: config file for `logging` module in the program, default to `None`. for more details: + https://docs.python.org/3/library/logging.config.html#logging.config.fileConfig. args_file: a JSON or YAML file to provide default values for `runner_id`, `meta_file`, `config_file`, `logging`, and override pairs. so that the command line inputs can be simplified. override: id-value pairs to override or add the corresponding config content.