Skip to content
Merged
29 changes: 0 additions & 29 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,3 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

import sys
import unittest
import warnings


def _enter_pr_4800(self):
"""
code from https://github.com/python/cpython/pull/4800
"""
# The __warningregistry__'s need to be in a pristine state for tests
# to work properly.
for v in list(sys.modules.values()):
if getattr(v, "__warningregistry__", None):
v.__warningregistry__ = {}
self.warnings_manager = warnings.catch_warnings(record=True)
self.warnings = self.warnings_manager.__enter__()
warnings.simplefilter("always", self.expected)
return self


# FIXME: workaround for https://bugs.python.org/issue29620
try:
# Suppression for issue #494: tests/__init__.py:34: error: Cannot assign to a method
unittest.case._AssertWarnsContext.__enter__ = _enter_pr_4800 # type: ignore
except AttributeError:
pass
3 changes: 1 addition & 2 deletions tests/apps/detection/networks/test_retinanet.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from monai.networks import eval_mode
from monai.networks.nets import resnet10, resnet18, resnet34, resnet50, resnet101, resnet152, resnet200
from monai.utils import ensure_tuple, optional_import
from tests.test_utils import SkipIfBeforePyTorchVersion, dict_product, skip_if_quick, test_onnx_save, test_script_save
from tests.test_utils import dict_product, skip_if_quick, test_onnx_save, test_script_save

_, has_torchvision = optional_import("torchvision")

Expand Down Expand Up @@ -94,7 +94,6 @@
TEST_CASES_TS = [[params["model"], *params["case"]] for params in dict_product(model=MODEL_LIST, case=[TEST_CASE_1])]


@SkipIfBeforePyTorchVersion((1, 12))
@unittest.skipUnless(has_torchvision, "Requires torchvision")
@skip_if_quick
class TestRetinaNet(unittest.TestCase):
Expand Down
3 changes: 1 addition & 2 deletions tests/apps/detection/networks/test_retinanet_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from monai.apps.detection.utils.anchor_utils import AnchorGeneratorWithAnchorShape
from monai.networks import eval_mode, train_mode
from monai.utils import optional_import
from tests.test_utils import SkipIfBeforePyTorchVersion, skip_if_quick, test_script_save
from tests.test_utils import skip_if_quick, test_script_save

_, has_torchvision = optional_import("torchvision")

Expand Down Expand Up @@ -110,7 +110,6 @@ def forward(self, images):
return {self.cls_key: [torch.randn(out_cls_shape)], self.box_reg_key: [torch.randn(out_box_reg_shape)]}


@SkipIfBeforePyTorchVersion((1, 11))
@unittest.skipUnless(has_torchvision, "Requires torchvision")
@skip_if_quick
class TestRetinaNetDetector(unittest.TestCase):
Expand Down
3 changes: 1 addition & 2 deletions tests/apps/detection/utils/test_anchor_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from monai.apps.detection.utils.anchor_utils import AnchorGenerator, AnchorGeneratorWithAnchorShape
from monai.utils import optional_import
from tests.test_utils import SkipIfBeforePyTorchVersion, assert_allclose, test_script_save
from tests.test_utils import assert_allclose, test_script_save

_, has_torchvision = optional_import("torchvision")

Expand All @@ -39,7 +39,6 @@
]


@SkipIfBeforePyTorchVersion((1, 11))
@unittest.skipUnless(has_torchvision, "Requires torchvision")
class TestAnchorGenerator(unittest.TestCase):
@parameterized.expand(TEST_CASES_2D)
Expand Down
6 changes: 0 additions & 6 deletions tests/apps/maisi/networks/test_autoencoderkl_maisi.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from monai.apps.generation.maisi.networks.autoencoderkl_maisi import AutoencoderKlMaisi
from monai.networks import eval_mode
from monai.utils import optional_import
from tests.test_utils import SkipIfBeforePyTorchVersion

