def test_rand_weighted_crop_default_roi(self): img = self.imt[0] n_samples = 3 crop = RandWeightedCrop((10, -1, -1), n_samples) weight = np.zeros_like(img) weight[0, 7, 17] = 1.1 weight[0, 13, 31] = 1.1 weight[0, 24, 21] = 1 crop.set_random_state(10) result = crop(img, weight) self.assertTrue(len(result) == n_samples) np.testing.assert_allclose(result[0].shape, (1, 10, 64, 80)) np.testing.assert_allclose(np.asarray(crop.centers), [[14, 32, 40], [41, 32, 40], [20, 32, 40]])
def test_rand_weighted_crop_small_roi(self): img = self.seg1[0] n_samples = 3 crop = RandWeightedCrop((8, 10, 12), n_samples) weight = np.zeros_like(img) weight[0, 5, 30, 17] = 1.1 weight[0, 8, 40, 31] = 1 weight[0, 11, 23, 21] = 1 crop.set_random_state(10) result = crop(img, weight) self.assertTrue(len(result) == n_samples) np.testing.assert_allclose(result[0].shape, (1, 8, 10, 12)) np.testing.assert_allclose(np.asarray(crop.centers), [[11, 23, 21], [5, 30, 17], [8, 40, 31]])
def test_rand_weighted_crop_bad_w(self): img = self.imt[0] n_samples = 3 crop = RandWeightedCrop((20, 40), n_samples) weight = np.zeros_like(img) weight[0, 30, 17] = np.inf weight[0, 10, 1] = -np.inf weight[0, 10, 20] = -np.nan crop.set_random_state(10) result = crop(img, weight) self.assertTrue(len(result) == n_samples) np.testing.assert_allclose(result[0].shape, (1, 20, 40)) np.testing.assert_allclose(np.asarray(crop.centers), [[63, 37], [31, 43], [66, 20]])
def test_rand_weighted_crop_large_roi(self): img = self.segn[0] n_samples = 3 crop = RandWeightedCrop((10000, 400), n_samples) weight = np.zeros_like(img) weight[0, 30, 17] = 1.1 weight[0, 10, 1] = 1 crop.set_random_state(10) result = crop(img, weight) self.assertTrue(len(result) == n_samples) np.testing.assert_allclose(result[0].shape, (1, 128, 64)) np.testing.assert_allclose(np.asarray(crop.centers), [[64, 32], [64, 32], [64, 32]]) for res in result: np.testing.assert_allclose(res, self.segn[0])
def test_rand_weighted_crop(self, _, input_params, img, weight, expected_shape, expected_vals): crop = RandWeightedCrop(**input_params) crop.set_random_state(10) result = crop(img, weight) self.assertTrue(len(result) == input_params["num_samples"]) assert_allclose(result[0].shape, expected_shape) for c, e in zip(crop.centers, expected_vals): assert_allclose(c, e, type_test=False) # if desired ROI is larger than image, check image is unchanged if all(s >= i for i, s in zip(img.shape[1:], input_params["spatial_size"])): for res in result: assert_allclose(res, img, type_test="tensor") self.assertEqual(len(res.applied_operations), 1)