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,
)
| 32 | |
| 33 | |
| 34 | def 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 |
no test coverage detected
searching dependent graphs…