tqdm, has_tqdm = optional_import("tqdm", name="tqdm")
_, has_einops = optional_import("einops")
Expand Down Expand Up @@ -87,7 +86,6 @@ def test_shape(self, input_param, input_shape, expected_shape, expected_latent_s
self.assertEqual(result[2].shape, expected_latent_shape)

@parameterized.expand(CASES)
@SkipIfBeforePyTorchVersion((1, 11))
def test_shape_with_convtranspose_and_checkpointing(
self, input_param, input_shape, expected_shape, expected_latent_shape
):
Expand Down Expand Up @@ -152,7 +150,6 @@ def test_shape_reconstruction(self):
result = net.reconstruct(torch.randn(input_shape).to(device))
self.assertEqual(result.shape, expected_shape)

@SkipIfBeforePyTorchVersion((1, 11))
def test_shape_reconstruction_with_convtranspose_and_checkpointing(self):
input_param, input_shape, expected_shape, _ = CASES[0]
input_param = input_param.copy()
Expand All @@ -170,7 +167,6 @@ def test_shape_encode(self):
self.assertEqual(result[0].shape, expected_latent_shape)
self.assertEqual(result[1].shape, expected_latent_shape)

@SkipIfBeforePyTorchVersion((1, 11))
def test_shape_encode_with_convtranspose_and_checkpointing(self):
input_param, input_shape, _, expected_latent_shape = CASES[0]
input_param = input_param.copy()
Expand All @@ -190,7 +186,6 @@ def test_shape_sampling(self):
)
self.assertEqual(result.shape, expected_latent_shape)

@SkipIfBeforePyTorchVersion((1, 11))
def test_shape_sampling_convtranspose_and_checkpointing(self):
input_param, _, _, expected_latent_shape = CASES[0]
input_param = input_param.copy()
Expand All @@ -209,7 +204,6 @@ def test_shape_decode(self):
result = net.decode(torch.randn(latent_shape).to(device))
self.assertEqual(result.shape, expected_input_shape)

@SkipIfBeforePyTorchVersion((1, 11))
def test_shape_decode_convtranspose_and_checkpointing(self):
input_param, expected_input_shape, _, latent_shape = CASES[0]
input_param = input_param.copy()
Expand Down
2 changes: 0 additions & 2 deletions tests/apps/maisi/networks/test_controlnet_maisi.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from monai.apps.generation.maisi.networks.controlnet_maisi import ControlNetMaisi
from monai.networks import eval_mode
from monai.utils import optional_import
from tests.test_utils import SkipIfBeforePyTorchVersion

_, has_einops = optional_import("einops")

Expand Down Expand Up @@ -127,7 +126,6 @@
]


@SkipIfBeforePyTorchVersion((2, 0))
class TestControlNet(unittest.TestCase):
@parameterized.expand(TEST_CASES)
@skipUnless(has_einops, "Requires einops")
Expand Down
File renamed without changes.
9 changes: 1 addition & 8 deletions tests/apps/test_auto3dseg_bundlegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,7 @@
from monai.bundle.config_parser import ConfigParser
from monai.data import create_test_image_3d
from monai.utils import set_determinism
from tests.test_utils import (
SkipIfBeforePyTorchVersion,
get_testing_algo_template_path,
skip_if_downloading_fails,
skip_if_no_cuda,
skip_if_quick,
)
from tests.test_utils import get_testing_algo_template_path, skip_if_downloading_fails, skip_if_no_cuda, skip_if_quick

num_images_perfold = max(torch.cuda.device_count(), 4)
num_images_per_batch = 2
Expand Down Expand Up @@ -104,7 +98,6 @@ def run_auto3dseg_before_bundlegen(test_path, work_dir):


@skip_if_no_cuda
@SkipIfBeforePyTorchVersion((1, 11, 1))
@skip_if_quick
class TestBundleGen(unittest.TestCase):
def setUp(self) -> None:
Expand Down
3 changes: 1 addition & 2 deletions tests/apps/vista3d/test_point_based_window_inferer.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from monai.networks import eval_mode
from monai.networks.nets.vista3d import vista3d132
from monai.utils import optional_import
from tests.test_utils import SkipIfBeforePyTorchVersion, skip_if_quick
from tests.test_utils import skip_if_quick

