From db4a70f2c4d443392c0903f099905e5072d437ee Mon Sep 17 00:00:00 2001 From: Nic Ma Date: Fri, 27 May 2022 01:24:29 +0800 Subject: [PATCH] [DLMED] improve doc and tests Signed-off-by: Nic Ma --- monai/transforms/croppad/array.py | 4 ++++ monai/transforms/croppad/dictionary.py | 4 ++++ tests/test_rand_crop_by_pos_neg_labeld.py | 7 +++++++ 3 files changed, 15 insertions(+) diff --git a/monai/transforms/croppad/array.py b/monai/transforms/croppad/array.py index 330c5124ee..1245daa62c 100644 --- a/monai/transforms/croppad/array.py +++ b/monai/transforms/croppad/array.py @@ -851,6 +851,8 @@ class RandCropByPosNegLabel(Randomizable, Transform): If a dimension of the expected spatial size is bigger than the input image size, will not crop that dimension. So the cropped result may be smaller than expected size, and the cropped results of several images may not have exactly same shape. + And if the crop ROI is partly out of the image, will automatically adjust the crop center to ensure the + valid crop ROI. Args: spatial_size: the spatial size of the crop region e.g. [224, 224, 128]. @@ -1021,6 +1023,8 @@ class RandCropByLabelClasses(Randomizable, Transform): If a dimension of the expected spatial size is bigger than the input image size, will not crop that dimension. So the cropped result may be smaller than expected size, and the cropped results of several images may not have exactly same shape. + And if the crop ROI is partly out of the image, will automatically adjust the crop center to ensure the + valid crop ROI. Args: spatial_size: the spatial size of the crop region e.g. [224, 224, 128]. diff --git a/monai/transforms/croppad/dictionary.py b/monai/transforms/croppad/dictionary.py index d136340669..57b257b428 100644 --- a/monai/transforms/croppad/dictionary.py +++ b/monai/transforms/croppad/dictionary.py @@ -1051,6 +1051,8 @@ class RandCropByPosNegLabeld(Randomizable, MapTransform, InvertibleTransform): If a dimension of the expected spatial size is bigger than the input image size, will not crop that dimension. So the cropped result may be smaller than the expected size, and the cropped results of several images may not have exactly the same shape. + And if the crop ROI is partly out of the image, will automatically adjust the crop center + to ensure the valid crop ROI. Args: keys: keys of the corresponding items to be transformed. @@ -1259,6 +1261,8 @@ class RandCropByLabelClassesd(Randomizable, MapTransform, InvertibleTransform): If a dimension of the expected spatial size is bigger than the input image size, will not crop that dimension. So the cropped result may be smaller than expected size, and the cropped results of several images may not have exactly same shape. + And if the crop ROI is partly out of the image, will automatically adjust the crop center to ensure the + valid crop ROI. Args: keys: keys of the corresponding items to be transformed. diff --git a/tests/test_rand_crop_by_pos_neg_labeld.py b/tests/test_rand_crop_by_pos_neg_labeld.py index 5a1eacfa93..a2808bd65d 100644 --- a/tests/test_rand_crop_by_pos_neg_labeld.py +++ b/tests/test_rand_crop_by_pos_neg_labeld.py @@ -148,6 +148,13 @@ def test_type_shape(self, input_param, input_data, expected_shape): for i, item in enumerate(result): self.assertEqual(item[PostFix.meta(k)]["patch_index"], i) + def test_correct_center(self): + cropper = RandCropByPosNegLabeld(keys="label", label_key="label", spatial_size=[3, 3]) + cropper.set_random_state(0) + test_image = {"label": np.asarray([[[1, 0, 0, 1], [0, 0, 0, 0], [0, 0, 0, 0], [1, 0, 0, 1]]])} + result = cropper(test_image) + np.testing.assert_allclose(result[0]["label"], np.asarray([[[0, 0, 1], [0, 0, 0], [0, 0, 0]]])) + if __name__ == "__main__": unittest.main()