From 52b9d60e427172f9ede8dcf405397ae599735862 Mon Sep 17 00:00:00 2001 From: Yiheng Wang Date: Mon, 27 Feb 2023 15:11:56 +0800 Subject: [PATCH 1/3] modify default values for run Signed-off-by: Yiheng Wang --- monai/bundle/scripts.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/monai/bundle/scripts.py b/monai/bundle/scripts.py index 0a4c3139b1..66a7e9faf7 100644 --- a/monai/bundle/scripts.py +++ b/monai/bundle/scripts.py @@ -609,15 +609,15 @@ def patch_bundle_tracking(parser: ConfigParser, settings: dict) -> None: def run( runner_id: str | Sequence[str] | None = None, - meta_file: str | Sequence[str] | None = None, + meta_file: str | Sequence[str] | None = "configs/metadata.json", config_file: str | Sequence[str] | None = None, - logging_file: str | None = None, + logging_file: str | None = "configs/logging.conf", tracking: str | dict | None = None, args_file: str | None = None, **override: Any, ) -> list: """ - Specify `meta_file` and `config_file` to run monai bundle components and workflows. + Specify `config_file` to run monai bundle components and workflows. Typical usage examples: @@ -642,10 +642,12 @@ def run( Args: runner_id: ID name of the expected config expression to run, can also be a list of IDs to run in order. meta_file: filepath of the metadata file, if it is a list of file paths, the content of them will be merged. + Default to "configs/metadata.json", which is commonly used for bundles in MONAI model zoo. 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`. for more details: + logging_file: config file for `logging` module in the program. for more details: https://docs.python.org/3/library/logging.config.html#logging.config.fileConfig. + Default to "configs/logging.conf", which is commonly used for bundles in MONAI model zoo. tracking: enable the experiment tracking feature at runtime with optionally configurable and extensible. if "mlflow", will add `MLFlowHandler` to the parsed bundle with default logging settings, if other string, treat it as file path to load the logging settings, if `dict`, @@ -718,8 +720,8 @@ def run( if "config_file" not in _args: warnings.warn("`config_file` not provided for 'monai.bundle run'.") _log_input_summary(tag="run", args=_args) - config_file_, meta_file_, runner_id_, logging_file_, tracking_ = _pop_args( - _args, config_file=None, meta_file=None, runner_id="", logging_file=None, tracking=None + meta_file_, logging_file_, config_file_, runner_id_, tracking_ = _pop_args( + _args, "meta_file", "logging_file", config_file=None, runner_id="", tracking=None ) if logging_file_ is not None: if not os.path.exists(logging_file_): From b6e086f8296bb1b0b76f86bb7169eab73d27e11e Mon Sep 17 00:00:00 2001 From: Yiheng Wang Date: Mon, 27 Feb 2023 16:22:57 +0800 Subject: [PATCH 2/3] modify integration test Signed-off-by: Yiheng Wang --- monai/bundle/scripts.py | 17 +++++++++++++---- tests/test_integration_bundle_run.py | 15 ++++++++++++++- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/monai/bundle/scripts.py b/monai/bundle/scripts.py index 66a7e9faf7..8607e30379 100644 --- a/monai/bundle/scripts.py +++ b/monai/bundle/scripts.py @@ -609,9 +609,9 @@ def patch_bundle_tracking(parser: ConfigParser, settings: dict) -> None: def run( runner_id: str | Sequence[str] | None = None, - meta_file: str | Sequence[str] | None = "configs/metadata.json", + meta_file: str | Sequence[str] | None = None, config_file: str | Sequence[str] | None = None, - logging_file: str | None = "configs/logging.conf", + logging_file: str | None = None, tracking: str | dict | None = None, args_file: str | None = None, **override: Any, @@ -719,9 +719,18 @@ def run( ) if "config_file" not in _args: warnings.warn("`config_file` not provided for 'monai.bundle run'.") + if meta_file is None: + _args["meta_file"] = None # None input is ignored in "_update_args" + if logging_file is None: + _args["logging_file"] = None _log_input_summary(tag="run", args=_args) - meta_file_, logging_file_, config_file_, runner_id_, tracking_ = _pop_args( - _args, "meta_file", "logging_file", config_file=None, runner_id="", tracking=None + config_file_, meta_file_, runner_id_, logging_file_, tracking_ = _pop_args( + _args, + config_file=None, + meta_file="configs/metadata.json", + runner_id="", + logging_file="configs/logging.conf", + tracking=None, ) if logging_file_ is not None: if not os.path.exists(logging_file_): diff --git a/tests/test_integration_bundle_run.py b/tests/test_integration_bundle_run.py index e2e313ca91..c9f96d77c6 100644 --- a/tests/test_integration_bundle_run.py +++ b/tests/test_integration_bundle_run.py @@ -60,7 +60,20 @@ def test_tiny(self): }, f, ) - cmd = ["coverage", "run", "-m", "monai.bundle", "run", "training", "--config_file", config_file] + cmd = [ + "coverage", + "run", + "-m", + "monai.bundle", + "run", + "training", + "--config_file", + config_file, + "--meta_file", + "None", + "--logging_file", + "None", + ] command_line_tests(cmd) @parameterized.expand([TEST_CASE_1, TEST_CASE_2]) From 1e7b7e85a56325be60803bddf0911b58426cf02e Mon Sep 17 00:00:00 2001 From: Yiheng Wang Date: Tue, 28 Feb 2023 00:11:54 +0800 Subject: [PATCH 3/3] add warnings Signed-off-by: Yiheng Wang --- monai/bundle/scripts.py | 19 +++++++++++-------- tests/test_integration_bundle_run.py | 15 +-------------- 2 files changed, 12 insertions(+), 22 deletions(-) diff --git a/monai/bundle/scripts.py b/monai/bundle/scripts.py index 8607e30379..3d4b7288d4 100644 --- a/monai/bundle/scripts.py +++ b/monai/bundle/scripts.py @@ -719,10 +719,6 @@ def run( ) if "config_file" not in _args: warnings.warn("`config_file` not provided for 'monai.bundle run'.") - if meta_file is None: - _args["meta_file"] = None # None input is ignored in "_update_args" - if logging_file is None: - _args["logging_file"] = None _log_input_summary(tag="run", args=_args) config_file_, meta_file_, runner_id_, logging_file_, tracking_ = _pop_args( _args, @@ -734,14 +730,21 @@ def run( ) if logging_file_ is not None: if not os.path.exists(logging_file_): - raise FileNotFoundError(f"can't find the logging config file: {logging_file_}.") - logger.info(f"set logging properties based on config: {logging_file_}.") - fileConfig(logging_file_, disable_existing_loggers=False) + if logging_file_ == "configs/logging.conf": + warnings.warn("default logging file in 'configs/logging.conf' not exists, skip logging.") + else: + raise FileNotFoundError(f"can't find the logging config file: {logging_file_}.") + else: + 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_) if meta_file_ is not None: - parser.read_meta(f=meta_file_) + if not os.path.exists(meta_file_): + warnings.warn("default meta file in 'configs/metadata.json' not exists.") + else: + parser.read_meta(f=meta_file_) # the rest key-values in the _args are to override config content parser.update(pairs=_args) diff --git a/tests/test_integration_bundle_run.py b/tests/test_integration_bundle_run.py index c9f96d77c6..e2e313ca91 100644 --- a/tests/test_integration_bundle_run.py +++ b/tests/test_integration_bundle_run.py @@ -60,20 +60,7 @@ def test_tiny(self): }, f, ) - cmd = [ - "coverage", - "run", - "-m", - "monai.bundle", - "run", - "training", - "--config_file", - config_file, - "--meta_file", - "None", - "--logging_file", - "None", - ] + cmd = ["coverage", "run", "-m", "monai.bundle", "run", "training", "--config_file", config_file] command_line_tests(cmd) @parameterized.expand([TEST_CASE_1, TEST_CASE_2])