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

Add more unit tests #455

Merged
merged 19 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 8 additions & 1 deletion ci/unit_tests/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from __future__ import annotations

import argparse
import importlib
import inspect
import os
import sys
Expand Down Expand Up @@ -107,7 +108,13 @@ def get_default_pattern(loader):
test_file_name = f"test_{args.bundle}_dist" if args.dist is True else f"test_{args.bundle}"
test_file = os.path.join(os.path.dirname(__file__), f"{test_file_name}.py")
if os.path.exists(test_file):
tests = unittest.TestLoader().loadTestsFromNames([test_file_name])
loader = unittest.TestLoader()
# if having the "test_order" function, will use it as the load order
sys.path.append(os.path.dirname(__file__))
module = importlib.import_module(test_file_name)
if hasattr(module, "test_order"):
loader.sortTestMethodsUsing = module.test_order
tests = loader.loadTestsFromNames([test_file_name])
test_runner = unittest.runner.TextTestRunner(
resultclass=TimeLoggingTestResult, verbosity=args.verbosity, failfast=args.failfast
)
Expand Down
22 changes: 5 additions & 17 deletions ci/unit_tests/test_brats_mri_axial_slices_generative_diffusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import numpy as np
from monai.bundle import ConfigWorkflow
from parameterized import parameterized
from utils import check_workflow

TEST_CASE_1 = [
{
Expand Down Expand Up @@ -98,9 +99,7 @@ def test_autoencoder_train(self, override):
meta_file=os.path.join(bundle_root, "configs/metadata.json"),
**override,
)
trainer.initialize()
trainer.run()
trainer.finalize()
check_workflow(trainer, check_properties=False)

@parameterized.expand([TEST_CASE_2])
def test_autoencoder_infer(self, override):
Expand All @@ -115,9 +114,7 @@ def test_autoencoder_infer(self, override):
meta_file=os.path.join(bundle_root, "configs/metadata.json"),
**override,
)
inferrer.initialize()
inferrer.run()
inferrer.finalize()
check_workflow(inferrer, check_properties=False)

@parameterized.expand([TEST_CASE_1])
def test_diffusion_train(self, override):
Expand All @@ -134,14 +131,7 @@ def test_diffusion_train(self, override):
meta_file=os.path.join(bundle_root, "configs/metadata.json"),
**override,
)
trainer.initialize()
# TODO: uncomment the following check after we have monai > 1.2.0
# https://github.com/Project-MONAI/MONAI/issues/6602
# check_result = trainer.check_properties()
# if check_result is not None and len(check_result) > 0:
# raise ValueError(f"check properties for overrided train config failed: {check_result}")
trainer.run()
trainer.finalize()
check_workflow(trainer, check_properties=False)

@parameterized.expand([TEST_CASE_2])
def test_diffusion_infer(self, override):
Expand All @@ -156,9 +146,7 @@ def test_diffusion_infer(self, override):
meta_file=os.path.join(bundle_root, "configs/metadata.json"),
**override,
)
inferrer.initialize()
inferrer.run()
inferrer.finalize()
check_workflow(inferrer, check_properties=False)


if __name__ == "__main__":
Expand Down
22 changes: 5 additions & 17 deletions ci/unit_tests/test_brats_mri_generative_diffusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import numpy as np
from monai.bundle import ConfigWorkflow
from parameterized import parameterized
from utils import check_workflow

TEST_CASE_1 = [
{
Expand Down Expand Up @@ -112,9 +113,7 @@ def test_autoencoder_train(self, override):
meta_file=os.path.join(bundle_root, "configs/metadata.json"),
**override,
)
trainer.initialize()
trainer.run()
trainer.finalize()
check_workflow(trainer, check_properties=False)

@parameterized.expand([TEST_CASE_3])
def test_autoencoder_infer(self, override):
Expand All @@ -129,9 +128,7 @@ def test_autoencoder_infer(self, override):
meta_file=os.path.join(bundle_root, "configs/metadata.json"),
**override,
)
inferrer.initialize()
inferrer.run()
inferrer.finalize()
check_workflow(inferrer, check_properties=False)

@parameterized.expand([TEST_CASE_2])
def test_diffusion_train(self, override):
Expand All @@ -148,14 +145,7 @@ def test_diffusion_train(self, override):
meta_file=os.path.join(bundle_root, "configs/metadata.json"),
**override,
)
trainer.initialize()
# TODO: uncomment the following check after we have monai > 1.2.0
KumoLiu marked this conversation as resolved.
Show resolved Hide resolved
# https://github.com/Project-MONAI/MONAI/issues/6602
# check_result = trainer.check_properties()
# if check_result is not None and len(check_result) > 0:
# raise ValueError(f"check properties for overrided train config failed: {check_result}")
trainer.run()
trainer.finalize()
check_workflow(trainer, check_properties=False)

