Skip to content

Commit

Permalink
[DLMED] update according to comments
Browse files Browse the repository at this point in the history
Signed-off-by: Nic Ma <nma@nvidia.com>
  • Loading branch information
Nic-Ma committed Mar 31, 2023
1 parent 9645194 commit 6a33669
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 27 deletions.
1 change: 1 addition & 0 deletions monai/bundle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
init_bundle,
load,
run,
run_workflow,
trt_export,
verify_metadata,
verify_net_in_out,
Expand Down
11 changes: 10 additions & 1 deletion monai/bundle/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@

from __future__ import annotations

from monai.bundle.scripts import ckpt_export, download, init_bundle, run, trt_export, verify_metadata, verify_net_in_out
from monai.bundle.scripts import (
ckpt_export,
download,
init_bundle,
run,
run_workflow,
trt_export,
verify_metadata,
verify_net_in_out,
)

if __name__ == "__main__":
from monai.utils import optional_import
Expand Down
29 changes: 7 additions & 22 deletions monai/bundle/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ def run(
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.
e.g. ``--net#input_chns 42``.
e.g. ``--net#input_chns 42``, ``--net %/data/other.json#net_arg``.
"""

Expand Down Expand Up @@ -664,7 +664,8 @@ def run(
logging_file="configs/logging.conf",
tracking=None,
)
workflow = ConfigWorkflow(
run_workflow(
workflow=ConfigWorkflow,
config_file=config_file_,
meta_file=meta_file_,
logging_file=logging_file_,
Expand All @@ -674,9 +675,6 @@ def run(
tracking=tracking_,
**_args,
)
workflow.initialize()
workflow.run()
workflow.finalize()


def run_workflow(workflow: str | BundleWorkflow | None = None, args_file: str | None = None, **kwargs: Any) -> None:
Expand All @@ -688,23 +686,10 @@ def run_workflow(workflow: str | BundleWorkflow | None = None, args_file: str |
.. code-block:: bash
# Execute this module as a CLI entry with default ConfigWorkflow:
python -m monai.bundle run --meta_file <meta path> --config_file <config path>
# Override config values at runtime by specifying the component id and its new value:
python -m monai.bundle run --net#input_chns 1 ...
# Override config values with another config file `/path/to/another.json`:
python -m monai.bundle run --net %/path/to/another.json ...
# Override config values with part content of another config file:
python -m monai.bundle run --net %/data/other.json#net_arg ...
# Set default args of `run` in a JSON / YAML file, help to record and simplify the command line.
# Other args still can override the default args at runtime:
python -m monai.bundle run --args_file "/workspace/data/args.json" --config_file <config path>
python -m monai.bundle run_workflow --meta_file <meta path> --config_file <config path>
# Set the workflow to other customized BundleWorkflow subclass:
python -m monai.bundle run --workflow CustomizedWorkflow ...
python -m monai.bundle run_workflow --workflow CustomizedWorkflow ...
Args:
workflow: specified bundle workflow name, should be a string or class, default to "ConfigWorkflow".
Expand All @@ -722,11 +707,11 @@ def run_workflow(workflow: str | BundleWorkflow | None = None, args_file: str |
if not has_built_in:
workflow_class = locate(f"{workflow_name}") # search dotted path
if workflow_class is None:
raise ValueError(f"can not locate specified workflow class: {workflow_name}.")
raise ValueError(f"cannot locate specified workflow class: {workflow_name}.")
elif issubclass(workflow_name, BundleWorkflow):
workflow_class = workflow_name
else:
raise ValueError(f"`workflow` must be the bundle workflow class name, but got: {workflow_name}.")
raise ValueError(f"argument `workflow` must be the bundle workflow class name or type, got: {workflow_name}.")

workflow_ = workflow_class(**_args)
workflow_.initialize()
Expand Down
9 changes: 5 additions & 4 deletions tests/test_integration_bundle_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ def test_tiny(self):
},
f,
)
cmd = ["coverage", "run", "-m", "monai.bundle", "run", "--run_id", "training", "--config_file", config_file]
command_line_tests(cmd)
cmd = ["coverage", "run", "-m", "monai.bundle"]
command_line_tests(cmd + ["run", "training", "--config_file", config_file])
command_line_tests(cmd + ["run_workflow", "--run_id", "training", "--config_file", config_file])

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

def test_customize_workflow(self):
def test_customized_workflow(self):
expected_shape = (64, 64, 64)
test_image = np.random.rand(*expected_shape)
filename = os.path.join(self.data_dir, "image.nii")
nib.save(nib.Nifti1Image(test_image, np.eye(4)), filename)

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

0 comments on commit 6a33669

Please sign in to comment.