Skip to content

Commit 377d3ea

Browse files
committed
tests(transforms): cover randomize_per_key across intensity and spatial dict transforms
Signed-off-by: Soumya Snigdha Kundu <soumya_snigdha.kundu@kcl.ac.uk>
1 parent f49d6b2 commit 377d3ea

1 file changed

Lines changed: 82 additions & 0 deletions

File tree

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Copyright (c) MONAI Consortium
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
# Unless required by applicable law or agreed to in writing, software
7+
# distributed under the License is distributed on an "AS IS" BASIS,
8+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
# See the License for the specific language governing permissions and
10+
# limitations under the License.
11+
12+
from __future__ import annotations
13+
14+
import unittest
15+
16+
import torch
17+
from parameterized import parameterized
18+
19+
from monai.transforms.intensity.dictionary import (
20+
RandAdjustContrastd,
21+
RandBiasFieldd,
22+
RandGaussianNoised,
23+
RandGaussianSharpend,
24+
RandGaussianSmoothd,
25+
RandGibbsNoised,
26+
RandHistogramShiftd,
27+
RandScaleIntensityd,
28+
RandScaleIntensityFixedMeand,
29+
RandShiftIntensityd,
30+
RandStdShiftIntensityd,
31+
)
32+
from monai.transforms.spatial.dictionary import RandAxisFlipd, RandGridDistortiond, RandRotated, RandZoomd
33+
from tests.test_utils import assert_allclose
34+
35+
KEYS = ["img1", "img2"]
36+
37+
TESTS = [
38+
(RandGaussianNoised, {}),
39+
(RandShiftIntensityd, {"offsets": 0.5}),
40+
(RandStdShiftIntensityd, {"factors": 0.5}),
41+
(RandScaleIntensityd, {"factors": 0.5}),
42+
(RandScaleIntensityFixedMeand, {"factors": 0.5}),
43+
(RandBiasFieldd, {}),
44+
(RandAdjustContrastd, {}),
45+
(RandGaussianSmoothd, {}),
46+
(RandGaussianSharpend, {}),
47+
(RandHistogramShiftd, {"num_control_points": (5, 20)}),
48+
(RandGibbsNoised, {}),
49+
(RandAxisFlipd, {}),
50+
(RandRotated, {"range_x": 1.0, "range_y": 1.0, "range_z": 1.0}),
51+
(RandZoomd, {"min_zoom": 0.7, "max_zoom": 1.3}),
52+
(RandGridDistortiond, {"num_cells": 3, "distort_limit": 0.2}),
53+
]
54+
55+
56+
class TestRandPerKey(unittest.TestCase):
57+
@parameterized.expand([(cls.__name__, cls, kwargs) for cls, kwargs in TESTS])
58+
def test_shared_default(self, _, cls, kwargs):
59+
t = cls(keys=KEYS, prob=1.0, **kwargs)
60+
t.set_random_state(0)
61+
img = torch.rand(1, 8, 8, 8) + 1.0
62+
out = t({k: img.clone() for k in KEYS})
63+
assert_allclose(out["img1"], out["img2"], type_test=False)
64+
65+
@parameterized.expand([(cls.__name__, cls, kwargs) for cls, kwargs in TESTS])
66+
def test_independent_per_key(self, _, cls, kwargs):
67+
# independent draws may coincide for a single seed (e.g. RandAxisFlipd's discrete axis),
68+
# so only require that some deterministic seed yields divergent per-key outputs
69+
img = torch.rand(1, 8, 8, 8) + 1.0
70+
differs = False
71+
for seed in range(10):
72+
t = cls(keys=KEYS, prob=1.0, randomize_per_key=True, **kwargs)
73+
t.set_random_state(seed)
74+
out = t({k: img.clone() for k in KEYS})
75+
if not torch.allclose(out["img1"], out["img2"]):
76+
differs = True
77+
break
78+
self.assertTrue(differs)
79+
80+
81+
if __name__ == "__main__":
82+
unittest.main()

0 commit comments

Comments
 (0)