From aaf6840abcf1c4882b03a35d147ecc53dedc904f Mon Sep 17 00:00:00 2001 From: Richard Brown <33289025+rijobro@users.noreply.github.com> Date: Thu, 14 Apr 2022 16:34:04 +0100 Subject: [PATCH 1/3] no skip if before pytorch 1.7 Signed-off-by: Richard Brown <33289025+rijobro@users.noreply.github.com> --- tests/test_cachedataset_persistent_workers.py | 2 -- tests/test_detect_envelope.py | 13 ++----------- tests/test_dice_ce_loss.py | 3 +-- tests/test_dice_focal_loss.py | 3 +-- tests/test_dice_loss.py | 3 +-- tests/test_focal_loss.py | 3 +-- tests/test_gaussian_filter.py | 3 +-- tests/test_generalized_dice_loss.py | 3 +-- tests/test_generalized_wasserstein_dice_loss.py | 3 +-- tests/test_handler_smartcache.py | 2 -- tests/test_hilbert_transform.py | 12 +----------- tests/test_integration_fast_train.py | 3 +-- tests/test_keep_largest_connected_component.py | 3 +-- tests/test_masked_loss.py | 3 +-- tests/test_multi_scale.py | 3 +-- tests/test_randtorchvisiond.py | 2 -- tests/test_torchvision.py | 3 +-- tests/test_torchvisiond.py | 2 -- tests/test_tversky_loss.py | 3 +-- tests/test_utils_pytorch_numpy_unification.py | 3 +-- 20 files changed, 17 insertions(+), 58 deletions(-) diff --git a/tests/test_cachedataset_persistent_workers.py b/tests/test_cachedataset_persistent_workers.py index 4bea0486bc..8cef298be7 100644 --- a/tests/test_cachedataset_persistent_workers.py +++ b/tests/test_cachedataset_persistent_workers.py @@ -13,10 +13,8 @@ from monai.data import CacheDataset, DataLoader, create_test_image_2d from monai.transforms import Compose, RandAffined, Spacingd -from tests.utils import SkipIfBeforePyTorchVersion -@SkipIfBeforePyTorchVersion((1, 7)) class TestTransformsWCacheDatasetAndPersistentWorkers(unittest.TestCase): def test_duplicate_transforms(self): data = [{"img": create_test_image_2d(128, 128, num_seg_classes=1, channel_dim=0)[0]} for _ in range(2)] diff --git a/tests/test_detect_envelope.py b/tests/test_detect_envelope.py index f1b9c7ad1a..5ea82a463d 100644 --- a/tests/test_detect_envelope.py +++ b/tests/test_detect_envelope.py @@ -16,8 +16,8 @@ from parameterized import parameterized from monai.transforms import DetectEnvelope -from monai.utils import InvalidPyTorchVersionError, OptionalImportError -from tests.utils import SkipIfAtLeastPyTorchVersion, SkipIfBeforePyTorchVersion, SkipIfModule, SkipIfNoModule +from monai.utils import OptionalImportError +from tests.utils import SkipIfModule, SkipIfNoModule n_samples = 500 hann_windowed_sine = np.sin(2 * np.pi * 10 * np.linspace(0, 1, n_samples)) * np.hanning(n_samples) @@ -112,7 +112,6 @@ TEST_CASE_INVALID_OBJ = [{}, "a string", "__call__"] # method expected to raise exception -@SkipIfBeforePyTorchVersion((1, 7)) @SkipIfNoModule("torch.fft") class TestDetectEnvelope(unittest.TestCase): @parameterized.expand( @@ -147,19 +146,11 @@ def test_value_error(self, arguments, image, method): raise ValueError("Expected raising method invalid. Should be __init__ or __call__.") -@SkipIfBeforePyTorchVersion((1, 7)) @SkipIfModule("torch.fft") class TestHilbertTransformNoFFTMod(unittest.TestCase): def test_no_fft_module_error(self): self.assertRaises(OptionalImportError, DetectEnvelope(), np.random.rand(1, 10)) -@SkipIfAtLeastPyTorchVersion((1, 7)) -class TestDetectEnvelopeInvalidPyTorch(unittest.TestCase): - def test_invalid_pytorch_error(self): - with self.assertRaisesRegex(InvalidPyTorchVersionError, "version"): - DetectEnvelope() - - if __name__ == "__main__": unittest.main() diff --git a/tests/test_dice_ce_loss.py b/tests/test_dice_ce_loss.py index ff2cd00b02..83ad5b8d9a 100644 --- a/tests/test_dice_ce_loss.py +++ b/tests/test_dice_ce_loss.py @@ -16,7 +16,7 @@ from parameterized import parameterized from monai.losses import DiceCELoss -from tests.utils import SkipIfBeforePyTorchVersion, test_script_save +from tests.utils import test_script_save TEST_CASES = [ [ # shape: (2, 2, 3), (2, 1, 3) @@ -85,7 +85,6 @@ def test_ill_reduction(self): loss = DiceCELoss(reduction="none") loss(torch.ones((1, 2, 3)), torch.ones((1, 1, 2, 3))) - @SkipIfBeforePyTorchVersion((1, 7, 0)) def test_script(self): loss = DiceCELoss() test_input = torch.ones(2, 1, 8, 8) diff --git a/tests/test_dice_focal_loss.py b/tests/test_dice_focal_loss.py index c611fe4160..b77a36e720 100644 --- a/tests/test_dice_focal_loss.py +++ b/tests/test_dice_focal_loss.py @@ -15,7 +15,7 @@ import torch from monai.losses import DiceFocalLoss, DiceLoss, FocalLoss -from tests.utils import SkipIfBeforePyTorchVersion, test_script_save +from tests.utils import test_script_save class TestDiceFocalLoss(unittest.TestCase): @@ -61,7 +61,6 @@ def test_ill_lambda(self): with self.assertRaisesRegex(ValueError, ""): DiceFocalLoss(lambda_dice=-1.0) - @SkipIfBeforePyTorchVersion((1, 7, 0)) def test_script(self): loss = DiceFocalLoss() test_input = torch.ones(2, 1, 8, 8) diff --git a/tests/test_dice_loss.py b/tests/test_dice_loss.py index 4e45393de6..223b09e624 100644 --- a/tests/test_dice_loss.py +++ b/tests/test_dice_loss.py @@ -16,7 +16,7 @@ from parameterized import parameterized from monai.losses import DiceLoss -from tests.utils import SkipIfBeforePyTorchVersion, test_script_save +from tests.utils import test_script_save TEST_CASES = [ [ # shape: (1, 1, 2, 2), (1, 1, 2, 2) @@ -184,7 +184,6 @@ def test_input_warnings(self): loss = DiceLoss(to_onehot_y=True) loss.forward(chn_input, chn_target) - @SkipIfBeforePyTorchVersion((1, 7, 0)) def test_script(self): loss = DiceLoss() test_input = torch.ones(2, 1, 8, 8) diff --git a/tests/test_focal_loss.py b/tests/test_focal_loss.py index d8a9c8ab5b..5a063ba6c8 100644 --- a/tests/test_focal_loss.py +++ b/tests/test_focal_loss.py @@ -17,7 +17,7 @@ from monai.losses import FocalLoss from monai.networks import one_hot -from tests.utils import SkipIfBeforePyTorchVersion, test_script_save +from tests.utils import test_script_save class TestFocalLoss(unittest.TestCase): @@ -261,7 +261,6 @@ def test_ill_class_weight(self): with self.assertRaisesRegex(ValueError, ""): FocalLoss(include_background=False, weight=(1.0, 1.0, -1.0))(chn_input, chn_target) - @SkipIfBeforePyTorchVersion((1, 7, 0)) def test_script(self): loss = FocalLoss() test_input = torch.ones(2, 2, 8, 8) diff --git a/tests/test_gaussian_filter.py b/tests/test_gaussian_filter.py index 9d76e44cec..c4ffe56896 100644 --- a/tests/test_gaussian_filter.py +++ b/tests/test_gaussian_filter.py @@ -16,7 +16,7 @@ from parameterized import parameterized from monai.networks.layers import GaussianFilter -from tests.utils import SkipIfBeforePyTorchVersion, skip_if_quick +from tests.utils import skip_if_quick TEST_CASES = [[{"type": "erf", "gt": 2.0}], [{"type": "scalespace", "gt": 3.0}], [{"type": "sampled", "gt": 5.0}]] TEST_CASES_GPU = [[{"type": "erf", "gt": 0.8, "device": "cuda"}], [{"type": "sampled", "gt": 5.0, "device": "cuda"}]] @@ -82,7 +82,6 @@ def code_to_run(self, input_args): ) @parameterized.expand(TEST_CASES + TEST_CASES_GPU + TEST_CASES_3d) - @SkipIfBeforePyTorchVersion((1, 7)) def test_train_quick(self, input_args): self.code_to_run(input_args) diff --git a/tests/test_generalized_dice_loss.py b/tests/test_generalized_dice_loss.py index fa301201e4..81f8f4c0b0 100644 --- a/tests/test_generalized_dice_loss.py +++ b/tests/test_generalized_dice_loss.py @@ -16,7 +16,7 @@ from parameterized import parameterized from monai.losses import GeneralizedDiceLoss -from tests.utils import SkipIfBeforePyTorchVersion, test_script_save +from tests.utils import test_script_save TEST_CASES = [ [ # shape: (1, 1, 2, 2), (1, 1, 2, 2) @@ -193,7 +193,6 @@ def test_batch(self): loss = generalized_dice_loss(prediction, target) self.assertNotEqual(loss.grad_fn, None) - @SkipIfBeforePyTorchVersion((1, 7, 0)) def test_script(self): loss = GeneralizedDiceLoss() test_input = torch.ones(2, 1, 8, 8) diff --git a/tests/test_generalized_wasserstein_dice_loss.py b/tests/test_generalized_wasserstein_dice_loss.py index 2c33d365f4..49a5aa0556 100644 --- a/tests/test_generalized_wasserstein_dice_loss.py +++ b/tests/test_generalized_wasserstein_dice_loss.py @@ -18,7 +18,7 @@ import torch.optim as optim from monai.losses import GeneralizedWassersteinDiceLoss -from tests.utils import SkipIfBeforePyTorchVersion, test_script_save +from tests.utils import test_script_save class TestGeneralizedWassersteinDiceLoss(unittest.TestCase): @@ -216,7 +216,6 @@ def forward(self, x): # check that the predicted segmentation has improved self.assertGreater(diff_start, diff_end) - @SkipIfBeforePyTorchVersion((1, 7, 0)) def test_script(self): target = torch.tensor([[0, 0, 0, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 0]]) diff --git a/tests/test_handler_smartcache.py b/tests/test_handler_smartcache.py index ec96d47e3d..7bc9011c2d 100644 --- a/tests/test_handler_smartcache.py +++ b/tests/test_handler_smartcache.py @@ -17,10 +17,8 @@ from monai.data import SmartCacheDataset from monai.handlers import SmartCacheHandler -from tests.utils import SkipIfBeforePyTorchVersion -@SkipIfBeforePyTorchVersion((1, 7)) class TestHandlerSmartCache(unittest.TestCase): def test_content(self): data = [0, 1, 2, 3, 4, 5, 6, 7, 8] diff --git a/tests/test_hilbert_transform.py b/tests/test_hilbert_transform.py index 10aa83293f..5377f4b6da 100644 --- a/tests/test_hilbert_transform.py +++ b/tests/test_hilbert_transform.py @@ -17,13 +17,7 @@ from monai.networks.layers import HilbertTransform from monai.utils import InvalidPyTorchVersionError, OptionalImportError -from tests.utils import ( - SkipIfAtLeastPyTorchVersion, - SkipIfBeforePyTorchVersion, - SkipIfModule, - SkipIfNoModule, - skip_if_no_cuda, -) +from tests.utils import SkipIfModule, SkipIfNoModule, skip_if_no_cuda def create_expected_numpy_output(input_datum, **kwargs): @@ -164,7 +158,6 @@ def create_expected_numpy_output(input_datum, **kwargs): # TESTS CHECKING PADDING, AXIS SELECTION ETC ARE COVERED BY test_detect_envelope.py -@SkipIfBeforePyTorchVersion((1, 7)) @SkipIfNoModule("torch.fft") class TestHilbertTransformCPU(unittest.TestCase): @parameterized.expand( @@ -183,7 +176,6 @@ def test_value(self, arguments, image, expected_data, atol): @skip_if_no_cuda -@SkipIfBeforePyTorchVersion((1, 7)) @SkipIfNoModule("torch.fft") class TestHilbertTransformGPU(unittest.TestCase): @parameterized.expand( @@ -204,14 +196,12 @@ def test_value(self, arguments, image, expected_data, atol): np.testing.assert_allclose(result, expected_data.squeeze(), atol=atol) -@SkipIfBeforePyTorchVersion((1, 7)) @SkipIfModule("torch.fft") class TestHilbertTransformNoFFTMod(unittest.TestCase): def test_no_fft_module_error(self): self.assertRaises(OptionalImportError, HilbertTransform(), torch.randn(1, 1, 10)) -@SkipIfAtLeastPyTorchVersion((1, 7)) class TestHilbertTransformInvalidPyTorch(unittest.TestCase): def test_invalid_pytorch_error(self): with self.assertRaisesRegex(InvalidPyTorchVersionError, "version"): diff --git a/tests/test_integration_fast_train.py b/tests/test_integration_fast_train.py index 51b2ac1d3f..4dbb70b102 100644 --- a/tests/test_integration_fast_train.py +++ b/tests/test_integration_fast_train.py @@ -52,12 +52,11 @@ ToDeviced, ) from monai.utils import set_determinism -from tests.utils import DistTestCase, SkipIfBeforePyTorchVersion, TimedCall, skip_if_no_cuda, skip_if_quick +from tests.utils import DistTestCase, TimedCall, skip_if_no_cuda, skip_if_quick @skip_if_no_cuda @skip_if_quick -@SkipIfBeforePyTorchVersion((1, 7)) class IntegrationFastTrain(DistTestCase): def setUp(self): set_determinism(seed=0) diff --git a/tests/test_keep_largest_connected_component.py b/tests/test_keep_largest_connected_component.py index 5c96b62131..6419914be6 100644 --- a/tests/test_keep_largest_connected_component.py +++ b/tests/test_keep_largest_connected_component.py @@ -19,7 +19,7 @@ from monai.transforms import KeepLargestConnectedComponent from monai.transforms.utils_pytorch_numpy_unification import moveaxis from monai.utils.type_conversion import convert_to_dst_type -from tests.utils import TEST_NDARRAYS, SkipIfBeforePyTorchVersion, assert_allclose +from tests.utils import TEST_NDARRAYS, assert_allclose def to_onehot(x): @@ -353,7 +353,6 @@ def test_correct_results(self, _, args, input_image, expected): assert_allclose(result, expected, type_test=False) @parameterized.expand(TESTS) - @SkipIfBeforePyTorchVersion((1, 7)) def test_correct_results_before_after_onehot(self, _, args, input_image, expected): """ From torch==1.7, torch.argmax changes its mechanism that if there are multiple maximal values then the diff --git a/tests/test_masked_loss.py b/tests/test_masked_loss.py index 9f28d51aa4..a00b3ae7e7 100644 --- a/tests/test_masked_loss.py +++ b/tests/test_masked_loss.py @@ -17,7 +17,7 @@ from monai.losses.dice import DiceFocalLoss, DiceLoss from monai.losses.spatial_mask import MaskedLoss from monai.utils import set_determinism -from tests.utils import SkipIfBeforePyTorchVersion, test_script_save +from tests.utils import test_script_save device = "cuda" if torch.cuda.is_available() else "cpu" @@ -73,7 +73,6 @@ def test_ill_opts(self): masked = MaskedLoss(loss=dice_loss) masked(input=torch.zeros((3, 3, 2, 2)), target=torch.zeros((3, 2, 2, 2)), mask=torch.zeros((3, 3, 2, 2))) - @SkipIfBeforePyTorchVersion((1, 7, 0)) def test_script(self): input_param, expected_val = TEST_CASES[0] size = [3, 3, 5, 5] diff --git a/tests/test_multi_scale.py b/tests/test_multi_scale.py index 963824f25e..f348c09512 100644 --- a/tests/test_multi_scale.py +++ b/tests/test_multi_scale.py @@ -16,7 +16,7 @@ from monai.losses import DiceLoss from monai.losses.multi_scale import MultiScaleLoss -from tests.utils import SkipIfBeforePyTorchVersion, test_script_save +from tests.utils import test_script_save dice_loss = DiceLoss(include_background=True, sigmoid=True, smooth_nr=1e-5, smooth_dr=1e-5) device = "cuda" if torch.cuda.is_available() else "cpu" @@ -67,7 +67,6 @@ def test_ill_opts(self): torch.ones((1, 1, 3), device=device), torch.ones((1, 1, 3), device=device) ) - @SkipIfBeforePyTorchVersion((1, 7, 0)) def test_script(self): input_param, input_data, expected_val = TEST_CASES[0] loss = MultiScaleLoss(**input_param) diff --git a/tests/test_randtorchvisiond.py b/tests/test_randtorchvisiond.py index 2e96d723ee..4fc1a1e630 100644 --- a/tests/test_randtorchvisiond.py +++ b/tests/test_randtorchvisiond.py @@ -16,7 +16,6 @@ from monai.transforms import Randomizable, RandTorchVisiond from monai.utils import set_determinism -from tests.utils import SkipIfBeforePyTorchVersion TEST_CASE_1 = [ {"keys": "img", "name": "ColorJitter"}, @@ -49,7 +48,6 @@ ] -@SkipIfBeforePyTorchVersion((1, 7)) class TestRandTorchVisiond(unittest.TestCase): @parameterized.expand([TEST_CASE_1, TEST_CASE_2, TEST_CASE_3]) def test_value(self, input_param, input_data, expected_value): diff --git a/tests/test_torchvision.py b/tests/test_torchvision.py index e0844eb4b9..68b9413e65 100644 --- a/tests/test_torchvision.py +++ b/tests/test_torchvision.py @@ -15,7 +15,7 @@ from monai.transforms import TorchVision from monai.utils import set_determinism -from tests.utils import TEST_NDARRAYS, SkipIfBeforePyTorchVersion, assert_allclose +from tests.utils import TEST_NDARRAYS, assert_allclose TESTS = [] for p in TEST_NDARRAYS: @@ -52,7 +52,6 @@ ) -@SkipIfBeforePyTorchVersion((1, 7)) class TestTorchVision(unittest.TestCase): @parameterized.expand(TESTS) def test_value(self, input_param, input_data, expected_value): diff --git a/tests/test_torchvisiond.py b/tests/test_torchvisiond.py index 4c62c6e41a..def26fa26b 100644 --- a/tests/test_torchvisiond.py +++ b/tests/test_torchvisiond.py @@ -16,7 +16,6 @@ from monai.transforms import TorchVisiond from monai.utils import set_determinism -from tests.utils import SkipIfBeforePyTorchVersion TEST_CASE_1 = [ {"keys": "img", "name": "ColorJitter"}, @@ -49,7 +48,6 @@ ] -@SkipIfBeforePyTorchVersion((1, 7)) class TestTorchVisiond(unittest.TestCase): @parameterized.expand([TEST_CASE_1, TEST_CASE_2, TEST_CASE_3]) def test_value(self, input_param, input_data, expected_value): diff --git a/tests/test_tversky_loss.py b/tests/test_tversky_loss.py index 2bb2409360..22f57cc8c6 100644 --- a/tests/test_tversky_loss.py +++ b/tests/test_tversky_loss.py @@ -16,7 +16,7 @@ from parameterized import parameterized from monai.losses import TverskyLoss -from tests.utils import SkipIfBeforePyTorchVersion, test_script_save +from tests.utils import test_script_save TEST_CASES = [ [ # shape: (1, 1, 2, 2), (1, 1, 2, 2) @@ -175,7 +175,6 @@ def test_input_warnings(self): loss = TverskyLoss(to_onehot_y=True) loss.forward(chn_input, chn_target) - @SkipIfBeforePyTorchVersion((1, 7, 0)) def test_script(self): loss = TverskyLoss() test_input = torch.ones(2, 1, 8, 8) diff --git a/tests/test_utils_pytorch_numpy_unification.py b/tests/test_utils_pytorch_numpy_unification.py index 6adf7093bf..1b08a5bd2f 100644 --- a/tests/test_utils_pytorch_numpy_unification.py +++ b/tests/test_utils_pytorch_numpy_unification.py @@ -16,7 +16,7 @@ from monai.transforms.utils_pytorch_numpy_unification import mode, percentile from monai.utils import set_determinism -from tests.utils import TEST_NDARRAYS, SkipIfBeforePyTorchVersion, assert_allclose +from tests.utils import TEST_NDARRAYS, assert_allclose TEST_MODE = [] for p in TEST_NDARRAYS: @@ -45,7 +45,6 @@ def test_fails(self): with self.assertRaises(ValueError): percentile(arr, q) - @SkipIfBeforePyTorchVersion((1, 7)) def test_dim(self): q = np.random.randint(0, 100, size=50) results = [] From 1530600fd423b9b8c11eec4b7e3ba1ed45f7c341 Mon Sep 17 00:00:00 2001 From: Richard Brown <33289025+rijobro@users.noreply.github.com> Date: Thu, 14 Apr 2022 18:46:06 +0100 Subject: [PATCH 2/3] fix Signed-off-by: Richard Brown <33289025+rijobro@users.noreply.github.com> --- tests/test_hilbert_transform.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/tests/test_hilbert_transform.py b/tests/test_hilbert_transform.py index 5377f4b6da..ab8e0cbfe8 100644 --- a/tests/test_hilbert_transform.py +++ b/tests/test_hilbert_transform.py @@ -202,11 +202,5 @@ def test_no_fft_module_error(self): self.assertRaises(OptionalImportError, HilbertTransform(), torch.randn(1, 1, 10)) -class TestHilbertTransformInvalidPyTorch(unittest.TestCase): - def test_invalid_pytorch_error(self): - with self.assertRaisesRegex(InvalidPyTorchVersionError, "version"): - HilbertTransform() - - if __name__ == "__main__": unittest.main() From 54604b3dc5ec9f5d2fb1253f702c214c48210b04 Mon Sep 17 00:00:00 2001 From: Richard Brown <33289025+rijobro@users.noreply.github.com> Date: Thu, 14 Apr 2022 19:36:45 +0100 Subject: [PATCH 3/3] fix Signed-off-by: Richard Brown <33289025+rijobro@users.noreply.github.com> --- tests/test_hilbert_transform.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_hilbert_transform.py b/tests/test_hilbert_transform.py index ab8e0cbfe8..f7954d6b24 100644 --- a/tests/test_hilbert_transform.py +++ b/tests/test_hilbert_transform.py @@ -16,7 +16,7 @@ from parameterized import parameterized from monai.networks.layers import HilbertTransform -from monai.utils import InvalidPyTorchVersionError, OptionalImportError +from monai.utils import OptionalImportError from tests.utils import SkipIfModule, SkipIfNoModule, skip_if_no_cuda