def test_rand_weighted_crop_large_roi(self):
     for p in TEST_NDARRAYS:
         for q in TEST_NDARRAYS:
             img = self.segn[0]
             n_samples = 3
             crop = RandWeightedCropd(("img", "seg"), "weight",
                                      (10000, 400), n_samples, "location")
             weight = np.zeros_like(img)
             weight[0, 30, 17] = 1.1
             weight[0, 10, 1] = 1
             crop.set_random_state(10)
             data = {
                 "img": p(img),
                 "seg": p(self.imt[0]),
                 "weight": q(weight)
             }
             result = crop(data)
             self.assertTrue(len(result) == n_samples)
             np.testing.assert_allclose(result[0]["img"].shape,
                                        (1, 128, 64))
             np.testing.assert_allclose(result[0]["seg"].shape,
                                        (1, 128, 64))
             for c, e in zip(crop.centers, [[64, 32], [64, 32], [64, 32]]):
                 assert_allclose(c, e, type_test=False)
             assert_allclose(result[1]["location"], [64, 32],
                             type_test=False)
 def test_rand_weighted_crop_patch_index(self):
     for p in TEST_NDARRAYS:
         for q in TEST_NDARRAYS:
             img = self.imt[0]
             n_samples = 3
             crop = RandWeightedCropd(("img", "seg"), "w", (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": p(img),
                 "seg": p(self.segn[0]),
                 "w": q(weight),
                 PostFix.meta("img"): {
                     "affine": None
                 }
             })
             self.assertTrue(len(result) == n_samples)
             for c, e in zip(crop.centers,
                             [[14, 32, 40], [41, 32, 40], [20, 32, 40]]):
                 assert_allclose(c, e, type_test=False)
             for i in range(n_samples):
                 np.testing.assert_allclose(result[i]["img"].shape,
                                            (1, 10, 64, 80))
                 np.testing.assert_allclose(result[i]["seg"].shape,
                                            (1, 10, 64, 80))
                 np.testing.assert_allclose(
                     result[i][PostFix.meta("img")]["patch_index"], i)
                 np.testing.assert_allclose(
                     result[i][PostFix.meta("seg")]["patch_index"], i)
 def test_rand_weighted_crop_bad_w(self):
     for p in TEST_NDARRAYS:
         for q in TEST_NDARRAYS:
             img = self.imt[0]
             n_samples = 3
             crop = RandWeightedCropd(("img", "seg"), "w", (48, 64, 80),
                                      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": p(img),
                 "seg": p(self.segn[0]),
                 "w": q(weight)
             })
             self.assertTrue(len(result) == n_samples)
             np.testing.assert_allclose(result[0]["img"].shape,
                                        (1, 48, 64, 80))
             np.testing.assert_allclose(result[0]["seg"].shape,
                                        (1, 48, 64, 80))
             for c, e in zip(crop.centers,
                             [[24, 32, 40], [24, 32, 40], [24, 32, 40]]):
                 assert_allclose(c, e, type_test=False)
 def test_rand_weighted_crop_default_roi(self):
     for p in TEST_NDARRAYS:
         for q in TEST_NDARRAYS:
             img = self.imt[0]
             n_samples = 3
             crop = RandWeightedCropd(("img", "seg"), "w", (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": p(img),
                 "seg": p(self.segn[0]),
                 "w": q(weight)
             })
             self.assertTrue(len(result) == n_samples)
             np.testing.assert_allclose(result[0]["img"].shape,
                                        (1, 10, 64, 80))
             np.testing.assert_allclose(result[0]["seg"].shape,
                                        (1, 10, 64, 80))
             for c, e in zip(crop.centers,
                             [[14, 32, 40], [41, 32, 40], [20, 32, 40]]):
                 assert_allclose(c, e, type_test=False)
예제 #5
0
 def test_rand_weighted_crop_patch_index(self):
     img = self.imt[0]
     n_samples = 3
     crop = RandWeightedCropd(("img", "seg"), "w", (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": img,
         "seg": self.segn[0],
         "w": weight,
         "img_meta_dict": {
             "affine": None
         }
     })
     self.assertTrue(len(result) == n_samples)
     np.testing.assert_allclose(np.asarray(crop.centers),
                                [[14, 32, 40], [41, 32, 40], [20, 32, 40]])
     for i in range(n_samples):
         np.testing.assert_allclose(result[i]["img"].shape, (1, 10, 64, 80))
         np.testing.assert_allclose(result[i]["seg"].shape, (1, 10, 64, 80))
         np.testing.assert_allclose(
             result[i]["img_meta_dict"]["patch_index"], i)
         np.testing.assert_allclose(
             result[i]["seg_meta_dict"]["patch_index"], i)
예제 #6
0
 def test_rand_weighted_cropd(self, _, init_params, input_data,
                              expected_shape, expected_centers):
     crop = RandWeightedCropd(**init_params)
     crop.set_random_state(10)
     result = crop(input_data)
     self.assertTrue(len(result) == init_params["num_samples"])
     _len = len(tuple(input_data.keys()))
     self.assertTupleEqual(
         tuple(result[0].keys())[:_len], tuple(input_data.keys()))
예제 #7
0
 def test_rand_weighted_crop_large_roi(self):
     img = self.segn[0]
     n_samples = 3
     crop = RandWeightedCropd("img", "w", (10000, 400, 80), n_samples)
     weight = np.zeros_like(img)
     weight[0, 30, 17, 20] = 1.1
     weight[0, 10, 1, 17] = 1
     crop.set_random_state(10)
     result = crop({"img": img, "w": weight})
     self.assertTrue(len(result) == n_samples)
     np.testing.assert_allclose(result[0]["img"].shape, (1, 48, 64, 80))
     np.testing.assert_allclose(np.asarray(crop.centers),
                                [[24, 32, 40], [24, 32, 40], [24, 32, 40]])
