Skip to content

Commit

Permalink
fixes unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Wenqi Li <wenqil@nvidia.com>
  • Loading branch information
wyli committed Sep 15, 2021
1 parent 0f38dea commit 8e41d48
Show file tree
Hide file tree
Showing 14 changed files with 35 additions and 51 deletions.
2 changes: 1 addition & 1 deletion tests/test_label_to_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_value(self, argments, image, expected_data):
self.assertEqual(type(result), type(image))
if isinstance(result, torch.Tensor):
self.assertEqual(result.device, image.device)
assert_allclose(result, expected_data)
assert_allclose(result, expected_data, type_test=False)


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion tests/test_label_to_maskd.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def test_value(self, argments, input_data, expected_data):
self.assertEqual(type(r), type(i))
if isinstance(r, torch.Tensor):
self.assertEqual(r.device, i.device)
assert_allclose(r, expected_data)
assert_allclose(r, expected_data, type_test=False)


if __name__ == "__main__":
Expand Down
24 changes: 8 additions & 16 deletions tests/test_rand_rotate90.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,44 +23,36 @@ def test_default(self):
for p in TEST_NDARRAYS:
rotate.set_random_state(123)
rotated = rotate(p(self.imt[0]))
expected = []
for channel in self.imt[0]:
expected.append(np.rot90(channel, 0, (0, 1)))
expected = [np.rot90(channel, 0, (0, 1)) for channel in self.imt[0]]
expected = np.stack(expected)
assert_allclose(rotated, expected, rtol=1.0e-5, atol=1.0e-8)
assert_allclose(rotated, p(expected), rtol=1.0e-5, atol=1.0e-8)

def test_k(self):
rotate = RandRotate90(max_k=2)
for p in TEST_NDARRAYS:
rotate.set_random_state(234)
rotated = rotate(p(self.imt[0]))
expected = []
for channel in self.imt[0]:
expected.append(np.rot90(channel, 0, (0, 1)))
expected = [np.rot90(channel, 0, (0, 1)) for channel in self.imt[0]]
expected = np.stack(expected)
assert_allclose(rotated, expected, rtol=1.0e-5, atol=1.0e-8)
assert_allclose(rotated, p(expected), rtol=1.0e-5, atol=1.0e-8)

def test_spatial_axes(self):
rotate = RandRotate90(spatial_axes=(0, 1))
for p in TEST_NDARRAYS:
rotate.set_random_state(234)
rotated = rotate(p(self.imt[0]))
expected = []
for channel in self.imt[0]:
expected.append(np.rot90(channel, 0, (0, 1)))
expected = [np.rot90(channel, 0, (0, 1)) for channel in self.imt[0]]
expected = np.stack(expected)
assert_allclose(rotated, expected, rtol=1.0e-5, atol=1.0e-8)
assert_allclose(rotated, p(expected), rtol=1.0e-5, atol=1.0e-8)

def test_prob_k_spatial_axes(self):
rotate = RandRotate90(prob=1.0, max_k=2, spatial_axes=(0, 1))
for p in TEST_NDARRAYS:
rotate.set_random_state(234)
rotated = rotate(p(self.imt[0]))
expected = []
for channel in self.imt[0]:
expected.append(np.rot90(channel, 1, (0, 1)))
expected = [np.rot90(channel, 1, (0, 1)) for channel in self.imt[0]]
expected = np.stack(expected)
assert_allclose(rotated, expected, rtol=1.0e-5, atol=1.0e-8)
assert_allclose(rotated, p(expected), rtol=1.0e-5, atol=1.0e-8)


if __name__ == "__main__":
Expand Down
24 changes: 8 additions & 16 deletions tests/test_rand_rotate90d.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,47 +24,39 @@ def test_default(self):
for p in TEST_NDARRAYS:
rotate.set_random_state(123)
rotated = rotate({key: p(self.imt[0])})
expected = []
for channel in self.imt[0]:
expected.append(np.rot90(channel, 0, (0, 1)))
expected = [np.rot90(channel, 0, (0, 1)) for channel in self.imt[0]]
expected = np.stack(expected)
assert_allclose(rotated[key], expected)
assert_allclose(rotated[key], p(expected))

def test_k(self):
key = "test"
rotate = RandRotate90d(keys=key, max_k=2)
for p in TEST_NDARRAYS:
rotate.set_random_state(234)
rotated = rotate({key: p(self.imt[0])})
expected = []
for channel in self.imt[0]:
expected.append(np.rot90(channel, 0, (0, 1)))
expected = [np.rot90(channel, 0, (0, 1)) for channel in self.imt[0]]
expected = np.stack(expected)
assert_allclose(rotated[key], expected)
assert_allclose(rotated[key], p(expected))

def test_spatial_axes(self):
key = "test"
rotate = RandRotate90d(keys=key, spatial_axes=(0, 1))
for p in TEST_NDARRAYS:
rotate.set_random_state(234)
rotated = rotate({key: p(self.imt[0])})
expected = []
for channel in self.imt[0]:
expected.append(np.rot90(channel, 0, (0, 1)))
expected = [np.rot90(channel, 0, (0, 1)) for channel in self.imt[0]]
expected = np.stack(expected)
assert_allclose(rotated[key], expected)
assert_allclose(rotated[key], p(expected))

