From dac6820c06df974afe942cd142b1e06bc7dda500 Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Tue, 31 May 2022 19:38:27 +0000 Subject: [PATCH 1/3] Move append items to list definition --- tests/test_anchor_box.py | 14 +++------- tests/test_fpn_block.py | 17 +++--------- ...est_generate_pos_neg_label_crop_centers.py | 7 ++--- tests/test_hardnegsampler.py | 8 ++---- tests/test_optim_novograd.py | 4 +-- tests/test_rand_crop_by_pos_neg_label.py | 27 +++++-------------- tests/test_to_tensor.py | 6 ++--- tests/test_upsample_block.py | 3 +-- 8 files changed, 22 insertions(+), 64 deletions(-) diff --git a/tests/test_anchor_box.py b/tests/test_anchor_box.py index c927f7d6f3..c342c36a11 100644 --- a/tests/test_anchor_box.py +++ b/tests/test_anchor_box.py @@ -20,23 +20,17 @@ _, has_torchvision = optional_import("torchvision") -TEST_CASES_2D = [] -TEST_CASES_2D.append( - [ +TEST_CASES_2D = [[ {"sizes": ((10, 12, 14, 16), (20, 24, 28, 32)), "aspect_ratios": ((1.0, 0.5, 2.0), (1.0, 0.5, 2.0))}, (5, 3, 128, 128), ((5, 7, 64, 32), (5, 7, 32, 16)), - ] -) + ]] -TEST_CASES_SHAPE_3D = [] -TEST_CASES_SHAPE_3D.append( - [ +TEST_CASES_SHAPE_3D = [[ {"feature_map_scales": (1, 2), "base_anchor_shapes": ((4, 3, 6), (8, 2, 4))}, (5, 3, 128, 128, 128), ((5, 7, 64, 32, 32), (5, 7, 32, 16, 16)), - ] -) + ]] @SkipIfBeforePyTorchVersion((1, 11)) diff --git a/tests/test_fpn_block.py b/tests/test_fpn_block.py index 531df6aba8..3a1888f13c 100644 --- a/tests/test_fpn_block.py +++ b/tests/test_fpn_block.py @@ -23,26 +23,17 @@ _, has_torchvision = optional_import("torchvision") -TEST_CASES = [] -TEST_CASES.append( - [ +TEST_CASES = [[ {"spatial_dims": 3, "in_channels_list": [32, 64], "out_channels": 6}, ((7, 32, 16, 32, 64), (7, 64, 8, 16, 32)), ((7, 6, 16, 32, 64), (7, 6, 8, 16, 32)), - ] -) -TEST_CASES.append( - [ + ], [ {"spatial_dims": 2, "in_channels_list": [32, 64], "out_channels": 6}, ((7, 32, 16, 32), (7, 64, 8, 16)), ((7, 6, 16, 32), (7, 6, 8, 16)), - ] -) + ]] -TEST_CASES2 = [] -TEST_CASES2.append( - [{"spatial_dims": 3, "returned_layers": [1]}, (7, 3, 32, 64, 32), ((7, 256, 16, 32, 16), (7, 256, 8, 16, 8))] -) +TEST_CASES2 = [[{"spatial_dims": 3, "returned_layers": [1]}, (7, 3, 32, 64, 32), ((7, 256, 16, 32, 16), (7, 256, 8, 16, 8))]] class TestFPNBlock(unittest.TestCase): diff --git a/tests/test_generate_pos_neg_label_crop_centers.py b/tests/test_generate_pos_neg_label_crop_centers.py index e1d9398fe3..5d8e039046 100644 --- a/tests/test_generate_pos_neg_label_crop_centers.py +++ b/tests/test_generate_pos_neg_label_crop_centers.py @@ -18,9 +18,7 @@ from monai.utils.misc import set_determinism from tests.utils import TEST_NDARRAYS, assert_allclose -TESTS = [] -TESTS.append( - [ +TESTS = [[ { "spatial_size": [2, 2, 2], "num_samples": 2, @@ -32,8 +30,7 @@ list, 2, 3, - ] -) + ]] class TestGeneratePosNegLabelCropCenters(unittest.TestCase): diff --git a/tests/test_hardnegsampler.py b/tests/test_hardnegsampler.py index 0c952954a1..5b444fb60b 100644 --- a/tests/test_hardnegsampler.py +++ b/tests/test_hardnegsampler.py @@ -17,17 +17,13 @@ from monai.apps.detection.utils.hard_negative_sampler import HardNegativeSampler from tests.utils import assert_allclose -TEST_CASE = [] -TEST_CASE.append([[], [], [], [torch.tensor([]), torch.tensor([])], [torch.tensor([]), torch.tensor([])]]) -TEST_CASE.append( - [ +TEST_CASE = [[[], [], [], [torch.tensor([]), torch.tensor([])], [torch.tensor([]), torch.tensor([])]], [ [0, 1], [1, 0, 2, 3], [0.1, 0.9, 0.4, 0.3, 0.3, 0.5], [torch.tensor([0, 1]), torch.tensor([1, 0, 1, 1])], [torch.tensor([1, 0]), torch.tensor([0, 1, 0, 0])], - ] -) + ]] select_sample_size_per_image = 6 positive_fraction = 0.5 diff --git a/tests/test_optim_novograd.py b/tests/test_optim_novograd.py index 0cf4c35cb6..0a625a2b63 100644 --- a/tests/test_optim_novograd.py +++ b/tests/test_optim_novograd.py @@ -32,9 +32,7 @@ def build_test_cases(data): {"params": [bias], "lr": 1e-2, "amsgrad": True, "grad_averaging": True, "weight_decay": 0.1}, ] - test_cases = [] - test_cases.append([test_case_same_param, default_params, weight, bias, input]) - test_cases.append([test_case_diff_param, default_params, weight, bias, input]) + test_cases = [[test_case_same_param, default_params, weight, bias, input], [test_case_diff_param, default_params, weight, bias, input]] return test_cases diff --git a/tests/test_rand_crop_by_pos_neg_label.py b/tests/test_rand_crop_by_pos_neg_label.py index 1cdc3d8c76..d391a6470d 100644 --- a/tests/test_rand_crop_by_pos_neg_label.py +++ b/tests/test_rand_crop_by_pos_neg_label.py @@ -18,9 +18,7 @@ from monai.transforms import RandCropByPosNegLabel from tests.utils import TEST_NDARRAYS -TESTS = [] -TESTS.append( - [ +TESTS = [[ { "label": np.random.randint(0, 2, size=[3, 3, 3, 3]), "spatial_size": [2, 2, -1], @@ -32,10 +30,7 @@ }, {"img": np.random.randint(0, 2, size=[3, 3, 3, 3])}, (3, 2, 2, 3), - ] -) -TESTS.append( - [ + ], [ { "label": np.random.randint(0, 2, size=[3, 3, 3, 3]), "spatial_size": [2, 2, 2], @@ -47,10 +42,7 @@ }, {"img": np.random.randint(0, 2, size=[3, 3, 3, 3])}, (3, 2, 2, 2), - ] -) -TESTS.append( - [ + ], [ { "label": None, "spatial_size": [2, 2, 2], @@ -66,10 +58,7 @@ "image": np.random.randint(0, 2, size=[3, 3, 3, 3]), }, (3, 2, 2, 2), - ] -) -TESTS.append( - [ + ], [ { "label": np.random.randint(0, 2, size=[3, 3, 3, 3]), "spatial_size": [4, 4, 2], @@ -81,10 +70,7 @@ }, {"img": np.random.randint(0, 2, size=[3, 3, 3, 3])}, (3, 3, 3, 2), - ] -) -TESTS.append( - [ + ], [ { "label": np.random.randint(0, 2, size=[3, 3, 3, 3]), "spatial_size": [4, 4, 4], @@ -96,8 +82,7 @@ }, {"img": np.random.randint(0, 2, size=[3, 3, 3, 3])}, (3, 3, 3, 3), - ] -) + ]] class TestRandCropByPosNegLabel(unittest.TestCase): diff --git a/tests/test_to_tensor.py b/tests/test_to_tensor.py index bfc61cdb19..fe6c4fb0d4 100644 --- a/tests/test_to_tensor.py +++ b/tests/test_to_tensor.py @@ -21,13 +21,11 @@ im = [[1, 2], [3, 4]] -TESTS = [] -TESTS.append((im, (2, 2))) +TESTS = [(im, (2, 2))] for p in TEST_NDARRAYS: TESTS.append((p(im), (2, 2))) -TESTS_SINGLE = [] -TESTS_SINGLE.append([5]) +TESTS_SINGLE = [[5]] for p in TEST_NDARRAYS: TESTS_SINGLE.append([p(5)]) diff --git a/tests/test_upsample_block.py b/tests/test_upsample_block.py index aa4141aabc..1bb8d7fb8b 100644 --- a/tests/test_upsample_block.py +++ b/tests/test_upsample_block.py @@ -75,9 +75,8 @@ for t in UpsampleMode: test_case = [ {"dimensions": 3, "in_channels": 3, "out_channels": 5, "mode": t, "scale_factor": s, "align_corners": True}, - (16, 3, 4, 5, 6), + (16, 3, 4, 5, 6),expected_shape ] - test_case.append(expected_shape) TEST_CASES_EQ.append(test_case) From b7cc1efee517df000ef833583e909b1af301fc7d Mon Sep 17 00:00:00 2001 From: monai-bot Date: Tue, 31 May 2022 19:43:39 +0000 Subject: [PATCH 2/3] [MONAI] code formatting Signed-off-by: monai-bot --- tests/test_anchor_box.py | 12 ++++++++---- tests/test_fpn_block.py | 13 +++++++++---- ...test_generate_pos_neg_label_crop_centers.py | 6 ++++-- tests/test_hardnegsampler.py | 7 +++++-- tests/test_optim_novograd.py | 5 ++++- tests/test_rand_crop_by_pos_neg_label.py | 18 ++++++++++++------ tests/test_upsample_block.py | 3 ++- 7 files changed, 44 insertions(+), 20 deletions(-) diff --git a/tests/test_anchor_box.py b/tests/test_anchor_box.py index c342c36a11..a6abbc0200 100644 --- a/tests/test_anchor_box.py +++ b/tests/test_anchor_box.py @@ -20,17 +20,21 @@ _, has_torchvision = optional_import("torchvision") -TEST_CASES_2D = [[ +TEST_CASES_2D = [ + [ {"sizes": ((10, 12, 14, 16), (20, 24, 28, 32)), "aspect_ratios": ((1.0, 0.5, 2.0), (1.0, 0.5, 2.0))}, (5, 3, 128, 128), ((5, 7, 64, 32), (5, 7, 32, 16)), - ]] + ] +] -TEST_CASES_SHAPE_3D = [[ +TEST_CASES_SHAPE_3D = [ + [ {"feature_map_scales": (1, 2), "base_anchor_shapes": ((4, 3, 6), (8, 2, 4))}, (5, 3, 128, 128, 128), ((5, 7, 64, 32, 32), (5, 7, 32, 16, 16)), - ]] + ] +] @SkipIfBeforePyTorchVersion((1, 11)) diff --git a/tests/test_fpn_block.py b/tests/test_fpn_block.py index 3a1888f13c..420fd04367 100644 --- a/tests/test_fpn_block.py +++ b/tests/test_fpn_block.py @@ -23,17 +23,22 @@ _, has_torchvision = optional_import("torchvision") -TEST_CASES = [[ +TEST_CASES = [ + [ {"spatial_dims": 3, "in_channels_list": [32, 64], "out_channels": 6}, ((7, 32, 16, 32, 64), (7, 64, 8, 16, 32)), ((7, 6, 16, 32, 64), (7, 6, 8, 16, 32)), - ], [ + ], + [ {"spatial_dims": 2, "in_channels_list": [32, 64], "out_channels": 6}, ((7, 32, 16, 32), (7, 64, 8, 16)), ((7, 6, 16, 32), (7, 6, 8, 16)), - ]] + ], +] -TEST_CASES2 = [[{"spatial_dims": 3, "returned_layers": [1]}, (7, 3, 32, 64, 32), ((7, 256, 16, 32, 16), (7, 256, 8, 16, 8))]] +TEST_CASES2 = [ + [{"spatial_dims": 3, "returned_layers": [1]}, (7, 3, 32, 64, 32), ((7, 256, 16, 32, 16), (7, 256, 8, 16, 8))] +] class TestFPNBlock(unittest.TestCase): diff --git a/tests/test_generate_pos_neg_label_crop_centers.py b/tests/test_generate_pos_neg_label_crop_centers.py index 5d8e039046..91db0e9d96 100644 --- a/tests/test_generate_pos_neg_label_crop_centers.py +++ b/tests/test_generate_pos_neg_label_crop_centers.py @@ -18,7 +18,8 @@ from monai.utils.misc import set_determinism from tests.utils import TEST_NDARRAYS, assert_allclose -TESTS = [[ +TESTS = [ + [ { "spatial_size": [2, 2, 2], "num_samples": 2, @@ -30,7 +31,8 @@ list, 2, 3, - ]] + ] +] class TestGeneratePosNegLabelCropCenters(unittest.TestCase): diff --git a/tests/test_hardnegsampler.py b/tests/test_hardnegsampler.py index 5b444fb60b..f4eff81810 100644 --- a/tests/test_hardnegsampler.py +++ b/tests/test_hardnegsampler.py @@ -17,13 +17,16 @@ from monai.apps.detection.utils.hard_negative_sampler import HardNegativeSampler from tests.utils import assert_allclose -TEST_CASE = [[[], [], [], [torch.tensor([]), torch.tensor([])], [torch.tensor([]), torch.tensor([])]], [ +TEST_CASE = [ + [[], [], [], [torch.tensor([]), torch.tensor([])], [torch.tensor([]), torch.tensor([])]], + [ [0, 1], [1, 0, 2, 3], [0.1, 0.9, 0.4, 0.3, 0.3, 0.5], [torch.tensor([0, 1]), torch.tensor([1, 0, 1, 1])], [torch.tensor([1, 0]), torch.tensor([0, 1, 0, 0])], - ]] + ], +] select_sample_size_per_image = 6 positive_fraction = 0.5 diff --git a/tests/test_optim_novograd.py b/tests/test_optim_novograd.py index 0a625a2b63..c1e63182e6 100644 --- a/tests/test_optim_novograd.py +++ b/tests/test_optim_novograd.py @@ -32,7 +32,10 @@ def build_test_cases(data): {"params": [bias], "lr": 1e-2, "amsgrad": True, "grad_averaging": True, "weight_decay": 0.1}, ] - test_cases = [[test_case_same_param, default_params, weight, bias, input], [test_case_diff_param, default_params, weight, bias, input]] + test_cases = [ + [test_case_same_param, default_params, weight, bias, input], + [test_case_diff_param, default_params, weight, bias, input], + ] return test_cases diff --git a/tests/test_rand_crop_by_pos_neg_label.py b/tests/test_rand_crop_by_pos_neg_label.py index d391a6470d..1d9e2612c7 100644 --- a/tests/test_rand_crop_by_pos_neg_label.py +++ b/tests/test_rand_crop_by_pos_neg_label.py @@ -18,7 +18,8 @@ from monai.transforms import RandCropByPosNegLabel from tests.utils import TEST_NDARRAYS -TESTS = [[ +TESTS = [ + [ { "label": np.random.randint(0, 2, size=[3, 3, 3, 3]), "spatial_size": [2, 2, -1], @@ -30,7 +31,8 @@ }, {"img": np.random.randint(0, 2, size=[3, 3, 3, 3])}, (3, 2, 2, 3), - ], [ + ], + [ { "label": np.random.randint(0, 2, size=[3, 3, 3, 3]), "spatial_size": [2, 2, 2], @@ -42,7 +44,8 @@ }, {"img": np.random.randint(0, 2, size=[3, 3, 3, 3])}, (3, 2, 2, 2), - ], [ + ], + [ { "label": None, "spatial_size": [2, 2, 2], @@ -58,7 +61,8 @@ "image": np.random.randint(0, 2, size=[3, 3, 3, 3]), }, (3, 2, 2, 2), - ], [ + ], + [ { "label": np.random.randint(0, 2, size=[3, 3, 3, 3]), "spatial_size": [4, 4, 2], @@ -70,7 +74,8 @@ }, {"img": np.random.randint(0, 2, size=[3, 3, 3, 3])}, (3, 3, 3, 2), - ], [ + ], + [ { "label": np.random.randint(0, 2, size=[3, 3, 3, 3]), "spatial_size": [4, 4, 4], @@ -82,7 +87,8 @@ }, {"img": np.random.randint(0, 2, size=[3, 3, 3, 3])}, (3, 3, 3, 3), - ]] + ], +] class TestRandCropByPosNegLabel(unittest.TestCase): diff --git a/tests/test_upsample_block.py b/tests/test_upsample_block.py index 1bb8d7fb8b..6e2e548042 100644 --- a/tests/test_upsample_block.py +++ b/tests/test_upsample_block.py @@ -75,7 +75,8 @@ for t in UpsampleMode: test_case = [ {"dimensions": 3, "in_channels": 3, "out_channels": 5, "mode": t, "scale_factor": s, "align_corners": True}, - (16, 3, 4, 5, 6),expected_shape + (16, 3, 4, 5, 6), + expected_shape, ] TEST_CASES_EQ.append(test_case) From 8845a800668a991056e6a99099908a01914f014e Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Tue, 31 May 2022 20:45:24 +0100 Subject: [PATCH 3/3] Remove assert statement from non-test files (#4418) --- monai/apps/detection/utils/anchor_utils.py | 3 ++- monai/networks/blocks/feature_pyramid_network.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/monai/apps/detection/utils/anchor_utils.py b/monai/apps/detection/utils/anchor_utils.py index 7f11302d0a..5821c0c353 100644 --- a/monai/apps/detection/utils/anchor_utils.py +++ b/monai/apps/detection/utils/anchor_utils.py @@ -234,7 +234,8 @@ def grid_anchors(self, grid_sizes: List[List[int]], strides: List[List[Tensor]]) """ anchors = [] cell_anchors = self.cell_anchors - assert cell_anchors is not None + if cell_anchors is None: + raise AssertionError if not (len(grid_sizes) == len(strides) == len(cell_anchors)): raise ValueError( diff --git a/monai/networks/blocks/feature_pyramid_network.py b/monai/networks/blocks/feature_pyramid_network.py index 09ed2c6e20..2f7b903a19 100644 --- a/monai/networks/blocks/feature_pyramid_network.py +++ b/monai/networks/blocks/feature_pyramid_network.py @@ -197,7 +197,8 @@ def __init__( nn.init.constant_(m.bias, 0.0) # type: ignore if extra_blocks is not None: - assert isinstance(extra_blocks, ExtraFPNBlock) + if not isinstance(extra_blocks, ExtraFPNBlock): + raise AssertionError self.extra_blocks = extra_blocks def get_result_from_inner_blocks(self, x: Tensor, idx: int) -> Tensor: