Skip to content
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

Rand affine fix #4528

Merged
merged 6 commits into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions monai/transforms/spatial/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,14 +834,22 @@ def __call__(self, data: Mapping[Hashable, NdarrayOrTensor]) -> Dict[Hashable, N
# do the transform
if do_resampling:
d[key] = self.rand_affine(d[key], mode=mode, padding_mode=padding_mode, grid=grid)
self.push_transform(d[key], extra_info={"do_resampling": do_resampling})
self.push_transform(
d[key],
extra_info={"do_resampling": True, "rand_affine_info": self.pop_transform(d[key], check=False)},
)
else:
self.push_transform(d[key], extra_info={"do_resampling": False})
return d

def inverse(self, data: Mapping[Hashable, NdarrayOrTensor]) -> Dict[Hashable, NdarrayOrTensor]:
d = deepcopy(dict(data))

for key in self.key_iterator(d):
if self.pop_transform(d[key])[TraceKeys.EXTRA_INFO]["do_resampling"]:
tr = self.pop_transform(d[key])
do_resampling = tr[TraceKeys.EXTRA_INFO]["do_resampling"]
if do_resampling:
d[key].applied_operations.append(tr[TraceKeys.EXTRA_INFO]["rand_affine_info"]) # type: ignore
d[key] = self.rand_affine.inverse(d[key])

return d
Expand Down
1 change: 0 additions & 1 deletion runtests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,6 @@ then
clang_format

echo "${green}done!${noColor}"
exit
fi

# unconditionally report on the state of monai
Expand Down
3 changes: 3 additions & 0 deletions tests/test_rand_affine.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import torch
from parameterized import parameterized

from monai.data.meta_tensor import MetaTensor
from monai.transforms import RandAffine
from monai.utils.type_conversion import convert_data_type
from tests.utils import TEST_NDARRAYS, assert_allclose, is_tf32_env, test_local_inversion
Expand Down Expand Up @@ -147,6 +148,8 @@ def test_rand_affine(self, input_param, input_data, expected_val):
test_local_inversion(g, result, input_data)

assert_allclose(result, expected_val, rtol=_rtol, atol=1e-4, type_test=False)
self.assertIsInstance(result, MetaTensor)
self.assertEqual(len(result.applied_operations), 1)

def test_ill_cache(self):
with self.assertWarns(UserWarning):
Expand Down
Loading