@parameterized.expand([TEST_CASE_3])
def test_diffusion_infer(self, override):
Expand All @@ -170,9 +160,7 @@ def test_diffusion_infer(self, override):
meta_file=os.path.join(bundle_root, "configs/metadata.json"),
**override,
)
inferrer.initialize()
inferrer.run()
inferrer.finalize()
check_workflow(inferrer, check_properties=False)


if __name__ == "__main__":
Expand Down
24 changes: 4 additions & 20 deletions ci/unit_tests/test_brats_mri_segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import numpy as np
from monai.bundle import ConfigWorkflow
from parameterized import parameterized
from utils import check_workflow

TEST_CASE_1 = [ # train, evaluate
{
Expand Down Expand Up @@ -75,13 +76,7 @@ def test_train_eval_config(self, override):
meta_file=os.path.join(bundle_root, "configs/metadata.json"),
**override,
)
trainer.initialize()
# check required and optional properties
check_result = trainer.check_properties()
if check_result is not None and len(check_result) > 0:
raise ValueError(f"check properties for train config failed: {check_result}")
trainer.run()
trainer.finalize()
check_workflow(trainer, check_properties=True)

validator = ConfigWorkflow(
# override train.json, thus set the workflow to "train" rather than "eval"
Expand All @@ -91,12 +86,7 @@ def test_train_eval_config(self, override):
meta_file=os.path.join(bundle_root, "configs/metadata.json"),
**override,
)
validator.initialize()
check_result = validator.check_properties()
if check_result is not None and len(check_result) > 0:
raise ValueError(f"check properties for overrided train config failed: {check_result}")
validator.run()
validator.finalize()
check_workflow(validator, check_properties=True)

@parameterized.expand([TEST_CASE_2])
def test_infer_config(self, override):
Expand All @@ -110,13 +100,7 @@ def test_infer_config(self, override):
meta_file=os.path.join(bundle_root, "configs/metadata.json"),
**override,
)
inferrer.initialize()
# check required and optional properties
check_result = inferrer.check_properties()
if check_result is not None and len(check_result) > 0:
raise ValueError(f"check properties for inference config failed: {check_result}")
inferrer.run()
inferrer.finalize()
check_workflow(inferrer, check_properties=True)


if __name__ == "__main__":
Expand Down
24 changes: 4 additions & 20 deletions ci/unit_tests/test_endoscopic_inbody_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from monai.bundle import ConfigWorkflow
from monai.data import PILWriter
from parameterized import parameterized
from utils import check_workflow

TEST_CASE_1 = [ # train, evaluate
{
Expand Down Expand Up @@ -77,13 +78,7 @@ def test_train_eval_config(self, override):
meta_file=os.path.join(bundle_root, "configs/metadata.json"),
**override,
)
trainer.initialize()
# check required and optional properties
check_result = trainer.check_properties()
if check_result is not None and len(check_result) > 0:
raise ValueError(f"check properties for train config failed: {check_result}")
trainer.run()
trainer.finalize()
check_workflow(trainer, check_properties=True)

validator = ConfigWorkflow(
# override train.json, thus set the workflow to "train" rather than "eval"
Expand All @@ -93,12 +88,7 @@ def test_train_eval_config(self, override):
meta_file=os.path.join(bundle_root, "configs/metadata.json"),
**override,
)
validator.initialize()
check_result = validator.check_properties()
if check_result is not None and len(check_result) > 0:
raise ValueError(f"check properties for overrided train config failed: {check_result}")
validator.run()
validator.finalize()
check_workflow(validator, check_properties=True)

@parameterized.expand([TEST_CASE_2])
def test_infer_config(self, override):
Expand All @@ -112,13 +102,7 @@ def test_infer_config(self, override):
meta_file=os.path.join(bundle_root, "configs/metadata.json"),
**override,
)
inferrer.initialize()
# check required and optional properties
check_result = inferrer.check_properties()
if check_result is not None and len(check_result) > 0:
raise ValueError(f"check properties for inference config failed: {check_result}")
inferrer.run()
inferrer.finalize()
check_workflow(inferrer, check_properties=True)


if __name__ == "__main__":
Expand Down
Loading