def test_torch(self, init_param, img: torch.Tensor, track_meta: bool, device): set_track_meta(track_meta) img = img.to(device) xform = Flipd("image", init_param) res = xform({"image": img}) self.assertEqual(img.shape, res["image"].shape) if track_meta: self.assertIsInstance(res["image"], MetaTensor) else: self.assertNotIsInstance(res["image"], MetaTensor) self.assertIsInstance(res["image"], torch.Tensor) with self.assertRaisesRegex(ValueError, "MetaTensor"): xform.inverse(res)
def test_correct_results(self, _, spatial_axis): for p in TEST_NDARRAYS: flip = Flipd(keys="img", spatial_axis=spatial_axis) expected = [np.flip(channel, spatial_axis) for channel in self.imt[0]] expected = np.stack(expected) result = flip({"img": p(self.imt[0])})["img"] assert_allclose(result, p(expected))
def test_tranform_dict(self, input): transforms = Compose([ Range("random flip dict")(Flipd(keys="image")), Range()(ToTensord("image")) ]) # Apply transforms output = transforms(input)["image"] # Decorate with NVTX Range transforms1 = Range()(transforms) transforms2 = Range("Transforms2")(transforms) transforms3 = Range(name="Transforms3", methods="__call__")(transforms) # Apply transforms with Range output1 = transforms1(input)["image"] output2 = transforms2(input)["image"] output3 = transforms3(input)["image"] # Check the outputs self.assertIsInstance(output, torch.Tensor) self.assertIsInstance(output1, torch.Tensor) self.assertIsInstance(output2, torch.Tensor) self.assertIsInstance(output3, torch.Tensor) np.testing.assert_equal(output.numpy(), output1.numpy()) np.testing.assert_equal(output.numpy(), output2.numpy()) np.testing.assert_equal(output.numpy(), output3.numpy())
def test_correct_results(self, _, spatial_axis): flip = Flipd(keys="img", spatial_axis=spatial_axis) expected = list() for channel in self.imt[0]: expected.append(np.flip(channel, spatial_axis)) expected = np.stack(expected) res = flip({"img": self.imt[0]}) assert np.allclose(expected, res["img"])
def test_correct_results(self, _, spatial_axis): for p in TEST_NDARRAYS_ALL: flip = Flipd(keys="img", spatial_axis=spatial_axis) expected = [np.flip(channel, spatial_axis) for channel in self.imt[0]] expected = np.stack(expected) im = p(self.imt[0]) result = flip({"img": im})["img"] assert_allclose(result, p(expected), type_test="tensor") test_local_inversion(flip, {"img": result}, {"img": im}, "img")
)) TESTS.append(("CropForegroundd 2d", "2D", 0, CropForegroundd(KEYS, source_key="label", margin=2))) TESTS.append( ("CropForegroundd 3d", "3D", 0, CropForegroundd(KEYS, source_key="label"))) TESTS.append(("ResizeWithPadOrCropd 3d", "3D", 0, ResizeWithPadOrCropd(KEYS, [201, 150, 105]))) TESTS.append(( "Flipd 3d", "3D", 0, Flipd(KEYS, [1, 2]), )) TESTS.append(( "Flipd 3d", "3D", 0, Flipd(KEYS, [1, 2]), )) TESTS.append(( "RandFlipd 3d", "3D", 0, RandFlipd(KEYS, 1, [1, 2]), ))
def test_invalid_cases(self, _, spatial_axis, raises): with self.assertRaises(raises): flip = Flipd(keys="img", spatial_axis=spatial_axis) flip({"img": self.imt[0]})
def test_meta_dict(self): xform = Flipd("image", [0, 1]) res = xform({"image": torch.zeros(1, 3, 4)}) self.assertTrue( res["image"].applied_operations == res["image_transforms"])
TESTS.append(("CenterSpatialCropd 3d", "3D", 0, True, CenterSpatialCropd(KEYS, roi_size=[95, 97, 98]))) TESTS.append(("CropForegroundd 2d", "2D", 0, True, CropForegroundd(KEYS, source_key="label", margin=2))) TESTS.append(("CropForegroundd 3d", "3D", 0, True, CropForegroundd(KEYS, source_key="label", k_divisible=[5, 101, 2]))) TESTS.append(("ResizeWithPadOrCropd 3d", "3D", 0, True, ResizeWithPadOrCropd(KEYS, [201, 150, 105]))) TESTS.append(("Flipd 3d", "3D", 0, True, Flipd(KEYS, [1, 2]))) TESTS.append(("Flipd 3d", "3D", 0, True, Flipd(KEYS, [1, 2]))) TESTS.append(("RandFlipd 3d", "3D", 0, True, RandFlipd(KEYS, 1, [1, 2]))) TESTS.append(("RandAxisFlipd 3d", "3D", 0, True, RandAxisFlipd(KEYS, 1))) TESTS.append(("RandAxisFlipd 3d", "3D", 0, True, RandAxisFlipd(KEYS, 1))) for acc in [True, False]: TESTS.append(("Orientationd 3d", "3D", 0, True, Orientationd(KEYS, "RAS", as_closest_canonical=acc))) TESTS.append(("Rotate90d 2d", "2D", 0, True, Rotate90d(KEYS))) TESTS.append( ("Rotate90d 3d", "3D", 0, True, Rotate90d(KEYS, k=2, spatial_axes=(1, 2))))