Skip to content

Commit 6a33669

Browse files
committed
[DLMED] update according to comments
Signed-off-by: Nic Ma <nma@nvidia.com>
1 parent 9645194 commit 6a33669

File tree

4 files changed

+23
-27
lines changed

4 files changed

+23
-27
lines changed

monai/bundle/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
init_bundle,
2525
load,
2626
run,
27+
run_workflow,
2728
trt_export,
2829
verify_metadata,
2930
verify_net_in_out,

monai/bundle/__main__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,16 @@
1111

1212
from __future__ import annotations
1313

14-
from monai.bundle.scripts import ckpt_export, download, init_bundle, run, trt_export, verify_metadata, verify_net_in_out
14+
from monai.bundle.scripts import (
15+
ckpt_export,
16+
download,
17+
init_bundle,
18+
run,
19+
run_workflow,
20+
trt_export,
21+
verify_metadata,
22+
verify_net_in_out,
23+
)
1524

1625
if __name__ == "__main__":
1726
from monai.utils import optional_import

monai/bundle/scripts.py

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ def run(
636636
args_file: a JSON or YAML file to provide default values for `runner_id`, `meta_file`,
637637
`config_file`, `logging`, and override pairs. so that the command line inputs can be simplified.
638638
override: id-value pairs to override or add the corresponding config content.
639-
e.g. ``--net#input_chns 42``.
639+
e.g. ``--net#input_chns 42``, ``--net %/data/other.json#net_arg``.
640640
641641
"""
642642

@@ -664,7 +664,8 @@ def run(
664664
logging_file="configs/logging.conf",
665665
tracking=None,
666666
)
667-
workflow = ConfigWorkflow(
667+
run_workflow(
668+
workflow=ConfigWorkflow,
668669
config_file=config_file_,
669670
meta_file=meta_file_,
670671
logging_file=logging_file_,
@@ -674,9 +675,6 @@ def run(
674675
tracking=tracking_,
675676
**_args,
676677
)
677-
workflow.initialize()
678-
workflow.run()
679-
workflow.finalize()
680678

681679

682680
def run_workflow(workflow: str | BundleWorkflow | None = None, args_file: str | None = None, **kwargs: Any) -> None:
@@ -688,23 +686,10 @@ def run_workflow(workflow: str | BundleWorkflow | None = None, args_file: str |
688686
.. code-block:: bash
689687
690688
# Execute this module as a CLI entry with default ConfigWorkflow:
691-
python -m monai.bundle run --meta_file <meta path> --config_file <config path>
692-
693-
# Override config values at runtime by specifying the component id and its new value:
694-
python -m monai.bundle run --net#input_chns 1 ...
695-
696-
# Override config values with another config file `/path/to/another.json`:
697-
python -m monai.bundle run --net %/path/to/another.json ...
698-
699-
# Override config values with part content of another config file:
700-
python -m monai.bundle run --net %/data/other.json#net_arg ...
701-
702-
# Set default args of `run` in a JSON / YAML file, help to record and simplify the command line.
703-
# Other args still can override the default args at runtime:
704-
python -m monai.bundle run --args_file "/workspace/data/args.json" --config_file <config path>
689+
python -m monai.bundle run_workflow --meta_file <meta path> --config_file <config path>
705690
706691
# Set the workflow to other customized BundleWorkflow subclass:
707-
python -m monai.bundle run --workflow CustomizedWorkflow ...
692+
python -m monai.bundle run_workflow --workflow CustomizedWorkflow ...
708693
709694
Args:
710695
workflow: specified bundle workflow name, should be a string or class, default to "ConfigWorkflow".
@@ -722,11 +707,11 @@ def run_workflow(workflow: str | BundleWorkflow | None = None, args_file: str |
722707
if not has_built_in:
723708
workflow_class = locate(f"{workflow_name}") # search dotted path
724709
if workflow_class is None:
725-
raise ValueError(f"can not locate specified workflow class: {workflow_name}.")
710+
raise ValueError(f"cannot locate specified workflow class: {workflow_name}.")
726711
elif issubclass(workflow_name, BundleWorkflow):
727712
workflow_class = workflow_name
728713
else:
729-
raise ValueError(f"`workflow` must be the bundle workflow class name, but got: {workflow_name}.")
714+
raise ValueError(f"argument `workflow` must be the bundle workflow class name or type, got: {workflow_name}.")
730715

731716
workflow_ = workflow_class(**_args)
732717
workflow_.initialize()

tests/test_integration_bundle_run.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ def test_tiny(self):
6262
},
6363
f,
6464
)
65-
cmd = ["coverage", "run", "-m", "monai.bundle", "run", "--run_id", "training", "--config_file", config_file]
66-
command_line_tests(cmd)
65+
cmd = ["coverage", "run", "-m", "monai.bundle"]
66+
command_line_tests(cmd + ["run", "training", "--config_file", config_file])
67+
command_line_tests(cmd + ["run_workflow", "--run_id", "training", "--config_file", config_file])
6768

6869
@parameterized.expand([TEST_CASE_1, TEST_CASE_2])
6970
def test_shape(self, config_file, expected_shape):
@@ -136,13 +137,13 @@ def test_shape(self, config_file, expected_shape):
136137
# test the saved execution configs
137138
self.assertTrue(len(glob(f"{tempdir}/config_*.json")), 2)
138139

139-
def test_customize_workflow(self):
140+
def test_customized_workflow(self):
140141
expected_shape = (64, 64, 64)
141142
test_image = np.random.rand(*expected_shape)
142143
filename = os.path.join(self.data_dir, "image.nii")
143144
nib.save(nib.Nifti1Image(test_image, np.eye(4)), filename)
144145

145-
cmd = "-m fire monai.bundle.scripts run --workflow tests.nonconfig_workflow.NonConfigWorkflow"
146+
cmd = "-m fire monai.bundle.scripts run_workflow --workflow tests.nonconfig_workflow.NonConfigWorkflow"
146147
cmd += f" --filename {filename} --output_dir {self.data_dir}"
147148
command_line_tests(["coverage", "run"] + cmd.split(" "))
148149
loader = LoadImage(image_only=True)

0 commit comments

Comments
 (0)