def get_data(keys): """Get the example data to be used. Use MarsAtlas as it only contains 1 image for quick download and that image is parcellated. """ cache_dir = os.environ.get("MONAI_DATA_DIRECTORY") or tempfile.mkdtemp() fname = "MarsAtlas-MNI-Colin27.zip" url = "https://www.dropbox.com/s/ndz8qtqblkciole/" + fname + "?dl=1" out_path = os.path.join(cache_dir, "MarsAtlas-MNI-Colin27") zip_path = os.path.join(cache_dir, fname) download_and_extract(url, zip_path, out_path) image, label = sorted(glob(os.path.join(out_path, "*.nii"))) data = {CommonKeys.IMAGE: image, CommonKeys.LABEL: label} transforms = Compose([ LoadImaged(keys), AddChanneld(keys), ScaleIntensityd(CommonKeys.IMAGE), Rotate90d(keys, spatial_axes=[0, 2]) ]) data = transforms(data) max_size = max(data[keys[0]].shape) padder = SpatialPadd(keys, (max_size, max_size, max_size)) return padder(data)
def test_prob_k_spatial_axes(self): key = 'test' rotate = Rotate90d(keys=key, k=2, spatial_axes=(0, 1)) rotated = rotate({key: self.imt[0]}) expected = list() for channel in self.imt[0]: expected.append(np.rot90(channel, 2, (0, 1))) expected = np.stack(expected) self.assertTrue(np.allclose(rotated[key], expected))
def test_k(self): key = None rotate = Rotate90d(keys=key, k=2) rotated = rotate({key: self.imt[0]}) expected = list() for channel in self.imt[0]: expected.append(np.rot90(channel, 2, (0, 1))) expected = np.stack(expected) self.assertTrue(np.allclose(rotated[key], expected))
def test_rotate90_default(self): key = 'test' rotate = Rotate90d(keys=key) rotated = rotate({key: self.imt[0]}) expected = list() for channel in self.imt[0]: expected.append(np.rot90(channel, 1, (0, 1))) expected = np.stack(expected) self.assertTrue(np.allclose(rotated[key], expected))
def test_spatial_axes(self): key = "test" rotate = Rotate90d(keys=key, spatial_axes=(0, 1)) rotated = rotate({key: self.imt[0]}) expected = [] for channel in self.imt[0]: expected.append(np.rot90(channel, 1, (0, 1))) expected = np.stack(expected) self.assertTrue(np.allclose(rotated[key], expected))
def test_prob_k_spatial_axes(self): key = "test" rotate = Rotate90d(keys=key, k=2, spatial_axes=(0, 1)) for p in TEST_NDARRAYS: rotated = rotate({key: p(self.imt[0])}) expected = [ np.rot90(channel, 2, (0, 1)) for channel in self.imt[0] ] expected = np.stack(expected) assert_allclose(rotated[key], p(expected))
def test_k(self): key = None rotate = Rotate90d(keys=key, k=2) for p in TEST_NDARRAYS: rotated = rotate({key: p(self.imt[0])}) expected = [ np.rot90(channel, 2, (0, 1)) for channel in self.imt[0] ] expected = np.stack(expected) assert_allclose(rotated[key], p(expected))
def test_rotate90_default(self): key = "test" rotate = Rotate90d(keys=key) for p in TEST_NDARRAYS: rotated = rotate({key: p(self.imt[0])}) expected = [ np.rot90(channel, 1, (0, 1)) for channel in self.imt[0] ] expected = np.stack(expected) assert_allclose(rotated[key], p(expected))
def test_prob_k_spatial_axes(self): key = "test" rotate = Rotate90d(keys=key, k=2, spatial_axes=(0, 1)) for p in TEST_NDARRAYS_ALL: im = p(self.imt[0]) rotated = rotate({key: im}) test_local_inversion(rotate, rotated, {key: im}, key) expected = [ np.rot90(channel, 2, (0, 1)) for channel in self.imt[0] ] expected = np.stack(expected) assert_allclose(rotated[key], p(expected), type_test="tensor")
def test_rotate90_default(self): key = "test" rotate = Rotate90d(keys=key) for p in TEST_NDARRAYS_ALL: im = p(self.imt[0]) set_track_meta(True) rotated = rotate({key: im}) test_local_inversion(rotate, rotated, {key: im}, key) expected = [ np.rot90(channel, 1, (0, 1)) for channel in self.imt[0] ] expected = np.stack(expected) assert_allclose(rotated[key], p(expected), type_test="tensor") set_track_meta(False) rotated = rotate({key: im}) self.assertNotIsInstance(rotated[key], MetaTensor) set_track_meta(True)
RandAxisFlipd(KEYS, 1), )) for acc in [True, False]: TESTS.append(( "Orientationd 3d", "3D", 0, Orientationd(KEYS, "RAS", as_closest_canonical=acc), )) TESTS.append(( "Rotate90d 2d", "2D", 0, Rotate90d(KEYS), )) TESTS.append(( "Rotate90d 3d", "3D", 0, Rotate90d(KEYS, k=2, spatial_axes=(1, 2)), )) TESTS.append(( "RandRotate90d 3d", "3D", 0, RandRotate90d(KEYS, prob=1, spatial_axes=(1, 2)), ))
def test_no_key(self): key = 'unknown' rotate = Rotate90d(keys=key) with self.assertRaisesRegex(KeyError, ''): rotate({'test': self.imt[0]})
def test_no_key(self): key = "unknown" rotate = Rotate90d(keys=key) with self.assertRaisesRegex(KeyError, ""): rotate({"test": self.imt[0]})
TESTS.append(("ResizeWithPadOrCropd 3d", "3D", 0, True, ResizeWithPadOrCropd(KEYS, [201, 150, 105]))) TESTS.append(("Flipd 3d", "3D", 0, True, Flipd(KEYS, [1, 2]))) TESTS.append(("Flipd 3d", "3D", 0, True, Flipd(KEYS, [1, 2]))) TESTS.append(("RandFlipd 3d", "3D", 0, True, RandFlipd(KEYS, 1, [1, 2]))) TESTS.append(("RandAxisFlipd 3d", "3D", 0, True, RandAxisFlipd(KEYS, 1))) TESTS.append(("RandAxisFlipd 3d", "3D", 0, True, RandAxisFlipd(KEYS, 1))) for acc in [True, False]: TESTS.append(("Orientationd 3d", "3D", 0, True, Orientationd(KEYS, "RAS", as_closest_canonical=acc))) TESTS.append(("Rotate90d 2d", "2D", 0, True, Rotate90d(KEYS))) TESTS.append( ("Rotate90d 3d", "3D", 0, True, Rotate90d(KEYS, k=2, spatial_axes=(1, 2)))) TESTS.append(("RandRotate90d 3d", "3D", 0, True, RandRotate90d(KEYS, prob=1, spatial_axes=(1, 2)))) TESTS.append(("Spacingd 3d", "3D", 3e-2, True, Spacingd(KEYS, [0.5, 0.7, 0.9], diagonal=False))) TESTS.append(("Resized 2d", "2D", 2e-1, True, Resized(KEYS, [50, 47]))) TESTS.append(("Resized 3d", "3D", 5e-2, True, Resized(KEYS, [201, 150, 78]))) TESTS.append(("Resized longest 2d", "2D", 2e-1, True,