Skip to content

Commit

Permalink
Width and height swapped when creating test image in data/synthetic/c…
Browse files Browse the repository at this point in the history
…reate_test_image_2d (#5627)

Signed-off-by: OeslleLucena <[email protected]>

Fixes #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
<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [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 <[email protected]>
  • Loading branch information
OeslleLucena authored Dec 15, 2022
1 parent e50fa88 commit fb3d9f1
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 53 deletions.
28 changes: 14 additions & 14 deletions monai/data/synthetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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`.
Expand All @@ -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:
Expand Down Expand Up @@ -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`
Expand All @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_adn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)


Expand Down
16 changes: 8 additions & 8 deletions tests/test_convolutions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -92,34 +92,34 @@ 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)

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)


Expand Down
6 changes: 3 additions & 3 deletions tests/test_denseblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -87,15 +87,15 @@ 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):
channels = [2, 4]
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)


Expand Down
2 changes: 1 addition & 1 deletion tests/test_integration_sliding_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_invert.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
8 changes: 4 additions & 4 deletions tests/test_invertd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions tests/test_rand_rotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
(
Expand All @@ -49,7 +49,7 @@
"nearest",
"border",
True,
(1, 89, 105, 104),
(1, 97, 100, 97),
)
)
TEST_CASES_3D.append(
Expand All @@ -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):
Expand Down
16 changes: 8 additions & 8 deletions tests/test_rand_rotated.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
(
Expand All @@ -43,7 +43,7 @@
GridSampleMode.NEAREST,
GridSamplePadMode.BORDER,
False,
(1, 87, 104, 109),
(1, 81, 110, 112),
)
)
TEST_CASES_3D.append(
Expand All @@ -56,7 +56,7 @@
"nearest",
"border",
True,
(1, 89, 105, 104),
(1, 97, 100, 97),
)
)
TEST_CASES_3D.append(
Expand All @@ -69,7 +69,7 @@
GridSampleMode.NEAREST,
GridSamplePadMode.BORDER,
True,
(1, 89, 105, 104),
(1, 97, 100, 97),
)
)
TEST_CASES_3D.append(
Expand All @@ -82,7 +82,7 @@
"nearest",
"zeros",
True,
(1, 48, 64, 80),
(1, 64, 48, 80),
)
)
TEST_CASES_3D.append(
Expand All @@ -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))
)


Expand Down
14 changes: 7 additions & 7 deletions tests/test_rand_weighted_crop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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]],
]
)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_synthetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit fb3d9f1

Please sign in to comment.