fix(data): avoid divide-by-zero in pydicom affine for single-slice volumes#8956
fix(data): avoid divide-by-zero in pydicom affine for single-slice volumes#8956SID-6921 wants to merge 4 commits into
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthrough
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes 🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 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.
Actionable comments posted: 2
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
monai/data/image_reader.py (1)
729-771: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick winDocument the new
_get_affinefallback.This definition changed, but its docstring still omits a
Returnssection and does not say thatlastImagePositionPatientis ignored unlessMetaKeys.SPATIAL_SHAPE[-1] > 1. Please document that single-slice inputs keep the default third column. As per path instructions, "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."🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@monai/data/image_reader.py` around lines 729 - 771, The _get_affine docstring is missing the new fallback behavior and return documentation. Update the Google-style docstring for _get_affine to include a Returns section for the affine matrix, and note that lastImagePositionPatient is only used when MetaKeys.SPATIAL_SHAPE[-1] > 1; for single-slice inputs the default third column remains unchanged.Source: Path instructions
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@tests/data/test_init_reader.py`:
- Around line 104-135: Add Google-style docstrings to the two new test methods
in PydicomReader’s test class:
test_pydicom_reader_get_affine_single_slice_with_last_position and
test_pydicom_reader_get_affine_multi_slice_uses_last_position. Briefly describe
the purpose of each test, the key metadata variables they set up, and the
expected affine assertions in the Args/Returns/Raises sections as appropriate,
matching the project’s docstring requirements for test definitions.
- Around line 120-134: The PydicomReader._get_affine multi-slice test is too
weak because the current lastImagePositionPatient and MetaKeys.SPATIAL_SHAPE
values produce the same affine result as the default branch, so it does not
prove the special path executed. Update
test_pydicom_reader_get_affine_multi_slice_uses_last_position to use a
lastImagePositionPatient value and spatial shape that yield a non-default k3
term, then assert that affine[2, 2] (or the full third column) reflects the
branch-specific result so the test fails if _get_affine skips the
lastImagePositionPatient logic.
---
Outside diff comments:
In `@monai/data/image_reader.py`:
- Around line 729-771: The _get_affine docstring is missing the new fallback
behavior and return documentation. Update the Google-style docstring for
_get_affine to include a Returns section for the affine matrix, and note that
lastImagePositionPatient is only used when MetaKeys.SPATIAL_SHAPE[-1] > 1; for
single-slice inputs the default third column remains unchanged.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 2ddf1ea8-6e46-466f-b496-8d91deca2125
📒 Files selected for processing (2)
monai/data/image_reader.pytests/data/test_init_reader.py
|
Thanks for the heads-up. I synced with current dev and removed the duplicate change in monai/data/image_reader.py from this PR. I kept the regression tests in ests/data/test_init_reader.py (single-slice and multi-slice affine metadata coverage), which still add value. Local validation passes for the updated PR scope. |
a8d8ca6 to
6140c73
Compare
|
Update done: I rebased the PR branch onto current dev and force-pushed it so the duplicate image_reader.py fix is no longer part of this PR. The PR now keeps only the added regression coverage in ests/data/test_init_reader.py (single-slice and multi-slice affine metadata cases, guarded by optional Pydicom availability). |
|
Thanks @SID-6921 I had minor comments about the module name and the Coderabbit comment is worth looking at. |
|
Thanks for the review comments. I applied the module-name fix and force-pushed: all SkipIfNoModule uses for pydicom in this test file are now lowercase (pydicom), including the two new affine regression tests. Local pytest and pre-commit passed after this change. |
…etadata Signed-off-by: SID <nandasiddhardha@gmail.com>
Signed-off-by: SID <nandasiddhardha@gmail.com>
fc56a23 to
42517f5
Compare
There was a problem hiding this comment.
Pull request overview
This PR fixes a ZeroDivisionError in PydicomReader._get_affine when handling single-slice (n=1) 3D DICOM metadata that includes lastImagePositionPatient, and adds regression tests to cover the single-slice and multi-slice affine paths.
Changes:
- Guard z-step (slice direction) affine derivation from
lastImagePositionPatientto avoid dividing by(n - 1)whenn == 1. - Add regression tests for
_get_affinewithlastImagePositionPatientfor both single-slice and multi-slice shapes. - Standardize
SkipIfNoModuleusage for pydicom by using the correct import name ("pydicom").
Reviewed changes
Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
monai/data/image_reader.py |
Prevents divide-by-zero in _get_affine when computing slice step from lastImagePositionPatient for single-slice volumes. |
tests/data/test_init_reader.py |
Adds regression tests for _get_affine single-slice and multi-slice paths; updates pydicom module skip guard. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Signed-off-by: Siddhardha Nanda <99672439+SID-6921@users.noreply.github.com>
Summary:
Why:
For single-slice 3D DICOM segmentation metadata, n == 1 caused division by zero in affine z-step computation.
Validation:
Closes #8925