def test_keep_size(self): zoom_fn = Zoom(zoom=[0.6, 0.6], keep_size=True, align_corners=True) zoomed = zoom_fn(self.imt[0], interp_order="bilinear") np.testing.assert_allclose(zoomed.shape, self.imt.shape[1:]) zoom_fn = Zoom(zoom=[1.3, 1.3], keep_size=True) zoomed = zoom_fn(self.imt[0]) np.testing.assert_allclose(zoomed.shape, self.imt.shape[1:])
def test_keep_size(self): zoom_fn = Zoom(zoom=0.6, keep_size=True) zoomed = zoom_fn(self.imt[0]) self.assertTrue(np.array_equal(zoomed.shape, self.imt.shape[1:])) zoom_fn = Zoom(zoom=1.3, keep_size=True) zoomed = zoom_fn(self.imt[0]) self.assertTrue(np.array_equal(zoomed.shape, self.imt.shape[1:]))
def test_keep_size(self): for p in TEST_NDARRAYS: zoom_fn = Zoom(zoom=[0.6, 0.6], keep_size=True, align_corners=True) zoomed = zoom_fn(p(self.imt[0]), mode="bilinear") assert_allclose(zoomed.shape, self.imt.shape[1:]) zoom_fn = Zoom(zoom=[1.3, 1.3], keep_size=True) zoomed = zoom_fn(p(self.imt[0])) assert_allclose(zoomed.shape, self.imt.shape[1:])
def test_keep_size(self): for p in TEST_NDARRAYS_ALL: zoom_fn = Zoom(zoom=[0.6, 0.6], keep_size=True, align_corners=True) im = p(self.imt[0]) zoomed = zoom_fn(im, mode="bilinear") assert_allclose(zoomed.shape, self.imt.shape[1:], type_test=False) test_local_inversion(zoom_fn, zoomed, im) zoom_fn = Zoom(zoom=[1.3, 1.3], keep_size=True) im = p(self.imt[0]) zoomed = zoom_fn(im) assert_allclose(zoomed.shape, self.imt.shape[1:], type_test=False) test_local_inversion(zoom_fn, zoomed, p(self.imt[0])) set_track_meta(False) rotated = zoom_fn(im) self.assertNotIsInstance(rotated, MetaTensor) np.testing.assert_allclose(zoomed.shape, self.imt.shape[1:]) set_track_meta(True)
def test_correct_results(self, zoom, order, mode, cval, prefilter, use_gpu, keep_size): zoom_fn = Zoom( zoom=zoom, order=order, mode=mode, cval=cval, prefilter=prefilter, use_gpu=use_gpu, keep_size=keep_size ) zoomed = zoom_fn(self.imt[0]) expected = list() for channel in self.imt[0]: expected.append(zoom_scipy(channel, zoom=zoom, mode=mode, order=order, cval=cval, prefilter=prefilter)) expected = np.stack(expected).astype(np.float32) self.assertTrue(np.allclose(expected, zoomed))
def test_correct_results(self, zoom, interp_order): zoom_fn = Zoom(zoom=zoom, interp_order=interp_order, keep_size=False) zoomed = zoom_fn(self.imt[0]) _order = 0 if interp_order.endswith("linear"): _order = 1 expected = list() for channel in self.imt[0]: expected.append(zoom_scipy(channel, zoom=zoom, mode="nearest", order=_order, prefilter=False)) expected = np.stack(expected).astype(np.float32) np.testing.assert_allclose(zoomed, expected, atol=1.0)
def test_padding_mode(self): zoom_fn = Zoom(zoom=0.5, mode="nearest", padding_mode="constant", keep_size=True) test_data = np.array([[[1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0]]]) zoomed = zoom_fn(test_data) expected = np.array([[[0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 1.0, 0.0], [0.0, 1.0, 1.0, 0.0], [0.0, 0.0, 0.0, 0.0]]]) np.testing.assert_allclose(zoomed, expected)
def test_gpu_zoom(self, _, zoom, order, mode, cval, prefilter): if importlib.util.find_spec("cupy"): zoom_fn = Zoom( zoom=zoom, order=order, mode=mode, cval=cval, prefilter=prefilter, use_gpu=True, keep_size=False ) zoomed = zoom_fn(self.imt[0]) expected = list() for channel in self.imt[0]: expected.append(zoom_scipy(channel, zoom=zoom, mode=mode, order=order, cval=cval, prefilter=prefilter)) expected = np.stack(expected).astype(np.float32) self.assertTrue(np.allclose(expected, zoomed))
def test_padding_mode(self): for p in TEST_NDARRAYS: zoom_fn = Zoom(zoom=0.5, mode="nearest", padding_mode="constant", keep_size=True) test_data = p([[[1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0]]]) zoomed = zoom_fn(test_data) expected = p([[[0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 1.0, 0.0], [0.0, 1.0, 1.0, 0.0], [0.0, 0.0, 0.0, 0.0]]]) torch.testing.assert_allclose(zoomed, expected)
def test_correct_results(self, zoom, mode): for p in TEST_NDARRAYS: zoom_fn = Zoom(zoom=zoom, mode=mode, keep_size=False) zoomed = zoom_fn(p(self.imt[0])) _order = 0 if mode.endswith("linear"): _order = 1 expected = [] for channel in self.imt[0]: expected.append( zoom_scipy(channel, zoom=zoom, mode="nearest", order=_order, prefilter=False)) expected = np.stack(expected).astype(np.float32) assert_allclose(zoomed, p(expected), atol=1.0)
def test_invalid_inputs(self, zoom, order, raises): with self.assertRaises(raises): zoom_fn = Zoom(zoom=zoom, interp_order=order) zoom_fn(self.imt[0])
def test_invalid_inputs(self, zoom, mode, raises): for p in TEST_NDARRAYS: with self.assertRaises(raises): zoom_fn = Zoom(zoom=zoom, mode=mode) zoom_fn(p(self.imt[0]))
def test_invalid_inputs(self, zoom, mode, raises): with self.assertRaises(raises): zoom_fn = Zoom(zoom=zoom, mode=mode) zoom_fn(self.imt[0])