Skip to content

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

Open
SID-6921 wants to merge 2 commits into
Project-MONAI:devfrom
SID-6921:fix/pydicom-affine-single-slice-zero-div
Open

fix(data): avoid divide-by-zero in pydicom affine for single-slice volumes#8956
SID-6921 wants to merge 2 commits into
Project-MONAI:devfrom
SID-6921:fix/pydicom-affine-single-slice-zero-div

Conversation

@SID-6921

Copy link
Copy Markdown

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

@coderabbitai

coderabbitai Bot commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

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 coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 win

Document the new _get_affine fallback.

This definition changed, but its docstring still omits a Returns section and does not say that lastImagePositionPatient is ignored unless MetaKeys.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

📥 Commits

Reviewing files that changed from the base of the PR and between 4ba89bd and 4a51549.

📒 Files selected for processing (2)
  • monai/data/image_reader.py
  • tests/data/test_init_reader.py

Comment thread tests/data/test_init_reader.py
Comment on lines +120 to +134
def test_pydicom_reader_get_affine_multi_slice_uses_last_position(self):
reader = PydicomReader()
metadata = {
"00200037": {"Value": [1.0, 0.0, 0.0, 0.0, 1.0, 0.0]},
"00200032": {"Value": [0.0, 0.0, 0.0]},
"00280030": {"Value": [1.0, 1.0]},
"lastImagePositionPatient": np.array([0.0, 0.0, 4.0]),
MetaKeys.SPATIAL_SHAPE: np.array([8, 8, 5]),
}

affine = reader._get_affine(metadata, lps_to_ras=False)

np.testing.assert_allclose(affine[0, 2], 0.0)
np.testing.assert_allclose(affine[1, 2], 0.0)
np.testing.assert_allclose(affine[2, 2], 1.0)

@coderabbitai coderabbitai Bot Jun 27, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Make the multi-slice fixture prove the branch ran.

With lastImagePositionPatient = [0, 0, 4] and MetaKeys.SPATIAL_SHAPE[-1] = 5, _get_affine() computes (k1, k2, k3) == (0, 0, 1), which is exactly the same as the pre-branch defaults. This test still passes if the lastImagePositionPatient path is skipped entirely.

Proposed fix
     def test_pydicom_reader_get_affine_multi_slice_uses_last_position(self):
         reader = PydicomReader()
         metadata = {
             "00200037": {"Value": [1.0, 0.0, 0.0, 0.0, 1.0, 0.0]},
             "00200032": {"Value": [0.0, 0.0, 0.0]},
             "00280030": {"Value": [1.0, 1.0]},
-            "lastImagePositionPatient": np.array([0.0, 0.0, 4.0]),
+            "lastImagePositionPatient": np.array([2.0, 4.0, 8.0]),
             MetaKeys.SPATIAL_SHAPE: np.array([8, 8, 5]),
         }

         affine = reader._get_affine(metadata, lps_to_ras=False)

