Skip to content

Commit

Permalink
Revert #6686 (#6736)
Browse files Browse the repository at this point in the history
Fixes #6685.

- Revert #6686 without changing the meaning of `allow_smaller` in
`generate_spatial_bounding_box`.
- Update the docstring of the `allow_smaller` to make it more clear.
- Add `deprecated_arg_default`, then after 1.3, `CropForeground` with
pad by default even if the image edges are smaller than the final box
edges.


### Types of changes
<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
- [ ] Breaking change (fix or new feature that would cause existing
functionality to change).
- [ ] New tests added to cover the changes.
- [ ] Integration tests passed locally by running `./runtests.sh -f -u
--net --coverage`.
- [ ] Quick tests passed locally by running `./runtests.sh --quick
--unittests --disttests`.
- [ ] In-line docstrings updated.
- [ ] Documentation updated, tested `make html` command in the `docs/`
folder.

---------

Signed-off-by: KumoLiu <yunl@nvidia.com>
  • Loading branch information
KumoLiu authored Jul 18, 2023
1 parent a5dff5b commit 38d73e8
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 20 deletions.
2 changes: 1 addition & 1 deletion monai/metrics/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def get_mask_edges(
else: # pytorch subvoxel, maybe on gpu, but croppad boolean values on GPU is not supported
seg_pred, seg_gt, or_vol = convert_to_tensor(channel_first, dtype=torch.float16)
cropper = CropForegroundD(
["pred", "gt"], source_key="src", margin=1, allow_smaller=True, start_coord_key=None, end_coord_key=None
["pred", "gt"], source_key="src", margin=1, allow_smaller=False, start_coord_key=None, end_coord_key=None
)
cropped = cropper({"pred": seg_pred, "gt": seg_gt, "src": or_vol}) # type: ignore
seg_pred, seg_gt = cropped["pred"][0], cropped["gt"][0]
Expand Down
9 changes: 5 additions & 4 deletions monai/transforms/croppad/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,12 +819,13 @@ def threshold_at_one(x):
"""

@deprecated_arg_default("allow_smaller", old_default=True, new_default=False, since="1.2", replaced="1.3")
def __init__(
self,
select_fn: Callable = is_positive,
channel_indices: IndexSelection | None = None,
margin: Sequence[int] | int = 0,
allow_smaller: bool = False,
allow_smaller: bool = True,
return_coords: bool = False,
k_divisible: Sequence[int] | int = 1,
mode: str = PytorchPadMode.CONSTANT,
Expand All @@ -837,9 +838,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 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.
allow_smaller: when computing box size with `margin`, whether to allow the image edges to be smaller than the
final box edges. 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. Default to `True`.
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
9 changes: 5 additions & 4 deletions monai/transforms/croppad/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -722,14 +722,15 @@ class CropForegroundd(Cropd):
for more information.
"""

@deprecated_arg_default("allow_smaller", old_default=True, new_default=False, since="1.2", replaced="1.3")
def __init__(
self,
keys: KeysCollection,
source_key: str,
select_fn: Callable = is_positive,
channel_indices: IndexSelection | None = None,
margin: Sequence[int] | int = 0,
allow_smaller: bool = False,
allow_smaller: bool = True,
k_divisible: Sequence[int] | int = 1,
mode: SequenceStr = PytorchPadMode.CONSTANT,
start_coord_key: str | None = "foreground_start_coord",
Expand All @@ -747,9 +748,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 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.
allow_smaller: when computing box size with `margin`, whether to allow the image edges to be smaller than the
final box edges. 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. Default to `True`.
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
12 changes: 7 additions & 5 deletions monai/transforms/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
SplineMode,
TraceKeys,
TraceStatusKeys,
deprecated_arg_default,
ensure_tuple,
ensure_tuple_rep,
ensure_tuple_size,
Expand Down Expand Up @@ -945,12 +946,13 @@ def _create_translate(
return array_func(affine) # type: ignore


@deprecated_arg_default("allow_smaller", old_default=True, new_default=False, since="1.2", replaced="1.3")
def generate_spatial_bounding_box(
img: NdarrayOrTensor,
select_fn: Callable = is_positive,
channel_indices: IndexSelection | None = None,
margin: Sequence[int] | int = 0,
allow_smaller: bool = False,
allow_smaller: bool = True,
) -> tuple[list[int], list[int]]:
"""
Generate the spatial bounding box of foreground in the image with start-end positions (inclusive).
Expand All @@ -969,9 +971,9 @@ 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 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`.
allow_smaller: when computing box size with `margin`, whether to allow the image edges to be smaller than the
final box edges. If `True`, the bounding boxes edges are aligned with the input image edges, if `False`,
the bounding boxes edges are aligned with the final box edges. Default to `True`.
"""
check_non_lazy_pending_ops(img, name="generate_spatial_bounding_box")
Expand Down Expand Up @@ -999,7 +1001,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 not allow_smaller:
if 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": 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, 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": 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, 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": False,
"allow_smaller": True,
},
{
"img": p(
Expand All @@ -107,7 +107,7 @@
"select_fn": lambda x: x > 0,
"channel_indices": None,
"margin": [2, 1],
"allow_smaller": True,
"allow_smaller": False,
},
{
"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": True,
"allow_smaller": False,
},
([-1, 0], [6, 5]),
]
Expand All @@ -96,7 +96,7 @@
"select_fn": lambda x: x > 0,
"channel_indices": None,
"margin": [2, 1],
"allow_smaller": False,
"allow_smaller": True,
},
([0, 0], [5, 5]),
]
Expand Down

0 comments on commit 38d73e8

Please sign in to comment.