예제 #8
0
 def test_rand_weighted_crop_small_roi(self):
     img = self.seg1[0]
     n_samples = 3
     crop = RandWeightedCropd("img", "w", (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": img, "w": weight})
     self.assertTrue(len(result) == n_samples)
     np.testing.assert_allclose(result[0]["img"].shape, (1, 8, 10, 12))
     np.testing.assert_allclose(np.asarray(crop.centers),
                                [[11, 23, 21], [5, 30, 17], [8, 40, 31]])
예제 #9
0
 def test_rand_weighted_crop_bad_w(self):
     img = self.imt[0]
     n_samples = 3
     crop = RandWeightedCropd(("img", "seg"), "w", (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": img, "seg": self.segn[0], "w": weight})
     self.assertTrue(len(result) == n_samples)
     np.testing.assert_allclose(result[0]["img"].shape, (1, 20, 40))
     np.testing.assert_allclose(result[0]["seg"].shape, (1, 20, 40))
     np.testing.assert_allclose(np.asarray(crop.centers),
                                [[63, 37], [31, 43], [66, 20]])
예제 #10
0
 def test_rand_weighted_crop_default_roi(self):
     img = self.imt[0]
     n_samples = 3
     crop = RandWeightedCropd(("img", "seg"), "w", (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": img, "seg": self.segn[0], "w": weight})
     self.assertTrue(len(result) == n_samples)
     np.testing.assert_allclose(result[0]["img"].shape, (1, 10, 64, 80))
     np.testing.assert_allclose(result[0]["seg"].shape, (1, 10, 64, 80))
     np.testing.assert_allclose(np.asarray(crop.centers),
                                [[14, 32, 40], [41, 32, 40], [20, 32, 40]])
예제 #11
0
 def test_rand_weighted_crop_default_roi(self):
     img = self.imt[0]
     n_samples = 3
     crop = RandWeightedCropd("im", "weight", (10, -1), n_samples, "coords")
     weight = np.zeros_like(img)
     weight[0, 30, 17] = 1.1
     weight[0, 40, 31] = 1
     weight[0, 80, 21] = 1
     crop.set_random_state(10)
     data = {"im": img, "weight": weight, "others": np.nan}
     result = crop(data)
     self.assertTrue(len(result) == n_samples)
     np.testing.assert_allclose(result[0]["im"].shape, (1, 10, 64))
     np.testing.assert_allclose(np.asarray(crop.centers),
                                [[14, 32], [105, 32], [20, 32]])
     np.testing.assert_allclose(result[1]["coords"], [105, 32])
 def test_rand_weighted_crop_small_roi(self):
     for p in TEST_NDARRAYS:
         for q in TEST_NDARRAYS:
             img = self.seg1[0]
             n_samples = 3
             crop = RandWeightedCropd("img", "w", (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": p(img), "w": q(weight)})
             self.assertTrue(len(result) == n_samples)
             np.testing.assert_allclose(result[0]["img"].shape, (1, 8, 10, 12))
             for c, e in zip(crop.centers, [[11, 23, 21], [5, 30, 17], [8, 40, 31]]):
                 assert_allclose(c, e, type_test=False)
예제 #13
0
 def test_rand_weighted_crop_large_roi(self):
     img = self.segn[0]
     n_samples = 3
     crop = RandWeightedCropd(("img", "seg"), "weight", (10000, 400),
                              n_samples, "location")
     weight = np.zeros_like(img)
     weight[0, 30, 17] = 1.1
     weight[0, 10, 1] = 1
     crop.set_random_state(10)
     data = {"img": img, "seg": self.imt[0], "weight": weight}
     result = crop(data)
     self.assertTrue(len(result) == n_samples)
     np.testing.assert_allclose(result[0]["img"].shape, (1, 128, 64))
     np.testing.assert_allclose(result[0]["seg"].shape, (1, 128, 64))
     np.testing.assert_allclose(np.asarray(crop.centers),
                                [[64, 32], [64, 32], [64, 32]])
     np.testing.assert_allclose(result[1]["location"], [64, 32])
 def test_rand_weighted_crop_default_roi(self):
     for p in TEST_NDARRAYS:
         for q in TEST_NDARRAYS:
             img = self.imt[0]
             n_samples = 3
             crop = RandWeightedCropd("im", "weight", (10, -1), n_samples, "coords")
             weight = np.zeros_like(img)
             weight[0, 30, 17] = 1.1
             weight[0, 40, 31] = 1
             weight[0, 80, 21] = 1
             crop.set_random_state(10)
             data = {"im": p(img), "weight": q(weight), "others": np.nan}
             result = crop(data)
             self.assertTrue(len(result) == n_samples)
             np.testing.assert_allclose(result[0]["im"].shape, (1, 10, 64))
             for c, e in zip(crop.centers, [[14, 32], [105, 32], [20, 32]]):
                 assert_allclose(c, e, type_test=False)
             assert_allclose(result[1]["coords"], [105, 32], type_test=False)
예제 #15
0
 def test_rand_weighted_cropd(self, _, init_params, input_data, expected_shape, expected_centers):
     crop = RandWeightedCropd(**init_params)
     crop.set_random_state(10)
     result = crop(input_data)
     self.assertTrue(len(result) == init_params["num_samples"])