From fb3d9f12b5211c2b29557baaf6f6c97685caf13b Mon Sep 17 00:00:00 2001 From: Oeslle Lucena Date: Thu, 15 Dec 2022 18:38:23 +0000 Subject: [PATCH] Width and height swapped when creating test image in data/synthetic/create_test_image_2d (#5627) Signed-off-by: OeslleLucena Fixes https://github.com/Project-MONAI/MONAI/issues/5373. ### Description Change the `synthetic.py` script to have the (height, width, depth) format. Edit all the tests that uses `create_test_image_3d` to match the new format. ### Types of changes - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [ ] New tests added to cover the changes. - [x] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [x] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. Signed-off-by: OeslleLucena --- monai/data/synthetic.py | 28 ++++++++++++------------ tests/test_adn.py | 4 ++-- tests/test_convolutions.py | 16 +++++++------- tests/test_denseblock.py | 6 ++--- tests/test_integration_sliding_window.py | 2 +- tests/test_invert.py | 2 +- tests/test_invertd.py | 8 +++---- tests/test_rand_rotate.py | 8 +++---- tests/test_rand_rotated.py | 16 +++++++------- tests/test_rand_weighted_crop.py | 14 ++++++------ tests/test_synthetic.py | 2 +- 11 files changed, 53 insertions(+), 53 deletions(-) diff --git a/monai/data/synthetic.py b/monai/data/synthetic.py index 7f51b687fb..a1a85338fe 100644 --- a/monai/data/synthetic.py +++ b/monai/data/synthetic.py @@ -19,8 +19,8 @@ def create_test_image_2d( - width: int, height: int, + width: int, num_objs: int = 12, rad_max: int = 30, rad_min: int = 5, @@ -37,8 +37,8 @@ def create_test_image_2d( an image without channel dimension, otherwise create an image with channel dimension as first dim or last dim. Args: - width: width of the image. The value should be larger than `2 * rad_max`. height: height of the image. The value should be larger than `2 * rad_max`. + width: width of the image. The value should be larger than `2 * rad_max`. num_objs: number of circles to generate. Defaults to `12`. rad_max: maximum circle radius. Defaults to `30`. rad_min: minimum circle radius. Defaults to `5`. @@ -50,25 +50,25 @@ def create_test_image_2d( random_state: the random generator to use. Defaults to `np.random`. Returns: - Randomised Numpy array with shape (`width`, `height`) + Randomised Numpy array with shape (`height`, `width`) """ if rad_max <= rad_min: raise ValueError("`rad_min` should be less than `rad_max`.") if rad_min < 1: raise ValueError("`rad_min` should be no less than 1.") - min_size = min(width, height) + min_size = min(height, width) if min_size <= 2 * rad_max: raise ValueError("the minimal size of the image should be larger than `2 * rad_max`.") - image = np.zeros((width, height)) + image = np.zeros((height, width)) rs: np.random.RandomState = np.random.random.__self__ if random_state is None else random_state # type: ignore for _ in range(num_objs): - x = rs.randint(rad_max, width - rad_max) - y = rs.randint(rad_max, height - rad_max) + x = rs.randint(rad_max, height - rad_max) + y = rs.randint(rad_max, width - rad_max) rad = rs.randint(rad_min, rad_max) - spy, spx = np.ogrid[-x : width - x, -y : height - y] + spy, spx = np.ogrid[-x : height - x, -y : width - y] circle = (spx * spx + spy * spy) <= rad * rad if num_seg_classes > 1: @@ -124,7 +124,7 @@ def create_test_image_3d( random_state: the random generator to use. Defaults to `np.random`. Returns: - Randomised Numpy array with shape (`width`, `height`, `depth`) + Randomised Numpy array with shape (`height`, `width`, `depth`) See also: :py:meth:`~create_test_image_2d` @@ -134,19 +134,19 @@ def create_test_image_3d( raise ValueError("`rad_min` should be less than `rad_max`.") if rad_min < 1: raise ValueError("`rad_min` should be no less than 1.") - min_size = min(width, height, depth) + min_size = min(height, width, depth) if min_size <= 2 * rad_max: raise ValueError("the minimal size of the image should be larger than `2 * rad_max`.") - image = np.zeros((width, height, depth)) + image = np.zeros((height, width, depth)) rs: np.random.RandomState = np.random.random.__self__ if random_state is None else random_state # type: ignore for _ in range(num_objs): - x = rs.randint(rad_max, width - rad_max) - y = rs.randint(rad_max, height - rad_max) + x = rs.randint(rad_max, height - rad_max) + y = rs.randint(rad_max, width - rad_max) z = rs.randint(rad_max, depth - rad_max) rad = rs.randint(rad_min, rad_max) - spy, spx, spz = np.ogrid[-x : width - x, -y : height - y, -z : depth - z] + spy, spx, spz = np.ogrid[-x : height - x, -y : width - y, -z : depth - z] circle = (spx * spx + spy * spy + spz * spz) <= rad * rad if num_seg_classes > 1: diff --git a/tests/test_adn.py b/tests/test_adn.py index 2352f5c1e2..ffd28f8fc8 100644 --- a/tests/test_adn.py +++ b/tests/test_adn.py @@ -46,7 +46,7 @@ [{"norm": "INSTANCE", "norm_dim": 3, "dropout_dim": 1, "dropout": 0.8, "ordering": "AND"}], [ { - "norm": ("layer", {"normalized_shape": (64, 80)}), + "norm": ("layer", {"normalized_shape": (48, 80)}), "norm_dim": 3, "dropout_dim": 1, "dropout": 0.8, @@ -76,7 +76,7 @@ def test_adn_3d(self, args): adn = ADN(**args) print(adn) out = adn(self.imt) - expected_shape = (1, self.input_channels, self.im_shape[1], self.im_shape[0], self.im_shape[2]) + expected_shape = (1, self.input_channels, self.im_shape[0], self.im_shape[1], self.im_shape[2]) self.assertEqual(out.shape, expected_shape) diff --git a/tests/test_convolutions.py b/tests/test_convolutions.py index 5c1681ad71..dc018248c1 100644 --- a/tests/test_convolutions.py +++ b/tests/test_convolutions.py @@ -70,19 +70,19 @@ class TestConvolution3D(TorchImageTestCase3D): def test_conv1(self): conv = Convolution(3, self.input_channels, self.output_channels, dropout=0.1, adn_ordering="DAN") out = conv(self.imt) - expected_shape = (1, self.output_channels, self.im_shape[1], self.im_shape[0], self.im_shape[2]) + expected_shape = (1, self.output_channels, self.im_shape[0], self.im_shape[1], self.im_shape[2]) self.assertEqual(out.shape, expected_shape) def test_conv1_no_acti(self): conv = Convolution(3, self.input_channels, self.output_channels, act=None, adn_ordering="AND") out = conv(self.imt) - expected_shape = (1, self.output_channels, self.im_shape[1], self.im_shape[0], self.im_shape[2]) + expected_shape = (1, self.output_channels, self.im_shape[0], self.im_shape[1], self.im_shape[2]) self.assertEqual(out.shape, expected_shape) def test_conv_only1(self): conv = Convolution(3, self.input_channels, self.output_channels, conv_only=True) out = conv(self.imt) - expected_shape = (1, self.output_channels, self.im_shape[1], self.im_shape[0], self.im_shape[2]) + expected_shape = (1, self.output_channels, self.im_shape[0], self.im_shape[1], self.im_shape[2]) self.assertEqual(out.shape, expected_shape) def test_stride1(self): @@ -92,8 +92,8 @@ def test_stride1(self): expected_shape = ( 1, self.output_channels, - self.im_shape[1] // 2, self.im_shape[0] // 2, + self.im_shape[1] // 2, self.im_shape[2] // 2, ) self.assertEqual(out.shape, expected_shape) @@ -101,25 +101,25 @@ def test_stride1(self): def test_dilation1(self): conv = Convolution(3, self.input_channels, self.output_channels, dilation=3) out = conv(self.imt) - expected_shape = (1, self.output_channels, self.im_shape[1], self.im_shape[0], self.im_shape[2]) + expected_shape = (1, self.output_channels, self.im_shape[0], self.im_shape[1], self.im_shape[2]) self.assertEqual(out.shape, expected_shape) def test_dropout1(self): conv = Convolution(3, self.input_channels, self.output_channels, dropout=0.15) out = conv(self.imt) - expected_shape = (1, self.output_channels, self.im_shape[1], self.im_shape[0], self.im_shape[2]) + expected_shape = (1, self.output_channels, self.im_shape[0], self.im_shape[1], self.im_shape[2]) self.assertEqual(out.shape, expected_shape) def test_transpose1(self): conv = Convolution(3, self.input_channels, self.output_channels, is_transposed=True) out = conv(self.imt) - expected_shape = (1, self.output_channels, self.im_shape[1], self.im_shape[0], self.im_shape[2]) + expected_shape = (1, self.output_channels, self.im_shape[0], self.im_shape[1], self.im_shape[2]) self.assertEqual(out.shape, expected_shape) def test_transpose2(self): conv = Convolution(3, self.input_channels, self.output_channels, strides=2, is_transposed=True) out = conv(self.imt) - expected_shape = (1, self.output_channels, self.im_shape[1] * 2, self.im_shape[0] * 2, self.im_shape[2] * 2) + expected_shape = (1, self.output_channels, self.im_shape[0] * 2, self.im_shape[1] * 2, self.im_shape[2] * 2) self.assertEqual(out.shape, expected_shape) diff --git a/tests/test_denseblock.py b/tests/test_denseblock.py index dd0a103022..3a3f61860e 100644 --- a/tests/test_denseblock.py +++ b/tests/test_denseblock.py @@ -42,8 +42,8 @@ def test_block_conv(self): expected_shape = ( 1, self.output_channels + self.input_channels * 2, - self.im_shape[1], self.im_shape[0], + self.im_shape[1], self.im_shape[2], ) self.assertEqual(out.shape, expected_shape) @@ -87,7 +87,7 @@ def test_block1(self): channels = [2, 4] conv = ConvDenseBlock(spatial_dims=3, in_channels=self.input_channels, channels=channels) out = conv(self.imt) - expected_shape = (1, self.input_channels + sum(channels), self.im_shape[1], self.im_shape[0], self.im_shape[2]) + expected_shape = (1, self.input_channels + sum(channels), self.im_shape[0], self.im_shape[1], self.im_shape[2]) self.assertEqual(out.shape, expected_shape) def test_block2(self): @@ -95,7 +95,7 @@ def test_block2(self): dilations = [1, 2] conv = ConvDenseBlock(spatial_dims=3, in_channels=self.input_channels, channels=channels, dilations=dilations) out = conv(self.imt) - expected_shape = (1, self.input_channels + sum(channels), self.im_shape[1], self.im_shape[0], self.im_shape[2]) + expected_shape = (1, self.input_channels + sum(channels), self.im_shape[0], self.im_shape[1], self.im_shape[2]) self.assertEqual(out.shape, expected_shape) diff --git a/tests/test_integration_sliding_window.py b/tests/test_integration_sliding_window.py index 9b1c7e5200..38cab0c2f0 100644 --- a/tests/test_integration_sliding_window.py +++ b/tests/test_integration_sliding_window.py @@ -67,7 +67,7 @@ class TestIntegrationSlidingWindow(DistTestCase): def setUp(self): set_determinism(seed=0) - im, seg = create_test_image_3d(25, 28, 63, rad_max=10, noise_max=1, num_objs=4, num_seg_classes=1) + im, seg = create_test_image_3d(28, 25, 63, rad_max=10, noise_max=1, num_objs=4, num_seg_classes=1) self.img_name = make_nifti_image(im) self.seg_name = make_nifti_image(seg) self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu:0") diff --git a/tests/test_invert.py b/tests/test_invert.py index ca2fb93460..5d4926ecd2 100644 --- a/tests/test_invert.py +++ b/tests/test_invert.py @@ -74,7 +74,7 @@ def test_invert(self): self.assertTupleEqual(orig.shape[1:], (100, 100, 100)) # check the nearest interpolation mode assert_allclose(i.to(torch.uint8).to(torch.float), i.to(torch.float)) - self.assertTupleEqual(i.shape[1:], (100, 101, 107)) + self.assertTupleEqual(i.shape[1:], (101, 100, 107)) # check labels match reverted = i.detach().cpu().numpy().astype(np.int32) original = LoadImage(image_only=True)(data[-1]) diff --git a/tests/test_invertd.py b/tests/test_invertd.py index 202874c02b..f3b504b39d 100644 --- a/tests/test_invertd.py +++ b/tests/test_invertd.py @@ -104,21 +104,21 @@ def test_invert(self): # check the nearest interpolation mode i = item["image_inverted"] assert_allclose(i.to(torch.uint8).to(torch.float), i.to(torch.float)) - self.assertTupleEqual(i.shape[1:], (100, 101, 107)) + self.assertTupleEqual(i.shape[1:], (101, 100, 107)) i = item["label_inverted"] assert_allclose(i.to(torch.uint8).to(torch.float), i.to(torch.float)) - self.assertTupleEqual(i.shape[1:], (100, 101, 107)) + self.assertTupleEqual(i.shape[1:], (101, 100, 107)) # check the case that different items use different interpolation mode to invert transforms d = item["image_inverted1"] # if the interpolation mode is nearest, accumulated diff should be smaller than 1 self.assertLess(torch.sum(d.to(torch.float) - d.to(torch.uint8).to(torch.float)).item(), 1.0) - self.assertTupleEqual(d.shape, (1, 100, 101, 107)) + self.assertTupleEqual(d.shape, (1, 101, 100, 107)) d = item["label_inverted1"] # if the interpolation mode is not nearest, accumulated diff should be greater than 10000 self.assertGreater(torch.sum(d.to(torch.float) - d.to(torch.uint8).to(torch.float)).item(), 10000.0) - self.assertTupleEqual(d.shape, (1, 100, 101, 107)) + self.assertTupleEqual(d.shape, (1, 101, 100, 107)) # check labels match reverted = item["label_inverted"].detach().cpu().numpy().astype(np.int32) diff --git a/tests/test_rand_rotate.py b/tests/test_rand_rotate.py index 5d3a76a86d..7cba4f99c9 100644 --- a/tests/test_rand_rotate.py +++ b/tests/test_rand_rotate.py @@ -37,7 +37,7 @@ TEST_CASES_3D: List[Tuple] = [] for p in TEST_NDARRAYS_ALL: TEST_CASES_3D.append( - (p, np.pi / 2, -np.pi / 6, (0.0, np.pi), False, "bilinear", "border", False, (1, 87, 104, 109)) + (p, np.pi / 2, -np.pi / 6, (0.0, np.pi), False, "bilinear", "border", False, (1, 81, 110, 112)) ) TEST_CASES_3D.append( ( @@ -49,7 +49,7 @@ "nearest", "border", True, - (1, 89, 105, 104), + (1, 97, 100, 97), ) ) TEST_CASES_3D.append( @@ -62,10 +62,10 @@ "nearest", "zeros", True, - (1, 48, 64, 80), + (1, 64, 48, 80), ) ) - TEST_CASES_3D.append((p, (-np.pi / 4, 0), 0, 0, False, "nearest", "zeros", False, (1, 48, 77, 90))) + TEST_CASES_3D.append((p, (-np.pi / 4, 0), 0, 0, False, "nearest", "zeros", False, (1, 64, 61, 87))) class TestRandRotate2D(NumpyImageTestCase2D): diff --git a/tests/test_rand_rotated.py b/tests/test_rand_rotated.py index e19f8a513f..fe57c641a5 100644 --- a/tests/test_rand_rotated.py +++ b/tests/test_rand_rotated.py @@ -31,7 +31,7 @@ TEST_CASES_3D: List[Tuple] = [] for p in TEST_NDARRAYS_ALL: TEST_CASES_3D.append( - (p, np.pi / 2, -np.pi / 6, (0.0, np.pi), False, "bilinear", "border", False, (1, 87, 104, 109)) + (p, np.pi / 2, -np.pi / 6, (0.0, np.pi), False, "bilinear", "border", False, (1, 81, 110, 112)) ) TEST_CASES_3D.append( ( @@ -43,7 +43,7 @@ GridSampleMode.NEAREST, GridSamplePadMode.BORDER, False, - (1, 87, 104, 109), + (1, 81, 110, 112), ) ) TEST_CASES_3D.append( @@ -56,7 +56,7 @@ "nearest", "border", True, - (1, 89, 105, 104), + (1, 97, 100, 97), ) ) TEST_CASES_3D.append( @@ -69,7 +69,7 @@ GridSampleMode.NEAREST, GridSamplePadMode.BORDER, True, - (1, 89, 105, 104), + (1, 97, 100, 97), ) ) TEST_CASES_3D.append( @@ -82,7 +82,7 @@ "nearest", "zeros", True, - (1, 48, 64, 80), + (1, 64, 48, 80), ) ) TEST_CASES_3D.append( @@ -95,12 +95,12 @@ GridSampleMode.NEAREST, GridSamplePadMode.ZEROS, True, - (1, 48, 64, 80), + (1, 64, 48, 80), ) ) - TEST_CASES_3D.append((p, (-np.pi / 4, 0), 0, 0, False, "nearest", "zeros", False, (1, 48, 77, 90))) + TEST_CASES_3D.append((p, (-np.pi / 4, 0), 0, 0, False, "nearest", "zeros", False, (1, 64, 61, 87))) TEST_CASES_3D.append( - (p, (-np.pi / 4, 0), 0, 0, False, GridSampleMode.NEAREST, GridSamplePadMode.ZEROS, False, (1, 48, 77, 90)) + (p, (-np.pi / 4, 0), 0, 0, False, GridSampleMode.NEAREST, GridSamplePadMode.ZEROS, False, (1, 64, 61, 87)) ) diff --git a/tests/test_rand_weighted_crop.py b/tests/test_rand_weighted_crop.py index 2e1fcad4b2..2e5554402d 100644 --- a/tests/test_rand_weighted_crop.py +++ b/tests/test_rand_weighted_crop.py @@ -112,8 +112,8 @@ def get_data(ndim): dict(spatial_size=(10, -1, -1), num_samples=3), p(im), q(weight), - (1, 10, 64, 80), - [[14, 32, 40], [41, 32, 40], [20, 32, 40]], + (1, 10, 48, 80), + [[14, 24, 40], [41, 24, 40], [20, 24, 40]], ] ) im = SEGN_3D @@ -126,8 +126,8 @@ def get_data(ndim): dict(spatial_size=(10000, 400, 80), num_samples=3), p(im), q(weight), - (1, 48, 64, 80), - [[24, 32, 40], [24, 32, 40], [24, 32, 40]], + (1, 64, 48, 80), + [[32, 24, 40], [32, 24, 40], [32, 24, 40]], ] ) im = IMT_3D @@ -138,11 +138,11 @@ def get_data(ndim): TESTS.append( [ "bad w 3d", - dict(spatial_size=(48, 64, 80), num_samples=3), + dict(spatial_size=(64, 48, 80), num_samples=3), p(im), q(weight), - (1, 48, 64, 80), - [[24, 32, 40], [24, 32, 40], [24, 32, 40]], + (1, 64, 48, 80), + [[32, 24, 40], [32, 24, 40], [32, 24, 40]], ] ) diff --git a/tests/test_synthetic.py b/tests/test_synthetic.py index fadf6255ff..0da7054a10 100644 --- a/tests/test_synthetic.py +++ b/tests/test_synthetic.py @@ -21,7 +21,7 @@ [2, {"width": 64, "height": 64, "rad_max": 10, "rad_min": 4}, 0.1479004, 0.739502, (64, 64), 5], [ 2, - {"width": 32, "height": 28, "num_objs": 3, "rad_max": 5, "rad_min": 1, "noise_max": 0.2}, + {"width": 28, "height": 32, "num_objs": 3, "rad_max": 5, "rad_min": 1, "noise_max": 0.2}, 0.1709315, 0.4040179, (32, 28),