Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the use of allow_smaller in CropForeground #6686

Merged
merged 8 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions monai/transforms/croppad/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 `True`. if the margined size is larger than image size, will pad with
specified `mode`.
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.
Expand Down
8 changes: 4 additions & 4 deletions monai/transforms/croppad/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 `True`. if the margined size is larger than image size, will pad with
specified `mode`.
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"``,
Expand Down
11 changes: 6 additions & 5 deletions monai/transforms/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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 `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:
Expand All @@ -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 `True`.
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:]
Expand All @@ -998,7 +999,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])

Expand Down
4 changes: 2 additions & 2 deletions tests/test_crop_foreground.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions tests/test_crop_foregroundd.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
"select_fn": lambda x: x > 0,
"channel_indices": None,
"margin": [2, 1],
"allow_smaller": True,
"allow_smaller": False,
},
{
"img": p(
Expand All @@ -107,7 +107,7 @@
"select_fn": lambda x: x > 0,
"channel_indices": None,
"margin": [2, 1],
"allow_smaller": False,
"allow_smaller": True,
},
{
"img": p(
Expand Down
4 changes: 2 additions & 2 deletions tests/test_generate_spatial_bounding_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
]
Expand All @@ -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]),
]
Expand Down