-        np.testing.assert_allclose(affine[0, 2], 0.0)
-        np.testing.assert_allclose(affine[1, 2], 0.0)
-        np.testing.assert_allclose(affine[2, 2], 1.0)
+        np.testing.assert_allclose(affine[0, 2], 0.5)
+        np.testing.assert_allclose(affine[1, 2], 1.0)
+        np.testing.assert_allclose(affine[2, 2], 2.0)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def test_pydicom_reader_get_affine_multi_slice_uses_last_position(self):
reader = PydicomReader()
metadata = {
"00200037": {"Value": [1.0, 0.0, 0.0, 0.0, 1.0, 0.0]},
"00200032": {"Value": [0.0, 0.0, 0.0]},
"00280030": {"Value": [1.0, 1.0]},
"lastImagePositionPatient": np.array([0.0, 0.0, 4.0]),
MetaKeys.SPATIAL_SHAPE: np.array([8, 8, 5]),
}
affine = reader._get_affine(metadata, lps_to_ras=False)
np.testing.assert_allclose(affine[0, 2], 0.0)
np.testing.assert_allclose(affine[1, 2], 0.0)
np.testing.assert_allclose(affine[2, 2], 1.0)
def test_pydicom_reader_get_affine_multi_slice_uses_last_position(self):
reader = PydicomReader()
metadata = {
"00200037": {"Value": [1.0, 0.0, 0.0, 0.0, 1.0, 0.0]},
"00200032": {"Value": [0.0, 0.0, 0.0]},
"00280030": {"Value": [1.0, 1.0]},
"lastImagePositionPatient": np.array([2.0, 4.0, 8.0]),
MetaKeys.SPATIAL_SHAPE: np.array([8, 8, 5]),
}
affine = reader._get_affine(metadata, lps_to_ras=False)
np.testing.assert_allclose(affine[0, 2], 0.5)
np.testing.assert_allclose(affine[1, 2], 1.0)
np.testing.assert_allclose(affine[2, 2], 2.0)
🤖 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 `@tests/data/test_init_reader.py` around lines 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SID-6921 please have a look at this comment, I think the change makes sense. It's possible the tests weren't being run if the module name was "Pydicom" instead of "pydicom".

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

fd test_utils.py tests . -a
ast-grep outline tests/test_utils.py --view expanded || true
rg -n -C3 'class SkipIfNoModule|def SkipIfNoModule|SkipIfNoModule\(' tests/test_utils.py tests -g '!**/.git/**'
sed -n '1,220p' tests/test_utils.py

Length of output: 42206


@ericspod Good catch.

I checked tests/test_utils.py: SkipIfNoModule calls optional_import(self.module_name) directly, so the module string matters here. In tests/data/test_init_reader.py these decorators use "Pydicom", while other tests use "pydicom".

So yes:

  • changing the decorators to @SkipIfNoModule("pydicom") looks correct, and
  • strengthening the multi-slice fixture still makes sense so the branch is actually proven.

🧬 Code Graph Analysis Results

tests/data/test_init_reader.py (around the Pydicom skip decorators)

Lines 49-76 (optional dependency gating for reader construction):

    `@SkipIfNoModule`("itk")
    `@SkipIfNoModule`("nibabel")
    `@SkipIfNoModule`("PIL")
    `@SkipIfNoModule`("nrrd")
    `@SkipIfNoModule`("Pydicom")
    def test_readers(self):
        inst = ITKReader()
        self.assertIsInstance(inst, ITKReader)
...
        inst = PydicomReader()
        self.assertIsInstance(inst, PydicomReader)
...
        inst = NrrdReader()
        self.assertIsInstance(inst, NrrdReader)

Lines 105-121 (optional dependency gating for _get_affine, single-slice case):

    `@SkipIfNoModule`("Pydicom")
    def test_pydicom_reader_get_affine_single_slice_with_last_position(self):
        reader = PydicomReader()
        metadata = {
            "00200037": {"Value": [1.0, 0.0, 0.0, 0.0, 1.0, 0.0]},
            "00200032": {"Value": [10.0, 20.0, 30.0]},
            "00280030": {"Value": [0.5, 0.25]},
            "lastImagePositionPatient": np.array([10.0, 20.0, 30.0]),
            MetaKeys.SPATIAL_SHAPE: np.array([64, 64, 1]),
        }

        affine = reader._get_affine(metadata, lps_to_ras=False)

Lines 122-138 (optional dependency gating for _get_affine, multi-slice case):

    `@SkipIfNoModule`("Pydicom")
    def test_pydicom_reader_get_affine_multi_slice_uses_last_position(self):
        reader = PydicomReader()
        metadata = {
            "00200037": {"Value": [1.0, 0.0, 0.0, 0.0, 1.0, 0.0]},
            "00200032": {"Value": [0.0, 0.0, 0.0]},
            "00280030": {"Value": [1.0, 1.0]},
            "lastImagePositionPatient": np.array([0.0, 0.0, 4.0]),
            MetaKeys.SPATIAL_SHAPE: np.array([8, 8, 5]),
        }

        affine = reader._get_affine(metadata, lps_to_ras=False)

