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>

 update

Signed-off-by: Wenqi Li <wenqil@nvidia.com>
  • Loading branch information
wyli committed Sep 15, 2021
1 parent 0f38dea commit d7fb56a
Show file tree
Hide file tree
Showing 19 changed files with 59 additions and 91 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
4 changes: 2 additions & 2 deletions tests/test_rand_shift_intensityd.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@

class TestRandShiftIntensityd(NumpyImageTestCase2D):
def test_value(self):
key = "img"
for p in TEST_NDARRAYS:
key = "img"
shifter = RandShiftIntensityd(keys=[key], offsets=1.0, prob=1.0)
shifter.set_random_state(seed=0)
result = shifter({key: p(self.imt)})
np.random.seed(0)
expected = self.imt + np.random.uniform(low=-1.0, high=1.0)
assert_allclose(result[key], expected)
assert_allclose(result[key], p(expected))

def test_factor(self):
key = "img"
Expand Down
24 changes: 8 additions & 16 deletions tests/test_rotate90.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,33 @@ def test_rotate90_default(self):
rotate = Rotate90()
for p in TEST_NDARRAYS:
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)

def test_k(self):
rotate = Rotate90(k=2)
for p in TEST_NDARRAYS:
rotated = rotate(p(self.imt[0]))
expected = []
for channel in self.imt[0]:
expected.append(np.rot90(channel, 2, (0, 1)))
expected = [np.rot90(channel, 2, (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 = Rotate90(spatial_axes=(0, -1))
for p in TEST_NDARRAYS:
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)

def test_prob_k_spatial_axes(self):
rotate = Rotate90(k=2, spatial_axes=(0, 1))
for p in TEST_NDARRAYS:
rotated = rotate(p(self.imt[0]))
expected = []
for channel in self.imt[0]:
expected.append(np.rot90(channel, 2, (0, 1)))
expected = [np.rot90(channel, 2, (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_rotate90d.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,44 +23,36 @@ def test_rotate90_default(self):
rotate = Rotate90d(keys=key)
for p in TEST_NDARRAYS:
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_k(self):
key = None
rotate = Rotate90d(keys=key, k=2)
for p in TEST_NDARRAYS:
rotated = rotate({key: p(self.imt[0])})
expected = []
for channel in self.imt[0]:
expected.append(np.rot90(channel, 2, (0, 1)))
expected = [np.rot90(channel, 2, (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 = Rotate90d(keys=key, spatial_axes=(0, 1))
for p in TEST_NDARRAYS:
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_prob_k_spatial_axes(self):
key = "test"
rotate = Rotate90d(keys=key, k=2, spatial_axes=(0, 1))
for p in TEST_NDARRAYS:
rotated = rotate({key: p(self.imt[0])})
expected = []
for channel in self.imt[0]:
expected.append(np.rot90(channel, 2, (0, 1)))
expected = [np.rot90(channel, 2, (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
4 changes: 2 additions & 2 deletions tests/test_scale_intensity.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ def test_range_scale(self):
maxa = self.imt.max()
norm = (self.imt - mina) / (maxa - mina)
expected = p((norm * (2.0 - 1.0)) + 1.0)
assert_allclose(result, expected, rtol=1e-7, atol=0)
assert_allclose(result, expected, type_test=False, rtol=1e-7, atol=0)

def test_factor_scale(self):
for p in TEST_NDARRAYS:
scaler = ScaleIntensity(minv=None, maxv=None, factor=0.1)
result = scaler(p(self.imt))
expected = p((self.imt * (1 + 0.1)).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
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
8 changes: 4 additions & 4 deletions tests/test_scale_intensityd.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@

class TestScaleIntensityd(NumpyImageTestCase2D):
def test_range_scale(self):
key = "img"
for p in TEST_NDARRAYS:
key = "img"
scaler = ScaleIntensityd(keys=[key], minv=1.0, maxv=2.0)
result = scaler({key: p(self.imt)})
mina = np.min(self.imt)
maxa = np.max(self.imt)
norm = (self.imt - mina) / (maxa - mina)
expected = (norm * (2.0 - 1.0)) + 1.0
assert_allclose(result[key], expected)
assert_allclose(result[key], p(expected))

def test_factor_scale(self):
key = "img"
for p in TEST_NDARRAYS:
key = "img"
scaler = ScaleIntensityd(keys=[key], minv=None, maxv=None, factor=0.1)
result = scaler({key: p(self.imt)})
expected = (self.imt * (1 + 0.1)).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_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
Loading

0 comments on commit d7fb56a

Please sign in to comment.