Skip to content

Commit 38d73e8

Browse files
authored
Revert #6686 (#6736)
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 <[email protected]>
1 parent a5dff5b commit 38d73e8

File tree

7 files changed

+24
-20
lines changed

7 files changed

+24
-20
lines changed

monai/metrics/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def get_mask_edges(
176176
else: # pytorch subvoxel, maybe on gpu, but croppad boolean values on GPU is not supported
177177
seg_pred, seg_gt, or_vol = convert_to_tensor(channel_first, dtype=torch.float16)
178178
cropper = CropForegroundD(
179-
["pred", "gt"], source_key="src", margin=1, allow_smaller=True, start_coord_key=None, end_coord_key=None
179+
["pred", "gt"], source_key="src", margin=1, allow_smaller=False, start_coord_key=None, end_coord_key=None
180180
)
181181
cropped = cropper({"pred": seg_pred, "gt": seg_gt, "src": or_vol}) # type: ignore
182182
seg_pred, seg_gt = cropped["pred"][0], cropped["gt"][0]

monai/transforms/croppad/array.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -819,12 +819,13 @@ def threshold_at_one(x):
819819
820820
"""
821821

822+
@deprecated_arg_default("allow_smaller", old_default=True, new_default=False, since="1.2", replaced="1.3")
822823
def __init__(
823824
self,
824825
select_fn: Callable = is_positive,
825826
channel_indices: IndexSelection | None = None,
826827
margin: Sequence[int] | int = 0,
827-
allow_smaller: bool = False,
828+
allow_smaller: bool = True,
828829
return_coords: bool = False,
829830
k_divisible: Sequence[int] | int = 1,
830831
mode: str = PytorchPadMode.CONSTANT,
@@ -837,9 +838,9 @@ def __init__(
837838
channel_indices: if defined, select foreground only on the specified channels
838839
of image. if None, select foreground on the whole image.
839840
margin: add margin value to spatial dims of the bounding box, if only 1 value provided, use it for all dims.
840-
allow_smaller: when computing box size with `margin`, whether to allow the final box edges to be outside of
841-
the image edges (the image is smaller than the box). If `True`, part of a padded output box might be outside
842-
of the original image, if `False`, the image edges will be used as the box edges.
841+
allow_smaller: when computing box size with `margin`, whether to allow the image edges to be smaller than the
842+
final box edges. If `False`, part of a padded output box might be outside of the original image, if `True`,
843+
the image edges will be used as the box edges. Default to `True`.
843844
return_coords: whether return the coordinates of spatial bounding box for foreground.
844845
k_divisible: make each spatial dimension to be divisible by k, default to 1.
845846
if `k_divisible` is an int, the same `k` be applied to all the input spatial dimensions.

monai/transforms/croppad/dictionary.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -722,14 +722,15 @@ class CropForegroundd(Cropd):
722722
for more information.
723723
"""
724724

725+
@deprecated_arg_default("allow_smaller", old_default=True, new_default=False, since="1.2", replaced="1.3")
725726
def __init__(
726727
self,
727728
keys: KeysCollection,
728729
source_key: str,
729730
select_fn: Callable = is_positive,
730731
channel_indices: IndexSelection | None = None,
731732
margin: Sequence[int] | int = 0,
732-
allow_smaller: bool = False,
733+
allow_smaller: bool = True,
733734
k_divisible: Sequence[int] | int = 1,
734735
mode: SequenceStr = PytorchPadMode.CONSTANT,
735736
start_coord_key: str | None = "foreground_start_coord",
@@ -747,9 +748,9 @@ def __init__(
747748
channel_indices: if defined, select foreground only on the specified channels
748749
of image. if None, select foreground on the whole image.
749750
margin: add margin value to spatial dims of the bounding box, if only 1 value provided, use it for all dims.
750-
allow_smaller: when computing box size with `margin`, whether to allow the final box edges to be outside of
751-
the image edges (the image is smaller than the box). If `True`, part of a padded output box might be outside
752-
of the original image, if `False`, the image edges will be used as the box edges.
751+
allow_smaller: when computing box size with `margin`, whether to allow the image edges to be smaller than the
752+
final box edges. If `False`, part of a padded output box might be outside of the original image, if `True`,
753+
the image edges will be used as the box edges. Default to `True`.
753754
k_divisible: make each spatial dimension to be divisible by k, default to 1.
754755
if `k_divisible` is an int, the same `k` be applied to all the input spatial dimensions.
755756
mode: available modes for numpy array:{``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``,

monai/transforms/utils.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
SplineMode,
5454
TraceKeys,
5555
TraceStatusKeys,
56+
deprecated_arg_default,
5657
ensure_tuple,
5758
ensure_tuple_rep,
5859
ensure_tuple_size,
@@ -945,12 +946,13 @@ def _create_translate(
945946
return array_func(affine) # type: ignore
946947

947948

949+
@deprecated_arg_default("allow_smaller", old_default=True, new_default=False, since="1.2", replaced="1.3")
948950
def generate_spatial_bounding_box(
949951
img: NdarrayOrTensor,
950952
select_fn: Callable = is_positive,
951953
channel_indices: IndexSelection | None = None,
952954
margin: Sequence[int] | int = 0,
953-
allow_smaller: bool = False,
955+
allow_smaller: bool = True,
954956
) -> tuple[list[int], list[int]]:
955957
"""
956958
Generate the spatial bounding box of foreground in the image with start-end positions (inclusive).
@@ -969,9 +971,9 @@ def generate_spatial_bounding_box(
969971
channel_indices: if defined, select foreground only on the specified channels
970972
of image. if None, select foreground on the whole image.
971973
margin: add margin value to spatial dims of the bounding box, if only 1 value provided, use it for all dims.
972-
allow_smaller: when computing box size with `margin`, whether to allow the final box edges to be outside of
973-
the image edges (the image is smaller than the box). If `False`, the bounding boxes edges are aligned
974-
with the input image edges, default to `False`.
974+
allow_smaller: when computing box size with `margin`, whether to allow the image edges to be smaller than the
975+
final box edges. If `True`, the bounding boxes edges are aligned with the input image edges, if `False`,
976+
the bounding boxes edges are aligned with the final box edges. Default to `True`.
975977
976978
"""
977979
check_non_lazy_pending_ops(img, name="generate_spatial_bounding_box")
@@ -999,7 +1001,7 @@ def generate_spatial_bounding_box(
9991001
arg_max = where(dt == dt.max())[0]
10001002
min_d = arg_max[0] - margin[di]
10011003
max_d = arg_max[-1] + margin[di] + 1
1002-
if not allow_smaller:
1004+
if allow_smaller:
10031005
min_d = max(min_d, 0)
10041006
max_d = min(max_d, spatial_size[di])
10051007

tests/test_crop_foreground.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363

6464
TESTS.append(
6565
[
66-
{"select_fn": lambda x: x > 0, "channel_indices": None, "margin": [2, 1], "allow_smaller": False},
66+
{"select_fn": lambda x: x > 0, "channel_indices": None, "margin": [2, 1], "allow_smaller": True},
6767
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]]]),
6868
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]]]),
6969
True,
@@ -72,7 +72,7 @@
7272

