diff --git a/monai/bundle/scripts.py b/monai/bundle/scripts.py index 44e92038b6..637fbb12d8 100644 --- a/monai/bundle/scripts.py +++ b/monai/bundle/scripts.py @@ -625,8 +625,11 @@ def run( Args: run_id: ID name of the expected config expression to run, default to "run". + to run the config, the target config must contain this ID. init_id: ID name of the expected config expression to initialize before running, default to "initialize". + it's optional for both configs and this `run` function. final_id: ID name of the expected config expression to finalize after running, default to "finalize". + it's optional for both configs and this `run` function. 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`. diff --git a/monai/bundle/workflows.py b/monai/bundle/workflows.py index 5a5380057e..48e2944798 100644 --- a/monai/bundle/workflows.py +++ b/monai/bundle/workflows.py @@ -148,8 +148,11 @@ class ConfigWorkflow(BundleWorkflow): Args: run_id: ID name of the expected config expression to run, default to "run". + to run the config, the target config must contain this ID. init_id: ID name of the expected config expression to initialize before running, default to "initialize". + allow a config to have no `initialize` logic and the ID. final_id: ID name of the expected config expression to finalize after running, default to "finalize". + allow a config to have no `finalize` logic and the ID. 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 it is a list of file paths, the content of them will be merged. @@ -235,6 +238,8 @@ def run(self) -> Any: Run the bundle workflow, it can be a training, evaluation or inference. """ + if self.run_id not in self.parser: + raise ValueError(f"run ID '{self.run_id}' doesn't exist in the config file.") return self._run_expr(id=self.run_id) def finalize(self) -> Any: diff --git a/tests/test_integration_bundle_run.py b/tests/test_integration_bundle_run.py index 2018ca801e..74ac93bc27 100644 --- a/tests/test_integration_bundle_run.py +++ b/tests/test_integration_bundle_run.py @@ -66,6 +66,9 @@ def test_tiny(self): # test both CLI entry "run" and "run_workflow" command_line_tests(cmd + ["run", "training", "--config_file", config_file]) command_line_tests(cmd + ["run_workflow", "--run_id", "training", "--config_file", config_file]) + with self.assertRaises(RuntimeError): + # test wrong run_id="run" + command_line_tests(cmd + ["run", "run", "--config_file", config_file]) @parameterized.expand([TEST_CASE_1, TEST_CASE_2]) def test_shape(self, config_file, expected_shape):