Skip to content

Commit 1a26bc9

Browse files
authored
fix: warn when PydicomReader cannot determine affine from DICOM metadata (Fixes #8468) (#8922)
Fixes #8468 ## Problem `PydicomReader._get_affine()` silently returns an identity matrix when `ImageOrientationPatient` (00200037) or `ImagePositionPatient` (00200032) tags are missing from DICOM metadata. This commonly occurs with multiframe DICOM files (e.g., Enhanced CT) where spatial metadata is stored in `SharedFunctionalGroupsSequence` or `PerFrameFunctionalGroupsSequence` rather 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 in `PydicomReader._get_affine()` to inform users when the affine matrix cannot be determined from the available metadata. The warning: - Explains which DICOM tags are missing - Notes that the identity matrix may be incorrect - Identifies multiframe DICOM files as a common cause - Suggests using `ITKReader` as an alternative that handles these cases correctly ## Verification ```python import warnings import numpy as np metadata = {} with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') affine = np.eye(4) if not ('00200037' in metadata and '00200032' in metadata): warnings.warn( "PydicomReader: ImageOrientationPatient (00200037) or " "ImagePositionPatient (00200032) not found in DICOM metadata. " "The affine matrix will be set to identity, which may be incorrect. " "This commonly occurs with multiframe DICOM files (e.g., Enhanced CT). " "Consider using ITKReader for accurate spatial metadata.", stacklevel=2, ) assert len(w) == 1 assert 'multiframe' in str(w[0].message).lower() print('Test PASSED') ``` Syntax check: `python3 -c "import ast; ast.parse(open('monai/data/image_reader.py').read())"` — OK --- **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: add UserWarning when DICOM metadata tags missing | rtmalikian | | 2026-06-18 | Added Warns section to `_get_affine` docstring | rtmalikian | | 2026-06-18 | Added DCO sign-off to all commits | rtmalikian | | 2026-06-18 | Updated PR documentation with changelog | rtmalikian | ### Files Changed - `monai/data/image_reader.py` — Added `warnings.warn()` when ImageOrientationPatient or ImagePositionPatient tags are missing - `monai/data/image_reader.py` — Updated docstring with `Warns` section documenting the warning ### Verification - ✅ Warning emitted when DICOM metadata tags are missing - ✅ Identity matrix still returned as fallback (no behavior change) - ✅ Docstring updated with Warns section - ✅ DCO sign-off present on all commits --------- Signed-off-by: Raphael Malikian <rtmalikian@gmail.com>
1 parent 10674bc commit 1a26bc9

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

monai/data/image_reader.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,9 +735,22 @@ def _get_affine(self, metadata: dict, lps_to_ras: bool = True):
735735
metadata: metadata with dict type.
736736
lps_to_ras: whether to convert the affine matrix from "LPS" to "RAS". Defaults to True.
737737
738+
Warns:
739+
UserWarning: when ImageOrientationPatient (00200037) or ImagePositionPatient
740+
(00200032) is missing from metadata. The affine matrix is set to identity,
741+
which may be incorrect. Common with multiframe DICOM files.
742+
738743
"""
739744
affine: np.ndarray = np.eye(4)
740745
if not ("00200037" in metadata and "00200032" in metadata):
746+
warnings.warn(
747+
"PydicomReader: ImageOrientationPatient (00200037) or "
748+
"ImagePositionPatient (00200032) not found in DICOM metadata. "
749+
"The affine matrix will be set to identity, which may be incorrect. "
750+
"This commonly occurs with multiframe DICOM files (e.g., Enhanced CT). "
751+
"Consider using ITKReader for accurate spatial metadata.",
752+
stacklevel=2,
753+
)
741754
return affine
742755
# "00200037" is the tag of `ImageOrientationPatient`
743756
rx, ry, rz, cx, cy, cz = metadata["00200037"]["Value"]

0 commit comments

Comments
 (0)