Repository navigation
Fix Spacing(diagonal=False) orientation preservation for sheared affines #9318
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
picspin
wants to merge
5
commits into
Project-MONAI:dev
Choose a base branch
from
picspin:fix-spacing-shear-orientation
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
Show all changes
5 commits
Select commit
Hold shift + click to select a range
128bec1
Fix Spacing(diagonal=False) orientation preservation for sheared affines
picspin bec9b6a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] da59f54
Fix ruff N806: rename uppercase locals in test_spacing_shear
picspin ce651f8
Fix ruff N803/N806: lowercase remaining identifiers in test_spacing_s…
picspin a3c2ea6
Address CodeRabbit review comments on test_spacing_shear
picspin 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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| # Copyright (c) MONAI Consortium | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| """ | ||
| Regression tests for Spacing / zoom_affine with non-orthogonal (sheared) input affines. | ||
|
|
||
| zoom_affine (monai/data/utils.py) recomposed the direction matrix via Cholesky/QR, which | ||
| silently removed any shear. Tests 1 and 2 pin the behaviour after the fix (shear-preserving | ||
| column rescale); tests 3 and 4 are regression guards for parts verified correct. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import unittest | ||
|
|
||
| import numpy as np | ||
| import torch | ||
|
|
||
| from monai.data import MetaTensor | ||
| from monai.transforms import Orientation, RandAffine, Spacing | ||
| from monai.utils import optional_import | ||
|
|
||
| nib, has_nib = optional_import("nibabel") | ||
|
|
||
|
|
||
| def _random_sheared_affine(rng: np.random.Generator, det_negative: bool = False) -> np.ndarray: | ||
| """Random 4x4 affine with anisotropic spacing, non-orthogonal (sheared) rotation.""" | ||
| sp = rng.uniform(0.8, 3.5, size=3) | ||
| a, b, c = rng.uniform(-np.pi, np.pi, size=3) | ||
| rx = np.array([[1, 0, 0], [0, np.cos(a), -np.sin(a)], [0, np.sin(a), np.cos(a)]]) | ||
| ry = np.array([[np.cos(b), 0, np.sin(b)], [0, 1, 0], [-np.sin(b), 0, np.cos(b)]]) | ||
| rz = np.array([[np.cos(c), -np.sin(c), 0], [np.sin(c), np.cos(c), 0], [0, 0, 1]]) | ||
| r = rz @ ry @ rx | ||
| shear = np.eye(3) | ||
| shear[0, 1] = rng.uniform(-0.35, 0.35) | ||
| shear[1, 2] = rng.uniform(-0.3, 0.3) | ||
| r = r @ shear | ||
| if det_negative: | ||
| r[:, 0] *= -1.0 | ||
| affine = np.eye(4) | ||
| affine[:3, :3] = r @ np.diag(sp) | ||
| affine[:3, 3] = rng.uniform(-20, 20, size=3) | ||
| return affine | ||
|
|
||
|
|
||
| def _smooth_volume(shape=(1, 16, 16, 16), seed=0) -> torch.Tensor: | ||
| z = np.linspace(-1, 1, shape[1]) | ||
| d = z[:, None, None] ** 2 + z[None, :, None] ** 2 + z[None, None, :] ** 2 | ||
| vol = np.sin(3 * d) + 0.5 * np.sin(5 * d)[::-1, ::-1, ::-1] | ||
| return torch.as_tensor(vol, dtype=torch.float32)[None] | ||
|
|
||
|
|
||
| def _axis_direction_changes(affine: np.ndarray, affine_b: np.ndarray) -> np.ndarray: | ||
| """Angles (deg) between corresponding column directions of two 3x3 matrices.""" | ||
| da = affine / np.linalg.norm(affine, axis=0, keepdims=True) | ||
| db = affine_b / np.linalg.norm(affine_b, axis=0, keepdims=True) | ||
| dots = np.clip(np.sum(da * db, axis=0), -1.0, 1.0) | ||
| return np.degrees(np.arccos(dots)) | ||
|
|
||
|
|
||
| class TestSpacingShearInvariance(unittest.TestCase): | ||
| """Spacing(same pixdim, diagonal=False) must not change the geometry of a sheared image.""" | ||
|
|
||
| def test_spacing_shear_invariance(self): | ||
| rng = np.random.default_rng(555) | ||
| worst = 0.0 | ||
| for i in range(10): | ||
| affine = _random_sheared_affine(rng) | ||
| data = _smooth_volume(seed=i) | ||
| mt = MetaTensor(data.clone()) | ||
| mt.affine = affine | ||
| orig_spacing = np.linalg.norm(affine[:3, :3], axis=0) | ||
| out = Spacing(pixdim=[float(s) for s in orig_spacing], diagonal=False, mode="bilinear")(mt) | ||
| out_affine = np.asarray(out.affine) | ||
| degs = _axis_direction_changes(affine[:3, :3], out_affine[:3, :3]) | ||
| worst = max(worst, float(degs.max())) | ||
| # resampling to the same spacing must not rotate the world direction of any axis | ||
| self.assertTrue( | ||
| np.allclose(out_affine[:3, :3], affine[:3, :3], atol=1e-4), | ||
| f"case {i}: Spacing with the input's own pixdim changed the affine:\n" | ||
| f"{out_affine[:3, :3]}\nvs input\n{affine[:3, :3]}\n" | ||
| f"max axis-direction change {degs.max():.2f} deg (docstring says orientation preserved)", | ||
| ) | ||
| # keep the measured magnitude in the failure report path | ||
| assert worst >= 0.0 | ||
|
|
||
| def test_spacing_roundtrip_back_to_original(self): | ||
| """Spacing(new) -> Spacing(original spacing) must restore the original linear affine.""" | ||
| rng = np.random.default_rng(123) | ||
| for i in range(6): | ||
| affine = _random_sheared_affine(rng, det_negative=(i % 3 == 2)) | ||
| data = _smooth_volume(seed=i) | ||
| mt = MetaTensor(data.clone()) | ||
| mt.affine = affine | ||
| orig_spacing = np.linalg.norm(affine[:3, :3], axis=0) | ||
| new_spacing = orig_spacing * np.array([1.3, 0.8, 1.1]) | ||
| t1 = Spacing(pixdim=[float(s) for s in new_spacing], diagonal=False, mode="bilinear")(mt) | ||
| t2 = Spacing(pixdim=[float(s) for s in orig_spacing], diagonal=False, mode="bilinear")(t1) | ||
| out_affine = np.asarray(t2.affine) | ||
| # the shear is removed by zoom_affine at every step and never comes back | ||
| degs = _axis_direction_changes(affine[:3, :3], out_affine[:3, :3]) | ||
| self.assertTrue( | ||
| np.allclose(out_affine[:3, :3], affine[:3, :3], atol=1e-4), | ||
| f"case {i}: round trip changed the linear affine, max axis-direction " f"change {degs.max():.2f} deg", | ||
| ) | ||
|
|
||
|
|
||
| class TestVerifiedCorrect(unittest.TestCase): | ||
| """Regression guards for behaviour that was measured CORRECT against SimpleITK.""" | ||
|
|
||
| @unittest.skipUnless(has_nib, "nibabel is required for Orientation") | ||
| def test_orientation_roundtrip(self): | ||
| rng = np.random.default_rng(31) | ||
| for i, ax in enumerate(["RAS", "LPS", "PLS", "ASL", "IRP", "SLA", "AIL", "RPI"]): | ||
| affine = _random_sheared_affine(rng, det_negative=(i % 2 == 1)) | ||
| data = torch.arange(16 * 16 * 16, dtype=torch.float32).reshape(1, 16, 16, 16) | ||
| mt = MetaTensor(data.clone()) | ||
| mt.affine = affine | ||
| t1 = Orientation(axcodes=ax)(mt) | ||
| orig_ax = nib.orientations.aff2axcodes(affine) | ||
| t2 = Orientation(axcodes="".join(orig_ax))(t1) | ||
| self.assertEqual(t2.shape, mt.shape) | ||
| self.assertLess(np.abs(t2.numpy()[0] - mt.numpy()[0]).max(), 1e-6) | ||
| self.assertLess(np.abs(np.asarray(t2.affine) - affine).max(), 1e-6) | ||
|
|
||
| def test_randaffine_inverse_consistent(self): | ||
| """RandAffine.inverse uses the exact inverse of the forward voxel affine (5.96e-7).""" | ||
| rng = np.random.default_rng(99) | ||
| affine = _random_sheared_affine(rng) | ||
| mt = MetaTensor(_smooth_volume(seed=0).clone()) | ||
| mt.affine = affine | ||
| tf = RandAffine( | ||
| prob=1.0, | ||
| rotate_range=0.15, | ||
| shear_range=0.1, | ||
| translate_range=2, | ||
| scale_range=0.1, | ||
| mode="bilinear", | ||
| padding_mode="border", | ||
| ) | ||
| tf.set_random_state(42) | ||
| out = tf(mt) | ||
| inv = tf.inverse(out) | ||
| self.assertEqual(tuple(inv.shape[1:]), tuple(mt.shape[1:])) | ||
| self.assertLess(np.abs(np.asarray(inv.affine) - affine).max(), 1e-5) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
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.
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.
Skip
test_orientation_roundtripwhen nibabel is absent.The minimum-dependency CI job installs
.[testing], which does not include nibabel. That job runstests/min_tests.py, which loads this module becausetest_spacing_shearis not in its exclusion list.test_orientation_roundtriprunsOrientationand then imports nibabel unconditionally, so it errors in that job. Guard the test with a nibabel availability check, and move the import to the optional-import flag.Suggested fix