device = "cuda" if torch.cuda.is_available() else "cpu"

Expand Down Expand Up @@ -60,7 +60,6 @@
]


@SkipIfBeforePyTorchVersion((1, 11))
@skip_if_quick
class TestPointBasedWindowInferer(unittest.TestCase):
@parameterized.expand(TEST_CASES)
Expand Down
70 changes: 68 additions & 2 deletions tests/bundle/test_bundle_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
from monai.bundle.scripts import _examine_monai_version, _list_latest_versions, download
from monai.utils import optional_import
from tests.test_utils import (
SkipIfBeforePyTorchVersion,
assert_allclose,
command_line_tests,
skip_if_downloading_fails,
skip_if_no_cuda,
skip_if_quick,
skip_if_windows,
)

_, has_huggingface_hub = optional_import("huggingface_hub")
Expand Down Expand Up @@ -95,6 +95,47 @@
{"model.pt": "27952767e2e154e3b0ee65defc5aed38", "model.ts": "97746870fe591f69ac09827175b00675"},
]

TEST_CASE_NGC_1 = [
"spleen_ct_segmentation",
"0.3.7",
None,
"monai_spleen_ct_segmentation",
"models/model.pt",
"b418a2dc8672ce2fd98dc255036e7a3d",
]
TEST_CASE_NGC_2 = [
"monai_spleen_ct_segmentation",
"0.3.7",
"monai_",
"spleen_ct_segmentation",
"models/model.pt",
"b418a2dc8672ce2fd98dc255036e7a3d",
]

TESTCASE_NGC_WEIGHTS = {
"key": "model.0.conv.unit0.adn.N.bias",
"value": torch.tensor(
[
-0.0705,
-0.0937,
-0.0422,
-0.2068,
0.1023,
-0.2007,
-0.0883,
0.0018,
-0.1719,
0.0116,
0.0285,
-0.0044,
0.1223,
-0.1287,
-0.1858,
0.0460,
]
),
}


class TestDownload(unittest.TestCase):
@parameterized.expand([TEST_CASE_1, TEST_CASE_2])
Expand Down Expand Up @@ -356,7 +397,6 @@ def test_load_weights_with_net_override(self, bundle_name, device, net_override)

