Skip to content

fix: guard division by zero in DICOMReader._get_affine for single-slice volumes (Fixes #8925)#8926

Open
rtmalikian wants to merge 1 commit into
Project-MONAI:devfrom
rtmalikian:fix/issue-8925-div-by-zero-affine
Open

fix: guard division by zero in DICOMReader._get_affine for single-slice volumes (Fixes #8925)#8926
rtmalikian wants to merge 1 commit into
Project-MONAI:devfrom
rtmalikian:fix/issue-8925-div-by-zero-affine

Conversation

@rtmalikian

@rtmalikian rtmalikian commented Jun 19, 2026

Copy link
Copy Markdown

Fixes #8925

Problem

In DICOMReader._get_affine, when processing single-slice 3D DICOM segmentation volumes (where n == 1), the code computes:

k1, k2, k3 = (t1n - sx) / (n - 1), (t2n - sy) / (n - 1), (t3n - sz) / (n - 1)

Since n - 1 = 0, this raises a ZeroDivisionError. The issue occurs in the segmentation code path (_get_seg_data, line 898) where lastImagePositionPatient is always set from the frame metadata regardless of the number of frames. For a single-frame segmentation, the first and last positions are identical, producing 0 / 0.

Solution

Added an n > 1 guard before computing the z-axis direction vector from lastImagePositionPatient. For single-slice volumes, the z-axis column of the affine remains as the identity [0, 0, 1, 0] from np.eye(4), which is a correct default — there is no meaningful z-direction for a single slice.

Verification

import numpy as np

# Single-slice scenario (n=1) — previously caused ZeroDivisionError
n = 1
sx, sy, sz = 0.0, 0.0, 0.0
t1n, t2n, t3n = 0.0, 0.0, 0.0

affine = np.eye(4)
if n > 1:
    affine[0, 2] = (t1n - sx) / (n - 1)
    affine[1, 2] = (t2n - sy) / (n - 1)
    affine[2, 2] = (t3n - sz) / (n - 1)

print(f'z-axis column: {affine[:, 2]}')  # [0, 0, 1, 0] — identity ✓

# Normal multi-slice case (n=10) — still works correctly
n = 10
t1n, t2n, t3n = 0.0, 0.0, 45.0
affine2 = np.eye(4)
if n > 1:
    affine2[0, 2] = (t1n - sx) / (n - 1)
    affine2[1, 2] = (t2n - sy) / (n - 1)
    affine2[2, 2] = (t3n - sz) / (n - 1)

print(f'z-axis column: {affine2[:, 2]}')  # [0, 0, 5.0, 0] — correct direction ✓

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

Date Change Author
2026-06-18 Initial fix: guard division by zero for single-slice DICOM volumes rtmalikian
2026-06-18 Added DCO sign-off to commit rtmalikian
2026-06-18 Updated PR documentation with changelog rtmalikian

Files Changed

  • monai/data/image_reader.py — Added n > 1 guard before computing affine offsets for multi-slice volumes in DICOMReader._get_affine()

Verification

  • ✅ Single-slice 3D DICOM volumes no longer trigger ZeroDivisionError
  • ✅ Multi-slice volumes continue to compute affine correctly
  • ✅ DCO sign-off present on all commits

@coderabbitai

coderabbitai Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

In PydicomReader._get_affine, the three affine z-axis component assignments (affine[0,2], affine[1,2], affine[2,2]) derived from lastImagePositionPatient are now wrapped in an if n > 1: guard, preventing division by zero when only a single image (n == 1) is processed.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~2 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the specific fix (division by zero guard) and the affected component (DICOMReader._get_affine) with the issue reference.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description check ✅ Passed Pull request provides comprehensive description with problem statement, solution, verification code, and changelog.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 and usage tips.

…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>
@rtmalikian rtmalikian force-pushed the fix/issue-8925-div-by-zero-affine branch from 6800a99 to 7a85527 Compare June 19, 2026 05:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: division by zero in DICOMReader._get_affine for single-slice 3D volumes

1 participant