-
Notifications
You must be signed in to change notification settings - Fork 1.6k
fix(data): avoid divide-by-zero in pydicom affine for single-slice volumes #8956
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SID-6921
wants to merge
2
commits into
Project-MONAI:dev
Choose a base branch
from
SID-6921:fix/pydicom-affine-single-slice-zero-div
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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]andMetaKeys.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 thelastImagePositionPatientpath 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
🤖 Prompt for AI Agents
There was a problem hiding this comment.
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".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Length of output: 42206
@ericspodGood catch.I checked
tests/test_utils.py:SkipIfNoModulecallsoptional_import(self.module_name)directly, so the module string matters here. Intests/data/test_init_reader.pythese decorators use"Pydicom", while other tests use"pydicom".So yes:
@SkipIfNoModule("pydicom")looks correct, and🧬 Code Graph Analysis Results
tests/data/test_init_reader.py(around the Pydicom skip decorators)Lines 49-76 (optional dependency gating for reader construction):
Lines 105-121 (optional dependency gating for
_get_affine, single-slice case):Lines 122-138 (optional dependency gating for
_get_affine, multi-slice case):tests/test_utils.py(the skip decorator used by the tests)Lines 262-271:
monai/data/image_reader.py(method under test:_get_affinebehavior)PydicomReader._get_affine— Lines 729-771: