fix: warn when PydicomReader cannot determine affine from DICOM metadata (Fixes #8468)#8922
Conversation
📝 WalkthroughWalkthrough
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~2 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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: 1
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-738: 🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick winDocument the warning in the docstring.
The method now emits a warning when orientation/position tags are missing. Add a "Warns:" section to the docstring.
📝 Suggested docstring update
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. + + Warns: + UserWarning: When ImageOrientationPatient (00200037) or ImagePositionPatient (00200032) + are missing from metadata. Identity matrix is returned in this case. """As per coding guidelines, Google-style docstrings should describe raised exceptions and warnings.
🤖 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 - 738, The _get_affine method docstring is missing documentation about the warning it emits when orientation or position tags are missing. Add a "Warns:" section to the docstring (following the existing Args and Returns sections) that documents what warning is raised and under what conditions it is raised, such as when the required orientation or position metadata tags are not present in the image metadata.Source: Coding guidelines
🤖 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 `@monai/data/image_reader.py`:
- Around line 741-748: The `_get_affine` method now emits a UserWarning when
ImageOrientationPatient or ImagePositionPatient DICOM tags are missing (the
warning block at lines 741-748), but the docstring for this method does not
document this side effect. Update the docstring for the `_get_affine` method to
include a note in the Warns or Side Effects section describing that a
UserWarning is raised when these required DICOM tags are absent. Additionally,
review the test methods (particularly `test_dicom_reader_consistency` and
related tests) that call this code path to ensure they handle the warning
appropriately, either by capturing it or by using test data that avoids
triggering it.
---
Outside diff comments:
In `@monai/data/image_reader.py`:
- Around line 729-738: The _get_affine method docstring is missing documentation
about the warning it emits when orientation or position tags are missing. Add a
"Warns:" section to the docstring (following the existing Args and Returns
sections) that documents what warning is raised and under what conditions it is
raised, such as when the required orientation or position metadata tags are not
present in the image metadata.
🪄 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: b84aab32-78dd-4c53-bb3a-ca337fd8fee7
📒 Files selected for processing (1)
monai/data/image_reader.py
|
Hi @coderabbitai, good catch. I've added a The update is pushed. Thanks for the review! |
|
Tip For best results, initiate chat on the files or code changes.
🐇 ʕ•ᴥ•ʔ If you found this review helpful, would you consider giving us a shout-out on X? Thank you for using CodeRabbit! |
e1af40f to
44f9af8
Compare
When ImageOrientationPatient (00200037) or ImagePositionPatient (00200032) are missing from DICOM metadata, PydicomReader silently returns an identity affine matrix. This commonly occurs with multiframe DICOM files (e.g., Enhanced CT) where spatial metadata is stored in per-frame functional groups. This change adds a warning to inform users that the affine matrix may be incorrect and suggests using ITKReader as an alternative. Fixes Project-MONAI#8468 Signed-off-by: Raphael Malikian <rtmalikian@gmail.com>
…ing-tag warning Signed-off-by: Raphael Malikian <rtmalikian@gmail.com>
44f9af8 to
78ac03b
Compare
Fixes #8468
Problem
PydicomReader._get_affine()silently returns an identity matrix whenImageOrientationPatient(00200037) orImagePositionPatient(00200032) tags are missing from DICOM metadata. This commonly occurs with multiframe DICOM files (e.g., Enhanced CT) where spatial metadata is stored inSharedFunctionalGroupsSequenceorPerFrameFunctionalGroupsSequencerather than at the top level.The silent fallback to an identity matrix leads to incorrect spatial metadata downstream, causing confusion in downstream processing pipelines.
Solution
Added a
warnings.warn()call inPydicomReader._get_affine()to inform users when the affine matrix cannot be determined from the available metadata. The warning:ITKReaderas an alternative that handles these cases correctlyVerification
Syntax check:
python3 -c "import ast; ast.parse(open('monai/data/image_reader.py').read())"— OKAbout 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
_get_affinedocstringFiles Changed
monai/data/image_reader.py— Addedwarnings.warn()when ImageOrientationPatient or ImagePositionPatient tags are missingmonai/data/image_reader.py— Updated docstring withWarnssection documenting the warningVerification