def test_affine(self, input_param, input_data, expected_val): g = Affined(**input_param) result = g(input_data)["img"] self.assertEqual(isinstance(result, torch.Tensor), isinstance(expected_val, torch.Tensor)) if isinstance(result, torch.Tensor): np.testing.assert_allclose(result.cpu().numpy(), expected_val.cpu().numpy(), rtol=1e-4, atol=1e-4) else: np.testing.assert_allclose(result, expected_val, rtol=1e-4, atol=1e-4)
def test_affine(self, input_param, input_data, expected_val): input_copy = deepcopy(input_data) g = Affined(**input_param) result = g(input_data) test_local_inversion(g, result, input_copy, dict_key="img") assert_allclose(result["img"], expected_val, rtol=1e-4, atol=1e-4, type_test="tensor")
TESTS.append(( "RandRotated 3d", "3D", 1e-1, RandRotated(KEYS, *[random.uniform(np.pi / 6, np.pi) for _ in range(3)], 1), # type: ignore )) TESTS.append(( "Affine 3d", "3D", 1e-1, Affined( KEYS, spatial_size=[155, 179, 192], rotate_params=[np.pi / 6, -np.pi / 5, np.pi / 7], shear_params=[0.5, 0.5], translate_params=[10, 5, -4], scale_params=[0.8, 1.3], ), )) TESTS.append(( "RandAffine 3d", "3D", 1e-1, RandAffined( KEYS, [155, 179, 192], prob=1, padding_mode="zeros", rotate_range=[np.pi / 6, -np.pi / 5, np.pi / 7],
def test_affine(self, input_param, input_data, expected_val): g = Affined(**input_param) result = g(input_data)["img"] assert_allclose(result, expected_val, rtol=1e-4, atol=1e-4)
def image_mixing(data, seed=None): #random.seed(seed) file_list = [x for x in data if int(x['_label']) == 1] random.shuffle(file_list) crop_foreground = CropForegroundd(keys=["image"], source_key="image", margin=(0, 0, 0), select_fn=lambda x: x != 0) WW, WL = 1500, -600 ct_window = CTWindowd(keys=["image"], width=WW, level=WL) resize2 = Resized(keys=["image"], spatial_size=(int(512 * 0.75), int(512 * 0.75), -1), mode="area") resize1 = Resized(keys=["image"], spatial_size=(-1, -1, 40), mode="nearest") gauss = GaussianSmooth(sigma=(1., 1., 0)) gauss2 = GaussianSmooth(sigma=(2.0, 2.0, 0)) affine = Affined(keys=["image"], scale_params=(1.0, 2.0, 1.0), padding_mode='zeros') common_transform = Compose([ LoadImaged(keys=["image"]), ct_window, CTSegmentation(keys=["image"]), AddChanneld(keys=["image"]), affine, crop_foreground, resize1, resize2, SqueezeDimd(keys=["image"]), ]) dirs = setup_directories() data_dir = dirs['data'] mixed_images_dir = os.path.join(data_dir, 'mixed_images') if not os.path.exists(mixed_images_dir): os.mkdir(mixed_images_dir) for img1, img2 in itertools.combinations(file_list, 2): img1 = {'image': img1["image"], 'seg': img1['seg']} img2 = {'image': img2["image"], 'seg': img2['seg']} img1_data = common_transform(img1)["image"] img2_data = common_transform(img2)["image"] img1_mask, img2_mask = (img1_data > 0), (img2_data > 0) img_presek = np.logical_and(img1_mask, img2_mask) img = np.maximum(img_presek * img1_data, img_presek * img2_data) multi_slice_viewer(img, "img1") loop = True while loop: save = input("Save image [y/n/e]: ") if save.lower() == 'y': loop = False k = str(time.time()).encode('utf-8') h = blake2b(key=k, digest_size=16) name = h.hexdigest() + '.nii.gz' out_path = os.path.join(mixed_images_dir, name) write_nifti(img, out_path, resample=False) elif save.lower() == 'n': loop = False break elif save.lower() == 'e': print("exeting") exit() else: print("wrong input!")