| 29 | return np.random.randint(100, size=shape).astype(float) |
| 30 | |
| 31 | def crop_test(self, input_param, input_shape, expected_shape, same_area=None): |
| 32 | base_comparison = None |
| 33 | input_image = self.get_arr(input_shape) |
| 34 | |
| 35 | for im_type in TEST_NDARRAYS_ALL: |
| 36 | with self.subTest(im_type=im_type): |
| 37 | # input parameters, such as roi_start can be numpy, torch, list etc. |
| 38 | for param_type in TEST_NDARRAYS_ALL + (None,): |
| 39 | with self.subTest(param_type=param_type): |
| 40 | input_param_mod = deepcopy(input_param) |
| 41 | if param_type is not None: |
| 42 | for k in ("roi_start", "roi_end", "roi_center", "roi_size", "roi_scale"): |
| 43 | if k in input_param: |
| 44 | input_param_mod[k] = param_type(input_param[k]) |
| 45 | im = im_type(input_image) |
| 46 | cropper = self.Cropper(**input_param_mod) |
| 47 | is_map = isinstance(cropper, MapTransform) |
| 48 | input_data = {"img": im} if is_map else im |
| 49 | result = cropper(input_data) |
| 50 | out_im = result["img"] if is_map else result |
| 51 | self.assertIsInstance(out_im, MetaTensor) |
| 52 | self.assertTupleEqual(out_im.shape, expected_shape) |
| 53 | if same_area is not None: |
| 54 | assert_allclose(out_im, im[same_area], type_test=False) |
| 55 | # check result is the same regardless of input type |
| 56 | if base_comparison is None: |
| 57 | base_comparison = out_im |
| 58 | else: |
| 59 | assert_allclose(out_im, base_comparison) |
| 60 | |
| 61 | # test inverse |
| 62 | inv = cropper.inverse(result) |
| 63 | inv_im = inv["img"] if is_map else inv |
| 64 | self.assertIsInstance(inv_im, MetaTensor) |
| 65 | if same_area is not None: |
| 66 | assert_allclose(inv_im[same_area], im[same_area], type_test=False) |
| 67 | self.assertEqual(inv_im.applied_operations, []) |
| 68 | |
| 69 | def crop_test_value(self, input_param, input_arr, expected_array): |
| 70 | cropper = self.Cropper(**input_param) |