From adbbbb057d243cb4e51e4a6810ba6b35878a8a61 Mon Sep 17 00:00:00 2001 From: KumoLiu Date: Sun, 2 Jul 2023 22:27:44 +0800 Subject: [PATCH 1/6] fix #6685 Signed-off-by: KumoLiu --- monai/transforms/croppad/array.py | 6 +++--- monai/transforms/croppad/dictionary.py | 6 +++--- monai/transforms/utils.py | 2 +- tests/test_crop_foreground.py | 4 ++-- tests/test_crop_foregroundd.py | 4 ++-- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/monai/transforms/croppad/array.py b/monai/transforms/croppad/array.py index 7c8d7b6a19..2621982372 100644 --- a/monai/transforms/croppad/array.py +++ b/monai/transforms/croppad/array.py @@ -824,7 +824,7 @@ def __init__( select_fn: Callable = is_positive, channel_indices: IndexSelection | None = None, margin: Sequence[int] | int = 0, - allow_smaller: bool = True, + allow_smaller: bool = False, return_coords: bool = False, k_divisible: Sequence[int] | int = 1, mode: str = PytorchPadMode.CONSTANT, @@ -838,8 +838,8 @@ def __init__( of image. if None, select foreground on the whole image. margin: add margin value to spatial dims of the bounding box, if only 1 value provided, use it for all dims. allow_smaller: when computing box size with `margin`, whether allow the image size to be smaller - than box size, default to `True`. if the margined size is larger than image size, will pad with - specified `mode`. + than box size, default to `False`. If `True`, the output will pad with specified `mode` + when the margined size is larger than image size. return_coords: whether return the coordinates of spatial bounding box for foreground. k_divisible: make each spatial dimension to be divisible by k, default to 1. if `k_divisible` is an int, the same `k` be applied to all the input spatial dimensions. diff --git a/monai/transforms/croppad/dictionary.py b/monai/transforms/croppad/dictionary.py index 6dc8f10c32..5c2b4b9278 100644 --- a/monai/transforms/croppad/dictionary.py +++ b/monai/transforms/croppad/dictionary.py @@ -729,7 +729,7 @@ def __init__( select_fn: Callable = is_positive, channel_indices: IndexSelection | None = None, margin: Sequence[int] | int = 0, - allow_smaller: bool = True, + allow_smaller: bool = False, k_divisible: Sequence[int] | int = 1, mode: SequenceStr = PytorchPadMode.CONSTANT, start_coord_key: str = "foreground_start_coord", @@ -748,8 +748,8 @@ def __init__( of image. if None, select foreground on the whole image. margin: add margin value to spatial dims of the bounding box, if only 1 value provided, use it for all dims. allow_smaller: when computing box size with `margin`, whether allow the image size to be smaller - than box size, default to `True`. if the margined size is larger than image size, will pad with - specified `mode`. + than box size, default to `False`. If `True`, the output will pad with specified `mode` + when the margined size is larger than image size. k_divisible: make each spatial dimension to be divisible by k, default to 1. if `k_divisible` is an int, the same `k` be applied to all the input spatial dimensions. mode: available modes for numpy array:{``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``, diff --git a/monai/transforms/utils.py b/monai/transforms/utils.py index c01f44635b..3283f0fc12 100644 --- a/monai/transforms/utils.py +++ b/monai/transforms/utils.py @@ -998,7 +998,7 @@ def generate_spatial_bounding_box( arg_max = where(dt == dt.max())[0] min_d = arg_max[0] - margin[di] max_d = arg_max[-1] + margin[di] + 1 - if allow_smaller: + if not allow_smaller: min_d = max(min_d, 0) max_d = min(max_d, spatial_size[di]) diff --git a/tests/test_crop_foreground.py b/tests/test_crop_foreground.py index 4435b128ba..fdc4ac0488 100644 --- a/tests/test_crop_foreground.py +++ b/tests/test_crop_foreground.py @@ -63,7 +63,7 @@ TESTS.append( [ - {"select_fn": lambda x: x > 0, "channel_indices": None, "margin": [2, 1], "allow_smaller": True}, + {"select_fn": lambda x: x > 0, "channel_indices": None, "margin": [2, 1], "allow_smaller": False}, p([[[0, 0, 0, 0, 0], [0, 1, 2, 1, 0], [0, 2, 3, 2, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]]), p([[[0, 0, 0, 0, 0], [0, 1, 2, 1, 0], [0, 2, 3, 2, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]]), True, @@ -72,7 +72,7 @@ TESTS.append( [ - {"select_fn": lambda x: x > 0, "channel_indices": None, "margin": [2, 1], "allow_smaller": False}, + {"select_fn": lambda x: x > 0, "channel_indices": None, "margin": [2, 1], "allow_smaller": True}, p([[[0, 0, 0, 0, 0], [0, 1, 2, 1, 0], [0, 2, 3, 2, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]]), p([[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 1, 2, 1, 0], [0, 2, 3, 2, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]]), True, diff --git a/tests/test_crop_foregroundd.py b/tests/test_crop_foregroundd.py index 776776f6c5..6f5910e64c 100644 --- a/tests/test_crop_foregroundd.py +++ b/tests/test_crop_foregroundd.py @@ -88,7 +88,7 @@ "select_fn": lambda x: x > 0, "channel_indices": None, "margin": [2, 1], - "allow_smaller": True, + "allow_smaller": False, }, { "img": p( @@ -107,7 +107,7 @@ "select_fn": lambda x: x > 0, "channel_indices": None, "margin": [2, 1], - "allow_smaller": False, + "allow_smaller": True, }, { "img": p( From 3b016178a8e37c76de1b69bff79c2b7f32e5df27 Mon Sep 17 00:00:00 2001 From: KumoLiu Date: Sun, 2 Jul 2023 22:40:50 +0800 Subject: [PATCH 2/6] minor fix Signed-off-by: KumoLiu --- monai/transforms/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/transforms/utils.py b/monai/transforms/utils.py index 3283f0fc12..b89a676a32 100644 --- a/monai/transforms/utils.py +++ b/monai/transforms/utils.py @@ -961,7 +961,7 @@ def generate_spatial_bounding_box( [1st_spatial_dim_start, 2nd_spatial_dim_start, ..., Nth_spatial_dim_start], [1st_spatial_dim_end, 2nd_spatial_dim_end, ..., Nth_spatial_dim_end] - If `allow_smaller`, the bounding boxes edges are aligned with the input image edges. + If not `allow_smaller`, the bounding boxes edges are aligned with the input image edges. This function returns [0, 0, ...], [0, 0, ...] if there's no positive intensity. Args: From d372325f2c6c1f733fbd7f10182667cc38bc506c Mon Sep 17 00:00:00 2001 From: KumoLiu Date: Sun, 2 Jul 2023 22:56:21 +0800 Subject: [PATCH 3/6] fix `TestGenerateSpatialBoundingBox` Signed-off-by: KumoLiu --- monai/transforms/utils.py | 4 ++-- tests/test_generate_spatial_bounding_box.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/monai/transforms/utils.py b/monai/transforms/utils.py index b89a676a32..cc9ebd686f 100644 --- a/monai/transforms/utils.py +++ b/monai/transforms/utils.py @@ -950,7 +950,7 @@ def generate_spatial_bounding_box( select_fn: Callable = is_positive, channel_indices: IndexSelection | None = None, margin: Sequence[int] | int = 0, - allow_smaller: bool = True, + allow_smaller: bool = False, ) -> tuple[list[int], list[int]]: """ Generate the spatial bounding box of foreground in the image with start-end positions (inclusive). @@ -971,7 +971,7 @@ def generate_spatial_bounding_box( of image. if None, select foreground on the whole image. margin: add margin value to spatial dims of the bounding box, if only 1 value provided, use it for all dims. allow_smaller: when computing box size with `margin`, whether allow the image size to be smaller - than box size, default to `True`. + than box size, default to `False`. """ check_non_lazy_pending_ops(img, name="generate_spatial_bounding_box") spatial_size = img.shape[1:] diff --git a/tests/test_generate_spatial_bounding_box.py b/tests/test_generate_spatial_bounding_box.py index a67e7d0175..36b7d3c812 100644 --- a/tests/test_generate_spatial_bounding_box.py +++ b/tests/test_generate_spatial_bounding_box.py @@ -82,7 +82,7 @@ "select_fn": lambda x: x > 0, "channel_indices": None, "margin": [2, 1], - "allow_smaller": False, + "allow_smaller": True, }, ([-1, 0], [6, 5]), ] @@ -96,7 +96,7 @@ "select_fn": lambda x: x > 0, "channel_indices": None, "margin": [2, 1], - "allow_smaller": True, + "allow_smaller": False, }, ([0, 0], [5, 5]), ] From d7d4dc0025fecf7bb14a79972b96451889f8d88a Mon Sep 17 00:00:00 2001 From: YunLiu <55491388+KumoLiu@users.noreply.github.com> Date: Mon, 3 Jul 2023 10:43:36 +0800 Subject: [PATCH 4/6] Update monai/transforms/croppad/array.py Co-authored-by: Wenqi Li <831580+wyli@users.noreply.github.com> Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com> --- monai/transforms/croppad/array.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/transforms/croppad/array.py b/monai/transforms/croppad/array.py index 2621982372..365efb1129 100644 --- a/monai/transforms/croppad/array.py +++ b/monai/transforms/croppad/array.py @@ -837,7 +837,7 @@ def __init__( channel_indices: if defined, select foreground only on the specified channels of image. if None, select foreground on the whole image. margin: add margin value to spatial dims of the bounding box, if only 1 value provided, use it for all dims. - allow_smaller: when computing box size with `margin`, whether allow the image size to be smaller + allow_smaller: when computing box size with `margin`, whether to allow the final box edges to be outside of the image edges (the image is smaller than the box). If `False`, part of a padded output box might be outside of the original image, if `True`, the image edges will be used as the box edges. than box size, default to `False`. If `True`, the output will pad with specified `mode` when the margined size is larger than image size. return_coords: whether return the coordinates of spatial bounding box for foreground. From 5eedb0f37bdb89562955a6f01b66b30a9984b70e Mon Sep 17 00:00:00 2001 From: KumoLiu Date: Mon, 3 Jul 2023 10:51:37 +0800 Subject: [PATCH 5/6] revert d7d4dc0 Signed-off-by: KumoLiu --- monai/transforms/croppad/array.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/transforms/croppad/array.py b/monai/transforms/croppad/array.py index 365efb1129..2621982372 100644 --- a/monai/transforms/croppad/array.py +++ b/monai/transforms/croppad/array.py @@ -837,7 +837,7 @@ def __init__( channel_indices: if defined, select foreground only on the specified channels of image. if None, select foreground on the whole image. margin: add margin value to spatial dims of the bounding box, if only 1 value provided, use it for all dims. - allow_smaller: when computing box size with `margin`, whether to allow the final box edges to be outside of the image edges (the image is smaller than the box). If `False`, part of a padded output box might be outside of the original image, if `True`, the image edges will be used as the box edges. + allow_smaller: when computing box size with `margin`, whether allow the image size to be smaller than box size, default to `False`. If `True`, the output will pad with specified `mode` when the margined size is larger than image size. return_coords: whether return the coordinates of spatial bounding box for foreground. From 7ba9f2b0b83e7fa88366c2726ab83d47b97d827c Mon Sep 17 00:00:00 2001 From: KumoLiu Date: Mon, 3 Jul 2023 14:44:59 +0800 Subject: [PATCH 6/6] update docstring Signed-off-by: KumoLiu --- monai/transforms/croppad/array.py | 6 +++--- monai/transforms/croppad/dictionary.py | 6 +++--- monai/transforms/utils.py | 7 ++++--- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/monai/transforms/croppad/array.py b/monai/transforms/croppad/array.py index 2621982372..06650a28b8 100644 --- a/monai/transforms/croppad/array.py +++ b/monai/transforms/croppad/array.py @@ -837,9 +837,9 @@ def __init__( channel_indices: if defined, select foreground only on the specified channels of image. if None, select foreground on the whole image. margin: add margin value to spatial dims of the bounding box, if only 1 value provided, use it for all dims. - allow_smaller: when computing box size with `margin`, whether allow the image size to be smaller - than box size, default to `False`. If `True`, the output will pad with specified `mode` - when the margined size is larger than image size. + allow_smaller: when computing box size with `margin`, whether to allow the final box edges to be outside of + the image edges (the image is smaller than the box). If `True`, part of a padded output box might be outside + of the original image, if `False`, the image edges will be used as the box edges. return_coords: whether return the coordinates of spatial bounding box for foreground. k_divisible: make each spatial dimension to be divisible by k, default to 1. if `k_divisible` is an int, the same `k` be applied to all the input spatial dimensions. diff --git a/monai/transforms/croppad/dictionary.py b/monai/transforms/croppad/dictionary.py index 5c2b4b9278..6b908dda8c 100644 --- a/monai/transforms/croppad/dictionary.py +++ b/monai/transforms/croppad/dictionary.py @@ -747,9 +747,9 @@ def __init__( channel_indices: if defined, select foreground only on the specified channels of image. if None, select foreground on the whole image. margin: add margin value to spatial dims of the bounding box, if only 1 value provided, use it for all dims. - allow_smaller: when computing box size with `margin`, whether allow the image size to be smaller - than box size, default to `False`. If `True`, the output will pad with specified `mode` - when the margined size is larger than image size. + allow_smaller: when computing box size with `margin`, whether to allow the final box edges to be outside of + the image edges (the image is smaller than the box). If `True`, part of a padded output box might be outside + of the original image, if `False`, the image edges will be used as the box edges. k_divisible: make each spatial dimension to be divisible by k, default to 1. if `k_divisible` is an int, the same `k` be applied to all the input spatial dimensions. mode: available modes for numpy array:{``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``, diff --git a/monai/transforms/utils.py b/monai/transforms/utils.py index cc9ebd686f..9b24d7038a 100644 --- a/monai/transforms/utils.py +++ b/monai/transforms/utils.py @@ -961,7 +961,6 @@ def generate_spatial_bounding_box( [1st_spatial_dim_start, 2nd_spatial_dim_start, ..., Nth_spatial_dim_start], [1st_spatial_dim_end, 2nd_spatial_dim_end, ..., Nth_spatial_dim_end] - If not `allow_smaller`, the bounding boxes edges are aligned with the input image edges. This function returns [0, 0, ...], [0, 0, ...] if there's no positive intensity. Args: @@ -970,8 +969,10 @@ def generate_spatial_bounding_box( channel_indices: if defined, select foreground only on the specified channels of image. if None, select foreground on the whole image. margin: add margin value to spatial dims of the bounding box, if only 1 value provided, use it for all dims. - allow_smaller: when computing box size with `margin`, whether allow the image size to be smaller - than box size, default to `False`. + allow_smaller: when computing box size with `margin`, whether to allow the final box edges to be outside of + the image edges (the image is smaller than the box). If `False`, the bounding boxes edges are aligned + with the input image edges, default to `False`. + """ check_non_lazy_pending_ops(img, name="generate_spatial_bounding_box") spatial_size = img.shape[1:]