7373
TESTS.append(
7474
[
75-
{"select_fn": lambda x: x > 0, "channel_indices": None, "margin": [2, 1], "allow_smaller": True},
75+
{"select_fn": lambda x: x > 0, "channel_indices": None, "margin": [2, 1], "allow_smaller": False},
7676
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]]]),
7777
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]]]),
7878
True,

tests/test_crop_foregroundd.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
"select_fn": lambda x: x > 0,
8989
"channel_indices": None,
9090
"margin": [2, 1],
91-
"allow_smaller": False,
91+
"allow_smaller": True,
9292
},
9393
{
9494
"img": p(
@@ -107,7 +107,7 @@
107107
"select_fn": lambda x: x > 0,
108108
"channel_indices": None,
109109
"margin": [2, 1],
110-
"allow_smaller": True,
110+
"allow_smaller": False,
111111
},
112112
{
113113
"img": p(

tests/test_generate_spatial_bounding_box.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
"select_fn": lambda x: x > 0,
8383
"channel_indices": None,
8484
"margin": [2, 1],
85-
"allow_smaller": True,
85+
"allow_smaller": False,
8686
},
8787
([-1, 0], [6, 5]),
8888
]
@@ -96,7 +96,7 @@
9696
"select_fn": lambda x: x > 0,
9797
"channel_indices": None,
9898
"margin": [2, 1],
99-
"allow_smaller": False,
99+
"allow_smaller": True,
100100
},
101101
([0, 0], [5, 5]),
102102
]

0 commit comments

Comments
 (0)