Improve Affine transform documentation#8727
Improve Affine transform documentation#8727engmohamedsalah wants to merge 1 commit intoProject-MONAI:devfrom
Conversation
- Add Note section to Affine class explaining center-origin assumption - Clarify 'normalized' parameter with user-friendly explanation - Add comprehensive docstring to compute_w_affine method - Improve readability for users unfamiliar with coordinate systems Fixes Project-MONAI#7092 Signed-off-by: Mohamed Salah <eng.mohamed.tawab@gmail.com>
📝 WalkthroughWalkthroughA new classmethod Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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.
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 (1)
monai/transforms/spatial/array.py (1)
2333-2358:⚠️ Potential issue | 🟡 MinorAdd focused unit test for
compute_w_affineand preserve input type.The method lacks direct test coverage. Add a focused test (e.g., 2D case with known center shifts). Additionally, align with MONAI patterns by preserving the input matrix type:
Type preservation refactor
- mat = shift_1 @ convert_data_type(mat, np.ndarray)[0] @ shift_2 - return mat + mat_np = shift_1 @ convert_data_type(mat, np.ndarray)[0] @ shift_2 + return convert_to_dst_type(mat_np, mat)[0]
Summary
This PR improves the documentation for the
Affinetransform to address long-standing confusion about its coordinate system assumptions and parameters.Addresses
Fixes #7092
Motivation
Users have been confused about:
normalized=Truevsnormalized=Falsecompute_w_affinemethod does (no docstring)These documentation gaps make it difficult for users to correctly use the Affine transform, especially when working with medical imaging data.
Changes Made
1. Added Note section to Affine class
Clearly documents that transformations are performed relative to the image spatial center:
2. Clarified
normalizedparameter documentationReplaced technical jargon with user-friendly explanation:
affineis defined to include a normalization transform converting the coordinates from[-(size-1)/2, (size-1)/2]..."False(default) if your affine matrix works with pixel/voxel coordinates centered at the image center. WhenFalse, MONAI will automatically apply the necessary coordinate transformations. Most users should use the defaultFalse."3. Added comprehensive docstring to
compute_w_affinemethodPreviously had no documentation at all. Now includes:
Verification
Code Evidence - Center-Origin Assumption
The matrix composition (translate_back @ transform @ translate_to_origin) confirms transformations happen around the image center.
Code Evidence - normalized Parameter
normalized=False→norm_coord=True→ normalization applied ✓normalized=True→norm_coord=False→ no normalization ✓Documentation accurately reflects this behavior.
Testing Performed
Type of change
Code Quality
Benefits to MONAI Community
normalizedparametercompute_w_affinemakes code easier to understandAdditional Notes