-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fix channel-first indices buffer for distance_transform_edt (return_indices=True) #8656 #8657
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
base: dev
Are you sure you want to change the base?
Fix channel-first indices buffer for distance_transform_edt (return_indices=True) #8656 #8657
Conversation
WalkthroughThis change fixes a shape mismatch bug in the Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes
Areas requiring attention:
Pre-merge checks and finishing touches✅ Passed checks (5 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (2)
monai/transforms/utils.py (2)
2499-2507: CuCIM path: allocateindicesonimg.device.Line 2501 allocates
indiceswithoutdevice=img.device; whenimgis CUDA,convert_to_cupy(indices)will fail because the CPU tensor cannot be converted to a CuPy array.- indices = torch.zeros((img.shape[0],) + (img.dim() - 1,) + img.shape[1:], dtype=dtype) # type: ignore + indices = torch.zeros( + (img.shape[0], img.dim() - 1, *img.shape[1:]), + dtype=dtype, + device=img.device, + ) # type: ignore
2484-2486: User-supplied output buffers are discarded by resetting toNoneimmediately after capture.Line 2484 captures
distancesandindicesparameters intodistances_originalandindices_original, but line 2485 immediately sets them toNone. This makes all downstream validation branches (lines 2492-2498, 2501-2507 in GPU path; 2514-2520, 2522-2525 in CPU path) unreachable dead code, anddistances_original/indices_originalare never used. The function ignores user-supplied buffers entirely.Fix line 2485:
- distances, indices = None, None + distances, indices = distances_original, indices_originalAdditionally, the device/type guards at lines 2493 and 2503 use
andwhereoris intended—they should reject if missing the tensor type or the wrong device, not requiring both conditions.
🧹 Nitpick comments (1)
monai/transforms/utils.py (1)
2533-2536: Tuple unpacking improves readability here.The proposed refactoring is correct. scipy.ndimage.distance_transform_edt with return_indices=True expects indices with dtype int32 and shape (input.ndim,) + input.shape. Since each channel is processed as a 2D slice, the current shape calculation is correct, and the tuple unpacking syntax is equivalent and cleaner:
- indices = np.zeros((img_.shape[0],) + (img_.ndim - 1,) + img_.shape[1:], dtype=np.int32) + indices = np.zeros((img_.shape[0], img_.ndim - 1, *img_.shape[1:]), dtype=np.int32)
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Cache: Disabled due to data retention organization setting
Knowledge base: Disabled due to Reviews -> Disable Knowledge Base setting
📒 Files selected for processing (1)
monai/transforms/utils.py(2 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.py
⚙️ CodeRabbit configuration file
Review the Python code for quality and correctness. Ensure variable names adhere to PEP8 style guides, are sensible and informative in regards to their function, though permitting simple names for loop and comprehension variables. Ensure routine names are meaningful in regards to their function and use verbs, adjectives, and nouns in a semantically appropriate way. Docstrings should be present for all definition which describe each variable, return value, and raised exception in the appropriate section of the Google-style of docstrings. Examine code for logical error or inconsistencies, and suggest what may be changed to addressed these. Suggest any enhancements for code improving efficiency, maintainability, comprehensibility, and correctness. Ensure new or modified definitions will be covered by existing or new unit tests.
Files:
monai/transforms/utils.py
🪛 Ruff (0.14.8)
monai/transforms/utils.py
2501-2501: Consider (img.shape[0], img.dim() - 1, *img.shape[1:]) instead of concatenation
Replace with (img.shape[0], img.dim() - 1, *img.shape[1:])
(RUF005)
2535-2535: Consider (img_.shape[0], img_.ndim - 1, *img_.shape[1:]) instead of concatenation
Replace with (img_.shape[0], img_.ndim - 1, *img_.shape[1:])
(RUF005)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (19)
- GitHub Check: min-dep-py3 (3.12)
- GitHub Check: min-dep-py3 (3.11)
- GitHub Check: min-dep-pytorch (2.7.1)
- GitHub Check: min-dep-py3 (3.9)
- GitHub Check: min-dep-pytorch (2.5.1)
- GitHub Check: min-dep-pytorch (2.8.0)
- GitHub Check: min-dep-pytorch (2.6.0)
- GitHub Check: quick-py3 (macOS-latest)
- GitHub Check: min-dep-os (ubuntu-latest)
- GitHub Check: min-dep-py3 (3.10)
- GitHub Check: min-dep-os (windows-latest)
- GitHub Check: min-dep-os (macOS-latest)
- GitHub Check: flake8-py3 (mypy)
- GitHub Check: flake8-py3 (pytype)
- GitHub Check: quick-py3 (ubuntu-latest)
- GitHub Check: quick-py3 (windows-latest)
- GitHub Check: flake8-py3 (codeformat)
- GitHub Check: packaging
- GitHub Check: build-docs
Fixes #8656
Distance_transform_edt indices preallocation to use channel-first (C, spatial_dims, ...) layout for both torch/cuCIM and NumPy/SciPy paths, resolving “indices array has wrong shape” errors when return_indices=True.
Description
Types of changes
./runtests.sh -f -u --net --coverage../runtests.sh --quick --unittests --disttests.make htmlcommand in thedocs/folder.