tests/test_utils.py (the skip decorator used by the tests)

Lines 262-271:

class SkipIfNoModule:
    """Decorator to be used if test should be skipped
    when optional module is not present."""

    def __init__(self, module_name):
        self.module_name = module_name
        self.module_missing = not optional_import(self.module_name)[1]

    def __call__(self, obj):
        return unittest.skipIf(self.module_missing, f"optional module not present: {self.module_name}")(obj)

monai/data/image_reader.py (method under test: _get_affine behavior)

PydicomReader._get_affine — Lines 729-771:

def _get_affine(self, metadata: dict, lps_to_ras: bool = True):
        """
        Get or construct the affine matrix of the image, it can be used to correct
        spacing, orientation or execute spatial transforms.

        Args:
            metadata: metadata with dict type.
            lps_to_ras: whether to convert the affine matrix from "LPS" to "RAS". Defaults to True.

        """
        affine: np.ndarray = np.eye(4)
        if not ("00200037" in metadata and "00200032" in metadata):
            return affine
        # "00200037" is the tag of `ImageOrientationPatient`
        rx, ry, rz, cx, cy, cz = metadata["00200037"]["Value"]
        # "00200032" is the tag of `ImagePositionPatient`
        sx, sy, sz = metadata["00200032"]["Value"]
        # "00280030" is the tag of `PixelSpacing`
        spacing = metadata["00280030"]["Value"] if "00280030" in metadata else (1.0, 1.0)
        dr, dc = metadata.get("spacing", spacing)[:2]
        affine[0, 0] = cx * dr
        affine[0, 1] = rx * dc
        affine[0, 3] = sx
        affine[1, 0] = cy * dr
        affine[1, 1] = ry * dc
        affine[1, 3] = sy
        affine[2, 0] = cz * dr
        affine[2, 1] = rz * dc
        affine[2, 2] = 1.0
        affine[2, 3] = sz

        # 3d
        if "lastImagePositionPatient" in metadata:
            t1n, t2n, t3n = metadata["lastImagePositionPatient"]
            n = metadata[MetaKeys.SPATIAL_SHAPE][-1]
            if n > 1:
                affine[0, 2] = (t1n - sx) / (n - 1)
                affine[1, 2] = (t2n - sy) / (n - 1)
                affine[2, 2] = (t3n - sz) / (n - 1)

        if lps_to_ras:
            affine = orientation_ras_lps(affine)
        return affine

📝 Learnings were identified and not saved (knowledge base disabled). Enable

Comment thread monai/data/image_reader.py Outdated
@SID-6921

Copy link
Copy Markdown
Author

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 SID-6921 force-pushed the fix/pydicom-affine-single-slice-zero-div branch from a8d8ca6 to 6140c73 Compare June 30, 2026 04:25
@SID-6921

Copy link
Copy Markdown
Author

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

Comment thread tests/data/test_init_reader.py Outdated
# (F-order) layout from nibabel should be preserved here.
self.assertFalse(data.flags.c_contiguous)

@SkipIfNoModule("Pydicom")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@SkipIfNoModule("Pydicom")
@SkipIfNoModule("pydicom")

Module names are case sensitive, same in the next test.

@ericspod

Copy link
Copy Markdown
Member

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

@SID-6921

Copy link
Copy Markdown
Author

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 added 2 commits June 30, 2026 13:27
…etadata

Signed-off-by: SID <nandasiddhardha@gmail.com>
Signed-off-by: SID <nandasiddhardha@gmail.com>
@SID-6921 SID-6921 force-pushed the fix/pydicom-affine-single-slice-zero-div branch from fc56a23 to 42517f5 Compare June 30, 2026 17:27
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

2 participants