def test_prob_k_spatial_axes(self):
key = "test"
rotate = RandRotate90d(keys=key, prob=1.0, max_k=2, spatial_axes=(0, 1))
for p in TEST_NDARRAYS:
rotate.set_random_state(234)
rotated = rotate({key: p(self.imt[0])})
expected = []
for channel in self.imt[0]:
expected.append(np.rot90(channel, 1, (0, 1)))
expected = [np.rot90(channel, 1, (0, 1)) for channel in self.imt[0]]
expected = np.stack(expected)
assert_allclose(rotated[key], expected)
assert_allclose(rotated[key], p(expected))

def test_no_key(self):
key = "unknown"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_rand_scale_intensity.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_value(self):
result = scaler(p(self.imt))
np.random.seed(0)
expected = p((self.imt * (1 + np.random.uniform(low=-0.5, high=0.5))).astype(np.float32))
assert_allclose(result, expected, rtol=1e-7, atol=0)
assert_allclose(result, p(expected), rtol=1e-7, atol=0)


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions tests/test_rand_scale_intensityd.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@

class TestRandScaleIntensityd(NumpyImageTestCase2D):
def test_value(self):
key = "img"
for p in TEST_NDARRAYS:
key = "img"
scaler = RandScaleIntensityd(keys=[key], factors=0.5, prob=1.0)
scaler.set_random_state(seed=0)
result = scaler({key: p(self.imt)})
np.random.seed(0)
expected = (self.imt * (1 + np.random.uniform(low=-0.5, high=0.5))).astype(np.float32)
assert_allclose(result[key], expected)
assert_allclose(result[key], p(expected))


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion tests/test_scale_intensity_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_image_scale_intensity_range(self):
scaled = scaler(p(self.imt))
expected = (self.imt - 20) / 88
expected = expected * 30 + 50
assert_allclose(scaled, expected)
assert_allclose(scaled, p(expected))


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion tests/test_scale_intensity_ranged.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_image_scale_intensity_ranged(self):
scaled = scaler({key: p(self.imt)})
expected = (self.imt - 20) / 88
expected = expected * 30 + 50
assert_allclose(scaled[key], expected)
assert_allclose(scaled[key], p(expected))


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion tests/test_shift_intensityd.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_value(self):
shifter = ShiftIntensityd(keys=[key], offset=1.0)
result = shifter({key: p(self.imt)})
expected = self.imt + 1.0
assert_allclose(result[key], expected)
assert_allclose(result[key], p(expected))

def test_factor(self):
key = "img"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_threshold_intensity.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class TestThresholdIntensity(unittest.TestCase):
def test_value(self, in_type, input_param, expected_value):
test_data = in_type(np.arange(10))
result = ThresholdIntensity(**input_param)(test_data)
assert_allclose(result, expected_value)
assert_allclose(result, in_type(expected_value))


if __name__ == "__main__":
Expand Down
6 changes: 3 additions & 3 deletions tests/test_threshold_intensityd.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ class TestThresholdIntensityd(unittest.TestCase):
def test_value(self, in_type, input_param, expected_value):
test_data = {"image": in_type(np.arange(10)), "label": in_type(np.arange(10)), "extra": in_type(np.arange(10))}
result = ThresholdIntensityd(**input_param)(test_data)
assert_allclose(result["image"], expected_value)
assert_allclose(result["label"], expected_value)
assert_allclose(result["extra"], expected_value)
assert_allclose(result["image"], in_type(expected_value))
assert_allclose(result["label"], in_type(expected_value))
assert_allclose(result["extra"], in_type(expected_value))


if __name__ == "__main__":
Expand Down
6 changes: 3 additions & 3 deletions tests/test_to_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ class TestToTensor(unittest.TestCase):
def test_array_input(self, test_data, expected_shape):
result = ToTensor()(test_data)
self.assertTrue(isinstance(result, Tensor))
assert_allclose(result, test_data)
assert_allclose(result, test_data, type_test=False)
self.assertTupleEqual(result.shape, expected_shape)

@parameterized.expand(TESTS_SINGLE)
def test_single_input(self, test_data):
result = ToTensor()(test_data)
self.assertTrue(isinstance(result, Tensor))
assert_allclose(result, test_data)
assert_allclose(result, test_data, type_test=False)
self.assertEqual(result.ndim, 0)

@unittest.skipUnless(has_cp, "CuPy is required.")
Expand All @@ -53,7 +53,7 @@ def test_cupy(self):
cupy_array = cp.ascontiguousarray(cp.asarray(test_data))
result = ToTensor()(cupy_array)
self.assertTrue(isinstance(result, Tensor))
assert_allclose(result, test_data)
assert_allclose(result, test_data, type_test=False)


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion tests/test_transpose.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_transpose(self, im, indices):
if isinstance(im, torch.Tensor):
im = im.cpu().numpy()
out2 = np.transpose(im, indices)
assert_allclose(out1, out2)
assert_allclose(out1, out2, type_test=False)


if __name__ == "__main__":
Expand Down
6 changes: 3 additions & 3 deletions tests/test_transposed.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ def test_transpose(self, im, indices):
if isinstance(im, torch.Tensor):
im = im.cpu().numpy()
out_gt = np.transpose(im, indices)
assert_allclose(out_im1, out_gt)
assert_allclose(out_im2, out_gt)
assert_allclose(out_im1, out_gt, type_test=False)
assert_allclose(out_im2, out_gt, type_test=False)

# test inverse
fwd_inv_data = tr.inverse(out_data)
for i, j in zip(data.values(), fwd_inv_data.values()):
assert_allclose(i, j)
assert_allclose(i, j, type_test=False)


if __name__ == "__main__":
Expand Down

0 comments on commit 8e41d48

Please sign in to comment.