MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / test_resampler_lazy

Function test_resampler_lazy

tests/lazy_transforms_utils.py:34–91  ·  view source on GitHub ↗

This test function is used to test the consistency between non-lazy and lazy transforms. Args: resampler: instance of a resampling transform. expected_output: output of non-lazy transform. init_param: parameters that are used to initialize the transform. call

(
    resampler,
    expected_output,
    init_param=None,
    call_param=None,
    output_key=None,
    output_idx=None,
    rtol=1e-5,
    atol=1e-7,
    skip_shape_check=False,
    seed=None,
)

Source from the content-addressed store, hash-verified

32
33
34def test_resampler_lazy(
35 resampler,
36 expected_output,
37 init_param=None,
38 call_param=None,
39 output_key=None,
40 output_idx=None,
41 rtol=1e-5,
42 atol=1e-7,
43 skip_shape_check=False,
44 seed=None,
45):
46 """
47 This test function is used to test the consistency between non-lazy and lazy transforms.
48 Args:
49 resampler: instance of a resampling transform.
50 expected_output: output of non-lazy transform.
51 init_param: parameters that are used to initialize the transform.
52 call_param: parameters that are used when calling the transform.
53 output_key: key to get the output of the transform. This argument is used for dictionary based transforms.
54 output_idx: index to get the expected output from multiple outputs of the transform.
55 rtol: relative tolerance. This argument is only used to compare the output.
56 atol: absolute tolerance. This argument is only used to compare the output.
57 skip_shape_check: skip the check of shapes.
58 seed: set the random state with an integer seed. This argument is used for randomizable transforms.
59
60 """
61 if isinstance(resampler, Randomizable):
62 resampler.set_random_state(seed=seed)
63 set_track_meta(True)
64 resampler.lazy = True
65 pending_output = resampler(**deepcopy(call_param))
66 if output_idx is not None:
67 expected_output, pending_output = (expected_output[output_idx], pending_output[output_idx])
68 if output_key is not None:
69 non_lazy_out, lazy_out = expected_output[output_key], pending_output[output_key]
70 else:
71 non_lazy_out, lazy_out = expected_output, pending_output
72 assert_allclose(lazy_out.peek_pending_affine(), non_lazy_out.affine)
73 if not skip_shape_check:
74 assert_allclose(lazy_out.peek_pending_shape(), non_lazy_out.shape[1:4])
75 apply_param = get_apply_param(init_param, call_param)
76 lazy_out = apply_pending(lazy_out, overrides=apply_param)[0]
77 assert_allclose(lazy_out, non_lazy_out, rtol=rtol, atol=atol)
78 if (
79 isinstance(resampler, InvertibleTransform)
80 and (not isinstance(resampler, MapTransform))
81 and isinstance(lazy_out, MetaTensor)
82 and isinstance(non_lazy_out, MetaTensor)
83 and non_lazy_out.applied_operations
84 ):
85 resampler.lazy = False
86 out = resampler.inverse(lazy_out.clone())
87 ref = resampler.inverse(non_lazy_out.clone())
88 assert_allclose(out.applied_operations, [])
89 assert_allclose(out.pending_operations, [])
90 assert_allclose(ref, out, type_test=False, rtol=1e-3, atol=1e-3)
91 resampler.lazy = True

Callers 15

test_correct_resultsMethod · 0.90
test_correct_resultsMethod · 0.90
test_defaultMethod · 0.90
test_kMethod · 0.90
test_spatial_axesMethod · 0.90
test_lazyMethod · 0.90
test_spacingMethod · 0.90
test_spacing_torchMethod · 0.90
test_rand_affinedMethod · 0.90
test_correct_resultsMethod · 0.90
test_torchMethod · 0.90

Calls 9

set_track_metaFunction · 0.90
assert_allcloseFunction · 0.90
apply_pendingFunction · 0.90
get_apply_paramFunction · 0.85
peek_pending_affineMethod · 0.80
peek_pending_shapeMethod · 0.80
cloneMethod · 0.80
set_random_stateMethod · 0.45
inverseMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…