Skip to content

Commit

Permalink
Always convert input to C-order in distance_transform_edt (#7675)
Browse files Browse the repository at this point in the history
Fixes #7660

`indices_` may not always be a shallow copy for F-order input as it
constantly converts to C-order

https://github.com/Project-MONAI/MONAI/blob/ffd4454b576abd4eaae30b364f41c213e30dca4c/monai/transforms/utils.py#L2209

https://github.com/Project-MONAI/MONAI/blob/ffd4454b576abd4eaae30b364f41c213e30dca4c/monai/utils/type_conversion.py#L262

### 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: YunLiu <55491388+KumoLiu@users.noreply.github.com>
  • Loading branch information
KumoLiu authored Apr 23, 2024
1 parent 178ebc8 commit ac9b186
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 3 deletions.
2 changes: 1 addition & 1 deletion monai/transforms/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2190,7 +2190,7 @@ def distance_transform_edt(
if return_distances:
dtype = torch.float64 if float64_distances else torch.float32
if distances is None:
distances = torch.zeros_like(img, dtype=dtype) # type: ignore
distances = torch.zeros_like(img, memory_format=torch.contiguous_format, dtype=dtype) # type: ignore
else:
if not isinstance(distances, torch.Tensor) and distances.device != img.device:
raise TypeError("distances must be a torch.Tensor on the same device as img")
Expand Down
3 changes: 1 addition & 2 deletions tests/test_clip_intensity_percentiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from monai.transforms import ClipIntensityPercentiles
from monai.transforms.utils import soft_clip
from monai.transforms.utils_pytorch_numpy_unification import clip, percentile
from monai.utils.type_conversion import convert_to_tensor
from tests.utils import TEST_NDARRAYS, NumpyImageTestCase2D, NumpyImageTestCase3D, assert_allclose


Expand All @@ -30,7 +29,7 @@ def test_hard_clipping_two_sided(self, p):
im = p(self.imt)
result = hard_clipper(im)
lower, upper = percentile(im, (5, 95))
expected = clip(convert_to_tensor(im), lower, upper)
expected = clip(im, lower, upper)
assert_allclose(result, p(expected), type_test="tensor", rtol=1e-4, atol=0)

@parameterized.expand([[p] for p in TEST_NDARRAYS])
Expand Down

0 comments on commit ac9b186

Please sign in to comment.