fix(data): avoid divide-by-zero in pydicom affine for single-slice volumes
Summary:
- fix PydicomReader._get_affine to skip lastImagePositionPatient z-step derivation when there is only one slice
- add regression tests for single-slice and multi-slice metadata paths
Why: For single-slice 3D DICOM segmentation metadata, n == 1 caused division by zero in affine z-step computation.
Validation:
- python -m pytest tests/data/test_init_reader.py -k pydicom_reader_get_affine -q
- python -m pre_commit run --files monai/data/image_reader.py tests/data/test_init_reader.py
Closes #8925
[!NOTE]
Reviews paused
It 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
reviews.auto_review.auto_pause_after_reviewed_commitssetting.Use the following commands to manage reviews:
@coderabbitai resumeto resume automatic reviews.@coderabbitai reviewto trigger a single review.Use the checkboxes below for quick actions:
- [ ] โถ๏ธ Resume reviews
- [ ] ๐ Trigger review
๐ Walkthrough
Walkthrough
tests/data/test_init_reader.py adds MetaKeys import, changes the Pydicom skip decorator to use pydicom, and adds two PydicomReader._get_affine tests for single-slice and multi-slice metadata. Both tests build lastImagePositionPatient and MetaKeys.SPATIAL_SHAPE, call _get_affine(..., lps_to_ras=False), and assert the Z-axis affine entries.
Estimated code review effort
๐ฏ 1 (Trivial) | โฑ๏ธ ~3 minutes
๐ฅ Pre-merge checks | โ 3 | โ 2
โ Failed checks (2 warnings)
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Linked Issues check | โ ๏ธ Warning | The linked bug fix in monai/data/image_reader.py is missing; only regression tests were added. | Add the n > 1 guard to DICOMReader._get_affine in monai/data/image_reader.py and keep the single- and multi-slice regression tests. |
| Docstring Coverage | โ ๏ธ Warning | Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. | Write docstrings for the functions missing them to satisfy the coverage threshold. |
โ Passed checks (3 passed)
| Check name | Status | Explanation |
|---|---|---|
| Title check | โ Passed | The title matches the intended fix for single-slice pydicom affine handling. |
| Description check | โ Passed | It covers the summary, rationale, validation, and issue reference, though it doesn't use the repository's exact template. |
| Out of Scope Changes check | โ Passed | The only changes are regression tests and a module-name skip tweak, both relevant to the pydicom fix. |
โจ Finishing Touches
๐งช Generate unit tests (beta)
- [ ] Create PR with unit tests
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 @coderabbitai help to get the list of available commands.
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.
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.