Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8185 - Refactor test #8231

Open
wants to merge 59 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
075ec11
Eliminates code repetition
garciadias Nov 20, 2024
e8d3e19
Replace relative import with absolute
garciadias Nov 20, 2024
d4b01e6
Directly tests export_ckpt function instead of using command_line_tests
garciadias Nov 21, 2024
bfc68a4
Style fix
garciadias Nov 21, 2024
10eb2af
Merge branch 'dev' into 8185-tests-refactor
garciadias Nov 21, 2024
55fcd22
Revert "Style fix"
garciadias Nov 23, 2024
ba16743
Revert "Replace relative import with absolute"
garciadias Nov 23, 2024
1667eb7
Revert "Directly tests export_ckpt function instead of using command_…
garciadias Nov 23, 2024
702e740
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 24, 2024
9a2c2f4
moves tests to their corresponding folder
garciadias Nov 28, 2024
f2cf158
replace relative imports with absolute import
garciadias Nov 28, 2024
b3fd8e9
Change locating integrations
garciadias Nov 28, 2024
114fc72
Corrects the download path for integration tests
garciadias Nov 28, 2024
626e050
Move array/* and dictionary/* files to parent directory
garciadias Dec 2, 2024
4c6b59c
Move single files on their parent folders
garciadias Dec 2, 2024
050e71b
Merge remote-tracking branch 'upstream/dev' into dev
garciadias Dec 2, 2024
7d68938
Merge branch 'dev' into 8185-tests-refactor
garciadias Dec 2, 2024
3508211
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 2, 2024
2d55074
Recovers deleted file
garciadias Dec 3, 2024
6e9c66e
Merge remote-tracking branch 'upstream/dev' into 8185-tests-refactor
garciadias Dec 3, 2024
fa5c75c
Merge remote-tracking branch 'upstream/dev' into dev
garciadias Dec 4, 2024
dda65c4
Merge branch 'dev' into 8185-tests-refactor
garciadias Dec 4, 2024
bf82225
Start fixing path issues after moving files
garciadias Dec 6, 2024
56c2a48
Keep fixing data paths
garciadias Dec 9, 2024
98d3ab2
rerouting paths
garciadias Dec 11, 2024
6d1af88
Merge remote-tracking branch 'origin/8185-tests-refactor' into 8185-t…
garciadias Dec 11, 2024
a0d2395
Solve many more issues
garciadias Dec 11, 2024
9977e98
Merge remote-tracking branch 'origin/8185-tests-refactor' into 8185-t…
garciadias Dec 11, 2024
8831dc0
reorder imports
garciadias Dec 11, 2024
58c461c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 11, 2024
bb85f53
Fix path issues
garciadias Dec 11, 2024
865dc22
Move utils inside utils to avoid coflict
garciadias Dec 12, 2024
cf1025a
Keep cleaning tests but reverting utils.utils change
Dec 16, 2024
d2e7a2e
Merge remote-tracking branch 'origin/8185-tests-refactor' into 8185-t…
garciadias Jan 8, 2025
5632fdf
Merge remote-tracking branch 'upstream/dev' into 8185-tests-refactor
garciadias Jan 20, 2025
4af739c
rename utils to distinguish from utils folder
garciadias Jan 20, 2025
19b2fdf
Restore deleted testing_data files
garciadias Jan 23, 2025
7a34f01
Corrects testing_data paths on tests
garciadias Jan 23, 2025
0730b56
Merge remote-tracking branch 'upstream/dev' into 8185-tests-refactor
garciadias Jan 23, 2025
069b07c
autofix
garciadias Jan 23, 2025
67c0521
correct reference to util
garciadias Jan 23, 2025
a4a0baa
Fixes more of the util problems
garciadias Jan 23, 2025
34627e8
revert changes unrelated to test restructuring
garciadias Jan 23, 2025
08cd2ac
Correct utils in the reverted files
garciadias Jan 23, 2025
d348aa6
Merge remote-tracking branch 'upstream/dev' into 8185-tests-refactor
garciadias Jan 25, 2025
dc7b39b
Restore deleted testing_data files
garciadias Jan 23, 2025
e789ddd
Corrects testing_data paths on tests
garciadias Jan 23, 2025
1638587
Fix bundle download error from ngc source (#8307)
KumoLiu Jan 21, 2025
74d0481
autofix
garciadias Jan 23, 2025
931f01d
correct reference to util
garciadias Jan 23, 2025
1091266
Fixes more of the util problems
garciadias Jan 23, 2025
58e9e3b
revert changes unrelated to test restructuring
garciadias Jan 23, 2025
f8d9bcf
Correct utils in the reverted files
garciadias Jan 23, 2025
cfd497b
Fix deprecated usage in zarr (#8313)
KumoLiu Jan 24, 2025
f31c919
Merge remote-tracking branch 'origin/8185-tests-refactor' into 8185-t…
garciadias Jan 27, 2025
6c51fce
DCO Remediation Commit for R. Garcia-Dias <[email protected]>
garciadias Jan 27, 2025
b4d6db4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 27, 2025
1d3fb15
DCO Remediation Commit for R. Garcia-Dias <[email protected]>
garciadias Jan 27, 2025
88ec64f
Merge remote-tracking branch 'origin/8185-tests-refactor' into 8185-t…
garciadias Jan 27, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 55 additions & 52 deletions tests/test_bundle_ckpt_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
from parameterized import parameterized

from monai.bundle import ConfigParser
from monai.bundle.scripts import ckpt_export
from monai.data import load_net_with_metadata
from monai.networks import save_state
from tests.utils import command_line_tests, skip_if_windows
from tests.utils import skip_if_windows

TEST_CASE_1 = ["", ""]

Expand All @@ -37,67 +38,69 @@ def setUp(self):
self.device = os.environ.get("CUDA_VISIBLE_DEVICES")
if not self.device:
os.environ["CUDA_VISIBLE_DEVICES"] = "0" # default
self.meta_file = os.path.join(os.path.dirname(__file__), "testing_data", "metadata.json")
self.config_file = os.path.join(os.path.dirname(__file__), "testing_data", "inference.json")
self.tempdir_obj = tempfile.TemporaryDirectory()
tempdir = self.tempdir_obj.name
self.def_args = {"meta_file": "will be replaced by `meta_file` arg"}
self.def_args_file = os.path.join(tempdir, "def_args.yaml")

self.ckpt_file = os.path.join(tempdir, "model.pt")
self.ts_file = os.path.join(tempdir, "model.ts")

self.parser = ConfigParser()
self.parser.export_config_file(config=self.def_args, filepath=self.def_args_file)
self.parser.read_config(self.config_file)
self.net = self.parser.get_parsed_content("network_def")

def tearDown(self):
if self.device is not None:
os.environ["CUDA_VISIBLE_DEVICES"] = self.device
else:
del os.environ["CUDA_VISIBLE_DEVICES"] # previously unset
self.tempdir_obj.cleanup()

@parameterized.expand([TEST_CASE_1, TEST_CASE_2, TEST_CASE_3])
def test_export(self, key_in_ckpt, use_trace):
meta_file = os.path.join(os.path.dirname(__file__), "testing_data", "metadata.json")
config_file = os.path.join(os.path.dirname(__file__), "testing_data", "inference.json")
with tempfile.TemporaryDirectory() as tempdir:
def_args = {"meta_file": "will be replaced by `meta_file` arg"}
def_args_file = os.path.join(tempdir, "def_args.yaml")

ckpt_file = os.path.join(tempdir, "model.pt")
ts_file = os.path.join(tempdir, "model.ts")

parser = ConfigParser()
parser.export_config_file(config=def_args, filepath=def_args_file)
parser.read_config(config_file)
net = parser.get_parsed_content("network_def")
save_state(src=net if key_in_ckpt == "" else {key_in_ckpt: net}, path=ckpt_file)

cmd = ["coverage", "run", "-m", "monai.bundle", "ckpt_export", "network_def", "--filepath", ts_file]
cmd += ["--meta_file", meta_file, "--config_file", f"['{config_file}','{def_args_file}']", "--ckpt_file"]
cmd += [ckpt_file, "--key_in_ckpt", key_in_ckpt, "--args_file", def_args_file]
if use_trace == "True":
cmd += ["--use_trace", use_trace, "--input_shape", "[1, 1, 96, 96, 96]"]
command_line_tests(cmd)
garciadias marked this conversation as resolved.
Show resolved Hide resolved
self.assertTrue(os.path.exists(ts_file))

_, metadata, extra_files = load_net_with_metadata(
ts_file, more_extra_files=["inference.json", "def_args.json"]
)
self.assertIn("schema", metadata)
self.assertIn("meta_file", json.loads(extra_files["def_args.json"]))
self.assertIn("network_def", json.loads(extra_files["inference.json"]))
garciadias marked this conversation as resolved.
Show resolved Hide resolved
def test_ckpt_export_default(self, key_in_ckpt, use_trace):
ckpt_file = os.path.join(self.tempdir_obj.name, "models/model.pt")
ts_file = os.path.join(self.tempdir_obj.name, "models/model.ts")

save_state(src=self.net if key_in_ckpt == "" else {key_in_ckpt: self.net}, path=ckpt_file)
ckpt_export(
net_id="network_def",
filepath=ts_file,
meta_file=self.meta_file,
config_file=self.config_file,
ckpt_file=ckpt_file,
key_in_ckpt=key_in_ckpt,
args_file=self.def_args_file,
use_trace=use_trace,
input_shape=[1, 1, 96, 96, 96] if use_trace == "True" else None,
)
self.assertTrue(os.path.exists(ts_file))

@parameterized.expand([TEST_CASE_1, TEST_CASE_2, TEST_CASE_3])
def test_default_value(self, key_in_ckpt, use_trace):
config_file = os.path.join(os.path.dirname(__file__), "testing_data", "inference.json")
with tempfile.TemporaryDirectory() as tempdir:
def_args = {"meta_file": "will be replaced by `meta_file` arg"}
def_args_file = os.path.join(tempdir, "def_args.yaml")
ckpt_file = os.path.join(tempdir, "models/model.pt")
ts_file = os.path.join(tempdir, "models/model.ts")

parser = ConfigParser()
parser.export_config_file(config=def_args, filepath=def_args_file)
parser.read_config(config_file)
net = parser.get_parsed_content("network_def")
save_state(src=net if key_in_ckpt == "" else {key_in_ckpt: net}, path=ckpt_file)

# check with default value
cmd = ["coverage", "run", "-m", "monai.bundle", "ckpt_export", "--key_in_ckpt", key_in_ckpt]
cmd += ["--config_file", config_file, "--bundle_root", tempdir]
if use_trace == "True":
cmd += ["--use_trace", use_trace, "--input_shape", "[1, 1, 96, 96, 96]"]
command_line_tests(cmd)
self.assertTrue(os.path.exists(ts_file))
def test_ckpt_export(self, key_in_ckpt, use_trace):
save_state(src=self.net if key_in_ckpt == "" else {key_in_ckpt: self.net}, path=self.ckpt_file)
ckpt_export(
net_id="network_def",
filepath=self.ts_file,
meta_file=self.meta_file,
config_file=[self.config_file, self.def_args_file],
ckpt_file=self.ckpt_file,
key_in_ckpt=key_in_ckpt,
args_file=self.def_args_file,
use_trace=use_trace,
input_shape=[1, 1, 96, 96, 96] if use_trace == "True" else None,
)
self.assertTrue(os.path.exists(self.ts_file))

_, metadata, extra_files = load_net_with_metadata(
self.ts_file, more_extra_files=["inference.json", "def_args.json"]
)
self.assertIn("schema", metadata)
self.assertIn("meta_file", json.loads(extra_files["def_args.json"]))
self.assertIn("network_def", json.loads(extra_files["inference.json"]))


if __name__ == "__main__":
Expand Down
3 changes: 1 addition & 2 deletions tests/test_clip_intensity_percentilesd.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
from monai.transforms import ClipIntensityPercentilesd
from monai.transforms.utils_pytorch_numpy_unification import clip, percentile
from monai.utils.type_conversion import convert_to_tensor
from tests.test_clip_intensity_percentiles import test_hard_clip_func, test_soft_clip_func
from tests.utils import TEST_NDARRAYS, NumpyImageTestCase2D, NumpyImageTestCase3D, assert_allclose

from .test_clip_intensity_percentiles import test_hard_clip_func, test_soft_clip_func


class TestClipIntensityPercentilesd2D(NumpyImageTestCase2D):

Expand Down
Loading