@parameterized.expand([TEST_CASE_9])
@skip_if_quick
@SkipIfBeforePyTorchVersion((1, 7, 1))
def test_load_ts_module(self, bundle_files, bundle_name, version, repo, device, model_file):
with skip_if_downloading_fails():
# load ts module
Expand Down Expand Up @@ -419,5 +459,31 @@ def test_url_download_large_files(self, bundle_files, bundle_name, url, hash_val
self.assertTrue(check_hash(filepath=file_path, val=hash_val[file]))


@skip_if_windows
class TestNgcBundleDownload(unittest.TestCase):
@parameterized.expand([TEST_CASE_NGC_1, TEST_CASE_NGC_2])
@skip_if_quick
def test_ngc_download_bundle(self, bundle_name, version, remove_prefix, download_name, file_path, hash_val):
with skip_if_downloading_fails():
with tempfile.TemporaryDirectory() as tempdir:
download(
name=bundle_name, source="ngc", version=version, bundle_dir=tempdir, remove_prefix=remove_prefix
)
full_file_path = os.path.join(tempdir, download_name, file_path)
self.assertTrue(os.path.exists(full_file_path))
self.assertTrue(check_hash(filepath=full_file_path, val=hash_val))

model = load(
name=bundle_name, source="ngc", version=version, bundle_dir=tempdir, remove_prefix=remove_prefix
)
assert_allclose(
model.state_dict()[TESTCASE_NGC_WEIGHTS["key"]],
TESTCASE_NGC_WEIGHTS["value"],
atol=1e-4,
rtol=1e-4,
type_test=False,
)


if __name__ == "__main__":
unittest.main()
File renamed without changes.
5 changes: 1 addition & 4 deletions tests/data/meta_tensor/test_meta_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from monai.data.utils import decollate_batch, list_data_collate
from monai.transforms import BorderPadd, Compose, DivisiblePadd, FromMetaTensord, ToMetaTensord
from monai.utils.enums import PostFix
from tests.test_utils import TEST_DEVICES, SkipIfBeforePyTorchVersion, assert_allclose, dict_product, skip_if_no_cuda
from tests.test_utils import TEST_DEVICES, assert_allclose, dict_product, skip_if_no_cuda

DTYPES = [[torch.float32], [torch.float64], [torch.float16], [torch.int64], [torch.int32], [None]]

Expand Down Expand Up @@ -297,7 +297,6 @@ def test_dataset(self, device, dtype):
self.check(im, ims[i], ids=True)

@parameterized.expand(DTYPES)
@SkipIfBeforePyTorchVersion((1, 8))
def test_dataloader(self, dtype):
batch_size = 5
ims = [self.get_im(dtype=dtype)[0] for _ in range(batch_size * 2)]
Expand All @@ -314,7 +313,6 @@ def test_dataloader(self, dtype):
self.assertTupleEqual(tuple(batch.affine.shape), expected_affine_shape)
self.assertEqual(len(batch.applied_operations), batch_size)

@SkipIfBeforePyTorchVersion((1, 9))
def test_indexing(self):
"""
Check the metadata is returned in the expected format depending on whether
Expand Down Expand Up @@ -406,7 +404,6 @@ def test_slicing(self):
self.assertEqual(x[[True, False, True]].shape, (2, 3, 4))

@parameterized.expand(DTYPES)
@SkipIfBeforePyTorchVersion((1, 8))
def test_decollate(self, dtype):
batch_size = 3
ims = [self.get_im(dtype=dtype)[0] for _ in range(batch_size * 2)]
Expand Down
2 changes: 0 additions & 2 deletions tests/data/test_shuffle_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@

from monai.data import DataLoader, ShuffleBuffer
from monai.utils import convert_data_type
from tests.test_utils import SkipIfBeforePyTorchVersion


@SkipIfBeforePyTorchVersion((1, 12))
class TestShuffleBuffer(unittest.TestCase):
def test_shape(self):
buffer = ShuffleBuffer([1, 2, 3, 4], seed=0)
Expand Down
3 changes: 1 addition & 2 deletions tests/fl/monai_algo/test_fl_monai_algo_dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from monai.fl.utils.constants import ExtraItems
from monai.fl.utils.exchange_object import ExchangeObject
from monai.networks import get_state_dict
from tests.test_utils import DistCall, DistTestCase, SkipIfBeforePyTorchVersion, SkipIfNoModule, skip_if_no_cuda
from tests.test_utils import DistCall, DistTestCase, SkipIfNoModule, skip_if_no_cuda

TESTS_PATH = TESTS_PATH = Path(__file__).parents[2].as_posix()
_root_dir = os.path.abspath(pathjoin(TESTS_PATH))
Expand All @@ -32,7 +32,6 @@


@SkipIfNoModule("ignite")
@SkipIfBeforePyTorchVersion((1, 11, 1))
class TestFLMonaiAlgo(DistTestCase):
@DistCall(nnodes=1, nproc_per_node=2, init_method="no_init")
@skip_if_no_cuda
Expand Down
2 changes: 0 additions & 2 deletions tests/handlers/test_handler_tb_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@
from monai.data import decollate_batch
from monai.handlers import TensorBoardImageHandler
from monai.utils import optional_import
from tests.test_utils import SkipIfBeforePyTorchVersion

_, has_tb = optional_import("torch.utils.tensorboard", name="SummaryWriter")

TEST_CASES = [[[20, 20]], [[2, 20, 20]], [[3, 20, 20]], [[20, 20, 20]], [[2, 20, 20, 20]], [[2, 2, 20, 20, 20]]]


@unittest.skipUnless(has_tb, "Requires SummaryWriter installation")
@SkipIfBeforePyTorchVersion((1, 13)) # issue 6683
class TestHandlerTBImage(unittest.TestCase):
@parameterized.expand(TEST_CASES)
def test_tb_image_shape(self, shape):
Expand Down
Loading
Loading