fix: guard division by zero in DICOMReader._get_affine for single-slice volumes (Fixes #8925)#8926
Open
rtmalikian wants to merge 1 commit into
Open
Conversation
Contributor
📝 WalkthroughWalkthroughIn Estimated code review effort🎯 1 (Trivial) | ⏱️ ~2 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 |
…ce volumes When n=1 (single-slice 3D DICOM segmentation), (n-1) is zero causing ZeroDivisionError in the direction vector computation for the z-axis. This happens in the segmentation code path where lastImagePositionPatient is always set regardless of the number of frames. Fix: add n > 1 guard before computing z-axis direction from lastImagePositionPatient. For single-slice volumes, the z-axis column remains as the identity [0, 0, 1, 0] from np.eye(4). Fixes Project-MONAI#8925 Signed-off-by: Raphael Malikian <rtmalikian@gmail.com>
6800a99 to
7a85527
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #8925
Problem
In
DICOMReader._get_affine, when processing single-slice 3D DICOM segmentation volumes (wheren == 1), the code computes:Since
n - 1 = 0, this raises aZeroDivisionError. The issue occurs in the segmentation code path (_get_seg_data, line 898) wherelastImagePositionPatientis always set from the frame metadata regardless of the number of frames. For a single-frame segmentation, the first and last positions are identical, producing0 / 0.Solution
Added an
n > 1guard before computing the z-axis direction vector fromlastImagePositionPatient. For single-slice volumes, the z-axis column of the affine remains as the identity[0, 0, 1, 0]fromnp.eye(4), which is a correct default — there is no meaningful z-direction for a single slice.Verification
About the Author: Raphael Malikian — Clinical AI Solutions Architect. I specialise in building and fixing AI/ML systems for healthcare, including vector databases, RAG pipelines, and clinical NLP. If you need help with your project or think I can add value to your organisation, feel free to reach out — I'd love to connect.
📧 rtmalikian@gmail.com
🔗 GitHub: https://github.com/rtmalikian
🔗 LinkedIn: http://www.linkedin.com/in/raphael-t-malikian-mbbs-bsc-hons-71075436a
Disclosure: This code was developed with assistance from mimo-2.5-pro (Xiaomi) via Hermes Agent (Nous Research). All changes were reviewed, tested against the actual codebase, and verified for correctness.
Changelog
Files Changed
monai/data/image_reader.py— Addedn > 1guard before computing affine offsets for multi-slice volumes inDICOMReader._get_affine()Verification