MONAI icon indicating copy to clipboard operation
MONAI copied to clipboard

fix(data): avoid divide-by-zero in pydicom affine for single-slice volumes

Open SID-6921 opened this issue 5 months ago โ€ข 1 comments

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

SID-6921 avatar Jun 27 '26 20:06 SID-6921

[!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_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to 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.

โค๏ธ Share

Comment @coderabbitai help to get the list of available commands.

coderabbitai[bot] avatar Jun 27 '26 20:06 coderabbitai[bot]

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.

SID-6921 avatar Jun 30 '26 04:06 SID-6921

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).

SID-6921 avatar Jun 30 '26 04:06 SID-6921

Thanks @SID-6921 I had minor comments about the module name and the Coderabbit comment is worth looking at.

ericspod avatar Jun 30 '26 15:06 ericspod

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.

SID-6921 avatar Jun 30 '26 16:06 SID-6921