-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |
|
|
||
| from monai.data import ITKReader, NibabelReader, NrrdReader, NumpyReader, PILReader, PydicomReader | ||
| from monai.transforms import LoadImage, LoadImaged | ||
| from monai.utils import MetaKeys | ||
| from tests.test_utils import SkipIfNoModule | ||
|
|
||
|
|
||
|
|
@@ -100,6 +101,40 @@ def test_nibabel_reader_avoids_eager_c_order_copy(self): | |
| # (F-order) layout from nibabel should be preserved here. | ||
| self.assertFalse(data.flags.c_contiguous) | ||
|
|
||
| @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) | ||
|
|
||
| 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) | ||
|
|
||
| @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) | ||
|
|
||
| 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) | ||
|
|
||
|
Comment on lines
+105
to
+137
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win Add docstrings to the new tests. Both new test defs are missing Google-style docstrings. 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 AgentsSource: Path instructions |
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
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