def test_construct_with_pre_applied_transforms(self): key = "im" _, im = self.get_im() tr = Compose([BorderPadd(key, 1), DivisiblePadd(key, 16)]) data = tr({key: im}) m = MetaTensor(im, applied_operations=data["im"].applied_operations) self.assertEqual(len(m.applied_operations), len(tr.transforms))
def test_transforms(self): key = "im" _, im = self.get_im() tr = Compose([ToMetaTensord(key), BorderPadd(key, 1), DivisiblePadd(key, 16), FromMetaTensord(key)]) num_tr = len(tr.transforms) data = {key: im, PostFix.meta(key): {"affine": torch.eye(4)}} # apply one at a time for i, _tr in enumerate(tr.transforms): data = _tr(data) is_meta = isinstance(_tr, (ToMetaTensord, BorderPadd, DivisiblePadd)) if is_meta: self.assertEqual(len(data), 1 if not config.USE_META_DICT else 2) # im, im_transforms, compatibility self.assertIsInstance(data[key], MetaTensor) n_applied = len(data[key].applied_operations) else: self.assertEqual(len(data), 3) # im, im_meta_dict, im_transforms self.assertIsInstance(data[key], torch.Tensor) self.assertNotIsInstance(data[key], MetaTensor) n_applied = len(data[PostFix.transforms(key)]) self.assertEqual(n_applied, i + 1) # inverse one at a time for i, _tr in enumerate(tr.transforms[::-1]): data = _tr.inverse(data) is_meta = isinstance(_tr, (FromMetaTensord, BorderPadd, DivisiblePadd)) if is_meta: self.assertEqual(len(data), 1) # im self.assertIsInstance(data[key], MetaTensor) n_applied = len(data[key].applied_operations) else: self.assertEqual(len(data), 3) # im, im_meta_dict, im_transforms self.assertIsInstance(data[key], torch.Tensor) self.assertNotIsInstance(data[key], MetaTensor) n_applied = len(data[PostFix.transforms(key)]) self.assertEqual(n_applied, num_tr - i - 1) # apply all in one go data = tr({key: im, PostFix.meta(key): {"affine": torch.eye(4)}}) self.assertEqual(len(data), 3) # im, im_meta_dict, im_transforms self.assertIsInstance(data[key], torch.Tensor) self.assertNotIsInstance(data[key], MetaTensor) n_applied = len(data[PostFix.transforms(key)]) self.assertEqual(n_applied, num_tr) # inverse all in one go data = tr.inverse(data) self.assertEqual(len(data), 3) # im, im_meta_dict, im_transforms self.assertIsInstance(data[key], torch.Tensor) self.assertNotIsInstance(data[key], MetaTensor) n_applied = len(data[PostFix.transforms(key)]) self.assertEqual(n_applied, 0)
"3D", 0, SpatialCropd(KEYS, [49, 51, 44], [90, 89, 93]), )) TESTS.append(("RandSpatialCropd 2d", "2D", 0, RandSpatialCropd(KEYS, [96, 93], True, False))) TESTS.append(("RandSpatialCropd 3d", "3D", 0, RandSpatialCropd(KEYS, [96, 93, 92], False, False))) TESTS.append(( "BorderPadd 2d", "2D", 0, BorderPadd(KEYS, [3, 7, 2, 5]), )) TESTS.append(( "BorderPadd 2d", "2D", 0, BorderPadd(KEYS, [3, 7]), )) TESTS.append(( "BorderPadd 3d", "3D", 0, BorderPadd(KEYS, [4]), ))
def test_pad_shape(self, input_param, input_data, expected_val): padder = BorderPadd(**input_param) result = padder(input_data) self.assertAlmostEqual(result["img"].shape, expected_val.shape)
)) TESTS.append( ("SpatialCropd 2d", "2D", 0, True, SpatialCropd(KEYS, [49, 51], [390, 89]))) TESTS.append(("SpatialCropd 3d", "3D", 0, True, SpatialCropd(KEYS, [49, 51, 44], [90, 89, 93]))) TESTS.append(("RandSpatialCropd 2d", "2D", 0, True, RandSpatialCropd(KEYS, [96, 93], None, True, False))) TESTS.append(("RandSpatialCropd 3d", "3D", 0, True, RandSpatialCropd(KEYS, [96, 93, 92], None, False, False))) TESTS.append(("BorderPadd 2d", "2D", 0, True, BorderPadd(KEYS, [3, 7, 2, 5]))) TESTS.append(("BorderPadd 2d", "2D", 0, True, BorderPadd(KEYS, [3, 7]))) TESTS.append(("BorderPadd 3d", "3D", 0, True, BorderPadd(KEYS, [4]))) TESTS.append(("DivisiblePadd 2d", "2D", 0, True, DivisiblePadd(KEYS, k=4))) TESTS.append( ("DivisiblePadd 3d", "3D", 0, True, DivisiblePadd(KEYS, k=[4, 8, 11]))) TESTS.append(("CenterSpatialCropd 2d", "2D", 0, True, CenterSpatialCropd(KEYS, roi_size=95))) TESTS.append(("CenterSpatialCropd 3d", "3D", 0, True, CenterSpatialCropd(KEYS, roi_size=[95, 97, 98])))