Skip to content

Commit 95ed4a1

Browse files
committed
minor updates
- `_pending_operations` defaults to `MetaObj.get_default_applied_operations()` - removes the unused `Apply` class - fixes unit tests, style checks - torch.pi doesn't exist before torch 1.10 Signed-off-by: Wenqi Li <wenqil@nvidia.com>
1 parent bd4f6d8 commit 95ed4a1

File tree

7 files changed

+15
-70
lines changed

7 files changed

+15
-70
lines changed

monai/data/meta_obj.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ def pop_pending_operation(self) -> Any:
214214
return self._pending_operations.pop()
215215

216216
def clear_pending_operations(self) -> Any:
217-
self._pending_operations = list()
217+
self._pending_operations = MetaObj.get_default_applied_operations()
218218

219219
@property
220220
def is_batch(self) -> bool:

monai/transforms/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,6 @@
227227
from .inverse_batch_transform import BatchInverseTransform, Decollated, DecollateD, DecollateDict
228228
from .io.array import SUPPORTED_READERS, LoadImage, SaveImage
229229
from .io.dictionary import LoadImaged, LoadImageD, LoadImageDict, SaveImaged, SaveImageD, SaveImageDict
230-
from .lazy.array import Apply
231230
from .lazy.functional import apply
232231
from .meta_matrix import Grid, Matrix, MatrixFactory, MetaMatrix, matmul
233232
from .meta_utility.dictionary import (

monai/transforms/lazy/array.py

Lines changed: 0 additions & 48 deletions
This file was deleted.

monai/transforms/utils.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -904,16 +904,10 @@ def _create_rotate_90(
904904
else:
905905
raise ValueError(f"'spatial_dims' must be 2 or 3 but is {spatial_dims}")
906906

907-
steps_ = steps % 4
908-
909907
affine = eye_func(spatial_dims + 1)
910908

911-
if spatial_dims == 2:
912-
a, b = 0, 1
913-
else:
914-
a, b = axis
915-
916-
affine[a, a], affine[a, b], affine[b, a], affine[b, b] = values[steps]
909+
a, b = (0, 1) if spatial_dims == 2 else axis
910+
affine[a, a], affine[a, b], affine[b, a], affine[b, b] = values[steps % 4]
917911
return affine
918912

919913

tests/test_apply.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import unittest
1313

14+
import numpy as np
1415
import torch
1516

1617
from monai.transforms.lazy.functional import apply
@@ -22,10 +23,10 @@ def single_2d_transform_cases():
2223
f = MatrixFactory(2, TransformBackends.TORCH, "cpu")
2324

2425
cases = [
25-
(torch.randn((1, 32, 32)), [MetaMatrix(f.rotate_euler(torch.pi / 4).matrix, {"id": "rotate"})], (1, 32, 32)),
26+
(torch.randn((1, 32, 32)), [MetaMatrix(f.rotate_euler(np.pi / 4).matrix, {"id": "rotate"})], (1, 32, 32)),
2627
(
2728
torch.randn((1, 16, 16)),
28-
[MetaMatrix(f.rotate_euler(torch.pi / 4).matrix, {"id": "rotate", "shape_override": (1, 45, 45)})],
29+
[MetaMatrix(f.rotate_euler(np.pi / 4).matrix, {"id": "rotate", "shape_override": (1, 45, 45)})],
2930
(1, 45, 45),
3031
),
3132
]
@@ -34,15 +35,15 @@ def single_2d_transform_cases():
3435

3536

3637
class TestApply(unittest.TestCase):
37-
def _test_apply_impl(self, tensor, pending_transforms):
38-
print(tensor.shape)
39-
result = tensor(*pending_transforms)
38+
def _test_apply_impl(self, tensor, pending_transforms, expected_shape):
39+
result = apply(tensor, pending_transforms)
4040
self.assertListEqual(result[1], pending_transforms)
41+
self.assertEqual(result[0].shape, expected_shape)
4142

4243
def _test_apply_metatensor_impl(self, tensor, pending_transforms, expected_shape, pending_as_parameter):
4344
tensor_ = convert_to_tensor(tensor, track_meta=True)
4445
if pending_as_parameter:
45-
result, transforms = tensor_(*pending_transforms)
46+
result, transforms = apply(tensor_, pending_transforms)
4647
else:
4748
for p in pending_transforms:
4849
tensor_.push_pending_operation(p)

tests/test_matmul.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,17 +146,17 @@ def get_matmul_2d_test_cases():
146146
f = MatrixFactory(2, TransformBackends.TORCH, "cpu")
147147
cases = [
148148
(
149-
f.rotate_euler(torch.pi / 4),
149+
f.rotate_euler(np.pi / 4),
150150
f.scale((0.5, 0.5)),
151151
torch.FloatTensor([[0.35355339, -0.35355339, 0], [0.35355339, 0.35355339, 0], [0, 0, 1]]),
152152
),
153153
(
154154
f.scale((0.5, 0.5)),
155-
f.rotate_euler(torch.pi / 4),
155+
f.rotate_euler(np.pi / 4),
156156
torch.FloatTensor([[0.35355339, -0.35355339, 0], [0.35355339, 0.35355339, 0], [0, 0, 1]]),
157157
),
158-
(f.translate((8, 8)), f.rotate_euler(torch.pi / 2), torch.FloatTensor([[0, -1, 8], [1, 0, 8], [0, 0, 1]])),
159-
(f.rotate_euler(torch.pi / 2), f.translate((8, 8)), torch.FloatTensor([[0, -1, -8], [1, 0, 8], [0, 0, 1]])),
158+
(f.translate((8, 8)), f.rotate_euler(np.pi / 2), torch.FloatTensor([[0, -1, 8], [1, 0, 8], [0, 0, 1]])),
159+
(f.rotate_euler(np.pi / 2), f.translate((8, 8)), torch.FloatTensor([[0, -1, -8], [1, 0, 8], [0, 0, 1]])),
160160
]
161161

162162
return cases

tests/test_resample.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ def rotate_45_2d():
2727

2828
class TestResampleFunction(unittest.TestCase):
2929
def _test_resample_function_impl(self, img, matrix):
30-
result = resample(convert_to_tensor(img), matrix)
31-
print(result)
30+
resample(convert_to_tensor(img), matrix)
3231

3332
RESAMPLE_FUNCTION_CASES = [(get_arange_img((1, 16, 16)), rotate_45_2d())]
3433

0 commit comments

Comments
 (0)