From 2438dde829ea746bbfaf1695a9a874adda67f54c Mon Sep 17 00:00:00 2001 From: Nic Ma Date: Thu, 8 Jul 2021 11:10:54 +0800 Subject: [PATCH 1/3] [DLMED] fix ToNumpy Signed-off-by: Nic Ma --- monai/transforms/utility/array.py | 10 ++++++---- tests/test_to_numpy.py | 7 +++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/monai/transforms/utility/array.py b/monai/transforms/utility/array.py index 02c4aa91c6..b963372f86 100644 --- a/monai/transforms/utility/array.py +++ b/monai/transforms/utility/array.py @@ -364,10 +364,12 @@ def __call__(self, img) -> np.ndarray: Apply the transform to `img` and make it contiguous. """ if isinstance(img, torch.Tensor): - img = img.detach().cpu().numpy() # type: ignore + img = img.detach().cpu().numpy() elif has_cp and isinstance(img, cp_ndarray): - img = cp.asnumpy(img) # type: ignore - return np.ascontiguousarray(img) + img = cp.asnumpy(img) + + img = np.asarray(img) + return np.ascontiguousarray(img) if img.ndim > 0 else img class ToCupy(Transform): @@ -380,7 +382,7 @@ def __call__(self, img): Apply the transform to `img` and make it contiguous. """ if isinstance(img, torch.Tensor): - img = img.detach().cpu().numpy() # type: ignore + img = img.detach().cpu().numpy() return cp.ascontiguousarray(cp.asarray(img)) diff --git a/tests/test_to_numpy.py b/tests/test_to_numpy.py index 6e112e6be8..d51fbd9ae1 100644 --- a/tests/test_to_numpy.py +++ b/tests/test_to_numpy.py @@ -58,6 +58,13 @@ def test_list_tuple(self): result = ToNumpy()(test_data) np.testing.assert_allclose(result, np.asarray(test_data)) + def test_single_value(self): + for test_data in [5, np.array(5)]: + result = ToNumpy()(test_data) + self.assertTrue(isinstance(result, np.ndarray)) + np.testing.assert_allclose(result, np.asarray(test_data)) + self.assertEqual(result.ndim, 0) + if __name__ == "__main__": unittest.main() From 0ac2d9d785148cdc41fe39b56d4ed66a1860c7e2 Mon Sep 17 00:00:00 2001 From: Nic Ma Date: Thu, 8 Jul 2021 11:28:58 +0800 Subject: [PATCH 2/3] [DLMED] fix flake8 Signed-off-by: Nic Ma --- monai/transforms/utility/array.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/monai/transforms/utility/array.py b/monai/transforms/utility/array.py index b963372f86..1d82bb3a44 100644 --- a/monai/transforms/utility/array.py +++ b/monai/transforms/utility/array.py @@ -368,8 +368,8 @@ def __call__(self, img) -> np.ndarray: elif has_cp and isinstance(img, cp_ndarray): img = cp.asnumpy(img) - img = np.asarray(img) - return np.ascontiguousarray(img) if img.ndim > 0 else img + array: np.ndarray = np.asarray(img) + return np.ascontiguousarray(array) if array.ndim > 0 else array class ToCupy(Transform): From 6c9268c15b42b394bc56d7ccafae47489effb0e1 Mon Sep 17 00:00:00 2001 From: Nic Ma Date: Thu, 8 Jul 2021 11:38:26 +0800 Subject: [PATCH 3/3] [DLMED] update according to comments Signed-off-by: Nic Ma --- tests/test_to_numpy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_to_numpy.py b/tests/test_to_numpy.py index d51fbd9ae1..291601ffeb 100644 --- a/tests/test_to_numpy.py +++ b/tests/test_to_numpy.py @@ -59,7 +59,7 @@ def test_list_tuple(self): np.testing.assert_allclose(result, np.asarray(test_data)) def test_single_value(self): - for test_data in [5, np.array(5)]: + for test_data in [5, np.array(5), torch.tensor(5)]: result = ToNumpy()(test_data) self.assertTrue(isinstance(result, np.ndarray)) np.testing.assert_allclose(result, np.asarray(test_data))