-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add bundle root directory to Python search directories automatically (#…
…6910) Fixes #6722 . ### Description Add scripts directory to Python search directories automatically in the `run` function in `ConfigWorkflow`. ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [ ] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [ ] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. --------- Signed-off-by: KumoLiu <[email protected]>
- Loading branch information
Showing
2 changed files
with
87 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
import json | ||
import os | ||
import shutil | ||
import subprocess | ||
import sys | ||
import tempfile | ||
import unittest | ||
|
@@ -44,6 +45,14 @@ def run(self): | |
return self.val | ||
|
||
|
||
class _Runnable43: | ||
def __init__(self, func): | ||
self.func = func | ||
|
||
def run(self): | ||
self.func() | ||
|
||
|
||
class TestBundleRun(unittest.TestCase): | ||
def setUp(self): | ||
self.data_dir = tempfile.mkdtemp() | ||
|
@@ -77,6 +86,69 @@ def test_tiny(self): | |
with self.assertRaises(RuntimeError): | ||
# test wrong run_id="run" | ||
command_line_tests(cmd + ["run", "run", "--config_file", config_file]) | ||
with self.assertRaises(RuntimeError): | ||
# test missing meta file | ||
command_line_tests(cmd + ["run", "training", "--config_file", config_file]) | ||
|
||
def test_scripts_fold(self): | ||
# test scripts directory has been added to Python search directories automatically | ||
config_file = os.path.join(self.data_dir, "tiny_config.json") | ||
meta_file = os.path.join(self.data_dir, "tiny_meta.json") | ||
scripts_dir = os.path.join(self.data_dir, "scripts") | ||
script_file = os.path.join(scripts_dir, "test_scripts_fold.py") | ||
init_file = os.path.join(scripts_dir, "__init__.py") | ||
|
||
with open(config_file, "w") as f: | ||
json.dump( | ||
{ | ||
"imports": ["$import scripts"], | ||
"trainer": { | ||
"_target_": "tests.test_integration_bundle_run._Runnable43", | ||
"func": "$scripts.tiny_test", | ||
}, | ||
# keep this test case to cover the "runner_id" arg | ||
"training": "[email protected]()", | ||
}, | ||
f, | ||
) | ||
with open(meta_file, "w") as f: | ||
json.dump( | ||
{"version": "0.1.0", "monai_version": "1.1.0", "pytorch_version": "1.13.1", "numpy_version": "1.22.2"}, | ||
f, | ||
) | ||
|
||
os.mkdir(scripts_dir) | ||
script_file_lines = ["def tiny_test():\n", " print('successfully added scripts fold!') \n"] | ||
init_file_line = "from .test_scripts_fold import tiny_test\n" | ||
with open(script_file, "w") as f: | ||
f.writelines(script_file_lines) | ||
f.close() | ||
with open(init_file, "w") as f: | ||
f.write(init_file_line) | ||
f.close() | ||
|
||
cmd = ["coverage", "run", "-m", "monai.bundle"] | ||
# test both CLI entry "run" and "run_workflow" | ||
expected_condition = "successfully added scripts fold!" | ||
command_run = cmd + ["run", "training", "--config_file", config_file, "--meta_file", meta_file] | ||
completed_process = subprocess.run(command_run, check=True, capture_output=True, text=True) | ||
output = repr(completed_process.stdout).replace("\\n", "\n").replace("\\t", "\t") # Get the captured output | ||
print(output) | ||
|
||
self.assertTrue(expected_condition in output) | ||
command_run_workflow = cmd + [ | ||
"run_workflow", | ||
"--run_id", | ||
"training", | ||
"--config_file", | ||
config_file, | ||
"--meta_file", | ||
meta_file, | ||
] | ||
completed_process = subprocess.run(command_run_workflow, check=True, capture_output=True, text=True) | ||
output = repr(completed_process.stdout).replace("\\n", "\n").replace("\\t", "\t") # Get the captured output | ||
print(output) | ||
self.assertTrue(expected_condition in output) | ||
|
||
with self.assertRaises(RuntimeError): | ||
# test missing meta file | ||
|